From: Philip Hazel Date: Thu, 7 Oct 2004 10:39:01 +0000 (+0000) Subject: Start X-Git-Tag: exim-4_50~150 X-Git-Url: https://git.exim.org/users/jgh/exim.git/commitdiff_plain/059ec3d9952740285fb1ebf47961b8aca2eb1b4a?ds=sidebyside;hp=61ec970df30325dbcd8c9d0f0e431dc793126656 Start --- diff --git a/src/exim_monitor/EDITME b/src/exim_monitor/EDITME new file mode 100644 index 000000000..5c642e682 --- /dev/null +++ b/src/exim_monitor/EDITME @@ -0,0 +1,181 @@ +# $Cambridge: exim/src/exim_monitor/EDITME,v 1.1 2004/10/07 10:39:01 ph10 Exp $ + +################################################## +# The Exim Monitor # +################################################## + +# This is the template for the Exim monitor's main build-time configuration +# file. It contains settings that are independent of any operating system. It +# should be edited and then saved to a file called Local/eximon.conf before +# running the make command to build the monitor, if any settings are required. +# Local/eximon.conf can be empty if no changes are needed. The examples given +# here (commented out) are the default settings. + +# Any settings made in the configuration file can be overridden at run time +# by setting up an environment variable with the same name as any of these +# options, but preceded by EXIMON_, for example, EXIMON_WINDOW_TITLE. + + +################################################################## +# Set these variables as appropriate for your system # +################################################################## + +# The qualifying name for your domain. The only use made of this is for +# testing that certain addresses are the same when displaying the +# log tail, and for shortening sender addresses in the queue display. + +# QUALIFY_DOMAIN= + +# The default minimum width and height for the whole window are 103 and +# 162 pixels respectively. This is enough to hold the left-most stripchart +# and the quit button. The values can be changed here. + +# MIN_HEIGHT=162 +# MIN_WIDTH=103 + +# If you uncomment the following setting, the window will start up at +# its minimum size, instead of the default maximum. There may be a quick +# flash during the start-up process. Defining it this way allows it to be +# overridden by an environment variable. + +# START_SMALL=${EXIMON_START_SMALL-yes} + +# The title for eximon's main display window. It is possible to have +# host name of the machine you are running on substituted into the +# title string. If you include the string ${fullhostname} then the +# complete name is used. If you include ${hostname} then the full +# host name will have the string contained in the DOMAIN variable +# stripped from its right-hand end before being substituted. Any other +# shell or environment variables may also be included. + +# If you use any substitutions, remember to ensure that the $ and {} +# characters are escaped from the shell, e.g. by using single quotes. + +# WINDOW_TITLE="${hostname} eximon" + +# The domain that you want to be stripped from the machine's full hostname +# when forming the short host name for the eximon window title, as +# described above. + +# DOMAIN= + +# Parameters for the rolling display of the tail of the exim log file. +# The width and depth are measured in pixels; LOG_BUFFER specifies the +# amount of store to set aside for holding the log tail, which is displayed +# in a scrolling window. When this store is full, the earlier 50% of it +# is discarded - this is much more efficient that throwing it away line +# by line. The number given can be followed by the letter K to indicate +# that the value is in kilobytes. A minimum value of 1K is enforced. + +# LOG_DEPTH=300 +# LOG_WIDTH=950 +# LOG_BUFFER=20K + +# The font which is used in the log tail display. This is defined in +# the normal X manner. It must be a "character cell" font, because this +# is required by the text widget. + +# LOG_FONT=-misc-fixed-medium-r-normal-*-14-140-*-*-*-*-iso8859-1 + +# Parameters for the display of message that are on the exim queue. +# The width and depth are measured in pixels. + +# QUEUE_DEPTH=200 +# QUEUE_WIDTH=950 + +# The font which is used in the queue display. + +# QUEUE_FONT=$LOG_FONT + +# When a message has more than one undelivered address, they are listed +# one below the other. A limit can be placed on the number of addresses +# displayed for any one message. If there are more, then "..." is used +# to indicate this. + +# QUEUE_MAX_ADDRESSES=10 + +# The display of the contents of the queue is updated every QUEUE_INTERVAL +# seconds by default (there is a button to request update). + +# QUEUE_INTERVAL=300 + +# The size of the popup text window that is used for looking at the +# contents of messages, etc. + +# TEXT_DEPTH=200 + +# The keystroke/mouse-operation that is used to pop up the menu in the +# queue window is configurable. The default is Shift with the lefthand +# mouse button. The name of an alternative can be specified in the standard +# X way of naming these things. With the default configuration for the monitor, +# individuals can override this by setting the EXIMON_MENU_EVENT environment +# variable. + +# MENU_EVENT='Shift' + +# When the menu is used to perform an operation on a message, the result of the +# operation is normally visible in the log window, so Eximon doesn't display +# the output of the generated Exim command. However, you can request that +# this output be shown in a separate window by setting ACTION_OUTPUT to "yes". +# This does not apply to the output generated from attempting to deliver a +# message, which is always shown. + +# ACTION_OUTPUT=no + +# When some action is taken on a message, such as freezing it, or changing +# its recipients, the queue display is normally automatically updated. On +# systems that have very large queues, this can take some time and be dis- +# tracting. If this option is set to "no", the queue display is no longer +# automatically updated after an action is applied to a message. + +# ACTION_QUEUE_UPDATE=yes + +# When the menu item to display a message's body is invoked, the amount +# of data is limited to BODY_MAX bytes. This limit is a safety precaution +# to save the screen scrolling for ever on an enormous message. + +# BODY_MAX=20000 + +# The stripcharts are updated every STRIPCHART_INTERVAL seconds. + +# STRIPCHART_INTERVAL=60 + +# A stripchart showing the count of messages in the queue is always +# displayed on the left of eximon's window. Its name is "queue" by +# default, but can be changed by this variable. + +# QUEUE_STRIPCHART_NAME=queue + +# The following variable may be set to the name of a disc partition. If +# it is, a stripchart showing the percentage fullness of the partition +# will be displayed as the second stripchart. This can be used to keep +# a display of a mail spool partition on the screen. + +# SIZE_STRIPCHART=/var/mail + +# The name of the size stripchart will be the last component of SIZE_STRIPCHART +# unless the following variable is set to override it. + +# SIZE_STRIPCHART_NAME=space + +# The following variable contains a specification of which stripcharts +# you want eximon to display based on log entries. The string consists of +# pairs of strings, delimited by slash characters. The first string in each +# pair is a regular expression that matches some distinguishing feature in a +# exim log entry. + +# Entries that match the expression will be counted and displayed in a +# stripchart whose title is given by the second string. The string may +# be continued over several input lines, provided that it is split +# after a slash, and an additional slash (optionally preceded by white +# space) is included at the start of the continuation line. + +# Stripcharts configured by the following parameter are displayed to the +# right of the queue and size stripcharts, in the order defined here. + +# LOG_STRIPCHARTS='/ <= /in/ +# / => /out/ +# / => .+ R=local/local/ +# / => .+ T=[^ ]*smtp/smtp/' + +# End of exim_monitor/EDITME diff --git a/src/exim_monitor/em_StripChart.c b/src/exim_monitor/em_StripChart.c new file mode 100644 index 000000000..316102147 --- /dev/null +++ b/src/exim_monitor/em_StripChart.c @@ -0,0 +1,507 @@ +/* $Cambridge: exim/src/exim_monitor/em_StripChart.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ +/* $XConsortium: StripChart.c,v 1.20 91/05/24 17:20:42 converse Exp $ */ + +/*********************************************************** +Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts, +and the Massachusetts Institute of Technology, Cambridge, Massachusetts. + + All Rights Reserved + +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose and without fee is hereby granted, +provided that the above copyright notice appear in all copies and that +both that copyright notice and this permission notice appear in +supporting documentation, and that the names of Digital or MIT not be +used in advertising or publicity pertaining to distribution of the +software without specific, written prior permission. + +DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING +ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL +DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR +ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS +SOFTWARE. + +******************************************************************/ + +/* This is the Athena StripChart widget, slightly hacked by +Philip Hazel in order to give access to +its repaint_window function so that a repaint can be forced. + +The repaint_window function has also been nobbled so that it only +ever changes scale to 10. There is probably a better way to handle +this - such as inventing some new resources, but I'm not up to +that just at the moment. + +On SunOS4 there are name clashes when trying to link this with the +Athena library. So to avoid them, rename a few things by inserting +"my" at the front of "strip". */ + + +#include +#include +#include +#include +#include +#include + +#define MS_PER_SEC 1000 + +/* Private Data */ + +#define offset(field) XtOffsetOf(StripChartRec, field) + +static XtResource resources[] = { + {XtNwidth, XtCWidth, XtRDimension, sizeof(Dimension), + offset(core.width), XtRImmediate, (XtPointer) 120}, + {XtNheight, XtCHeight, XtRDimension, sizeof(Dimension), + offset(core.height), XtRImmediate, (XtPointer) 120}, + {XtNupdate, XtCInterval, XtRInt, sizeof(int), + offset(strip_chart.update), XtRImmediate, (XtPointer) 10}, + {XtNminScale, XtCScale, XtRInt, sizeof(int), + offset(strip_chart.min_scale), XtRImmediate, (XtPointer) 1}, + {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel), + offset(strip_chart.fgpixel), XtRString, XtDefaultForeground}, + {XtNhighlight, XtCForeground, XtRPixel, sizeof(Pixel), + offset(strip_chart.hipixel), XtRString, XtDefaultForeground}, + {XtNgetValue, XtCCallback, XtRCallback, sizeof(XtPointer), + offset(strip_chart.get_value), XtRImmediate, (XtPointer) NULL}, + {XtNjumpScroll, XtCJumpScroll, XtRInt, sizeof(int), + offset(strip_chart.jump_val), XtRImmediate, (XtPointer) DEFAULT_JUMP}, +}; + +#undef offset + +/* Added argument types to these to shut picky compilers up. PH */ + +static void CreateGC(StripChartWidget, unsigned int); +static void DestroyGC(StripChartWidget, unsigned int); +static void Initialize(), Destroy(), Redisplay(); +static void MoveChart(StripChartWidget, Boolean); +static void SetPoints(StripChartWidget); +static Boolean SetValues(); + +int repaint_window(StripChartWidget, int, int); /* PH hack */ +/* static int repaint_window(); */ + +StripChartClassRec stripChartClassRec = { + { /* core fields */ + /* superclass */ (WidgetClass) &simpleClassRec, + /* class_name */ "StripChart", + /* size */ sizeof(StripChartRec), + /* class_initialize */ XawInitializeWidgetSet, + /* class_part_initialize */ NULL, + /* class_inited */ FALSE, + /* initialize */ Initialize, + /* initialize_hook */ NULL, + /* realize */ XtInheritRealize, + /* actions */ NULL, + /* num_actions */ 0, + /* resources */ resources, + /* num_resources */ XtNumber(resources), + /* xrm_class */ NULLQUARK, + /* compress_motion */ TRUE, + /* compress_exposure */ XtExposeCompressMultiple | + XtExposeGraphicsExposeMerged, + /* compress_enterleave */ TRUE, + /* visible_interest */ FALSE, + /* destroy */ Destroy, + /* resize */ (void (*)(Widget))SetPoints, + /* expose */ Redisplay, + /* set_values */ SetValues, + /* set_values_hook */ NULL, + /* set_values_almost */ NULL, + /* get_values_hook */ NULL, + /* accept_focus */ NULL, + /* version */ XtVersion, + /* callback_private */ NULL, + /* tm_table */ NULL, + /* query_geometry */ XtInheritQueryGeometry, + /* display_accelerator */ XtInheritDisplayAccelerator, + /* extension */ NULL + }, + { /* Simple class fields */ + /* change_sensitive */ XtInheritChangeSensitive + } +}; + +WidgetClass mystripChartWidgetClass = (WidgetClass) &stripChartClassRec; + +/**************************************************************** + * + * Private Procedures + * + ****************************************************************/ + +static void draw_it(); + +/* Function Name: CreateGC + * Description: Creates the GC's + * Arguments: w - the strip chart widget. + * which - which GC's to create. + * Returns: none + */ + +static void +CreateGC(w, which) +StripChartWidget w; +unsigned int which; +{ + XGCValues myXGCV; + + if (which & FOREGROUND) { + myXGCV.foreground = w->strip_chart.fgpixel; + w->strip_chart.fgGC = XtGetGC((Widget) w, GCForeground, &myXGCV); + } + + if (which & HIGHLIGHT) { + myXGCV.foreground = w->strip_chart.hipixel; + w->strip_chart.hiGC = XtGetGC((Widget) w, GCForeground, &myXGCV); + } +} + +/* Function Name: DestroyGC + * Description: Destroys the GC's + * Arguments: w - the strip chart widget. + * which - which GC's to destroy. + * Returns: none + */ + +static void +DestroyGC(w, which) +StripChartWidget w; +unsigned int which; +{ + if (which & FOREGROUND) + XtReleaseGC((Widget) w, w->strip_chart.fgGC); + + if (which & HIGHLIGHT) + XtReleaseGC((Widget) w, w->strip_chart.hiGC); +} + +/* ARGSUSED */ +static void Initialize (greq, gnew) + Widget greq, gnew; +{ + StripChartWidget w = (StripChartWidget)gnew; + + if (w->strip_chart.update > 0) + w->strip_chart.interval_id = XtAppAddTimeOut( + XtWidgetToApplicationContext(gnew), + w->strip_chart.update * MS_PER_SEC, + draw_it, (XtPointer) gnew); + CreateGC(w, (unsigned int) ALL_GCS); + + w->strip_chart.scale = w->strip_chart.min_scale; + w->strip_chart.interval = 0; + w->strip_chart.max_value = 0.0; + w->strip_chart.points = NULL; + SetPoints(w); +} + +static void Destroy (gw) + Widget gw; +{ + StripChartWidget w = (StripChartWidget)gw; + + if (w->strip_chart.update > 0) + XtRemoveTimeOut (w->strip_chart.interval_id); + if (w->strip_chart.points) + XtFree((char *) w->strip_chart.points); + DestroyGC(w, (unsigned int) ALL_GCS); +} + +/* + * NOTE: This function really needs to recieve graphics exposure + * events, but since this is not easily supported until R4 I am + * going to hold off until then. + */ + +/* ARGSUSED */ +static void Redisplay(w, event, region) + Widget w; + XEvent *event; + Region region; +{ + if (event->type == GraphicsExpose) + (void) repaint_window ((StripChartWidget)w, event->xgraphicsexpose.x, + event->xgraphicsexpose.width); + else + (void) repaint_window ((StripChartWidget)w, event->xexpose.x, + event->xexpose.width); +} + +/* ARGSUSED */ +static void +draw_it(client_data, id) +XtPointer client_data; +XtIntervalId *id; /* unused */ +{ + StripChartWidget w = (StripChartWidget)client_data; + double value; + + if (w->strip_chart.update > 0) + w->strip_chart.interval_id = + XtAppAddTimeOut(XtWidgetToApplicationContext( (Widget) w), + w->strip_chart.update * MS_PER_SEC,draw_it,client_data); + + if (w->strip_chart.interval >= (int)w->core.width) + MoveChart( (StripChartWidget) w, TRUE); + + /* Get the value, stash the point and draw corresponding line. */ + + if (w->strip_chart.get_value == NULL) + return; + + XtCallCallbacks( (Widget)w, XtNgetValue, (XtPointer)&value ); + + /* + * Keep w->strip_chart.max_value up to date, and if this data + * point is off the graph, change the scale to make it fit. + */ + + if (value > w->strip_chart.max_value) { + w->strip_chart.max_value = value; + if (w->strip_chart.max_value > w->strip_chart.scale) { + XClearWindow( XtDisplay (w), XtWindow (w)); + w->strip_chart.interval = repaint_window(w, 0, (int) w->core.width); + } + } + + w->strip_chart.valuedata[w->strip_chart.interval] = value; + if (XtIsRealized((Widget)w)) { + int y = (int) (w->core.height + - (int)(w->core.height * value) / w->strip_chart.scale); + + XFillRectangle(XtDisplay(w), XtWindow(w), w->strip_chart.fgGC, + w->strip_chart.interval, y, + (unsigned int) 1, w->core.height - y); + /* + * Fill in the graph lines we just painted over. + */ + + if (w->strip_chart.points != NULL) { + w->strip_chart.points[0].x = w->strip_chart.interval; + XDrawPoints(XtDisplay(w), XtWindow(w), w->strip_chart.hiGC, + w->strip_chart.points, w->strip_chart.scale - 1, + CoordModePrevious); + } + + XFlush(XtDisplay(w)); /* Flush output buffers */ + } + w->strip_chart.interval++; /* Next point */ +} /* draw_it */ + +/* Blts data according to current size, then redraws the stripChart window. + * Next represents the number of valid points in data. Returns the (possibly) + * adjusted value of next. If next is 0, this routine draws an empty window + * (scale - 1 lines for graph). If next is less than the current window width, + * the returned value is identical to the initial value of next and data is + * unchanged. Otherwise keeps half a window's worth of data. If data is + * changed, then w->strip_chart.max_value is updated to reflect the + * largest data point. + */ + +/* static int */ +int /* PH hack */ +repaint_window(w, left, width) +StripChartWidget w; +int left, width; +{ + register int i, j; + register int next = w->strip_chart.interval; + int scale = w->strip_chart.scale; + int scalewidth = 0; + + /* Compute the minimum scale required to graph the data, but don't go + lower than min_scale. */ + if (w->strip_chart.interval != 0 || scale <= (int)w->strip_chart.max_value) + scale = ((int) (w->strip_chart.max_value)) + 1; + if (scale < w->strip_chart.min_scale) + scale = w->strip_chart.min_scale; + +/* if (scale != w->strip_chart.scale) { */ + + if (scale != w->strip_chart.scale && scale == 10) { + w->strip_chart.scale = scale; + left = 0; + width = next; + scalewidth = w->core.width; + + SetPoints(w); + + if (XtIsRealized ((Widget) w)) + XClearWindow (XtDisplay (w), XtWindow (w)); + + } + + if (XtIsRealized((Widget)w)) { + Display *dpy = XtDisplay(w); + Window win = XtWindow(w); + + width += left - 1; + if (!scalewidth) scalewidth = width; + + if (next < ++width) width = next; + + /* Draw data point lines. */ + for (i = left; i < width; i++) { + int y = (int) (w->core.height - + (int)(w->core.height * w->strip_chart.valuedata[i]) / + w->strip_chart.scale); + + XFillRectangle(dpy, win, w->strip_chart.fgGC, + i, y, (unsigned int) 1, + (unsigned int) (w->core.height - y)); + } + + /* Draw graph reference lines */ + for (i = 1; i < w->strip_chart.scale; i++) { + j = i * ((int)w->core.height / w->strip_chart.scale); + XDrawLine(dpy, win, w->strip_chart.hiGC, left, j, scalewidth, j); + } + } + return(next); +} + +/* Function Name: MoveChart + * Description: moves the chart over when it would run off the end. + * Arguments: w - the load widget. + * blit - blit the bits? (TRUE/FALSE). + * Returns: none. + */ + +static void +MoveChart(StripChartWidget w, Boolean blit) +{ + double old_max; + int left, i, j; + register int next = w->strip_chart.interval; + + if (!XtIsRealized((Widget) w)) return; + + if (w->strip_chart.jump_val == DEFAULT_JUMP) + j = w->core.width >> 1; /* Half the window width. */ + else { + j = w->core.width - w->strip_chart.jump_val; + if (j < 0) j = 0; + } + + bcopy((char *)(w->strip_chart.valuedata + next - j), + (char *)(w->strip_chart.valuedata), j * sizeof(double)); + next = w->strip_chart.interval = j; + + /* + * Since we just lost some data, recompute the + * w->strip_chart.max_value. + */ + + old_max = w->strip_chart.max_value; + w->strip_chart.max_value = 0.0; + for (i = 0; i < next; i++) { + if (w->strip_chart.valuedata[i] > w->strip_chart.max_value) + w->strip_chart.max_value = w->strip_chart.valuedata[i]; + } + + if (!blit) return; /* we are done... */ + + if ( ((int) old_max) != ( (int) w->strip_chart.max_value) ) { + XClearWindow(XtDisplay(w), XtWindow(w)); + repaint_window(w, 0, (int) w->core.width); + return; + } + + XCopyArea(XtDisplay((Widget)w), XtWindow((Widget)w), XtWindow((Widget)w), + w->strip_chart.hiGC, (int) w->core.width - j, 0, + (unsigned int) j, (unsigned int) w->core.height, + 0, 0); + + XClearArea(XtDisplay((Widget)w), XtWindow((Widget)w), + (int) j, 0, + (unsigned int) w->core.width - j, (unsigned int)w->core.height, + FALSE); + + /* Draw graph reference lines */ + left = j; + for (i = 1; i < w->strip_chart.scale; i++) { + j = i * ((int)w->core.height / w->strip_chart.scale); + XDrawLine(XtDisplay((Widget) w), XtWindow( (Widget) w), + w->strip_chart.hiGC, left, j, (int)w->core.width, j); + } + return; +} + +/* ARGSUSED */ +static Boolean SetValues (current, request, new) + Widget current, request, new; +{ + StripChartWidget old = (StripChartWidget)current; + StripChartWidget w = (StripChartWidget)new; + Boolean ret_val = FALSE; + unsigned int new_gc = NO_GCS; + + if (w->strip_chart.update != old->strip_chart.update) { + if (old->strip_chart.update > 0) + XtRemoveTimeOut (old->strip_chart.interval_id); + if (w->strip_chart.update > 0) + w->strip_chart.interval_id = + XtAppAddTimeOut(XtWidgetToApplicationContext(new), + w->strip_chart.update * MS_PER_SEC, + draw_it, (XtPointer)w); + } + + if ( w->strip_chart.min_scale > (int) ((w->strip_chart.max_value) + 1) ) + ret_val = TRUE; + + if ( w->strip_chart.fgpixel != old->strip_chart.fgpixel ) { + new_gc |= FOREGROUND; + ret_val = True; + } + + if ( w->strip_chart.hipixel != old->strip_chart.hipixel ) { + new_gc |= HIGHLIGHT; + ret_val = True; + } + + DestroyGC(old, new_gc); + CreateGC(w, new_gc); + + return( ret_val ); +} + +/* Function Name: SetPoints + * Description: Sets up the polypoint that will be used to draw in + * the graph lines. + * Arguments: w - the StripChart widget. + * Returns: none. + */ + +#define HEIGHT ( (unsigned int) w->core.height) + +static void +SetPoints(w) +StripChartWidget w; +{ + XPoint * points; + Cardinal size; + int i; + + if (w->strip_chart.scale <= 1) { /* no scale lines. */ + XtFree ((char *) w->strip_chart.points); + w->strip_chart.points = NULL; + return; + } + + size = sizeof(XPoint) * (w->strip_chart.scale - 1); + + points = (XPoint *) XtRealloc( (XtPointer) w->strip_chart.points, size); + w->strip_chart.points = points; + + /* Draw graph reference lines into clip mask */ + + for (i = 1; i < w->strip_chart.scale; i++) { + points[i - 1].x = 0; + points[i - 1].y = HEIGHT / w->strip_chart.scale; + } +} diff --git a/src/exim_monitor/em_TextPop.c b/src/exim_monitor/em_TextPop.c new file mode 100644 index 000000000..b2b684d7f --- /dev/null +++ b/src/exim_monitor/em_TextPop.c @@ -0,0 +1,770 @@ +/* $Cambridge: exim/src/exim_monitor/em_TextPop.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ +/* $XConsortium: TextPop.c,v 1.22 91/07/25 18:10:22 rws Exp $ */ + +/*********************************************************** +Copyright 1989 by the Massachusetts Institute of Technology, +Cambridge, Massachusetts. + + All Rights Reserved + +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose and without fee is hereby granted, +provided that the above copyright notice appear in all copies and that +both that copyright notice and this permission notice appear in +supporting documentation, and that the names of Digital or MIT not be +used in advertising or publicity pertaining to distribution of the +software without specific, written prior permission. + +DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING +ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL +DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR +ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS +SOFTWARE. + +******************************************************************/ + + +/**************************************************************************** +* Modified by Philip Hazel for use with Exim. The "replace" and "insert * +* file" features of the search facility have been removed. Also took out * +* the declaration of sys_errlist, as it isn't used and causes trouble on * +* some systems that declare it differently. September 1996. * +* Added the arguments into the static functions declared at the head, to * +* stop some compiler warnings. August 1999. * +* Took out the separate declarations of errno and sys_nerr at the start, * +* because they too aren't actually used, and the declaration causes trouble * +* on some systems. December 2002. * +****************************************************************************/ + + +/************************************************************ + * + * This file is broken up into three sections one dealing with + * each of the three popups created here: + * + * FileInsert, Search, and Replace. + * + * There is also a section at the end for utility functions + * used by all more than one of these dialogs. + * + * The following functions are the only non-static ones defined + * in this module. They are located at the begining of the + * section that contains this dialog box that uses them. + * + * void _XawTextInsertFileAction(w, event, params, num_params); + * void _XawTextDoSearchAction(w, event, params, num_params); + * void _XawTextDoReplaceAction(w, event, params, num_params); + * void _XawTextInsertFile(w, event, params, num_params); + * + *************************************************************/ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include /* for O_RDONLY */ +#include + +/* extern int errno, sys_nerr; */ +/* extern char* sys_errlist[]; */ + +#define DISMISS_NAME ("cancel") +#define DISMISS_NAME_LEN 6 +#define FORM_NAME ("form") +#define LABEL_NAME ("label") +#define TEXT_NAME ("text") + +#define R_OFFSET 1 + +/* Argument types added by PH August 1999 */ + +static void CenterWidgetOnPoint(Widget, XEvent *); +static void PopdownSearch(Widget, XtPointer, XtPointer); +static void InitializeSearchWidget(struct SearchAndReplace *, + XawTextScanDirection, Boolean); +static void SetResource(Widget, char *, XtArgVal); +static void SetSearchLabels(struct SearchAndReplace *, String, String, + Boolean); +static Widget CreateDialog(Widget, String, String, + void (*)(Widget, char *, Widget)); +static Widget GetShell(Widget); +static void SetWMProtocolTranslations(Widget w); +static Boolean DoSearch(struct SearchAndReplace *); +static String GetString(Widget); + +static void AddSearchChildren(Widget, char *, Widget); + +static char radio_trans_string[] = + ",: set() notify()"; + +static char search_text_trans[] = + "~ShiftReturn: DoSearchAction(Popdown) \n\ + Ctrlc: PopdownSearchAction() \n\ + "; + + + +/************************************************************ + * + * This section of the file contains all the functions that + * the search dialog box uses. + * + ************************************************************/ + +/* Function Name: _XawTextDoSearchAction + * Description: Action routine that can be bound to dialog box's + * Text Widget that will search for a string in the main + * Text Widget. + * Arguments: (Standard Action Routine args) + * Returns: none. + * + * Note: + * + * If the search was sucessful and the argument popdown is passed to + * this action routine then the widget will automatically popdown the + * search widget. + */ + +/* ARGSUSED */ +void +_XawTextDoSearchAction(w, event, params, num_params) +Widget w; +XEvent *event; +String * params; +Cardinal * num_params; +{ + TextWidget tw = (TextWidget) XtParent(XtParent(XtParent(w))); + Boolean popdown = FALSE; + + if ( (*num_params == 1) && + ((params[0][0] == 'p') || (params[0][0] == 'P')) ) + popdown = TRUE; + + if (DoSearch(tw->text.search) && popdown) + PopdownSearch(w, (XtPointer) tw->text.search, NULL); +} + +/* Function Name: _XawTextPopdownSearchAction + * Description: Action routine that can be bound to dialog box's + * Text Widget that will popdown the search widget. + * Arguments: (Standard Action Routine args) + * Returns: none. + */ + +/* ARGSUSED */ +void +_XawTextPopdownSearchAction(w, event, params, num_params) +Widget w; +XEvent *event; +String * params; +Cardinal * num_params; +{ + TextWidget tw = (TextWidget) XtParent(XtParent(XtParent(w))); + + PopdownSearch(w, (XtPointer) tw->text.search, NULL); +} + +/* Function Name: PopdownSeach + * Description: Pops down the search widget and resets it. + * Arguments: w - *** NOT USED ***. + * closure - a pointer to the search structure. + * call_data - *** NOT USED ***. + * Returns: none + */ + +/* ARGSUSED */ +static void +PopdownSearch(w, closure, call_data) +Widget w; +XtPointer closure; +XtPointer call_data; +{ + struct SearchAndReplace * search = (struct SearchAndReplace *) closure; + + SetSearchLabels(search, "Search", "", FALSE); + XtPopdown( search->search_popup ); +} + +/* Function Name: SearchButton + * Description: Performs a search when the button is clicked. + * Arguments: w - *** NOT USED **. + * closure - a pointer to the search info. + * call_data - *** NOT USED ***. + * Returns: + */ + +/* ARGSUSED */ +static void +SearchButton(w, closure, call_data) +Widget w; +XtPointer closure; +XtPointer call_data; +{ + (void) DoSearch( (struct SearchAndReplace *) closure ); +} + +/* Function Name: _XawTextSearch + * Description: Action routine that can be bound to the text widget + * it will popup the search dialog box. + * Arguments: w - the text widget. + * event - X Event (used to get x and y location). + * params, num_params - the parameter list. + * Returns: none. + * + * NOTE: + * + * The parameter list contains one or two entries that may be the following. + * + * First Entry: The first entry is the direction to search by default. + * This arguement must be specified and may have a value of + * "left" or "right". + * + * Second Entry: This entry is optional and contains the value of the default + * string to search for. + */ + +#define SEARCH_HEADER ("Text Widget - Search():") + +void +_XawTextSearch(w, event, params, num_params) +Widget w; +XEvent *event; +String * params; +Cardinal * num_params; +{ + TextWidget ctx = (TextWidget)w; + XawTextScanDirection dir; + char * ptr, buf[BUFSIZ]; + XawTextEditType edit_mode; + Arg args[1]; + +#ifdef notdef + if (ctx->text.source->Search == NULL) { + XBell(XtDisplay(w), 0); + return; + } +#endif + + if ( (*num_params < 1) || (*num_params > 2) ) { + sprintf(buf, "%s %s\n%s", SEARCH_HEADER, "This action must have only", + "one or two parameters"); + XtAppWarning(XtWidgetToApplicationContext(w), buf); + return; + } + else if (*num_params == 1) + ptr = ""; + else + ptr = params[1]; + + switch(params[0][0]) { + case 'b': /* Left. */ + case 'B': + dir = XawsdLeft; + break; + case 'f': /* Right. */ + case 'F': + dir = XawsdRight; + break; + default: + sprintf(buf, "%s %s\n%s", SEARCH_HEADER, "The first parameter must be", + "Either 'backward' or 'forward'"); + XtAppWarning(XtWidgetToApplicationContext(w), buf); + return; + } + + if (ctx->text.search== NULL) { + ctx->text.search = XtNew(struct SearchAndReplace); + ctx->text.search->search_popup = CreateDialog(w, ptr, "search", + AddSearchChildren); + XtRealizeWidget(ctx->text.search->search_popup); + SetWMProtocolTranslations(ctx->text.search->search_popup); + } + else if (*num_params > 1) { + XtVaSetValues(ctx->text.search->search_text, XtNstring, ptr, NULL); + } + + XtSetArg(args[0], XtNeditType,&edit_mode); + XtGetValues(ctx->text.source, args, ONE); + + InitializeSearchWidget(ctx->text.search, dir, (edit_mode == XawtextEdit)); + + CenterWidgetOnPoint(ctx->text.search->search_popup, event); + XtPopup(ctx->text.search->search_popup, XtGrabNone); +} + +/* Function Name: InitializeSearchWidget + * Description: This function initializes the search widget and + * is called each time the search widget is poped up. + * Arguments: search - the search widget structure. + * dir - direction to search. + * replace_active - state of the sensitivity for the + * replace button. + * Returns: none. + */ + +static void +InitializeSearchWidget(struct SearchAndReplace *search, + XawTextScanDirection dir, Boolean replace_active) +{ +replace_active = replace_active; /* PH - shuts compilers up */ + + switch (dir) { + case XawsdLeft: + SetResource(search->left_toggle, XtNstate, (XtArgVal) TRUE); + break; + case XawsdRight: + SetResource(search->right_toggle, XtNstate, (XtArgVal) TRUE); + break; + default: + break; + } +} + +/* Function Name: AddSearchChildren + * Description: Adds all children to the Search Dialog Widget. + * Arguments: form - the form widget for the search widget. + * ptr - a pointer to the initial string for the Text Widget. + * tw - the main text widget. + * Returns: none. + */ + +static void +AddSearchChildren(form, ptr, tw) +Widget form, tw; +char * ptr; +{ + Arg args[10]; + Cardinal num_args; + Widget cancel, search_button, s_label, s_text; + XtTranslations trans; + struct SearchAndReplace * search = ((TextWidget) tw)->text.search; + + num_args = 0; + XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++; + XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++; + XtSetArg(args[num_args], XtNresizable, TRUE ); num_args++; + XtSetArg(args[num_args], XtNborderWidth, 0 ); num_args++; + search->label1 = XtCreateManagedWidget("label1", labelWidgetClass, + form, args, num_args); + + /* + * We need to add R_OFFSET to the radio_data, because the value zero (0) + * has special meaning. + */ + + num_args = 0; + XtSetArg(args[num_args], XtNlabel, "Backward"); num_args++; + XtSetArg(args[num_args], XtNfromVert, search->label1); num_args++; + XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++; + XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++; + XtSetArg(args[num_args], XtNradioData, (caddr_t) XawsdLeft + R_OFFSET); + num_args++; + search->left_toggle = XtCreateManagedWidget("backwards", toggleWidgetClass, + form, args, num_args); + + num_args = 0; + XtSetArg(args[num_args], XtNlabel, "Forward"); num_args++; + XtSetArg(args[num_args], XtNfromVert, search->label1); num_args++; + XtSetArg(args[num_args], XtNfromHoriz, search->left_toggle); num_args++; + XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++; + XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++; + XtSetArg(args[num_args], XtNradioGroup, search->left_toggle); num_args++; + XtSetArg(args[num_args], XtNradioData, (caddr_t) XawsdRight + R_OFFSET); + num_args++; + search->right_toggle = XtCreateManagedWidget("forwards", toggleWidgetClass, + form, args, num_args); + + { + XtTranslations radio_translations; + + radio_translations = XtParseTranslationTable(radio_trans_string); + XtOverrideTranslations(search->left_toggle, radio_translations); + XtOverrideTranslations(search->right_toggle, radio_translations); + } + + num_args = 0; + XtSetArg(args[num_args], XtNfromVert, search->left_toggle); num_args++; + XtSetArg(args[num_args], XtNlabel, "Search for: ");num_args++; + XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++; + XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++; + XtSetArg(args[num_args], XtNborderWidth, 0 ); num_args++; + s_label = XtCreateManagedWidget("searchLabel", labelWidgetClass, + form, args, num_args); + + num_args = 0; + XtSetArg(args[num_args], XtNfromVert, search->left_toggle); num_args++; + XtSetArg(args[num_args], XtNfromHoriz, s_label); num_args++; + XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++; + XtSetArg(args[num_args], XtNright, XtChainRight); num_args++; + XtSetArg(args[num_args], XtNeditType, XawtextEdit); num_args++; + XtSetArg(args[num_args], XtNresizable, TRUE); num_args++; + XtSetArg(args[num_args], XtNresize, XawtextResizeWidth); num_args++; + XtSetArg(args[num_args], XtNstring, ptr); num_args++; + s_text = XtCreateManagedWidget("searchText", asciiTextWidgetClass, form, + args, num_args); + search->search_text = s_text; + + num_args = 0; + XtSetArg(args[num_args], XtNlabel, "Search"); num_args++; + XtSetArg(args[num_args], XtNfromVert, s_text); num_args++; + XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++; + XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++; + search_button = XtCreateManagedWidget("search", commandWidgetClass, form, + args, num_args); + + num_args = 0; + XtSetArg(args[num_args], XtNlabel, "Cancel"); num_args++; + XtSetArg(args[num_args], XtNfromVert, s_text); num_args++; + XtSetArg(args[num_args], XtNfromHoriz, search_button); num_args++; + XtSetArg(args[num_args], XtNleft, XtChainLeft); num_args++; + XtSetArg(args[num_args], XtNright, XtChainLeft); num_args++; + cancel = XtCreateManagedWidget(DISMISS_NAME, commandWidgetClass, form, + args, num_args); + + XtAddCallback(search_button, XtNcallback, SearchButton, (XtPointer) search); + XtAddCallback(cancel, XtNcallback, PopdownSearch, (XtPointer) search); + +/* + * Initialize the text entry fields. + */ + + SetSearchLabels(search, "Search", "", FALSE); + XtSetKeyboardFocus(form, search->search_text); + +/* + * Bind Extra translations. + */ + + trans = XtParseTranslationTable(search_text_trans); + XtOverrideTranslations(search->search_text, trans); +} + +/* Function Name: DoSearch + * Description: Performs a search. + * Arguments: search - the serach structure. + * Returns: TRUE if sucessful. + */ + +/* ARGSUSED */ +static Boolean +DoSearch(search) +struct SearchAndReplace * search; +{ + char msg[BUFSIZ]; + Widget tw = XtParent(search->search_popup); + XawTextPosition pos; + XawTextScanDirection dir; + XawTextBlock text; + + text.ptr = GetString(search->search_text); + text.length = strlen(text.ptr); + text.firstPos = 0; + text.format = FMT8BIT; + + dir = (XawTextScanDirection)(int) ((caddr_t)XawToggleGetCurrent(search->left_toggle) - + R_OFFSET); + + pos = XawTextSearch( tw, dir, &text); + + if (pos == XawTextSearchError) + sprintf( msg, "Could not find string '%s'.", text.ptr); + else { + if (dir == XawsdRight) + XawTextSetInsertionPoint( tw, pos + text.length); + else + XawTextSetInsertionPoint( tw, pos); + + XawTextSetSelection( tw, pos, pos + text.length); + search->selection_changed = FALSE; /* selection is good. */ + return(TRUE); + } + + XawTextUnsetSelection(tw); + SetSearchLabels(search, msg, "", TRUE); + return(FALSE); +} + + +/* Function Name: SetSearchLabels + * Description: Sets both the search labels, and also rings the bell + * HACKED: Only one label needed now + * Arguments: search - the search structure. + * msg1, msg2 - message to put in each search label. + * bell - if TRUE then ring bell. + * Returns: none. + */ + +static void +SetSearchLabels(struct SearchAndReplace *search, String msg1, String msg2, + Boolean bell) +{ +msg2 = msg2; /* PH - shuts compilers up */ + (void) SetResource( search->label1, XtNlabel, (XtArgVal) msg1); + /* (void) SetResource( search->label2, XtNlabel, (XtArgVal) msg2); */ + if (bell) + XBell(XtDisplay(search->search_popup), 0); +} + +/************************************************************ + * + * This section of the file contains utility routines used by + * other functions in this file. + * + ************************************************************/ + + +/* Function Name: SetResource + * Description: Sets a resource in a widget + * Arguments: w - the widget. + * res_name - name of the resource. + * value - the value of the resource. + * Returns: none. + */ + +static void +SetResource(w, res_name, value) +Widget w; +char * res_name; +XtArgVal value; +{ + Arg args[1]; + + XtSetArg(args[0], res_name, value); + XtSetValues( w, args, ONE ); +} + +/* Function Name: GetString + * Description: Gets the value for the string in the popup. + * Arguments: text - the text widget whose string we will get. + * Returns: the string. + */ + +static String +GetString(text) +Widget text; +{ + String string; + Arg args[1]; + + XtSetArg( args[0], XtNstring, &string ); + XtGetValues( text, args, ONE ); + return(string); +} + +/* Function Name: CenterWidgetOnPoint. + * Description: Centers a shell widget on a point relative to + * the root window. + * Arguments: w - the shell widget. + * event - event containing the location of the point + * Returns: none. + * + * NOTE: The widget is not allowed to go off the screen. + */ + +static void +CenterWidgetOnPoint(w, event) +Widget w; +XEvent *event; +{ + Arg args[3]; + Cardinal num_args; + Dimension width, height, b_width; + Position x=0, y=0, max_x, max_y; + + if (event != NULL) { + switch (event->type) { + case ButtonPress: + case ButtonRelease: + x = event->xbutton.x_root; + y = event->xbutton.y_root; + break; + case KeyPress: + case KeyRelease: + x = event->xkey.x_root; + y = event->xkey.y_root; + break; + default: + return; + } + } + + num_args = 0; + XtSetArg(args[num_args], XtNwidth, &width); num_args++; + XtSetArg(args[num_args], XtNheight, &height); num_args++; + XtSetArg(args[num_args], XtNborderWidth, &b_width); num_args++; + XtGetValues(w, args, num_args); + + width += 2 * b_width; + height += 2 * b_width; + + x -= ( (Position) width/2 ); + if (x < 0) x = 0; + if ( x > (max_x = (Position) (XtScreen(w)->width - width)) ) x = max_x; + + y -= ( (Position) height/2 ); + if (y < 0) y = 0; + if ( y > (max_y = (Position) (XtScreen(w)->height - height)) ) y = max_y; + + num_args = 0; + XtSetArg(args[num_args], XtNx, x); num_args++; + XtSetArg(args[num_args], XtNy, y); num_args++; + XtSetValues(w, args, num_args); +} + +/* Function Name: CreateDialog + * Description: Actually creates a dialog. + * Arguments: parent - the parent of the dialog - the main text widget. + * ptr - initial_string for the dialog. + * name - name of the dialog. + * func - function to create the children of the dialog. + * Returns: the popup shell of the dialog. + * + * NOTE: + * + * The function argument is passed the following arguements. + * + * form - the from widget that is the dialog. + * ptr - the initial string for the dialog's text widget. + * parent - the parent of the dialog - the main text widget. + */ + +static Widget +CreateDialog(parent, ptr, name, func) +Widget parent; +String ptr, name; +void (*func)(); +{ + Widget popup, form; + Arg args[5]; + Cardinal num_args; + + num_args = 0; + XtSetArg(args[num_args], XtNiconName, name); num_args++; + XtSetArg(args[num_args], XtNgeometry, NULL); num_args++; + XtSetArg(args[num_args], XtNallowShellResize, TRUE); num_args++; + XtSetArg(args[num_args], XtNtransientFor, GetShell(parent)); num_args++; + popup = XtCreatePopupShell(name, transientShellWidgetClass, + parent, args, num_args); + + form = XtCreateManagedWidget(FORM_NAME, formWidgetClass, popup, + NULL, ZERO); + + (*func) (form, ptr, parent); + return(popup); +} + + /* Function Name: GetShell + * Description: Walks up the widget hierarchy to find the + * nearest shell widget. + * Arguments: w - the widget whose parent shell should be returned. + * Returns: The shell widget among the ancestors of w that is the + * fewest levels up in the widget hierarchy. + */ + +static Widget +GetShell(w) +Widget w; +{ + while ((w != NULL) && !XtIsShell(w)) + w = XtParent(w); + + return (w); +} + +/* Add proper prototype to keep IRIX 6 compiler happy. PH */ + +static Boolean InParams(String, String *, Cardinal); + +static Boolean InParams(str, p, n) + String str; + String *p; + Cardinal n; +{ + int i; + for (i=0; i < n; p++, i++) + if (! XmuCompareISOLatin1(*p, str)) return True; + return False; +} + +static char *WM_DELETE_WINDOW = "WM_DELETE_WINDOW"; + +static void WMProtocols(w, event, params, num_params) + Widget w; /* popup shell */ + XEvent *event; + String *params; + Cardinal *num_params; +{ + Atom wm_delete_window; + Atom wm_protocols; + + wm_delete_window = XInternAtom(XtDisplay(w), WM_DELETE_WINDOW, True); + wm_protocols = XInternAtom(XtDisplay(w), "WM_PROTOCOLS", True); + + /* Respond to a recognized WM protocol request iff + * event type is ClientMessage and no parameters are passed, or + * event type is ClientMessage and event data is matched to parameters, or + * event type isn't ClientMessage and parameters make a request. + */ +#define DO_DELETE_WINDOW InParams(WM_DELETE_WINDOW, params, *num_params) + + if ((event->type == ClientMessage && + event->xclient.message_type == wm_protocols && + event->xclient.data.l[0] == wm_delete_window && + (*num_params == 0 || DO_DELETE_WINDOW)) + || + (event->type != ClientMessage && DO_DELETE_WINDOW)) { + +#undef DO_DELETE_WINDOW + + Widget cancel; + char descendant[DISMISS_NAME_LEN + 2]; + sprintf(descendant, "*%s", DISMISS_NAME); + cancel = XtNameToWidget(w, descendant); + if (cancel) XtCallCallbacks(cancel, XtNcallback, (XtPointer)NULL); + } +} + +static void SetWMProtocolTranslations(w) + Widget w; /* realized popup shell */ +{ + int i; + XtAppContext app_context; + Atom wm_delete_window; + static XtTranslations compiled_table; /* initially 0 */ + static XtAppContext *app_context_list; /* initially 0 */ + static Cardinal list_size; /* initially 0 */ + + app_context = XtWidgetToApplicationContext(w); + + /* parse translation table once */ + if (! compiled_table) compiled_table = XtParseTranslationTable + ("WM_PROTOCOLS: XawWMProtocols()\n"); + + /* add actions once per application context */ + for (i=0; i < list_size && app_context_list[i] != app_context; i++) ; + if (i == list_size) { + XtActionsRec actions[1]; + actions[0].string = "XawWMProtocols"; + actions[0].proc = WMProtocols; + list_size++; + app_context_list = (XtAppContext *) XtRealloc + ((char *)app_context_list, list_size * sizeof(XtAppContext)); + XtAppAddActions(app_context, actions, 1); + app_context_list[i] = app_context; + } + + /* establish communication between the window manager and each shell */ + XtAugmentTranslations(w, compiled_table); + wm_delete_window = XInternAtom(XtDisplay(w), WM_DELETE_WINDOW, False); + (void) XSetWMProtocols(XtDisplay(w), XtWindow(w), &wm_delete_window, 1); +} diff --git a/src/exim_monitor/em_globals.c b/src/exim_monitor/em_globals.c new file mode 100644 index 000000000..ae38e9f8a --- /dev/null +++ b/src/exim_monitor/em_globals.c @@ -0,0 +1,197 @@ +/* $Cambridge: exim/src/exim_monitor/em_globals.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim Monitor * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + + +#include "em_hdr.h" + +/* This source module contains all the global variables used in +the exim monitor, including those that are used by the standard +Exim modules that are included in Eximon. For comments on their +usage, see em_hdr.h and globals.h. */ + + +/* The first set are unique to Eximon */ + +Display *X_display; +XtAppContext X_appcon; + +XtActionsRec actionTable[] = { + { "dialogAction", (XtActionProc)dialogAction}}; + +int actionTableSize = sizeof(actionTable)/sizeof(XtActionsRec); + +XtTranslations queue_trans; +XtTranslations text_trans; + +Widget dialog_ref_widget; +Widget toplevel_widget; +Widget log_widget = NULL; +Widget queue_widget; +Widget unhide_widget = NULL; + + +FILE *LOG; + +int action_output = FALSE; +int action_queue_update = TRUE; +uschar actioned_message[24]; +uschar *action_required; +uschar *alternate_config = NULL; + +int body_max = 20000; + +uschar *exim_path = US BIN_DIRECTORY "/exim" + "\0<---------------Space to patch exim_path->"; + +int eximon_initialized = FALSE; + +int log_buffer_size = 10240; +BOOL log_datestamping = FALSE; +int log_depth = 150; +uschar *log_display_buffer; +uschar *log_file = NULL; +uschar log_file_open[256]; +uschar *log_font = NULL; +ino_t log_inode; +long int log_position; +int log_width = 600; + +uschar *menu_event = US"Shift"; +int menu_is_up = FALSE; +int min_height = 162; +int min_width = 103; + +pipe_item *pipe_chain = NULL; + +uschar *qualify_domain = NULL; +int queue_depth = 200; +uschar *queue_font = NULL; +int queue_max_addresses = 10; +skip_item *queue_skip = NULL; +uschar *queue_stripchart_name = NULL; +int queue_update = 60; +int queue_width = 600; + +pcre *yyyymmdd_regex; + +uschar *size_stripchart = NULL; +uschar *size_stripchart_name = NULL; +int spool_is_split = FALSE; +int start_small = FALSE; +int stripchart_height = 90; +int stripchart_number = 1; +pcre **stripchart_regex; +uschar **stripchart_title; +int *stripchart_total; +int stripchart_update = 60; +int stripchart_width = 80; +int stripchart_varstart = 1; + +int text_depth = 200; +int tick_queue_accumulator = 999999; + +uschar *window_title = US"exim monitor"; + + +/***********************************************************/ +/***********************************************************/ + + +/* These ones are used by Exim modules included in Eximon. Not all are +actually relevant to the operation of Eximon. If SPOOL_DIRECTORY is not +defined (Exim was compiled with it unset), just define it empty. The script +that fires up the monitor fishes the value out by using -bP anyway. */ + +#ifndef SPOOL_DIRECTORY +#define SPOOL_DIRECTORY "" +#endif + + +uschar *acl_var[ACL_C_MAX+ACL_M_MAX]; + +uschar *active_hostname = NULL; +BOOL allow_unqualified_recipient = FALSE; +BOOL allow_unqualified_sender = FALSE; +uschar *authenticated_id = NULL; +uschar *authenticated_sender = NULL; + +uschar *big_buffer = NULL; +int big_buffer_size = BIG_BUFFER_SIZE; +int body_linecount = 0; +int body_zerocount = 0; + +BOOL deliver_firsttime = FALSE; +BOOL deliver_freeze = FALSE; +int deliver_frozen_at = 0; +BOOL deliver_manual_thaw = FALSE; +BOOL dont_deliver = FALSE; + +header_line *header_last = NULL; +header_line *header_list = NULL; + +BOOL host_lookup_failed = FALSE; +uschar *interface_address = NULL; +int interface_port = 0; + +BOOL local_error_message = FALSE; +uschar *local_scan_data = NULL; +BOOL log_timezone = FALSE; +int message_age = 0; +uschar *message_id; +uschar *message_id_external; +uschar message_id_option[MESSAGE_ID_LENGTH + 3]; + +int message_linecount = 0; +int message_size = 0; +uschar message_subdir[2] = { 0, 0 }; + +gid_t originator_gid; +uschar *originator_login; +uid_t originator_uid; + +uschar *primary_hostname = NULL; + +int received_count = 0; +uschar *received_protocol = NULL; +int received_time = 0; +int recipients_count = 0; +recipient_item *recipients_list = NULL; +int recipients_list_max = 0; +int running_in_test_harness=FALSE; + +uschar *sender_address = NULL; +uschar *sender_fullhost = NULL; +uschar *sender_helo_name = NULL; +uschar *sender_host_address = NULL; +uschar *sender_host_authenticated = NULL; +uschar *sender_host_name = NULL; +int sender_host_port = 0; +uschar *sender_ident = NULL; +BOOL sender_local = FALSE; +BOOL sender_set_untrusted = FALSE; + +BOOL split_spool_directory = FALSE; +uschar *spool_directory = US SPOOL_DIRECTORY; +int string_datestamp_offset=-1; + +BOOL timestamps_utc = FALSE; +BOOL tls_certificate_verified = FALSE; +uschar *tls_cipher = NULL; +uschar *tls_peerdn = NULL; + +tree_node *tree_duplicates = NULL; +tree_node *tree_nonrecipients = NULL; +tree_node *tree_unusable = NULL; + +uschar *version_date = US"?"; +uschar *version_string = US"?"; + +int warning_count = 0; + +/* End of em_globals.c */ diff --git a/src/exim_monitor/em_hdr.h b/src/exim_monitor/em_hdr.h new file mode 100644 index 000000000..c89b6db55 --- /dev/null +++ b/src/exim_monitor/em_hdr.h @@ -0,0 +1,327 @@ +/* $Cambridge: exim/src/exim_monitor/em_hdr.h,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */; iline// +/************************************************* +* Exim Monitor * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + + +/* This is the general header file for all the modules that comprise +the exim monitor program. */ + +/* If this macro is defined, Eximon will anonymize all email addresses. This +feature is just so that screen shots can be obtained for documentation +purposes! */ + +/* #define ANONYMIZE */ + +/* System compilation parameters */ + +#define queue_index_size 10 /* Size of index into queue */ + +/* Assume most systems have statfs() unless os.h undefines this macro */ + +#define HAVE_STATFS + +/* Bring in the system-dependent stuff */ + +#include "os.h" + + +/* ANSI C includes */ + +#include +#include +#include +#include +#include +#include +#include +#include + +/* Not-fully-ANSI systems (e.g. SunOS4 are missing some things) */ + +#ifndef SEEK_SET +#define SEEK_SET 0 +#define SEEK_CUR 1 +#define SEEK_END 2 +#endif + +/* Unix includes */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* The new standard is statvfs; some OS have statfs. Also arrange +to be able to cut it out altogether for way-out OS that don't have +anything. */ + +#ifdef HAVE_STATFS +#ifdef HAVE_SYS_STATVFS_H +#include + +#else + #define statvfs statfs + #ifdef HAVE_SYS_VFS_H + #include + #ifdef HAVE_SYS_STATFS_H + #include + #endif + #endif + #ifdef HAVE_SYS_MOUNT_H + #include + #endif +#endif +#endif + +#include + +/* Regular expression include */ + +#include "pcre/pcre.h" + +/* Includes from the main source of Exim. We need to have MAXPACKET defined for +the benefit of structs.h. One of these days I should tidy up this interface so +that this kind of kludge isn't needed. */ + +#define MAXPACKET 1024 + +#include "mytypes.h" +#include "macros.h" +#include "config.h" + +#include "local_scan.h" +#include "structs.h" +#include "globals.h" +#include "functions.h" +#include "osfunctions.h" +#include "store.h" + +/* The sys/resource.h header on SunOS 4 causes trouble with the gcc +compiler. Just stuff the bit we want in here; pragmatic easy way out. */ + +#ifdef NO_SYS_RESOURCE_H +#define RLIMIT_NOFILE 6 /* maximum descriptor index + 1 */ +struct rlimit { + int rlim_cur; /* current (soft) limit */ + int rlim_max; /* maximum value for rlim_cur */ +}; +#else +#include +#include +#endif + +/* X11 includes */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* These are required because exim monitor has its own munged +version of the stripchart widget. */ + +#include +#include +#include +#include + +extern WidgetClass mystripChartWidgetClass; + + + +/************************************************* +* Enumerations * +*************************************************/ + +/* Operations on the in-store message queue */ + +enum { queue_noop, queue_add }; + +/* Operations on the destinations queue */ + +enum { dest_noop, dest_add, dest_remove }; + + +/************************************************* +* Structure for destinations * +*************************************************/ + +typedef struct dest_item { + struct dest_item *next; + struct dest_item *parent; + uschar address[1]; +} dest_item; + + + +/************************************************* +* Structure for queue items * +*************************************************/ + +typedef struct queue_item { + struct queue_item *next; + struct queue_item *prev; + struct dest_item *destinations; + int input_time; + int update_time; + int size; + uschar *sender; + uschar name[17]; + uschar seen; + uschar frozen; + uschar dir_char; +} queue_item; + + +/************************************************* +* Structure for queue skip items * +*************************************************/ + +typedef struct skip_item { + struct skip_item *next; + time_t reveal; + uschar text[1]; +} skip_item; + + +/************************************************* +* Structure for delivery displays * +*************************************************/ + +typedef struct pipe_item { + struct pipe_item *next; + int fd; + Widget widget; +} pipe_item; + + + +/************************************************* +* Global variables * +*************************************************/ + +extern Display *X_display; /* Current display */ +extern XtAppContext X_appcon; /* Application context */ +extern XtActionsRec actionTable[]; /* Actions table */ + +extern XtTranslations queue_trans; /* translation table for queue text widget */ +extern XtTranslations text_trans; /* translation table for other text widgets */ + +extern Widget dialog_ref_widget; /* for positioning dialog box */ +extern Widget toplevel_widget; +extern Widget log_widget; /* widget for tail display */ +extern Widget queue_widget; /* widget for queue display */ +extern Widget unhide_widget; /* widget for unhide button */ + +extern FILE *LOG; + +extern int action_output; /* TRUE when wanting action command output */ +extern int action_queue_update; /* controls auto updates */ +extern int actionTableSize; /* # entries in actionTable */ +extern uschar actioned_message[]; /* For menu handling */ +extern uschar *action_required; +extern uschar *alternate_config; /* Alternate Exim configuration file */ + +extern int body_max; /* Max size of body to display */ + +extern int eximon_initialized; /* TRUE when initialized */ + +extern int log_buffer_size; /* size of log buffer */ +extern BOOL log_datestamping; /* TRUE if logs are datestamped */ +extern int log_depth; /* depth of log tail window */ +extern uschar *log_display_buffer; /* to hold display text */ +extern uschar *log_file; /* supplied name of exim log file */ +extern uschar log_file_open[256]; /* actual open file */ +extern uschar *log_font; /* font for log display */ +extern ino_t log_inode; /* the inode of the log file */ +extern long int log_position; /* position in log file */ +extern int log_width; /* width of log tail window */ + +extern uschar *menu_event; /* name of menu event */ +extern int menu_is_up; /* TRUE when menu displayed */ +extern int min_height; /* min window height */ +extern int min_width; /* min window width */ + +extern pipe_item *pipe_chain; /* for delivery displays */ + +extern uschar *qualify_domain; +extern int queue_depth; /* depth of queue window */ +extern uschar *queue_font; /* font for queue display */ +extern int queue_max_addresses; /* limit on per-message list */ +extern skip_item *queue_skip; /* for hiding bits of queue */ +extern uschar *queue_stripchart_name; /* sic */ +extern int queue_update; /* update interval */ +extern int queue_width; /* width of queue window */ + +extern pcre *yyyymmdd_regex; /* for matching yyyy-mm-dd */ + +extern uschar *size_stripchart; /* path for size monitoring */ +extern uschar *size_stripchart_name; /* name for size stripchart */ +extern uschar *spool_directory; /* Name of exim spool directory */ +extern int spool_is_split; /* True if detected split spool */ +extern int start_small; /* True to start with small window */ +extern int stripchart_height; /* height of stripcharts */ +extern int stripchart_number; /* number of stripcharts */ +extern pcre **stripchart_regex; /* vector of regexps */ +extern uschar **stripchart_title; /* vector of titles */ +extern int *stripchart_total; /* vector of accumulating values */ +extern int stripchart_update; /* update interval */ +extern int stripchart_width; /* width of stripcharts */ +extern int stripchart_varstart; /* starting number for variable charts */ + +extern int text_depth; /* depth of text windows */ +extern int tick_queue_accumulator; /* For timing next auto update */ + +extern uschar *window_title; /* title of the exim monitor window */ + + +/************************************************* +* Global functions * +*************************************************/ + +extern XtActionProc dialogAction(Widget, XEvent *, String *, Cardinal *); + +extern uschar *copystring(uschar *); +extern void create_dialog(uschar *, uschar *); +extern void create_stripchart(Widget, uschar *); +extern void debug(char *, ...); +extern dest_item *find_dest(queue_item *, uschar *, int, BOOL); +extern queue_item *find_queue(uschar *, int, int); +extern void init(int, uschar **); +extern void menu_create(Widget, XEvent *, String *, Cardinal *); +extern void NonMessageDialogue(uschar *); +extern void queue_display(void); +extern void read_log(void); +extern int read_spool(uschar *); +extern int read_spool_init(uschar *); +extern void read_spool_tidy(void); +extern int repaint_window(StripChartWidget, int, int); +extern void scan_spool_input(int); +extern void stripchart_init(void); +extern void text_empty(Widget); +extern void text_show(Widget, uschar *); +extern void text_showf(Widget, char *, ...); +extern void xs_SetValues(Widget, Cardinal, ...); + +/* End of em_hdr.h */ diff --git a/src/exim_monitor/em_init.c b/src/exim_monitor/em_init.c new file mode 100644 index 000000000..ca71eb5f0 --- /dev/null +++ b/src/exim_monitor/em_init.c @@ -0,0 +1,239 @@ +/* $Cambridge: exim/src/exim_monitor/em_init.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim monitor * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* This module contains code to initialize things from the +environment and the arguments. */ + + +#include "em_hdr.h" + + + +/************************************************* +* Decode stripchart config * +*************************************************/ + +/* First determine how many are requested, then compile the +regular expressions and save the title strings. Note that +stripchart_number is initialized to 1 or 2 to count the always- +present queue stripchart, and the optional size-monitoring +stripchart. */ + +static void decode_stripchart_config(uschar *s) +{ +int i; + +/* Loop: first time just counts, second time does the +work. */ + +for (i = 0; i <= 1; i++) + + { + int first = 1; + int count = 0; + uschar *p = s; + + if (*p == '/') p++; /* allow optional / at start */ + + /* This loops for all the substrings, using the first flag + to determine whether each is the first or second of the pairs. */ + + while (*p) + { + uschar *pp; + /* Handle continuations */ + if (*p == '\n') + { + while (*(++p) == ' ' || *p == '\t'); + if (*p == '/') p++; + } + + /* Find the end of the string and count if first string */ + + pp = p; + while (*p && *p != '/') p++; + if (first) count++; + + /* Take action on the second time round. */ + + if (i != 0) + { + uschar buffer[256]; + int indx = count + stripchart_varstart - 1; + Ustrncpy(buffer, pp, p-pp); + buffer[p-pp] = 0; + if (first) + { + int offset; + const uschar *error; + stripchart_regex[indx] = pcre_compile(CS buffer, PCRE_COPT, + (const char **)&error, &offset, NULL); + if (stripchart_regex[indx] == NULL) + { + printf("regular expression error: %s at offset %d " + "while compiling %s\n", error, offset, buffer); + exit(99); + } + } + else stripchart_title[indx] = string_copy(buffer); + } + + /* Advance past the delimiter and flip the first/second flag */ + + p++; + first = !first; + } + + /* On the first pass, we now know the number of stripcharts. Get + store for holding the pointers to the regular expressions and + title strings. */ + + if (i == 0) + { + stripchart_number += count; + stripchart_regex = (pcre **)store_malloc(stripchart_number * sizeof(pcre *)); + stripchart_title = (uschar **)store_malloc(stripchart_number * sizeof(uschar *)); + } + } +} + + +/************************************************* +* Initialize * +*************************************************/ + +void init(int argc, uschar **argv) +{ +int x; +int erroroffset; +uschar *s; +const uschar *error; + +argc = argc; /* These are currently unused. */ +argv = argv; + +/* Deal with simple values in the environment. */ + +s = US getenv("ACTION_OUTPUT"); +if (s != NULL) + { + if (Ustrcmp(s, "no") == 0) action_output = FALSE; + if (Ustrcmp(s, "yes") == 0) action_output = TRUE; + } + +s = US getenv("ACTION_QUEUE_UPDATE"); +if (s != NULL) + { + if (Ustrcmp(s, "no") == 0) action_queue_update = FALSE; + if (Ustrcmp(s, "yes") == 0) action_queue_update = TRUE; + } + +s = US getenv("BODY_MAX"); +if (s != NULL && (x = Uatoi(s)) != 0) body_max = x; + +s = US getenv("EXIM_PATH"); +if (s != NULL) exim_path = string_copy(s); + +s = US getenv("EXIMON_EXIM_CONFIG"); +if (s != NULL) alternate_config = string_copy(s); + +s = US getenv("LOG_BUFFER"); +if (s != NULL) + { + uschar c[1]; + if (sscanf(CS s, "%d%c", &x, c) > 0) + { + if (c[0] == 'K' || c[0] == 'k') x *= 1024; + if (x < 1024) x = 1024; + log_buffer_size = x; + } + } + +s = US getenv("LOG_DEPTH"); +if (s != NULL && (x = Uatoi(s)) != 0) log_depth = x; + +s = US getenv("LOG_FILE_NAME"); +if (s != NULL) log_file = string_copy(s); + +s = US getenv("LOG_FONT"); +if (s != NULL) log_font = string_copy(s); + +s = US getenv("LOG_WIDTH"); +if (s != NULL && (x = Uatoi(s)) != 0) log_width = x; + +s = US getenv("MENU_EVENT"); +if (s != NULL) menu_event = string_copy(s); + +s = US getenv("MIN_HEIGHT"); +if (s != NULL && (x = Uatoi(s)) > 0) min_height = x; + +s = US getenv("MIN_WIDTH"); +if (s != NULL && (x = Uatoi(s)) > 0) min_width = x; + +s = US getenv("QUALIFY_DOMAIN"); +if (s != NULL) qualify_domain = string_copy(s); + else qualify_domain = US""; /* Don't want NULL */ + +s = US getenv("QUEUE_DEPTH"); +if (s != NULL && (x = Uatoi(s)) != 0) queue_depth = x; + +s = US getenv("QUEUE_FONT"); +if (s != NULL) queue_font = string_copy(s); + +s = US getenv("QUEUE_INTERVAL"); +if (s != NULL && (x = Uatoi(s)) != 0) queue_update = x; + +s = US getenv("QUEUE_MAX_ADDRESSES"); +if (s != NULL && (x = Uatoi(s)) != 0) queue_max_addresses = x; + +s = US getenv("QUEUE_WIDTH"); +if (s != NULL && (x = Uatoi(s)) != 0) queue_width = x; + +s = US getenv("SPOOL_DIRECTORY"); +if (s != NULL) spool_directory = string_copy(s); + +s = US getenv("START_SMALL"); +if (s != NULL && Ustrcmp(s, "yes") == 0) start_small = 1; + +s = US getenv("TEXT_DEPTH"); +if (s != NULL && (x = Uatoi(s)) != 0) text_depth = x; + +s = US getenv("WINDOW_TITLE"); +if (s != NULL) window_title = string_copy(s); + +/* Deal with stripchart configuration. First see if we are monitoring +the size of a partition, then deal with log stripcharts in a separate +function */ + +s = US getenv("SIZE_STRIPCHART"); +if (s != NULL && *s != 0) + { + stripchart_number++; + stripchart_varstart++; + size_stripchart = string_copy(s); + s = US getenv("SIZE_STRIPCHART_NAME"); + if (s != NULL && *s != 0) size_stripchart_name = string_copy(s); + } + +s = US getenv("LOG_STRIPCHARTS"); +if (s != NULL) decode_stripchart_config(s); + +s = US getenv("STRIPCHART_INTERVAL"); +if (s != NULL && (x = Uatoi(s)) != 0) stripchart_update = x; + +s = US getenv("QUEUE_STRIPCHART_NAME"); +queue_stripchart_name = (s != NULL)? string_copy(s) : US"queue"; + +/* Compile the regex for matching yyyy-mm-dd at the start of a string. */ + +yyyymmdd_regex = pcre_compile("^\\d{4}-\\d\\d-\\d\\d\\s", PCRE_COPT, + (const char **)&error, &erroroffset, NULL); +} + +/* End of em_init.c */ diff --git a/src/exim_monitor/em_log.c b/src/exim_monitor/em_log.c new file mode 100644 index 000000000..5f0f9b29d --- /dev/null +++ b/src/exim_monitor/em_log.c @@ -0,0 +1,398 @@ +/* $Cambridge: exim/src/exim_monitor/em_log.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim Monitor * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* This module contains code for scanning the smaill log, +extracting information from it, and displaying a "tail". */ + +#include "em_hdr.h" + +#define log_buffer_len 4096 /* For each log entry */ + +/* If anonymizing, don't alter these strings (this is all an ad hoc hack). */ + +#ifdef ANONYMIZE +static char *oklist[] = { + "Completed", + "defer", + "from", + "Connection timed out", + "Start queue run: pid=", + "End queue run: pid=", + "host lookup did not complete", + "unexpected disconnection while reading SMTP command from", + "verify failed for SMTP recipient", + "H=", + "U=", + "id=", + "<", + ">", + "(", + ")", + "[", + "]", + "@", + "=", + "*", + ".", + "-", + "\"", + " ", + "\n"}; +static int oklist_size = sizeof(oklist) / sizeof(uschar *); +#endif + + + +/************************************************* +* Write to the log display * +*************************************************/ + +static int visible = 0; +static int scrolled = FALSE; +static int size = 0; +static int top = 0; + +static void show_log(char *s, ...) +{ +int length, newtop; +va_list ap; +XawTextBlock b; +uschar buffer[log_buffer_len + 24]; + +/* Do nothing if not tailing a log */ + +if (log_widget == NULL) return; + +/* Initialize the text block structure */ + +b.firstPos = 0; +b.ptr = CS buffer; +b.format = FMT8BIT; + +/* We want to know whether the window has been scrolled back or not, +so that we can cease automatically scrolling with new text. This turns +out to be tricky with the text widget. We can detect whether the +scroll bar has been operated by checking on the "top" value, but it's +harder to detect that it has been returned to the bottom. The following +heuristic does its best. */ + +newtop = XawTextTopPosition(log_widget); +if (newtop != top) + { + if (!scrolled) + { + visible = size - top; /* save size of window */ + scrolled = newtop < top; + } + else if (newtop > size - visible) scrolled = FALSE; + top = newtop; + } + +/* Format the text that is to be written. */ + +va_start(ap, s); +vsprintf(CS buffer, s, ap); +va_end(ap); +length = Ustrlen(buffer); + +/* If we are anonymizing for screen shots, flatten various things. */ + +#ifdef ANONYMIZE + { + uschar *p = buffer + 9; + if (p[6] == '-' && p[13] == '-') p += 17; + + while (p < buffer + length) + { + int i; + + /* Check for strings to be left alone */ + + for (i = 0; i < oklist_size; i++) + { + int len = Ustrlen(oklist[i]); + if (Ustrncmp(p, oklist[i], len) == 0) + { + p += len; + break; + } + } + if (i < oklist_size) continue; + + /* Leave driver names, size, protocol, alone */ + + if ((*p == 'D' || *p == 'P' || *p == 'T' || *p == 'S' || *p == 'R') && + p[1] == '=') + { + p += 2; + while (*p != ' ' && *p != 0) p++; + continue; + } + + /* Leave C= text alone */ + + if (Ustrncmp(p, "C=\"", 3) == 0) + { + p += 3; + while (*p != 0 && *p != '"') p++; + continue; + } + + /* Flatten remaining chars */ + + if (isdigit(*p)) *p++ = 'x'; + else if (isalpha(*p)) *p++ = 'x'; + else *p++ = '$'; + } + } +#endif + +/* If this would overflow the buffer, throw away 50% of the +current stuff in the buffer. Code defensively against odd +extreme cases that shouldn't actually arise. */ + +if (size + length > log_buffer_size) + { + if (size == 0) length = log_buffer_size/2; else + { + int cutcount = log_buffer_size/2; + if (cutcount > size) cutcount = size; else + { + while (cutcount < size && log_display_buffer[cutcount] != '\n') + cutcount++; + cutcount++; + } + b.length = 0; + XawTextReplace(log_widget, 0, cutcount, &b); + size -= cutcount; + top -= cutcount; + if (top < 0) top = 0; + if (top < cutcount) XawTextInvalidate(log_widget, 0, 999999); + xs_SetValues(log_widget, 1, "displayPosition", top); + } + } + +/* Insert the new text at the end of the buffer. */ + +b.length = length; +XawTextReplace(log_widget, 999999, 999999, &b); +size += length; + +/* When not scrolled back, we want to keep the bottom line +always visible. Put the insert point at the start of it because +this stops left/right scrolling with some X libraries. */ + +if (!scrolled) + { + XawTextSetInsertionPoint(log_widget, size - length); + top = XawTextTopPosition(log_widget); + } +} + + + + +/************************************************* +* Function to read the log * +*************************************************/ + +/* We read any new log entries, and use their data to +updated total counts for the configured stripcharts. +The count for the queue chart is handled separately. +We also munge the log entries and display a one-line +version in the log window. */ + +void read_log(void) +{ +struct stat statdata; +uschar buffer[log_buffer_len]; + +/* If log is not yet open, skip all of this. */ + +if (LOG != NULL) + { + fseek(LOG, log_position, SEEK_SET); + + while (Ufgets(buffer, log_buffer_len, LOG) != NULL) + { + uschar *id; + uschar *p = buffer; + void *reset_point; + int length = Ustrlen(buffer); + int i; + + /* Skip totally blank lines (paranoia: there shouldn't be any) */ + + while (*p == ' ' || *p == '\t') p++; + if (*p == '\n') continue; + + /* We should now have a complete log entry in the buffer; check + it for various regular expression matches and take appropriate + action. Get the current store point so we can reset to it. */ + + reset_point = store_get(0); + + /* First, update any stripchart data values, noting that the zeroth + stripchart is the queue length, which is handled elsewhere, and the + 1st may the a size monitor. */ + + for (i = stripchart_varstart; i < stripchart_number; i++) + { + if (pcre_exec(stripchart_regex[i], NULL, CS buffer, length, 0, PCRE_EOPT, + NULL, 0) >= 0) + stripchart_total[i]++; + } + + /* Munge the log entry and display shortened form on one line. + We omit the date and show only the time. Remove any time zone offset. */ + + if (pcre_exec(yyyymmdd_regex,NULL,CS buffer,length,0,PCRE_EOPT,NULL,0) >= 0) + { + if ((buffer[20] == '+' || buffer[20] == '-') && + isdigit(buffer[21]) && buffer[25] == ' ') + memmove(buffer + 20, buffer + 26, Ustrlen(buffer + 26) + 1); + id = string_copyn(buffer + 20, MESSAGE_ID_LENGTH); + show_log("%s", buffer+11); + } + else + { + id = US""; + show_log("%s", buffer); + } + + /* Deal with frozen and unfrozen messages */ + + if (strstric(buffer, US"frozen", FALSE) != NULL) + { + queue_item *qq = find_queue(id, queue_noop, 0); + if (qq != NULL) + { + if (strstric(buffer, US"unfrozen", FALSE) != NULL) + qq->frozen = FALSE; + else qq->frozen = TRUE; + } + } + + /* Notice defer messages, and add the destination if it + isn't already on the list for this message, with a pointer + to the parent if we can. */ + + if ((p = Ustrstr(buffer, "==")) != NULL) + { + queue_item *qq = find_queue(id, queue_noop, 0); + if (qq != NULL) + { + dest_item *d; + uschar *q, *r; + p += 2; + while (isspace(*p)) p++; + q = p; + while (*p != 0 && !isspace(*p)) + { + if (*p++ != '\"') continue; + while (*p != 0) + { + if (*p == '\\') p += 2; + else if (*p++ == '\"') break; + } + } + *p++ = 0; + if ((r = strstric(q, qualify_domain, FALSE)) != NULL && + *(--r) == '@') *r = 0; + + /* If we already have this destination, as tested case-insensitively, + do not add it to the destinations list. */ + + d = find_dest(qq, q, dest_add, TRUE); + + if (d->parent == NULL) + { + while (isspace(*p)) p++; + if (*p == '<') + { + dest_item *dd; + q = ++p; + while (*p != 0 && *p != '>') p++; + *p = 0; + if ((p = strstric(q, qualify_domain, FALSE)) != NULL && + *(--p) == '@') *p = 0; + dd = find_dest(qq, q, dest_noop, FALSE); + if (dd != NULL && dd != d) d->parent = dd; + } + } + } + } + + store_reset(reset_point); + } + } + + +/* We have to detect when the log file is changed, and switch to the new file. +In practice, for non-datestamped files, this means that some deliveries might +go unrecorded, since they'll be written to the old file, but this usually +happens in the middle of the night, and I don't think the hassle of keeping +track of two log files is worth it. + +First we check the datestamped name of the log file if necessary; if it is +different to the file we currently have open, go for the new file. As happens +in Exim itself, we leave in the following inode check, even when datestamping +because it does no harm and will cope should a file actually be renamed for +some reason. + +The test for a changed log file is to look up the inode of the file by name and +compare it with the saved inode of the file we currently are processing. This +accords with the usual interpretation of POSIX and other Unix specs that imply +"one file, one inode". However, it appears that on some Digital systems, if an +open file is unlinked, a new file may be created with the same inode while the +old file remains in existence. This can happen if the old log file is renamed, +processed in some way, and then deleted. To work round this, also test for a +link count of zero on the currently open file. */ + +if (log_datestamping) + { + uschar log_file_wanted[256]; + string_format(log_file_wanted, sizeof(log_file_wanted), CS log_file); + if (Ustrcmp(log_file_wanted, log_file_open) != 0) + { + if (LOG != NULL) + { + fclose(LOG); + LOG = NULL; + } + Ustrcpy(log_file_open, log_file_wanted); + } + } + +if (LOG == NULL || + (fstat(fileno(LOG), &statdata) == 0 && statdata.st_nlink == 0) || + (Ustat(log_file, &statdata) == 0 && log_inode != statdata.st_ino)) + { + FILE *TEST; + + /* Experiment shows that sometimes you can't immediately open + the new log file - presumably immediately after the old one + is renamed and before the new one exists. Therefore do a + trial open first to be sure. */ + + if ((TEST = fopen(CS log_file_open, "r")) != NULL) + { + if (LOG != NULL) fclose(LOG); + LOG = TEST; + fstat(fileno(LOG), &statdata); + log_inode = statdata.st_ino; + } + } + +/* Save the position we have got to in the log. */ + +if (LOG != NULL) log_position = ftell(LOG); +} + +/* End of em_log.c */ diff --git a/src/exim_monitor/em_main.c b/src/exim_monitor/em_main.c new file mode 100644 index 000000000..6de846624 --- /dev/null +++ b/src/exim_monitor/em_main.c @@ -0,0 +1,939 @@ +/* $Cambridge: exim/src/exim_monitor/em_main.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim Monitor * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + + +#include "em_hdr.h" + +/* This module contains the main program of the Exim monitor, which +sets up the world and then lets the XtAppMainLoop function +run things off X events. */ + + +/************************************************* +* Static variables * +*************************************************/ + +/* Fallback resources */ + +static String fallback_resources[] = {"eximon.geometry: +150+0", NULL}; + +/* X11 fixed argument lists */ + +static Arg quit_args[] = { + {XtNfromVert, (XtArgVal) NULL}, /* must be first */ + {XtNlabel, (XtArgVal) " Quit "}, + {"left", XawChainLeft}, + {"right", XawChainLeft}, + {"top", XawChainTop}, + {"bottom", XawChainTop} +}; + +static Arg resize_args[] = { + {XtNfromVert, (XtArgVal) NULL}, /* must be first */ + {XtNfromHoriz,(XtArgVal) NULL}, /* must be second */ + {XtNlabel, (XtArgVal) " Size "}, + {"left", XawChainLeft}, + {"right", XawChainLeft}, + {"top", XawChainTop}, + {"bottom", XawChainTop} +}; + +static Arg update_args[] = { + {XtNfromVert, (XtArgVal) NULL}, /* must be first */ + {XtNlabel, (XtArgVal) " Update "}, + {"left", XawChainLeft}, + {"right", XawChainLeft}, + {"top", XawChainTop}, + {"bottom", XawChainTop} +}; + +static Arg hide_args[] = { + {XtNfromVert, (XtArgVal) NULL}, /* must be first */ + {XtNfromHoriz,(XtArgVal) NULL}, /* must be second */ + {XtNlabel, (XtArgVal) " Hide "}, + {"left", XawChainLeft}, + {"right", XawChainLeft}, + {"top", XawChainTop}, + {"bottom", XawChainTop} +}; + +static Arg unhide_args[] = { + {XtNfromVert, (XtArgVal) NULL}, /* must be first */ + {XtNfromHoriz,(XtArgVal) NULL}, /* must be second */ + {XtNlabel, (XtArgVal) " Unhide "}, + {"left", XawChainLeft}, + {"right", XawChainLeft}, + {"top", XawChainTop}, + {"bottom", XawChainTop} +}; + +static Arg log_args[] = { + {XtNfromVert, (XtArgVal) NULL}, /* must be first */ + {"editType", XawtextEdit}, + {"useStringInPlace", (XtArgVal)TRUE}, + {"string", (XtArgVal)""}, /* dummy to get it going */ + {"scrollVertical", XawtextScrollAlways}, + {"scrollHorizontal", XawtextScrollAlways}, + {"right", XawChainRight}, + {"top", XawChainTop}, + {"bottom", XawChainTop} +}; + +static Arg queue_args[] = { + {XtNfromVert, (XtArgVal) NULL}, /* must be first */ + {"editType", XawtextEdit}, + {"string", (XtArgVal)""}, /* dummy to get it going */ + {"scrollVertical", XawtextScrollAlways}, + {"right", XawChainRight}, + {"top", XawChainTop}, + {"bottom", XawChainBottom} +}; + +static Arg sizepos_args[] = { + {"width", (XtArgVal)NULL}, + {"height", (XtArgVal)NULL}, + {"x", (XtArgVal)NULL}, + {"y", (XtArgVal)NULL} +}; + +XtActionsRec menu_action_table[] = { + { "menu-create", menu_create } }; + +/* Types of non-message dialog action */ + +enum { da_hide }; + +/* Miscellaneous local variables */ + +static int dialog_action; +static int tick_stripchart_accumulator = 999999; +static int tick_interval = 2; +static int maxposset = 0; +static int minposset = 0; +static int x_adjustment = -1; +static int y_adjustment = -1; +static Dimension screenwidth, screenheight; +static Dimension original_x, original_y; +static Dimension maxposx, maxposy; +static Dimension minposx, minposy; +static Dimension maxwidth, maxheight; +static Widget outer_form_widget; +static Widget hide_widget; +static Widget above_queue_widget; + + + + +#ifdef STRERROR_FROM_ERRLIST +/************************************************* +* Provide strerror() for non-ANSI libraries * +*************************************************/ + +/* Some old-fashioned systems still around (e.g. SunOS4) don't have strerror() +in their libraries, but can provide the same facility by this simple +alternative function. */ + +uschar * +strerror(int n) +{ +if (n < 0 || n >= sys_nerr) return "unknown error number"; +return sys_errlist[n]; +} +#endif /* STRERROR_FROM_ERRLIST */ + + + +/************************************************* +* Handle attempts to write the log * +*************************************************/ + +/* The message gets written to stderr when log_write() is called from a +utility. The message always gets '\n' added on the end of it. These calls come +from modules such as store.c when things go drastically wrong (e.g. malloc() +failing). In normal use they won't get obeyed. + +Arguments: + selector not relevant when running a utility + flags not relevant when running a utility + format a printf() format + ... arguments for format + +Returns: nothing +*/ + +void +log_write(unsigned int selector, int flags, char *format, ...) +{ +va_list ap; +va_start(ap, format); +vfprintf(stderr, format, ap); +fprintf(stderr, "\n"); +va_end(ap); +selector = selector; /* Keep picky compilers happy */ +flags = flags; +} + + + + +/************************************************* +* Extract port from address string * +*************************************************/ + +/* In the spool file, a host plus port is given as an IP address followed by a +dot and a port number. This function decodes this. It is needed by the +spool-reading function, and copied here to avoid having to include the whole +host.c module. One day the interaction between exim and eximon with regard to +included code MUST be tidied up! + +Argument: + address points to the string; if there is a port, the '.' in the string + is overwritten with zero to terminate the address + +Returns: 0 if there is no port, else the port number. +*/ + +int +host_extract_port(uschar *address) +{ +int skip = -3; /* Skip 3 dots in IPv4 addresses */ +address--; +while (*(++address) != 0) + { + int ch = *address; + if (ch == ':') skip = 0; /* Skip 0 dots in IPv6 addresses */ + else if (ch == '.' && skip++ >= 0) break; + } +if (*address == 0) return 0; +*address++ = 0; +return Uatoi(address); +} + + + + +/************************************************* +* SIGCHLD handler * +*************************************************/ + +/* Operations on messages are done in subprocesses; this handler +just catches them when they finish. It causes a queue display update +unless configured not to. */ + +static void sigchld_handler(int sig) +{ +while (waitpid(-1, NULL, WNOHANG) > 0); +signal(sig, sigchld_handler); +if (action_queue_update) tick_queue_accumulator = 999999; +} + + + +/************************************************* +* Callback routines * +*************************************************/ + + +void updateAction(Widget w, XtPointer client_data, XtPointer call_data) +{ +w = w; /* Keep picky compilers happy */ +client_data = client_data; +call_data = call_data; +scan_spool_input(TRUE); +queue_display(); +tick_queue_accumulator = 0; +} + +void hideAction(Widget w, XtPointer client_data, XtPointer call_data) +{ +w = w; /* Keep picky compilers happy */ +client_data = client_data; +call_data = call_data; +actioned_message[0] = 0; +dialog_ref_widget = w; +dialog_action = da_hide; +create_dialog(US"Hide addresses ending with", US""); +} + +void unhideAction(Widget w, XtPointer client_data, XtPointer call_data) +{ +skip_item *sk = queue_skip; + +w = w; /* Keep picky compilers happy */ +client_data = client_data; +call_data = call_data; + +while (sk != NULL) + { + skip_item *next = sk->next; + store_free(sk); + sk = next; + } +queue_skip = NULL; + +XtDestroyWidget(unhide_widget); +unhide_widget = NULL; + +scan_spool_input(TRUE); +queue_display(); +tick_queue_accumulator = 0; +} + +void quitAction(Widget w, XtPointer client_data, XtPointer call_data) +{ +w = w; /* Keep picky compilers happy */ +client_data = client_data; +call_data = call_data; +exit(0); +} + + +/* Action when the "Size" button is pressed. This is a kludged up mess +that I made work after much messing around. Reading the position of the +toplevel widget gets the absolute position of the data portion of the window, +excluding the window manager's furniture. However, positioning the toplevel +widget's window seems to position the top corner of the furniture under the twm +window manager, but not under fwvm and others. The two cases are distinguished +by the values of x_adjustment and y_adjustment. + +For twm (adjustment >= 0), one has to fudge the miminizing function to ensure +that we go back to exactly the same position as before. + +For fwvm (adjustment < 0), one has to fudge the "top left hand corner" +positioning to ensure that the window manager's furniture gets displayed on the +screen. I haven't found a way of discovering the thickness of the furniture, so +some screwed-in values are used. + +This is all ad hoc, developed by floundering around as I haven't found any +documentation that tells me what I really should do. */ + +void resizeAction(Widget button, XtPointer client_data, XtPointer call_data) +{ +Dimension x, y; +Dimension width, height; +XWindowAttributes a; +Window w = XtWindow(toplevel_widget); + +button = button; /* Keep picky compilers happy */ +client_data = client_data; +call_data = call_data; + +/* Get the position and size of the top level widget. */ + +sizepos_args[0].value = (XtArgVal)(&width); +sizepos_args[1].value = (XtArgVal)(&height); +sizepos_args[2].value = (XtArgVal)(&x); +sizepos_args[3].value = (XtArgVal)(&y); +XtGetValues(toplevel_widget, sizepos_args, 4); + +/* Get the position of the widget's window relative to its parent; this +gives the thickness of the window manager's furniture. At least it does +in twm. For fwvm it gives zero. The size/movement function uses this data. +I tried doing this before entering the main loop, but it didn't always +work properly with twm. Running it every time seems to be OK. */ + +XGetWindowAttributes(X_display, XtWindow(toplevel_widget), &a); +if (a.x != 0) x_adjustment = a.x; +if (a.y != 0) y_adjustment = a.y; + +/* If at maximum size, reduce to minimum and move back to where it was +when maximized, if that value is set, allowing for the furniture in cases +where the positioning includes the furniture. */ + +if (width == maxwidth && height == maxheight) + { + maxposx = x; + maxposy = y; + maxposset = 1; + + if (minposset) + xs_SetValues(toplevel_widget, 4, + "width", min_width, + "height", min_height, + "x", minposx - ((x_adjustment >= 0)? x_adjustment : 0), + "y", minposy - ((y_adjustment >= 0)? y_adjustment : 0)); + else + xs_SetValues(toplevel_widget, 2, + "width", min_width, + "height", min_height); + } + +/* Else always expand to maximum. If currently at minimum size, remember where +it was for coming back. If we don't have a value for the thickness of the +furniture, the implication is that the coordinates position the application +window, so we can't use (0,0) because that loses the furniture. Use screwed in +values that seem to work with fvwm. */ + +else + { + int xx = x; + int yy = y; + + if (width == min_width && height == min_height) + { + minposx = x; + minposy = y; + minposset = 1; + } + + if ((int)(x + maxwidth) > (int)screenwidth || + (int)(y + maxheight + 10) > (int)screenheight) + { + if (maxposset) + { + xx = maxposx - ((x_adjustment >= 0)? x_adjustment : 0); + yy = maxposy - ((y_adjustment >= 0)? y_adjustment : 0); + } + else + { + if ((int)(x + maxwidth) > (int)screenwidth) + xx = (x_adjustment >= 0)? 0 : 4; + if ((int)(y + maxheight + 10) > (int)screenheight) + yy = (y_adjustment >= 0)? 0 : 21; + } + + xs_SetValues(toplevel_widget, 4, + "width", maxwidth, + "height", maxheight, + "x", xx, + "y", yy); + } + + else xs_SetValues(toplevel_widget, 2, + "width", maxwidth, + "height", maxheight); + } + +/* Ensure the window is at the top */ + +XRaiseWindow(X_display, w); +} + + + + +/************************************************* +* Handle input from non-msg dialogue * +*************************************************/ + +/* The various cases here are: hide domain, (no more yet) */ + +void NonMessageDialogue(uschar *s) +{ +skip_item *sk; + +switch(dialog_action) + { + case da_hide: + + /* Create the unhide button if not present */ + + if (unhide_widget == NULL) + { + unhide_args[0].value = (XtArgVal) above_queue_widget; + unhide_args[1].value = (XtArgVal) hide_widget; + unhide_widget = XtCreateManagedWidget("unhide", commandWidgetClass, + outer_form_widget, unhide_args, XtNumber(unhide_args)); + XtAddCallback(unhide_widget, "callback", unhideAction, NULL); + } + + /* Add item to skip queue */ + + sk = (skip_item *)store_malloc(sizeof(skip_item) + Ustrlen(s)); + sk->next = queue_skip; + queue_skip = sk; + Ustrcpy(sk->text, s); + sk->reveal = time(NULL) + 60 * 60; + scan_spool_input(TRUE); + queue_display(); + tick_queue_accumulator = 0; + break; + } +} + + + +/************************************************* +* Ticker function * +*************************************************/ + +/* This function is called initially to set up the starting data +values; it then sets a timeout so that it continues to be called +every 2 seconds. */ + +static void ticker(XtPointer pt, XtIntervalId *i) +{ +pipe_item **pp = &pipe_chain; +pipe_item *p = pipe_chain; +tick_queue_accumulator += tick_interval; +tick_stripchart_accumulator += tick_interval; +read_log(); + +pt = pt; /* Keep picky compilers happy */ +i = i; + +/* If we have passed the queue update time, we must do a full +scan of the queue, checking for new arrivals, etc. This will +as a by-product set the count of items for use by the stripchart +display. On some systems, SIGCHLD signals can get lost at busy times, +so just in case, clean up any completed children here. */ + +if (tick_queue_accumulator >= queue_update) + { + scan_spool_input(TRUE); + queue_display(); + tick_queue_accumulator = 0; + if (tick_stripchart_accumulator >= stripchart_update) + tick_stripchart_accumulator = 0; + while (waitpid(-1, NULL, WNOHANG) > 0); + } + +/* Otherwise, if we have exceeded the stripchart interval, +do a reduced queue scan that simply provides the count for +the stripchart. */ + +else if (tick_stripchart_accumulator >= stripchart_update) + { + scan_spool_input(FALSE); + tick_stripchart_accumulator = 0; + } + +/* Scan any pipes that are set up for listening to delivery processes, +and display their output if their windows are still open. */ + +while (p != NULL) + { + int count; + uschar buffer[256]; + + while ((count = read(p->fd, buffer, 254)) > 0) + { + buffer[count] = 0; + if (p->widget != NULL) text_show(p->widget, buffer); + } + + if (count == 0) + { + close(p->fd); + *pp = p->next; + store_free(p); + /* If configured, cause display update */ + if (action_queue_update) tick_queue_accumulator = 999999; + } + + else pp = &(p->next); + + p = *pp; + } + +/* Reset the timer for next time */ + +XtAppAddTimeOut(X_appcon, tick_interval * 1000, ticker, 0); +} + + + +/************************************************* +* Find Num Lock modifiers * +*************************************************/ + +/* Return a string with the modifiers generated by XK_Num_Lock, or return +NULL if XK_Num_Lock doesn't generate any modifiers. This is needed because Num +Lock isn't always the same modifier on all servers. + +Arguments: + display the Display + buf a buffer in which to put the answers (long enough to hold 5) + +Returns: points to the buffer, or NULL +*/ + +static uschar * +numlock_modifiers(Display *display, uschar *buf) +{ +XModifierKeymap *m; +int i, j; +uschar *ret = NULL; + +m = XGetModifierMapping(display); +if (m == NULL) + { + printf("Not enough memory\n"); + exit (EXIT_FAILURE); + } + +/* Look at Mod1 through Mod5, and fill in the buffer as necessary. */ + +buf[0] = 0; +for (i = 3; i < 8; i++) + { + for (j = 0; j < m->max_keypermod; j++) + { + if (XKeycodeToKeysym(display, m->modifiermap [i*m->max_keypermod + j], 0) + == XK_Num_Lock) + { + sprintf(CS(buf+Ustrlen(buf)), " Mod%d", i-2); + ret = buf; + } + } + } + +XFreeModifiermap(m); +return ret; +} + + + +/************************************************* +* Initialize * +*************************************************/ + +int main(int argc, char **argv) +{ +int i; +struct stat statdata; +uschar modbuf[] = " Mod1 Mod2 Mod3 Mod4 Mod5"; +uschar *numlock; +Widget stripchart_form_widget, + update_widget, + quit_widget, + resize_widget; + +/* The exim global message_id needs to get set */ + +message_id_external = message_id_option + 1; +message_id = message_id_external + 1; +message_subdir[1] = 0; + +/* Some store needs getting for big_buffer, which is used for +constructing file names and things. This call will initialize +the store_get() function. */ + +big_buffer_size = 1024; +big_buffer = store_get(big_buffer_size); + +/* Set up the version string and date and output them */ + +version_init(); +printf("\nExim Monitor version %s (compiled %s) initializing\n", + version_string, version_date); + +/* Initialize various things from the environment and arguments. */ + +init(argc, USS argv); + +/* Set up the SIGCHLD handler */ + +signal(SIGCHLD, sigchld_handler); + +/* Get the buffer for storing the string for the log display. */ + +log_display_buffer = (uschar *)store_malloc(log_buffer_size); +log_display_buffer[0] = 0; + +/* Initialize the data structures for the stripcharts */ + +stripchart_init(); + +/* If log_file contains the empty string, then Exim is running using syslog +only, and we can't tail the log. If not, open the log file and position to the +end of it. Before doing so, we have to detect whether the log files are +datestamped, and if so, sort out the name. The string in log_file already has +%s replaced by "main"; if datestamping is occurring, %D will be present. In +fact, we don't need to test explicitly - just process the string with +string_format. + +Once opened, save the file's inode so that we can detect when the file is +switched to another one for non-datestamped files. However, allow the monitor +to start up without a log file (can happen if no messages have been sent +today.) */ + +if (log_file[0] != 0) + { + (void)string_format(log_file_open, sizeof(log_file_open), CS log_file); + log_datestamping = string_datestamp_offset >= 0; + + LOG = fopen(CS log_file_open, "r"); + + if (LOG == NULL) + { + printf("*** eximon warning: can't open log file %s - will try " + "periodically\n", log_file_open); + } + else + { + fseek(LOG, 0, SEEK_END); + log_position = ftell(LOG); + fstat(fileno(LOG), &statdata); + log_inode = statdata.st_ino; + } + } +else + { + printf("*** eximon warning: no log file available to tail\n"); + } + +/* Now initialize the X world and create the top-level widget */ + +toplevel_widget = XtAppInitialize(&X_appcon, "Eximon", NULL, 0, &argc, argv, + fallback_resources, NULL, 0); +X_display = XtDisplay(toplevel_widget); +xs_SetValues(toplevel_widget, 4, + "title", window_title, + "iconName", window_title, + "minWidth", min_width, + "minHeight", min_height); + + +/* Create the action for setting up the menu in the queue display +window, and register the action for positioning the menu. */ + +XtAppAddActions(X_appcon, menu_action_table, 1); +XawSimpleMenuAddGlobalActions(X_appcon); + +/* Set up translation tables for the text widgets we use. We don't +want all the generality of editing, etc. that the defaults provide. +This cannot be done before initializing X - the parser complains +about unknown events, modifiers, etc. in an unhelpful way... The +queue text widget has a different table which includes the button +for popping up the menu. Note that the order of things in these +tables is significant. Shift must come before as +otherwise it isn't noticed. */ + +/* + : display-caret(on)\n\ + : display-caret(off)\n\ +*/ + +/* The translation manager sets up passive grabs for the menu popups as a +result of MenuPopup(), but the grabs match only the exact modifiers listed, +hence combinations with and without caps-lock and num-lock must be given, +rather than just one "Shift" (or whatever menu_event is set to), +despite the fact that that notation (without a leading !) should ignore the +state of other modifiers. Thanks to Kevin Ryde for this information, and for +the function above that discovers which modifier is Num Lock, because it turns +out that it varies from server to server. */ + +sprintf(CS big_buffer, + "!%s: menu-create() XawPositionSimpleMenu(menu) MenuPopup(menu)\n\ + !Lock %s: menu-create() XawPositionSimpleMenu(menu) MenuPopup(menu)\n\ + ", menu_event, menu_event); + +numlock = numlock_modifiers(X_display, modbuf); /* Get Num Lock modifier(s) */ + +if (numlock != NULL) sprintf(CS big_buffer + Ustrlen(big_buffer), + "!%s %s: menu-create() XawPositionSimpleMenu(menu) MenuPopup(menu)\n\ + !Lock %s %s: menu-create() XawPositionSimpleMenu(menu) MenuPopup(menu)\n\ + ", numlock, menu_event, numlock, menu_event); + +sprintf(CS big_buffer + Ustrlen(big_buffer), + ": select-start()\n\ + : extend-adjust()\n\ + : extend-end(PRIMARY,CUT_BUFFER0)\n\ + : extend-start()\n\ + : extend-adjust()\n\ + : extend-end(PRIMARY,CUT_BUFFER0)\n\ + Up: scroll-one-line-down()\n\ + Down: scroll-one-line-up()\n\ + CtrlR: search(backward)\n\ + CtrlS: search(forward)\n\ + "); + +queue_trans = XtParseTranslationTable(CS big_buffer); + +text_trans = XtParseTranslationTable( + ": select-start()\n\ + : extend-adjust()\n\ + : extend-end(PRIMARY,CUT_BUFFER0)\n\ + : extend-start()\n\ + : extend-adjust()\n\ + : extend-end(PRIMARY,CUT_BUFFER0)\n\ + Up: scroll-one-line-down()\n\ + Down: scroll-one-line-up()\n\ + CtrlR: search(backward)\n\ + CtrlS: search(forward)\n\ + "); + + +/* Create a toplevel form widget to hold all the other things */ + +outer_form_widget = XtCreateManagedWidget("form", formWidgetClass, + toplevel_widget, NULL, 0); + +/* Now create an inner form to hold the stripcharts */ + +stripchart_form_widget = XtCreateManagedWidget("form", formWidgetClass, + outer_form_widget, NULL, 0); +xs_SetValues(stripchart_form_widget, 5, + "defaultDistance", 8, + "left", XawChainLeft, + "right", XawChainLeft, + "top", XawChainTop, + "bottom", XawChainTop); + +/* Create the queue count stripchart and its label. */ + +create_stripchart(stripchart_form_widget, queue_stripchart_name); + +/* If configured, create the size monitoring stripchart, but +only if the OS supports statfs(). */ + +if (size_stripchart != NULL) + { +#ifdef HAVE_STATFS + if (size_stripchart_name == NULL) + { + size_stripchart_name = size_stripchart + Ustrlen(size_stripchart) - 1; + while (size_stripchart_name > size_stripchart && + *size_stripchart_name == '/') size_stripchart_name--; + while (size_stripchart_name > size_stripchart && + *size_stripchart_name != '/') size_stripchart_name--; + } + create_stripchart(stripchart_form_widget, size_stripchart_name); +#else + printf("Can't create size stripchart: statfs() function not available\n"); +#endif + } + +/* Now create the configured input/output stripcharts; note +the total number includes the queue stripchart. */ + +for (i = stripchart_varstart; i < stripchart_number; i++) + create_stripchart(stripchart_form_widget, stripchart_title[i]); + +/* Next in vertical order come the Resize & Quit buttons */ + +quit_args[0].value = (XtArgVal) stripchart_form_widget; +quit_widget = XtCreateManagedWidget("quit", commandWidgetClass, + outer_form_widget, quit_args, XtNumber(quit_args)); +XtAddCallback(quit_widget, "callback", quitAction, NULL); + +resize_args[0].value = (XtArgVal) stripchart_form_widget; +resize_args[1].value = (XtArgVal) quit_widget; +resize_widget = XtCreateManagedWidget("resize", commandWidgetClass, + outer_form_widget, resize_args, XtNumber(resize_args)); +XtAddCallback(resize_widget, "callback", resizeAction, NULL); + +/* In the absence of log tailing, the quit widget is the one above the +queue listing. */ + +above_queue_widget = quit_widget; + +/* Create an Ascii text widget for the log tail display if we are tailing a +log. Skip it if not. */ + +if (log_file[0] != 0) + { + log_args[0].value = (XtArgVal) quit_widget; + log_widget = XtCreateManagedWidget("log", asciiTextWidgetClass, + outer_form_widget, log_args, XtNumber(log_args)); + XawTextDisplayCaret(log_widget, TRUE); + xs_SetValues(log_widget, 6, + "editType", XawtextEdit, + "translations", text_trans, + "string", log_display_buffer, + "length", log_buffer_size, + "height", log_depth, + "width", log_width); + + if (log_font != NULL) + { + XFontStruct *f = XLoadQueryFont(X_display, CS log_font); + if (f != NULL) xs_SetValues(log_widget, 1, "font", f); + } + + above_queue_widget = log_widget; + } + +/* The update button */ + +update_args[0].value = (XtArgVal) above_queue_widget; +update_widget = XtCreateManagedWidget("update", commandWidgetClass, + outer_form_widget, update_args, XtNumber(update_args)); +XtAddCallback(update_widget, "callback", updateAction, NULL); + +/* The hide button */ + +hide_args[0].value = (XtArgVal) above_queue_widget; +hide_args[1].value = (XtArgVal) update_widget; +hide_widget = XtCreateManagedWidget("hide", commandWidgetClass, + outer_form_widget, hide_args, XtNumber(hide_args)); +XtAddCallback(hide_widget, "callback", hideAction, NULL); + +/* Create an Ascii text widget for the queue display. */ + +queue_args[0].value = (XtArgVal) update_widget; +queue_widget = XtCreateManagedWidget("queue", asciiTextWidgetClass, + outer_form_widget, queue_args, XtNumber(queue_args)); +XawTextDisplayCaret(queue_widget, TRUE); + +xs_SetValues(queue_widget, 4, + "editType", XawtextEdit, + "height", queue_depth, + "width", queue_width, + "translations", queue_trans); + +if (queue_font != NULL) + { + XFontStruct *f = XLoadQueryFont(X_display, CS queue_font); + if (f != NULL) xs_SetValues(queue_widget, 1, "font", f); + } + +/* Call the ticker function to get the initial data set up. It +arranges to have itself recalled every 2 seconds. */ + +ticker(NULL, NULL); + +/* Everything is now set up; this flag is used by the regerror +function and also by the queue reader. */ + +eximon_initialized = TRUE; +printf("\nExim Monitor running\n"); + +/* Realize the toplevel and thereby get things displayed */ + +XtRealizeWidget(toplevel_widget); + +/* Find out the size of the initial window, and set that as its +maximum. While we are at it, get the initial position. */ + +sizepos_args[0].value = (XtArgVal)(&maxwidth); +sizepos_args[1].value = (XtArgVal)(&maxheight); +sizepos_args[2].value = (XtArgVal)(&original_x); +sizepos_args[3].value = (XtArgVal)(&original_y); +XtGetValues(toplevel_widget, sizepos_args, 4); + +xs_SetValues(toplevel_widget, 2, + "maxWidth", maxwidth, + "maxHeight", maxheight); + +/* Set up the size of the screen */ + +screenwidth = XDisplayWidth(X_display, 0); +screenheight= XDisplayHeight(X_display,0); + +/* Register the action table */ + +XtAppAddActions(X_appcon, actionTable, actionTableSize); + +/* Reduce the window to the small size if this is wanted */ + +if (start_small) resizeAction(NULL, NULL, NULL); + +/* Enter the application loop which handles things from here +onwards. The return statement is never obeyed, but is needed to +keep pedantic ANSI compilers happy. */ + +XtAppMainLoop(X_appcon); + +return 0; +} + +/* End of em_main.c */ + diff --git a/src/exim_monitor/em_menu.c b/src/exim_monitor/em_menu.c new file mode 100644 index 000000000..ead5923a8 --- /dev/null +++ b/src/exim_monitor/em_menu.c @@ -0,0 +1,986 @@ +/* $Cambridge: exim/src/exim_monitor/em_menu.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim Monitor * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + + +#include "em_hdr.h" + +/* This module contains code for handling the popup menus. */ + +static Widget menushell; +static Widget queue_text_sink; +static Widget dialog_shell, dialog_widget; + +static Widget text_create(uschar *, int); + +static int highlighted_start, highlighted_end, highlighted_x, highlighted_y; + + + +static Arg queue_get_arg[] = { + { "textSink", (XtArgVal)NULL }, + { "textSource", (XtArgVal)NULL }, + { "string", (XtArgVal)NULL } }; + +static Arg dialog_arg[] = { + { "label", (XtArgVal)"dialog" }, + { "value", (XtArgVal)"value" } }; + +static Arg get_pos_args[] = { + {"x", (XtArgVal)NULL }, + {"y", (XtArgVal)NULL } }; + +static Arg menushell_arg[] = { + { "label", (XtArgVal)NULL } }; + +static Arg button_arg[] = { + { XtNfromVert, (XtArgVal) NULL }, /* must be first */ + { XtNlabel, (XtArgVal) " Dismiss " }, + { "left", XawChainLeft }, + { "right", XawChainLeft }, + { "top", XawChainBottom }, + { "bottom", XawChainBottom } }; + +static Arg text_arg[] = { + { XtNfromVert, (XtArgVal) NULL }, /* must be first */ + { "editType", XawtextEdit }, + { "string", (XtArgVal)"" }, /* dummy to get it going */ + { "scrollVertical", XawtextScrollAlways }, + { "wrap", XawtextWrapWord }, + { "top", XawChainTop }, + { "bottom", XawChainBottom } }; + +static Arg item_1_arg[] = { + { XtNfromVert, (XtArgVal)NULL }, /* must be first */ + { "label", (XtArgVal)" Message log" } }; + +static Arg item_2_arg[] = { + { XtNfromVert, (XtArgVal) NULL }, /* must be first */ + { "label", (XtArgVal)" Headers" } }; + +static Arg item_3_arg[] = { + { XtNfromVert, (XtArgVal) NULL }, /* must be first */ + { "label", (XtArgVal)" Body" } }; + +static Arg item_4_arg[] = { + { XtNfromVert, (XtArgVal) NULL }, /* must be first */ + { "label", (XtArgVal)" Deliver message" } }; + +static Arg item_5_arg[] = { + { XtNfromVert, (XtArgVal) NULL }, /* must be first */ + { "label", (XtArgVal)" Freeze message" } }; + +static Arg item_6_arg[] = { + { XtNfromVert, (XtArgVal) NULL }, /* must be first */ + { "label", (XtArgVal)" Thaw message" } }; + +static Arg item_7_arg[] = { + { XtNfromVert, (XtArgVal) NULL }, /* must be first */ + { "label", (XtArgVal)" Give up on msg" } }; + +static Arg item_8_arg[] = { + { XtNfromVert, (XtArgVal) NULL }, /* must be first */ + { "label", (XtArgVal)" Remove message" } }; + +static Arg item_9_arg[] = { + { XtNfromVert, (XtArgVal) NULL }, /* must be first */ + { "label", (XtArgVal)"----------------" } }; + +static Arg item_10_arg[] = { + { XtNfromVert, (XtArgVal) NULL }, /* must be first */ + { "label", (XtArgVal)" Add recipient" } }; + +static Arg item_11_arg[] = { + { XtNfromVert, (XtArgVal) NULL }, /* must be first */ + { "label", (XtArgVal)" Mark delivered" } }; + +static Arg item_12_arg[] = { + { XtNfromVert, (XtArgVal) NULL }, /* must be first */ + { "label", (XtArgVal)" Mark all delivered" } }; + +static Arg item_13_arg[] = { + { XtNfromVert, (XtArgVal) NULL }, /* must be first */ + { "label", (XtArgVal)" Edit sender" } }; + +static Arg item_99_arg[] = { + { XtNfromVert, (XtArgVal) NULL }, /* must be first */ + { "label", (XtArgVal)" " } }; + + + +/************************************************* +* Destroy the menu when popped down * +*************************************************/ + +static void popdownAction(Widget w, XtPointer client_data, XtPointer call_data) +{ +client_data = client_data; /* Keep picky compilers happy */ +call_data = call_data; +if (highlighted_x >= 0) + XawTextSinkDisplayText(queue_text_sink, + highlighted_x, highlighted_y, + highlighted_start, highlighted_end, 0); +XtDestroyWidget(w); +menu_is_up = FALSE; +} + + + +/************************************************* +* Display the message log * +*************************************************/ + +static void msglogAction(Widget w, XtPointer client_data, XtPointer call_data) +{ +int i; +uschar buffer[256]; +Widget text = text_create((uschar *)client_data, text_depth); +FILE *f = NULL; + +w = w; /* Keep picky compilers happy */ +call_data = call_data; + +/* End up with the split version, so message looks right when non-exist */ + +for (i = 0; i < (spool_is_split? 2:1); i++) + { + message_subdir[0] = (i != 0)? ((uschar *)client_data)[5] : 0; + sprintf(CS buffer, "%s/msglog/%s/%s", spool_directory, message_subdir, + (uschar *)client_data); + f = fopen(CS buffer, "r"); + if (f != NULL) break; + } + +if (f == NULL) + text_showf(text, "%s: %s\n", buffer, strerror(errno)); +else + { + while (Ufgets(buffer, 256, f) != NULL) text_show(text, buffer); + fclose(f); + } +} + + + +/************************************************* +* Display the message body * +*************************************************/ + +static void bodyAction(Widget w, XtPointer client_data, XtPointer call_data) +{ +int i; +uschar buffer[256]; +Widget text = text_create((uschar *)client_data, text_depth); +FILE *f = NULL; + +w = w; /* Keep picky compilers happy */ +call_data = call_data; + +for (i = 0; i < (spool_is_split? 2:1); i++) + { + message_subdir[0] = (i != 0)? ((uschar *)client_data)[5] : 0; + sprintf(CS buffer, "%s/input/%s/%s-D", spool_directory, message_subdir, + (uschar *)client_data); + f = fopen(CS buffer, "r"); + if (f != NULL) break; + } + +if (f == NULL) + text_showf(text, "Failed to open file: %s\n", strerror(errno)); +else + { + int count = 0; + while (Ufgets(buffer, 256, f) != NULL) + { + text_show(text, buffer); + count += Ustrlen(buffer); + if (count > body_max) + { + text_show(text, US"\n*** Message length exceeds BODY_MAX ***\n"); + break; + } + } + fclose(f); + } +} + + + +/************************************************* +* Do something to a message * +*************************************************/ + +/* The output is not shown in a window for non-delivery actions that succeed, +unless action_output is set. We can't, however, tell until we have run +the command whether we want the output or not, so the pipe has to be set up in +all cases. */ + +static void ActOnMessage(uschar *id, uschar *action, uschar *address_arg) +{ +int pid; +int pipe_fd[2]; +int delivery = Ustrcmp(action + Ustrlen(action) - 2, "-M") == 0; +uschar *quote = US""; +uschar *at = US""; +uschar *qualify = US""; +uschar buffer[256]; +queue_item *qq; +Widget text = NULL; + +/* If the address arg is not empty and does not contain @ and there is a +qualify domain, qualify it. (But don't qualify '<>'.)*/ + +if (address_arg[0] != 0) + { + quote = US"\'"; + if (Ustrchr(address_arg, '@') == NULL && + Ustrcmp(address_arg, "<>") != 0 && + qualify_domain != NULL && + qualify_domain[0] != 0) + { + at = US"@"; + qualify = qualify_domain; + } + } +sprintf(CS buffer, "%s %s %s %s %s %s%s%s%s%s", exim_path, + (alternate_config == NULL)? US"" : US"-C", + (alternate_config == NULL)? US"" : alternate_config, + action, id, quote, address_arg, at, qualify, quote); + +/* If we know we are going to need the window, create it now. */ + +if (action_output || delivery) + { + text = text_create(id, text_depth); + text_showf(text, "%s\n", buffer); + } + +/* Create the pipe for output. Remember, on most systems pipe[0] is +for reading and pipe[1] is for writing! Solaris, with its two-way +pipes is a trap! */ + +if (pipe(pipe_fd) != 0) + { + if (text == NULL) + { + text = text_create(id, text_depth); + text_showf(text, "%s\n", buffer); + } + text_show(text, US"*** Failed to create pipe ***\n"); + return; + } + +fcntl(pipe_fd[0], F_SETFL, O_NONBLOCK); +fcntl(pipe_fd[1], F_SETFL, O_NONBLOCK); + +/* Delivering a message can take some time, and we want to show the +output as it goes along. This requires subprocesses and is coded below. For +other commands, we can assume an immediate response, and so need not waste +resources with subprocesses. If action_output is FALSE, don't show the +output at all. */ + +if (!delivery) + { + int count, rc; + int save_stdout = dup(1); + int save_stderr = dup(2); + + close(1); + close(2); + + dup2(pipe_fd[1], 1); + dup2(pipe_fd[1], 2); + close(pipe_fd[1]); + + rc = system(CS buffer); + + close(1); + close(2); + + if (action_output || rc != 0) + { + if (text == NULL) + { + text = text_create(id, text_depth); + text_showf(text, "%s\n", buffer); + } + while ((count = read(pipe_fd[0], buffer, 254)) > 0) + { + buffer[count] = 0; + text_show(text, buffer); + } + } + + close(pipe_fd[0]); + + dup2(save_stdout, 1); + dup2(save_stderr, 2); + close(save_stdout); + close(save_stderr); + + /* If action was to change the sender, and it succeeded, we have to + update the in-store data. */ + + if (rc == 0 && Ustrcmp(action + Ustrlen(action) - 4, "-Mes") == 0) + { + queue_item *q = find_queue(id, queue_noop, 0); + if (q != NULL) + { + if (q->sender != NULL) store_free(q->sender); + q->sender = store_malloc(Ustrlen(address_arg) + 1); + Ustrcpy(q->sender, address_arg); + } + } + + /* If configured, cause a display update and return */ + + if (action_queue_update) tick_queue_accumulator = 999999; + return; + } + +/* Message is to be delivered. Ensure that it is marked unfrozen, +because nothing will get written to the log to show that this has +happened. (Other freezing/unfreezings get logged and picked up from +there.) */ + +qq = find_queue(id, queue_noop, 0); +if (qq != NULL) qq->frozen = FALSE; + +/* New, asynchronous code runs in a subprocess for commands that +will take some time. The main process does not wait. There is a +SIGCHLD handler in the main program that cleans up any terminating +sub processes. */ + +if ((pid = fork()) == 0) + { + close(1); + close(2); + + dup2(pipe_fd[1], 1); + dup2(pipe_fd[1], 2); + close(pipe_fd[1]); + + system(CS buffer); + + close(1); + close(2); + close(pipe_fd[0]); + _exit(0); + } + +/* Main process - set up an item for the main ticker to watch. */ + +if (pid < 0) text_showf(text, "Failed to fork: %s\n", strerror(pid)); else + { + pipe_item *p = (pipe_item *)store_malloc(sizeof(pipe_item)); + + if (p == NULL) + { + text_show(text, US"Run out of store\n"); + return; + } + + p->widget = text; + p->fd = pipe_fd[0]; + + p->next = pipe_chain; + pipe_chain = p; + + close(pipe_fd[1]); + } +} + + + + +/************************************************* +* Cause a message to be delivered * +*************************************************/ + +static void deliverAction(Widget w, XtPointer client_data, XtPointer call_data) +{ +w = w; /* Keep picky compilers happy */ +call_data = call_data; +ActOnMessage((uschar *)client_data, US"-v -M", US""); +} + + + +/************************************************* +* Cause a message to be Frozen * +*************************************************/ + +static void freezeAction(Widget w, XtPointer client_data, XtPointer call_data) +{ +w = w; /* Keep picky compilers happy */ +call_data = call_data; +ActOnMessage((uschar *)client_data, US"-Mf", US""); +} + + + +/************************************************* +* Cause a message to be thawed * +*************************************************/ + +static void thawAction(Widget w, XtPointer client_data, XtPointer call_data) +{ +w = w; /* Keep picky compilers happy */ +call_data = call_data; +ActOnMessage((uschar *)client_data, US"-Mt", US""); +} + + + +/************************************************* +* Take action using dialog data * +*************************************************/ + +/* This function is called after a dialog box has been filled +in. It is global because it is set up in the action table at +start-up time. If the string is empty, do nothing. */ + +XtActionProc dialogAction(Widget w, XEvent *event, String *ss, Cardinal *c) +{ +uschar *s = US XawDialogGetValueString(dialog_widget); + +w = w; /* Keep picky compilers happy */ +event = event; +ss = ss; +c = c; + +XtPopdown((Widget)dialog_shell); +XtDestroyWidget((Widget)dialog_shell); +while (isspace(*s)) s++; +if (s[0] != 0) + { + if (actioned_message[0] != 0) + ActOnMessage(actioned_message, action_required, s); + else + NonMessageDialogue(s); /* When called from somewhere else */ + } +return NULL; +} + + + +/************************************************* +* Create a dialog box * +*************************************************/ + +/* The focus is grabbed exclusively, so nothing else can +be done to the application until the box is filled in. This +function is also used by the Hide button handler. */ + +void create_dialog(uschar *label, uschar *value) +{ +Arg warg[4]; +Dimension x, y, xx, yy; +XtTranslations pop_trans; +Widget text; + +/* Get the position of a reference widget so the dialog box can be put +near to it. */ + +get_pos_args[0].value = (XtArgVal)(&x); +get_pos_args[1].value = (XtArgVal)(&y); +XtGetValues(dialog_ref_widget, get_pos_args, 2); + +/* When this is not a message_specific thing, the position of the reference +widget is relative to the window. Get the position of the top level widget and +add to the position. */ + +if (dialog_ref_widget != menushell) + { + get_pos_args[0].value = (XtArgVal)(&xx); + get_pos_args[1].value = (XtArgVal)(&yy); + XtGetValues(toplevel_widget, get_pos_args, 2); + x += xx; + y += yy; + } + +/* Create a transient shell for the dialog box. */ + +XtSetArg(warg[0], XtNtransientFor, queue_widget); +XtSetArg(warg[1], XtNx, x + 50); +XtSetArg(warg[2], XtNy, y + 50); +XtSetArg(warg[3], XtNallowShellResize, True); +dialog_shell = XtCreatePopupShell("forDialog", transientShellWidgetClass, + toplevel_widget, warg, 4); + +/* Create the dialog box. */ + +dialog_arg[0].value = (XtArgVal)label; +dialog_arg[1].value = (XtArgVal)value; +dialog_widget = XtCreateManagedWidget("dialog", dialogWidgetClass, dialog_shell, + dialog_arg, XtNumber(dialog_arg)); + +/* Get the text widget from within the dialog box, give it the keyboard focus, +make it wider than the default, and override its translations to make Return +call the dialog action function. */ + +text = XtNameToWidget(dialog_widget, "value"); +XawTextSetInsertionPoint(text, Ustrlen(value)); +XtSetKeyboardFocus(dialog_widget, text); +xs_SetValues(text, 1, "width", 200); +pop_trans = XtParseTranslationTable( + "Return: dialogAction()\n"); +XtOverrideTranslations(text, pop_trans); + +/* Pop the thing up. */ + +XtPopup(dialog_shell, XtGrabExclusive); +XFlush(X_display); +} + + + + + +/************************************************* +* Cause a recipient to be added * +*************************************************/ + +/* This just sets up the dialog box; the action happens when it has been filled +in. */ + +static void addrecipAction(Widget w, XtPointer client_data, XtPointer call_data) +{ +w = w; /* Keep picky compilers happy */ +call_data = call_data; +Ustrcpy(actioned_message, (uschar *)client_data); +action_required = US"-Mar"; +dialog_ref_widget = menushell; +create_dialog(US"Recipient address to add?", US""); +} + + + +/************************************************* +* Cause an address to be marked delivered * +*************************************************/ + +static void markdelAction(Widget w, XtPointer client_data, XtPointer call_data) +{ +w = w; /* Keep picky compilers happy */ +call_data = call_data; +Ustrcpy(actioned_message, (uschar *)client_data); +action_required = US"-Mmd"; +dialog_ref_widget = menushell; +create_dialog(US"Recipient address to mark delivered?", US""); +} + + +/************************************************* +* Cause all addresses to be marked delivered * +*************************************************/ + +static void markalldelAction(Widget w, XtPointer client_data, XtPointer call_data) +{ +w = w; /* Keep picky compilers happy */ +call_data = call_data; +ActOnMessage((uschar *)client_data, US"-Mmad", US""); +} + + +/************************************************* +* Edit the message's sender * +*************************************************/ + +static void editsenderAction(Widget w, XtPointer client_data, + XtPointer call_data) +{ +queue_item *q; +uschar *sender; +w = w; /* Keep picky compilers happy */ +call_data = call_data; +Ustrcpy(actioned_message, (uschar *)client_data); +q = find_queue(actioned_message, queue_noop, 0); +sender = (q == NULL)? US"" : (q->sender[0] == 0)? US"<>" : q->sender; +action_required = US"-Mes"; +dialog_ref_widget = menushell; +create_dialog(US"New sender address?", sender); +} + + +/************************************************* +* Cause a message to be returned to sender * +*************************************************/ + +static void giveupAction(Widget w, XtPointer client_data, XtPointer call_data) +{ +w = w; /* Keep picky compilers happy */ +call_data = call_data; +ActOnMessage((uschar *)client_data, US"-v -Mg", US""); +} + + + +/************************************************* +* Cause a message to be cancelled * +*************************************************/ + +static void removeAction(Widget w, XtPointer client_data, XtPointer call_data) +{ +w = w; /* Keep picky compilers happy */ +call_data = call_data; +ActOnMessage((uschar *)client_data, US"-Mrm", US""); +} + + + +/************************************************* +* Display a message's headers * +*************************************************/ + +static void headersAction(Widget w, XtPointer client_data, XtPointer call_data) +{ +uschar buffer[256]; +header_line *h, *next; +Widget text = text_create((uschar *)client_data, text_depth); +void *reset_point; + +w = w; /* Keep picky compilers happy */ +call_data = call_data; + +/* Remember the point in the dynamic store so we can recover to it afterwards. +Then use Exim's function to read the header. */ + +reset_point = store_get(0); + +sprintf(CS buffer, "%s-H", (uschar *)client_data); +if (spool_read_header(buffer, TRUE, FALSE) != spool_read_OK) + { + if (errno == ERRNO_SPOOLFORMAT) + { + struct stat statbuf; + sprintf(CS big_buffer, "%s/input/%s", spool_directory, buffer); + if (Ustat(big_buffer, &statbuf) == 0) + text_showf(text, "Format error in spool file %s: size=%d\n", buffer, + statbuf.st_size); + else text_showf(text, "Format error in spool file %s\n", buffer); + } + else text_showf(text, "Read error for spool file %s\n", buffer); + store_reset(reset_point); + return; + } + +if (sender_address != NULL) + { + text_showf(text, "%s sender: <%s>\n", sender_local? "Local" : "Remote", + sender_address); + } + +if (recipients_list != NULL) + { + int i; + text_show(text, US"Recipients:\n"); + for (i = 0; i < recipients_count; i++) + { + text_showf(text, " %s %s\n", + (tree_search(tree_nonrecipients, recipients_list[i].address) == NULL)? + " ":"*", recipients_list[i].address); + } + text_show(text, US"\n"); + } + +for (h = header_list; h != NULL; h = next) + { + next = h->next; + text_showf(text, "%c ", h->type); /* Don't push h->text through a %s */ + text_show(text, h->text); /* expansion as it may be v large */ + } + +store_reset(reset_point); +} + + + + +/************************************************* +* Dismiss a text window * +*************************************************/ + +static void dismissAction(Widget w, XtPointer client_data, XtPointer call_data) +{ +pipe_item *p = pipe_chain; + +w = w; /* Keep picky compilers happy */ +call_data = call_data; + +XtPopdown((Widget)client_data); +XtDestroyWidget((Widget)client_data); + +/* If this is a text widget for a sub-process, clear it out of +the chain so that subsequent data doesn't try to use it. We have +to search the parents of the saved widget to see if one of them +is what we have just destroyed. */ + +while (p != NULL) + { + Widget pp = p->widget; + while (pp != NULL) + { + if (pp == (Widget)client_data) { p->widget = NULL; return; } + pp = XtParent(pp); + } + p = p->next; + } +} + + + +/************************************************* +* Set up popup text window * +*************************************************/ + +static Widget text_create(uschar *name, int height) +{ +Widget textshell, form, text, button; + +/* Create a popup shell widget to display as an additional +toplevel window. */ + +textshell = XtCreatePopupShell("textshell", topLevelShellWidgetClass, + toplevel_widget, NULL, 0); +xs_SetValues(textshell, 4, + "title", name, + "iconName", name, + "minWidth", 100, + "minHeight", 100); + +/* Create a form widget, containing the text widget and the +dismiss button widget. */ + +form = XtCreateManagedWidget("textform", formWidgetClass, + textshell, NULL, 0); +xs_SetValues(form, 1, "defaultDistance", 8); + +text = XtCreateManagedWidget("texttext", asciiTextWidgetClass, + form, text_arg, XtNumber(text_arg)); +xs_SetValues(text, 4, + "editType", XawtextAppend, + "width", 700, + "height", height, + "translations", text_trans); +XawTextDisplayCaret(text, TRUE); + +/* Use the same font as for the queue display */ + +if (queue_font != NULL) + { + XFontStruct *f = XLoadQueryFont(X_display, CS queue_font); + if (f != NULL) xs_SetValues(text, 1, "font", f); + } + +button_arg[0].value = (XtArgVal)text; +button = XtCreateManagedWidget("dismiss", commandWidgetClass, + form, button_arg, XtNumber(button_arg)); +XtAddCallback(button, "callback", dismissAction, (XtPointer)textshell); + +/* Get the toplevel popup displayed, and yield the text widget so +that text can be put into it. */ + +XtPopup(textshell, XtGrabNone); +return text; +} + + + + +/************************************************* +* Set up menu in queue window * +*************************************************/ + +/* We have added an action table that causes this function to +be called, and set up button 2 in the text widgets to call it. */ + +void menu_create(Widget w, XEvent *event, String *actargs, Cardinal *count) +{ +int line; +int i; +uschar *s; +XawTextPosition p; +Widget src, menu_line, item_1, item_2, item_3, item_4, + item_5, item_6, item_7, item_8, item_9, item_10, item_11, + item_12, item_13; +XtTranslations menu_trans = XtParseTranslationTable( + ": highlight()\n\ + : unhighlight()\n\ + : highlight()\n\ + : MenuPopdown()notify()unhighlight()\n\ + "); + +actargs = actargs; /* Keep picky compilers happy */ +count = count; + +/* Get the sink and source and the current text pointer */ + +queue_get_arg[0].value = (XtArgVal)(&queue_text_sink); +queue_get_arg[1].value = (XtArgVal)(&src); +queue_get_arg[2].value = (XtArgVal)(&s); +XtGetValues(w, queue_get_arg, 3); + +/* Find the line number of the pointer in the window, and the +character offset of the top lefthand of the window. */ + +line = (event->xbutton).y / XawTextSinkMaxHeight(queue_text_sink, 1); +p = XawTextTopPosition(w); + +/* Find the start of the line on which the button was clicked. */ + +i = line; +while (i-- > 0) + { + while (s[p] != 0 && s[p++] != '\n'); + } + +/* Now pointing either at 0 or 1st uschar after \n, or very 1st uschar. +If 0, the click was beyond the end of the data; just set up a dummy +menu. (Not easy to ignore as several actions are specified for the +mouse click and it expects this one to set up a menu.) If on a +continuation line, move back to the main line. */ + +if (s[p] == 0) + { + menushell_arg[0].value = (XtArgVal)"No message selected"; + menushell = XtCreatePopupShell("menu", simpleMenuWidgetClass, + queue_widget, menushell_arg, XtNumber(menushell_arg)); + XtAddCallback(menushell, "popdownCallback", popdownAction, NULL); + xs_SetValues(menushell, 2, + "cursor", XCreateFontCursor(X_display, XC_arrow), + "translations", menu_trans); + + /* To keep the widgets in XFree86 happy, we have to create at least one menu + item, it seems. (Openwindows doesn't mind a menu with no items.) Otherwise + there's a complaint about a zero width menu, and a crash. */ + + menu_line = XtCreateManagedWidget("line", smeLineObjectClass, menushell, + NULL, 0); + + item_99_arg[0].value = (XtArgVal)menu_line; + (void)XtCreateManagedWidget("item99", smeBSBObjectClass, menushell, + item_99_arg, XtNumber(item_99_arg)); + + highlighted_x = -1; + return; + } + +while (p > 0 && s[p+11] == ' ') + { + line--; + p--; + while (p > 0 && s[p-1] != '\n') p--; + } + +/* Now pointing at first character of a main line. */ + +Ustrncpy(message_id, s+p+11, MESSAGE_ID_LENGTH); +message_id[MESSAGE_ID_LENGTH] = 0; + +/* Highlight the line being menued, and save its parameters so that it +can be de-highlighted at popdown. */ + +highlighted_start = highlighted_end = p; +while (s[highlighted_end] != '\n') highlighted_end++; +highlighted_x = 17; +highlighted_y = line * XawTextSinkMaxHeight(queue_text_sink, 1) + 2; + +XawTextSinkDisplayText(queue_text_sink, + highlighted_x, highlighted_y, + highlighted_start, highlighted_end, 1); + +/* Create the popup shell and the other widgets that comprise the menu. +Set the translations and pointer shape, and add the callback pointers. */ + +menushell_arg[0].value = (XtArgVal)message_id; +menushell = XtCreatePopupShell("menu", simpleMenuWidgetClass, + queue_widget, menushell_arg, XtNumber(menushell_arg)); +XtAddCallback(menushell, "popdownCallback", popdownAction, NULL); + +xs_SetValues(menushell, 2, + "cursor", XCreateFontCursor(X_display, XC_arrow), + "translations", menu_trans); + +menu_line = XtCreateManagedWidget("line", smeLineObjectClass, menushell, + NULL, 0); + +item_1_arg[0].value = (XtArgVal)menu_line; +item_1 = XtCreateManagedWidget("item1", smeBSBObjectClass, menushell, + item_1_arg, XtNumber(item_1_arg)); +XtAddCallback(item_1, "callback", msglogAction, (XtPointer)message_id); + +item_2_arg[0].value = (XtArgVal)item_1; +item_2 = XtCreateManagedWidget("item2", smeBSBObjectClass, menushell, + item_2_arg, XtNumber(item_2_arg)); +XtAddCallback(item_2, "callback", headersAction, (XtPointer)message_id); + +item_3_arg[0].value = (XtArgVal)item_2; +item_3 = XtCreateManagedWidget("item3", smeBSBObjectClass, menushell, + item_3_arg, XtNumber(item_3_arg)); +XtAddCallback(item_3, "callback", bodyAction, (XtPointer)message_id); + +item_4_arg[0].value = (XtArgVal)item_3; +item_4 = XtCreateManagedWidget("item4", smeBSBObjectClass, menushell, + item_4_arg, XtNumber(item_4_arg)); +XtAddCallback(item_4, "callback", deliverAction, (XtPointer)message_id); + +item_5_arg[0].value = (XtArgVal)item_4; +item_5 = XtCreateManagedWidget("item5", smeBSBObjectClass, menushell, + item_5_arg, XtNumber(item_5_arg)); +XtAddCallback(item_5, "callback", freezeAction, (XtPointer)message_id); + +item_6_arg[0].value = (XtArgVal)item_5; +item_6 = XtCreateManagedWidget("item6", smeBSBObjectClass, menushell, + item_6_arg, XtNumber(item_6_arg)); +XtAddCallback(item_6, "callback", thawAction, (XtPointer)message_id); + +item_7_arg[0].value = (XtArgVal)item_6; +item_7 = XtCreateManagedWidget("item7", smeBSBObjectClass, menushell, + item_7_arg, XtNumber(item_7_arg)); +XtAddCallback(item_7, "callback", giveupAction, (XtPointer)message_id); + +item_8_arg[0].value = (XtArgVal)item_7; +item_8 = XtCreateManagedWidget("item8", smeBSBObjectClass, menushell, + item_8_arg, XtNumber(item_8_arg)); +XtAddCallback(item_8, "callback", removeAction, (XtPointer)message_id); + +item_9_arg[0].value = (XtArgVal)item_8; +item_9 = XtCreateManagedWidget("item9", smeBSBObjectClass, menushell, + item_9_arg, XtNumber(item_9_arg)); + +item_10_arg[0].value = (XtArgVal)item_9; +item_10 = XtCreateManagedWidget("item10", smeBSBObjectClass, menushell, + item_10_arg, XtNumber(item_10_arg)); +XtAddCallback(item_10, "callback", addrecipAction, (XtPointer)message_id); + +item_11_arg[0].value = (XtArgVal)item_10; +item_11 = XtCreateManagedWidget("item11", smeBSBObjectClass, menushell, + item_11_arg, XtNumber(item_11_arg)); +XtAddCallback(item_11, "callback", markdelAction, (XtPointer)message_id); + +item_12_arg[0].value = (XtArgVal)item_11; +item_12 = XtCreateManagedWidget("item12", smeBSBObjectClass, menushell, + item_12_arg, XtNumber(item_12_arg)); +XtAddCallback(item_12, "callback", markalldelAction, (XtPointer)message_id); + +item_13_arg[0].value = (XtArgVal)item_12; +item_13 = XtCreateManagedWidget("item13", smeBSBObjectClass, menushell, + item_13_arg, XtNumber(item_13_arg)); +XtAddCallback(item_13, "callback", editsenderAction, (XtPointer)message_id); + +/* Arrange that the menu pops up with the first item selected. */ + +xs_SetValues(menushell, 1, "popupOnEntry", item_1); + +/* Flag that the menu is up to suppress queue updates. */ + +menu_is_up = TRUE; +} + +/* End of em_menu.c */ diff --git a/src/exim_monitor/em_queue.c b/src/exim_monitor/em_queue.c new file mode 100644 index 000000000..d670720fe --- /dev/null +++ b/src/exim_monitor/em_queue.c @@ -0,0 +1,813 @@ +/* $Cambridge: exim/src/exim_monitor/em_queue.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim Monitor * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + + +#include "em_hdr.h" + + +/* This module contains functions to do with scanning exim's +queue and displaying the data therefrom. */ + + +/* If we are anonymizing for screen shots, define a function to anonymize +addresses. Otherwise, define a macro that does nothing. */ + +#ifdef ANONYMIZE +static uschar *anon(uschar *s) +{ +static uschar anon_result[256]; +uschar *ss = anon_result; +for (; *s != 0; s++) *ss++ = (*s == '@' || *s == '.')? *s : 'x'; +*ss = 0; +return anon_result; +} +#else +#define anon(x) x +#endif + + +/************************************************* +* Static variables * +*************************************************/ + +static int queue_total = 0; /* number of items in queue */ + +/* Table for turning base-62 numbers into binary */ + +static uschar tab62[] = + {0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, /* 0-9 */ + 0,10,11,12,13,14,15,16,17,18,19,20, /* A-K */ + 21,22,23,24,25,26,27,28,29,30,31,32, /* L-W */ + 33,34,35, 0, 0, 0, 0, 0, /* X-Z */ + 0,36,37,38,39,40,41,42,43,44,45,46, /* a-k */ + 47,48,49,50,51,52,53,54,55,56,57,58, /* l-w */ + 59,60,61}; /* x-z */ + +/* Index for quickly finding things in the ordered queue. */ + +static queue_item *queue_index[queue_index_size]; + + + +/************************************************* +* Find/Create/Delete a destination * +*************************************************/ + +/* If the action is dest_noop, then just return item or NULL; +if it is dest_add, then add if not present, and return item; +if it is dest_remove, remove if present and return NULL. The +address is lowercased to start with, unless it begins with +"*", which it does for error messages. */ + +dest_item *find_dest(queue_item *q, uschar *name, int action, BOOL caseless) +{ +dest_item *dd; +dest_item **d = &(q->destinations); + +while (*d != NULL) + { + if ((caseless? strcmpic(name,(*d)->address) : Ustrcmp(name,(*d)->address)) + == 0) + { + dest_item *ddd; + + if (action != dest_remove) return *d; + dd = *d; + *d = dd->next; + store_free(dd); + + /* Unset any parent pointers that were to this address */ + + for (ddd = q->destinations; ddd != NULL; ddd = ddd->next) + { + if (ddd->parent == dd) ddd->parent = NULL; + } + + return NULL; + } + d = &((*d)->next); + } + +if (action != dest_add) return NULL; + +dd = (dest_item *)store_malloc(sizeof(dest_item) + Ustrlen(name)); +Ustrcpy(dd->address, name); +dd->next = NULL; +dd->parent = NULL; +*d = dd; +return dd; +} + + + +/************************************************* +* Clean up a dead queue item * +*************************************************/ + +static void clean_up(queue_item *p) +{ +dest_item *dd = p->destinations; +while (dd != NULL) + { + dest_item *next = dd->next; + store_free(dd); + dd = next; + } +if (p->sender != NULL) store_free(p->sender); +store_free(p); +} + + +/************************************************* +* Set up new queue item * +*************************************************/ + +static queue_item *set_up(uschar *name, int dir_char) +{ +int i, rc, save_errno; +struct stat statdata; +void *reset_point; +uschar *p; +queue_item *q = (queue_item *)store_malloc(sizeof(queue_item)); +uschar buffer[256]; + +/* Initialize the block */ + +q->next = q->prev = NULL; +q->destinations = NULL; +Ustrcpy(q->name, name); +q->seen = TRUE; +q->frozen = FALSE; +q->dir_char = dir_char; +q->sender = NULL; +q->size = 0; + +/* Read the header file from the spool; if there is a failure it might mean +inaccessibility as a result of protections. A successful read will have caused +sender_address to get set and the recipients fields to be initialized. If +there's a format error in the headers, we can still display info from the +envelope. + +Before reading the header remember the position in the dynamic store so that +we can recover the store into which the header is read. All data read by +spool_read_header that is to be preserved is copied into malloc store. */ + +reset_point = store_get(0); +message_size = 0; +message_subdir[0] = dir_char; +sprintf(CS buffer, "%s-H", name); +rc = spool_read_header(buffer, FALSE, TRUE); +save_errno = errno; + +/* If we failed to read the envelope, compute the input time by +interpreting the id as a base-62 number. */ + +if (rc != spool_read_OK && rc != spool_read_hdrerror) + { + int t = 0; + for (i = 0; i < 6; i++) t = t * 62 + tab62[name[i] - '0']; + q->update_time = q->input_time = t; + } + +/* Envelope read; get input time and remove qualify_domain from sender address, +if it's there. */ + +else + { + q->update_time = q->input_time = received_time; + if ((p = strstric(sender_address+1, qualify_domain, FALSE)) != NULL && + *(--p) == '@') *p = 0; + } + +/* If we didn't read the whole header successfully, generate an error +message. If the envelope was read, this appears as a first recipient; +otherwise it sets set up in the sender field. */ + +if (rc != spool_read_OK) + { + uschar *msg; + + if (save_errno == ERRNO_SPOOLFORMAT) + { + struct stat statbuf; + sprintf(CS big_buffer, "%s/input/%s", spool_directory, buffer); + if (Ustat(big_buffer, &statbuf) == 0) + msg = string_sprintf("*** Format error in spool file: size = %d ***", + statbuf.st_size); + else msg = string_sprintf("*** Format error in spool file ***"); + } + else msg = string_sprintf("*** Cannot read spool file ***"); + + if (rc == spool_read_hdrerror) + { + (void)find_dest(q, msg, dest_add, FALSE); + } + else + { + deliver_freeze = FALSE; + sender_address = msg; + recipients_count = 0; + } + } + +/* Now set up the remaining data. */ + +q->frozen = deliver_freeze; + +if (sender_set_untrusted) + { + if (sender_address[0] == 0) + { + q->sender = store_malloc(Ustrlen(originator_login) + 6); + sprintf(CS q->sender, "<> (%s)", originator_login); + } + else + { + q->sender = store_malloc(Ustrlen(sender_address) + + Ustrlen(originator_login) + 4); + sprintf(CS q->sender, "%s (%s)", sender_address, originator_login); + } + } +else + { + q->sender = store_malloc(Ustrlen(sender_address) + 1); + Ustrcpy(q->sender, sender_address); + } + +sender_address = NULL; + +sprintf(CS buffer, "%s/input/%s/%s-D", spool_directory, message_subdir, name); +if (Ustat(buffer, &statdata) == 0) + q->size = message_size + statdata.st_size - SPOOL_DATA_START_OFFSET + 1; + +/* Scan and process the recipients list, skipping any that have already +been delivered, and removing visible names. */ + +if (recipients_list != NULL) + { + for (i = 0; i < recipients_count; i++) + { + uschar *r = recipients_list[i].address; + if (tree_search(tree_nonrecipients, r) == NULL) + { + if ((p = strstric(r+1, qualify_domain, FALSE)) != NULL && + *(--p) == '@') *p = 0; + (void)find_dest(q, r, dest_add, FALSE); + } + } + } + +/* Recover the dynamic store used by spool_read_header(). */ + +store_reset(reset_point); +return q; +} + + + +/************************************************* +* Find/Create a queue item * +*************************************************/ + +/* The queue is kept as a doubly-linked list, sorted by name. However, +to speed up searches, an index into the list is used. This is maintained +by the scan_spool_input function when it goes down the list throwing +out entries that are no longer needed. When the action is "add" and +we don't need to add, mark the found item as seen. */ + + +#ifdef never +static void debug_queue(void) +{ +int i; +int count = 0; +queue_item *p; +printf("\nqueue_total=%d\n", queue_total); + +for (i = 0; i < queue_index_size; i++) + printf("index %d = %d %s\n", i, (int)(queue_index[i]), + (queue_index[i])->name); + +printf("Queue is:\n"); +p = queue_index[0]; +while (p != NULL) + { + count++; + for (i = 0; i < queue_index_size; i++) + { + if (queue_index[i] == p) printf("count=%d index=%d\n", count, (int)p); + } + printf("%d %d %d %s\n", (int)p, (int)p->next, (int)p->prev, p->name); + p = p->next; + } +} +#endif + + + +queue_item *find_queue(uschar *name, int action, int dir_char) +{ +int first = 0; +int last = queue_index_size - 1; +int middle = (first + last)/2; +queue_item *p, *q, *qq; + +/* Handle the empty queue as a special case. */ + +if (queue_total == 0) + { + if (action != queue_add) return NULL; + if ((qq = set_up(name, dir_char)) != NULL) + { + int i; + for (i = 0; i < queue_index_size; i++) queue_index[i] = qq; + queue_total++; + return qq; + } + return NULL; + } + +/* Also handle insertion at the start or end of the queue +as special cases. */ + +if (Ustrcmp(name, (queue_index[0])->name) < 0) + { + if (action != queue_add) return NULL; + if ((qq = set_up(name, dir_char)) != NULL) + { + qq->next = queue_index[0]; + (queue_index[0])->prev = qq; + queue_index[0] = qq; + queue_total++; + return qq; + } + return NULL; + } + +if (Ustrcmp(name, (queue_index[queue_index_size-1])->name) > 0) + { + if (action != queue_add) return NULL; + if ((qq = set_up(name, dir_char)) != NULL) + { + qq->prev = queue_index[queue_index_size-1]; + (queue_index[queue_index_size-1])->next = qq; + queue_index[queue_index_size-1] = qq; + queue_total++; + return qq; + } + return NULL; + } + +/* Use binary chopping on the index to get a range of the queue to search +when the name is somewhere in the middle, if present. */ + +while (middle > first) + { + if (Ustrcmp(name, (queue_index[middle])->name) >= 0) first = middle; + else last = middle; + middle = (first + last)/2; + } + +/* Now search down the part of the queue in which the item must +lie if it exists. Both end points are inclusive - though in fact +the bottom one can only be = if it is the original bottom. */ + +p = queue_index[first]; +q = queue_index[last]; + +for (;;) + { + int c = Ustrcmp(name, p->name); + + /* Already on queue; mark seen if required. */ + + if (c == 0) + { + if (action == queue_add) p->seen = TRUE; + return p; + } + + /* Not on the queue; add an entry if required. Note that set-up might + fail (the file might vanish under our feet). Note also that we know + there is always a previous item to p because the end points are + inclusive. */ + + else if (c < 0) + { + if (action == queue_add) + { + if ((qq = set_up(name, dir_char)) != NULL) + { + qq->next = p; + qq->prev = p->prev; + p->prev->next = qq; + p->prev = qq; + queue_total++; + return qq; + } + } + return NULL; + } + + /* Control should not reach here if p == q, because the name + is supposed to be <= the name of the bottom item. */ + + if (p == q) return NULL; + + /* Else might be further down the queue; continue */ + + p = p->next; + } + +/* Control should never reach here. */ +} + + + +/************************************************* +* Scan the exim spool directory * +*************************************************/ + +/* If we discover that there are subdirectories, set a flag so that the menu +code knows to look for them. We count the entries to set the value for the +queue stripchart, and set up data for the queue display window if the "full" +option is given. */ + +void scan_spool_input(int full) +{ +int i; +int subptr; +int subdir_max = 1; +int count = 0; +int indexptr = 1; +queue_item *p; +struct dirent *ent; +DIR *dd; +uschar input_dir[256]; +uschar subdirs[64]; + +subdirs[0] = 0; +stripchart_total[0] = 0; + +sprintf(CS input_dir, "%s/input", spool_directory); +subptr = Ustrlen(input_dir); +input_dir[subptr+2] = 0; /* terminator for lengthened name */ + +/* Loop for each spool file on the queue - searching any subdirectories that +may exist. When initializing eximon, every file will have to be read. To show +there is progress, output a dot for each one to the standard output. */ + +for (i = 0; i < subdir_max; i++) + { + int subdirchar = subdirs[i]; /* 0 for main directory */ + if (subdirchar != 0) + { + input_dir[subptr] = '/'; + input_dir[subptr+1] = subdirchar; + } + + dd = opendir(CS input_dir); + if (dd == NULL) continue; + + while ((ent = readdir(dd)) != NULL) + { + uschar *name = US ent->d_name; + int len = Ustrlen(name); + + /* If we find a single alphameric sub-directory on the first + pass, add it to the list for subsequent scans, and remember that + we are dealing with a split directory. */ + + if (i == 0 && len == 1 && isalnum(*name)) + { + subdirs[subdir_max++] = *name; + spool_is_split = TRUE; + continue; + } + + /* Otherwise, if it is a header spool file, add it to the list */ + + if (len == SPOOL_NAME_LENGTH && + name[SPOOL_NAME_LENGTH - 2] == '-' && + name[SPOOL_NAME_LENGTH - 1] == 'H') + { + uschar basename[SPOOL_NAME_LENGTH]; + stripchart_total[0]++; + if (!eximon_initialized) { printf("."); fflush(stdout); } + Ustrcpy(basename, name); + basename[SPOOL_NAME_LENGTH - 2] = 0; + if (full) find_queue(basename, queue_add, subdirchar); + } + } + closedir(dd); + } + +/* If simply counting the number, we are done; same if there are no +items in the in-store queue. */ + +if (!full || queue_total == 0) return; + +/* Now scan the queue and remove any items that were not in the directory. At +the same time, set up the index pointers into the queue. Because we are +removing items, the total that we are comparing against isn't actually correct, +but in a long queue it won't make much difference, and in a short queue it +doesn't matter anyway!*/ + +p = queue_index[0]; +while (p != NULL) + { + if (!p->seen) + { + queue_item *next = p->next; + if (p->prev == NULL) queue_index[0] = next; + else p->prev->next = next; + if (next == NULL) + { + int i; + queue_item *q = queue_index[queue_index_size-1]; + for (i = queue_index_size - 1; i >= 0; i--) + if (queue_index[i] == q) queue_index[i] = p->prev; + } + else next->prev = p->prev; + clean_up(p); + queue_total--; + p = next; + } + else + { + if (++count > (queue_total * indexptr)/(queue_index_size-1)) + { + queue_index[indexptr++] = p; + } + p->seen = FALSE; /* for next time */ + p = p->next; + } + } + +/* If a lot of messages have been removed at the bottom, we may not +have got the index all filled in yet. Make sure all the pointers +are legal. */ + +while (indexptr < queue_index_size - 1) + { + queue_index[indexptr++] = queue_index[queue_index_size-1]; + } +} + + + + +/************************************************* +* Update the recipients list for a message * +*************************************************/ + +/* We read the spool file only if its update time differs from last time, +or if there is a journal file in existence. */ + +/* First, a local subroutine to scan the non-recipients tree and +remove any of them from the address list */ + +static void +scan_tree(queue_item *p, tree_node *tn) +{ +if (tn != NULL) + { + if (tn->left != NULL) scan_tree(p, tn->left); + if (tn->right != NULL) scan_tree(p, tn->right); + (void)find_dest(p, tn->name, dest_remove, FALSE); + } +} + +/* The main function */ + +static void update_recipients(queue_item *p) +{ +int i; +FILE *jread; +void *reset_point; +struct stat statdata; +uschar buffer[1024]; + +message_subdir[0] = p->dir_char; + +sprintf(CS buffer, "%s/input/%s/%s-J", spool_directory, message_subdir, p->name); +jread = fopen(CS buffer, "r"); +if (jread == NULL) + { + sprintf(CS buffer, "%s/input/%s/%s-H", spool_directory, message_subdir, p->name); + if (Ustat(buffer, &statdata) < 0 || p->update_time == statdata.st_mtime) + return; + } + +/* Get the contents of the header file; if any problem, just give up. +Arrange to recover the dynamic store afterwards. */ + +reset_point = store_get(0); +sprintf(CS buffer, "%s-H", p->name); +if (spool_read_header(buffer, FALSE, TRUE) != spool_read_OK) + { + store_reset(reset_point); + if (jread != NULL) fclose(jread); + return; + } + +/* If there's a journal file, add its contents to the non-recipients tree */ + +if (jread != NULL) + { + while (Ufgets(big_buffer, big_buffer_size, jread) != NULL) + { + int n = Ustrlen(big_buffer); + big_buffer[n-1] = 0; + tree_add_nonrecipient(big_buffer); + } + fclose(jread); + } + +/* Scan and process the recipients list, removing any that have already +been delivered, and removing visible names. In the nonrecipients tree, +domains are lower cased. */ + +if (recipients_list != NULL) + { + for (i = 0; i < recipients_count; i++) + { + uschar *pp; + uschar *r = recipients_list[i].address; + tree_node *node = tree_search(tree_nonrecipients, r); + + if (node == NULL) + { + uschar temp[256]; + uschar *rr = temp; + Ustrcpy(temp, r); + while (*rr != 0 && *rr != '@') rr++; + while (*rr != 0) { *rr = tolower(*rr); rr++; } + node = tree_search(tree_nonrecipients, temp); + } + + if ((pp = strstric(r+1, qualify_domain, FALSE)) != NULL && + *(--pp) == '@') *pp = 0; + if (node == NULL) + (void)find_dest(p, r, dest_add, FALSE); + else + (void)find_dest(p, r, dest_remove, FALSE); + } + } + +/* We also need to scan the tree of non-recipients, which might +contain child addresses that are not in the recipients list, but +which may have got onto the address list as a result of eximon +noticing an == line in the log. Then remember the update time, +recover the dynamic store, and we are done. */ + +scan_tree(p, tree_nonrecipients); +p->update_time = statdata.st_mtime; +store_reset(reset_point); +} + + + +/************************************************* +* Display queue data * +*************************************************/ + +/* The present implementation simple re-writes the entire information each +time. Take some care to keep the scrolled position as it previously was, but, +if it was at the bottom, keep it at the bottom. Take note of any hide list, and +time out the entries as appropriate. */ + +void +queue_display(void) +{ +int now = (int)time(NULL); +queue_item *p = queue_index[0]; + +if (menu_is_up) return; /* Avoid nasty interactions */ + +text_empty(queue_widget); + +while (p != NULL) + { + int count = 1; + dest_item *dd, *ddd; + uschar u = 'm'; + int t = (now - p->input_time)/60; /* minutes on queue */ + + if (t > 90) + { + u = 'h'; + t = (t + 30)/60; + if (t > 72) + { + u = 'd'; + t = (t + 12)/24; + if (t > 99) /* someone had > 99 days */ + { + u = 'w'; + t = (t + 3)/7; + if (t > 99) /* so, just in case */ + { + u = 'y'; + t = (t + 26)/52; + } + } + } + } + + update_recipients(p); /* update destinations */ + + /* Can't set this earlier, as header data may change things. */ + + dd = p->destinations; + + /* Check to see if this message is on the hide list; if any hide + item has timed out, remove it from the list. Hide if all destinations + are on the hide list. */ + + for (ddd = dd; ddd != NULL; ddd = ddd->next) + { + skip_item *sk; + skip_item **skp; + int len_address; + + if (ddd->address[0] == '*') break; + len_address = Ustrlen(ddd->address); + + for (skp = &queue_skip; ; skp = &(sk->next)) + { + int len_skip; + + sk = *skp; + while (sk != NULL && now >= sk->reveal) + { + *skp = sk->next; + store_free(sk); + sk = *skp; + if (queue_skip == NULL) + { + XtDestroyWidget(unhide_widget); + unhide_widget = NULL; + } + } + if (sk == NULL) break; + + /* If this address matches the skip item, break (sk != NULL) */ + + len_skip = Ustrlen(sk->text); + if (len_skip <= len_address && + Ustrcmp(ddd->address + len_address - len_skip, sk->text) == 0) + break; + } + + if (sk == NULL) break; + } + + /* Don't use more than one call of anon() in one statement - it uses + a fixed static buffer. */ + + if (ddd != NULL || dd == NULL) + { + text_showf(queue_widget, "%c%2d%c %s %s %-8s ", + (p->frozen)? '*' : ' ', + t, u, + string_format_size(p->size, big_buffer), + p->name, + (p->sender == NULL)? US" " : + (p->sender[0] == 0)? US"<> " : anon(p->sender)); + + text_showf(queue_widget, "%s%s%s", + (dd == NULL || dd->address[0] == '*')? "" : "<", + (dd == NULL)? US"" : anon(dd->address), + (dd == NULL || dd->address[0] == '*')? "" : ">"); + + if (dd != NULL && dd->parent != NULL && dd->parent->address[0] != '*') + text_showf(queue_widget, " parent <%s>", anon(dd->parent->address)); + + text_show(queue_widget, US"\n"); + + if (dd != NULL) dd = dd->next; + while (dd != NULL && count++ < queue_max_addresses) + { + text_showf(queue_widget, " <%s>", + anon(dd->address)); + if (dd->parent != NULL && dd->parent->address[0] != '*') + text_showf(queue_widget, " parent <%s>", anon(dd->parent->address)); + text_show(queue_widget, US"\n"); + dd = dd->next; + } + if (dd != NULL) + text_showf(queue_widget, " ...\n"); + } + + p = p->next; + } +} + +/* End of em_queue.c */ diff --git a/src/exim_monitor/em_strip.c b/src/exim_monitor/em_strip.c new file mode 100644 index 000000000..a16179cbf --- /dev/null +++ b/src/exim_monitor/em_strip.c @@ -0,0 +1,266 @@ +/* $Cambridge: exim/src/exim_monitor/em_strip.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim Monitor * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + + +#include "em_hdr.h" + +/* This module contains functions for handling stripcharts */ + + +/************************************************* +* Static variables * +*************************************************/ + +static int queue_first_time = 1; /* flag for resetting time */ +static int size_first_time = 1; /* and another */ + +static int stripchart_count = 0; /* count stripcharts created */ +static int *stripchart_delay; /* vector of delay counts */ +static Widget *stripchart_label; /* vector of label widgets */ +static int *stripchart_last_total; /* vector of prevous values */ +static int *stripchart_max; /* vector of maxima */ +static int *stripchart_middelay; /* vector of */ +static int *stripchart_midmax; /* vector of */ +static uschar **stripchart_name; /* vector of name strings */ +static Widget stripchart_prev_chart = NULL; /* previously created chart */ +static Widget stripchart_prev_label = NULL; /* previously created label */ + + + +/************************************************* +* Initialize * +*************************************************/ + +void stripchart_init(void) +{ +stripchart_delay = (int *)store_malloc(stripchart_number * sizeof(int)); +stripchart_label = (Widget *)store_malloc(stripchart_number * sizeof(Widget)); +stripchart_last_total = (int *)store_malloc(stripchart_number * sizeof(int)); +stripchart_max = (int *)store_malloc(stripchart_number * sizeof(int)); +stripchart_middelay = (int *)store_malloc(stripchart_number * sizeof(int)); +stripchart_midmax = (int *)store_malloc(stripchart_number * sizeof(int)); +stripchart_name = (uschar **)store_malloc(stripchart_number * sizeof(uschar *)); +stripchart_total = (int *)store_malloc(stripchart_number * sizeof(int)); +} + + + +/************************************************* +* Stripchart callback function * +*************************************************/ + +/* The client data is the index of the stripchart. We have to play +a little game in order to ensure that the double value is correctly +passed back via the value pointer without the compiler doing an +unwanted cast. */ + +static void stripchartAction(Widget w, XtPointer client_data, XtPointer value) +{ +double *ptr = (double *)value; +static int thresholds[] = + {10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 0}; +int num = (int)client_data; +int oldmax = 0; +int newmax = 0; +int newvalue = 0; +int i = 0; + +/* For the queue stripchart, the value is the current vector value. +We reset the initial delay of 1 second to the normal value. */ + +if (num == 0) + { + newvalue = stripchart_total[0]; + if (queue_first_time) + { + xs_SetValues(w, 1, "update", stripchart_update); + queue_first_time = 0; + } + } + +/* For the size monitoring stripchart, the value is the percentage +fullness of the partition. A similar fudge to the above is implemented +for the first time. Not all OS have statvfs(); for those that don't this +code is omitted. In fact it should never be obeyed, as we don't allow +size_stripchart to get set in that case. For some OS the old function +and struct name statfs is used; that is handled by a macro. */ + +else if (size_stripchart != NULL && num == 1) + { +#ifdef HAVE_STATFS + struct statvfs statbuf; + if (statvfs(CS size_stripchart, &statbuf) == 0) + { + int used = statbuf.f_blocks - statbuf.f_bfree; + int max = used + statbuf.f_bavail; + double fraction = ((double)used) / ((double)max); + newvalue = (int)((fraction + 0.005) * 100.0); + } +#endif + if (size_first_time) + { + xs_SetValues(w, 1, "update", stripchart_update); + size_first_time = 0; + } + } + +/* For the configured stripcharts, the value to be set is +the difference from last time; save the current total for +next time. */ + +else + { + newvalue = stripchart_total[num] - stripchart_last_total[num]; + stripchart_last_total[num] = stripchart_total[num]; + } + +/* Adjust the scale of the stripchart according to the value; +we delay enlarging the scale for a while after the values +reduce. Keep the maximum value while delaying, and reset +down to that. For the size stripchart, the threshold is always +forced to be at least 100. */ + +while (thresholds[i] > 0) + { + int thresh = (size_stripchart != NULL && num == 1)? 100 : thresholds[i++]; + if (newvalue < (double)thresh) + { + /* If the current maximum is less than required, or if it is + greater and we have delayed long enough, adjust the scale. */ + + if (stripchart_max[num] < thresh || + (stripchart_max[num] > thresh && stripchart_delay[num]++ > 20)) + { + uschar buffer[128]; + newmax = (thresh > stripchart_midmax[num])? + thresh : stripchart_midmax[num]; + if (newmax == 10) sprintf(CS buffer, "%s", stripchart_name[num]); + else sprintf(CS buffer, "%s x%d", stripchart_name[num], newmax/10); + if (size_stripchart != NULL && num == 1) Ustrcat(buffer, "%"); + xs_SetValues(stripchart_label[num], 1, "label", buffer); + oldmax = stripchart_max[num]; + stripchart_max[num] = newmax; + stripchart_midmax[num] = 0; + stripchart_delay[num] -= stripchart_middelay[num]; + } + + /* Otherwise, if the current maximum is greater than required, + keep the highest value encountered during the delay, and its + position so we can adjust the delay when re-scaling. */ + + else if (stripchart_max[num] > thresh) + { + if (thresh > stripchart_midmax[num]) + { + stripchart_midmax[num] = thresh; + stripchart_middelay[num] = stripchart_delay[num]; + } + } + + /* If the maximum is exactly what we need, reset the delay. */ + + if (stripchart_max[num] == thresh) stripchart_delay[num] = 0; + break; + } + } + +/* The vanilla Athena stripchart widget does not support change of +scale - it just draws scale lines closer and closer together, which +doesn't work when the number gets very large. However, we can cause +it to change scale quite simply by recomputing all the values and +then calling its repaint routine. I had to nobble the repaint routine +too, to stop it changing scale to anything other than 10. There's +probably a better way to do this, like adding some new resource, but +I'm not a widget programmer and want to get on with the rest of +eximon... */ + +if (oldmax > 0) + { + int i; + StripChartWidget ww = (StripChartWidget)w; + ww->strip_chart.max_value = 0; + for (i = 0; i < (int)ww->strip_chart.interval; i++) + { + ww->strip_chart.valuedata[i] = + (ww->strip_chart.valuedata[i] * oldmax)/newmax; + if (ww->strip_chart.valuedata[i] > ww->strip_chart.max_value) + ww->strip_chart.max_value = ww->strip_chart.valuedata[i]; + } + XClearWindow( XtDisplay(w), XtWindow(w)); + ww->strip_chart.interval = repaint_window(ww, 0, (int)w->core.width); + } + +/* Pass back the new value at the new scale */ + +*ptr = ((double)newvalue * 10.0)/(double)(stripchart_max[num]); +} + + + +/************************************************* +* Create one stripchart * +*************************************************/ + +/* This function creates two widgets, one being the title and the other being +the stripchart. The client_data values for each stripchart are index into the +stripchart_values vector; each new stripchart just gets the next number. There +is a fudge for the very first stripchart, which is the queue length display, +and for the second if it is a partition size display; its update time is +initially set to 1 second so that it gives an immediate display of the queue. +The first time its callback function is obeyed, the update time gets reset. */ + +void create_stripchart(Widget parent, uschar *title) +{ +Widget chart; + +Widget label = XtCreateManagedWidget("label", + labelWidgetClass, parent, NULL, 0); + +xs_SetValues(label, 10, + "label", title, + "width", stripchart_width + 2, + "borderWidth", 0, + "internalHeight", 0, + "internalWidth", 0, + "left", XawChainLeft, + "right", XawChainLeft, + "top", XawChainTop, + "bottom", XawChainTop, + XtNfromHoriz, stripchart_prev_label); + +chart = XtCreateManagedWidget("stripchart", + mystripChartWidgetClass, parent, NULL, 0); + +xs_SetValues(chart, 11, + "jumpScroll", 1, + "update", (stripchart_count < stripchart_varstart)? 1:stripchart_update, + "minScale", 10, + "width", stripchart_width, + "height", stripchart_height, + "left", XawChainLeft, + "right", XawChainLeft, + "top", XawChainTop, + "bottom", XawChainTop, + XtNfromHoriz, stripchart_prev_chart, + XtNfromVert, label); + +XtAddCallback(chart, "getValue", stripchartAction, + (XtPointer)stripchart_count); + +stripchart_last_total[stripchart_count] = 0; +stripchart_max[stripchart_count] = 10; +stripchart_midmax[stripchart_count] = 0; +stripchart_name[stripchart_count] = title; +stripchart_prev_label = stripchart_label[stripchart_count] = label; +stripchart_prev_chart = chart; +stripchart_total[stripchart_count] = 0; +stripchart_count++; +} + +/* End of em_strip.c */ diff --git a/src/exim_monitor/em_text.c b/src/exim_monitor/em_text.c new file mode 100644 index 000000000..1b3503c82 --- /dev/null +++ b/src/exim_monitor/em_text.c @@ -0,0 +1,73 @@ +/* $Cambridge: exim/src/exim_monitor/em_text.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim Monitor * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + + +#include "em_hdr.h" + + +/* This module contains functions for displaying text in a +text widget. It is not used for the log widget, because that +is dynamically updated and has special scrolling requirements. */ + + +/* Count of characters displayed */ + +static int text_count = 0; + + +/************************************************* +* Empty the widget * +*************************************************/ + +void text_empty(Widget w) +{ +XawTextBlock b; +b.firstPos = 0; +b.ptr = CS &b; +b.format = FMT8BIT; +b.length = 0; +XawTextReplace(w, 0, text_count, &b); +text_count = 0; +XawTextSetInsertionPoint(w, text_count); +} + + + +/************************************************* +* Display text * +*************************************************/ + +void text_show(Widget w, uschar *s) +{ +XawTextBlock b; +b.firstPos = 0; +b.ptr = CS s; +b.format = FMT8BIT; +b.length = Ustrlen(s); +XawTextReplace(w, text_count, text_count, &b); +text_count += b.length; +XawTextSetInsertionPoint(w, text_count); +} + + +/************************************************* +* Display text from format * +*************************************************/ + +void text_showf(Widget w, char *s, ...) +{ +va_list ap; +uschar buffer[1024]; +va_start(ap, s); +vsprintf(CS buffer, s, ap); +va_end(ap); +text_show(w, buffer); +} + +/* End of em_text.c */ diff --git a/src/exim_monitor/em_version.c b/src/exim_monitor/em_version.c new file mode 100644 index 000000000..7d35cbf7a --- /dev/null +++ b/src/exim_monitor/em_version.c @@ -0,0 +1,39 @@ +/* $Cambridge: exim/src/exim_monitor/em_version.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim Monitor * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +#include "mytypes.h" +#include "macros.h" +#include +#include + +extern uschar *version_string; +extern uschar *version_date; + +void +version_init(void) +{ +int i = 0; +uschar today[20]; + +version_string = US"2.05"; + +Ustrcpy(today, __DATE__); +if (today[4] == ' ') i = 1; +today[3] = today[6] = '-'; + +version_date = (uschar *)malloc(32); +version_date[0] = 0; +Ustrncat(version_date, today+4+i, 3-i); +Ustrncat(version_date, today, 4); +Ustrncat(version_date, today+7, 4); +Ustrcat(version_date, " "); +Ustrcat(version_date, __TIME__); +} + +/* End of em_version.c */ diff --git a/src/exim_monitor/em_xs.c b/src/exim_monitor/em_xs.c new file mode 100644 index 000000000..e3368f5d0 --- /dev/null +++ b/src/exim_monitor/em_xs.c @@ -0,0 +1,46 @@ +/* $Cambridge: exim/src/exim_monitor/em_xs.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim Monitor * +*************************************************/ + +/* Copyright (c) University of Cambridge, 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* This file contains a number of subroutines that are in effect +just alternative packaging for calls to various X functions that +happen to be convenient for this program. */ + +#include "em_hdr.h" + + + +/************************************************* +* xs_SetValues * +*************************************************/ + +/* Unpick a variable-length argument list and set up an +appropriate call to XtSetValues. To make it reasonably +efficient, we keep a working Arg structure of length 15; +the largest call in eximon sets 11 values. The code uses +malloc/free if more, just in case there is ever a longer +one that gets overlooked. */ + +static Arg xs_temparg[15]; + +void xs_SetValues(Widget w, Cardinal num_args, ...) +{ +int i; +va_list ap; +Arg *aa = (num_args > 15)? (Arg *)malloc(num_args*sizeof(Arg)) : xs_temparg; +va_start(ap, num_args); +for (i = 0; i < num_args; i++) + { + aa[i].name = va_arg(ap, String); + aa[i].value = va_arg(ap, XtArgVal); + } +XtSetValues(w, aa, num_args); +if (num_args > 15) free(aa); +} + +/* End of em_xs.c */ diff --git a/src/src/EDITME b/src/src/EDITME new file mode 100644 index 000000000..a7af6e8eb --- /dev/null +++ b/src/src/EDITME @@ -0,0 +1,1020 @@ +# $Cambridge: exim/src/src/EDITME,v 1.1 2004/10/07 10:39:01 ph10 Exp $ + +################################################## +# The Exim mail transport agent # +################################################## + +# This is the template for Exim's main build-time configuration file. It +# contains settings that are independent of any operating system. These are +# things that are mostly sysadmin choices. The items below are divided into +# those you must specify, those you probably want to specify, those you might +# often want to specify, and those that you almost never need to mention. + +# Edit this file and save the result to a file called Local/Makefile within the +# Exim distribution directory before running the "make" command. + +# Things that depend on the operating system have default settings in +# OS/Makefile-Default, but these are overridden for some OS by files called +# called OS/Makefile-. You can further override these by creating files +# called Local/Makefile-, where "" stands for the name of your +# operating system - look at the names in the OS directory to see which names +# are recognized. + +# However, if you are building Exim for a single OS only, you don't need to +# worry about setting up Local/Makefile-. Any build-time configuration +# settings you require can in fact be placed in the one file called +# Local/Makefile. It is only if you are building for several OS from the same +# source files that you need to worry about splitting off your own OS-dependent +# settings into separate files. (There's more explanation about how this all +# works in the toplevel README file, under "Modifying the building process", as +# well as in the Exim specification.) + +# One OS-specific thing that may need to be changed is the command for running +# the C compiler; the overall default is gcc, but some OS Makefiles specify cc. +# You can override anything that is set by putting CC=whatever in your +# Local/Makefile. + +# NOTE: You should never need to edit any of the distributed Makefiles; all +# overriding can be done in your Local/Makefile(s). This will make it easier +# for you when the next release comes along. + +# The location of the X11 libraries is something else that is quite variable +# even between different versions of the same operating system (and indeed +# there are different versions of X11 as well, of course). The four settings +# concerned here are X11, XINCLUDE, XLFLAGS (linking flags) and X11_LD_LIB +# (dynamic run-time library). You need not worry about X11 unless you want to +# compile the Exim monitor utility. Exim itself does not use X11. + +# Another area of variability between systems is the type and location of the +# DBM library package. Exim has support for ndbm, gdbm, tdb, and Berkeley DB. +# By default the code assumes ndbm; this often works with gdbm or DB, provided +# they are correctly installed, via their compatibility interfaces. However, +# Exim can also be configured to use the native calls for Berkeley DB (obsolete +# versions 1.85, 2.x, 3.x, or the current 4.x version) and also for gdbm. + +# For some operating systems, a default DBM library (other than ndbm) is +# selected by a setting in the OS-specific Makefile. Most modern OS now have +# a DBM library installed as standard, and in many cases this will be selected +# for you by the OS-specific configuration. If Exim compiles without any +# problems, you probably do not have to worry about the DBM library. If you +# do want or need to change it, you should first read the discussion in the +# file doc/dbm.discuss.txt, which also contains instructions for testing Exim's +# interface to the DBM library. + +# In Local/Makefiles blank lines and lines starting with # are ignored. It is +# also permitted to use the # character to add a comment to a setting, for +# example +# +# EXIM_GID=42 # the "mail" group +# +# However, with some versions of "make" this works only if there is no white +# space between the end of the setting and the #, so perhaps it is best +# avoided. A consequence of this facility is that it is not possible to have +# the # character present in any setting, but I can't think of any cases where +# this would be wanted. +############################################################################### + + + +############################################################################### +# THESE ARE THINGS YOU MUST SPECIFY # +############################################################################### + +# Exim will not build unless you specify BIN_DIRECTORY, CONFIGURE_FILE, and +# EXIM_USER. You also need EXIM_GROUP if EXIM_USER specifies a uid by number. + +# If you don't specify SPOOL_DIRECTORY, Exim won't fail to build. However, it +# really is a very good idea to specify it here rather than at run time. This +# is particularly true if you let the logs go to their default location in the +# spool directory, because it means that the location of the logs is known +# before Exim has read the run time configuration file. + +#------------------------------------------------------------------------------ +# BIN_DIRECTORY defines where the exim binary will be installed by "make +# install". The path is also used internally by Exim when it needs to re-invoke +# itself, either to send an error message, or to recover root privilege. Exim's +# utility binaries and scripts are also installed in this directory. There is +# no "standard" place for the binary directory. Some people like to keep all +# the Exim files under one directory such as /usr/exim; others just let the +# Exim binaries go into an existing directory such as /usr/sbin or +# /usr/local/sbin. The installation script will try to create this directory, +# and any superior directories, if they do not exist. + +BIN_DIRECTORY=/usr/exim/bin + + +#------------------------------------------------------------------------------ +# CONFIGURE_FILE defines where Exim's run time configuration file is to be +# found. It is the complete pathname for the file, not just a directory. The +# location of all other run time files and directories can be changed in the +# run time configuration file. There is a lot of variety in the choice of +# location in different OS, and in the preferences of different sysadmins. Some +# common locations are in /etc or /etc/mail or /usr/local/etc or +# /usr/local/etc/mail. Another possibility is to keep all the Exim files under +# a single directory such as /usr/exim. Whatever you choose, the installation +# script will try to make the directory and any superior directories if they +# don't exist. It will also install a default runtime configuration if this +# file does not exist. + +CONFIGURE_FILE=/usr/exim/configure + +# It is possible to specify a colon-separated list of files for CONFIGURE_FILE. +# In this case, Exim will use the first of them that exists when it is run. +# However, if a list is specified, the installation script no longer tries to +# make superior directories or to install a default runtime configuration. + + +#------------------------------------------------------------------------------ +# The Exim binary must normally be setuid root, so that it starts executing as +# root, but (depending on the options with which it is called) it does not +# always need to retain the root privilege. These settings define the user and +# group that is used for Exim processes when they no longer need to be root. In +# particular, this applies when receiving messages and when doing remote +# deliveries. (Local deliveries run as various non-root users, typically as the +# owner of a local mailbox.) Specifying these values as root is very strongly +# discouraged. + +EXIM_USER= + +# If you specify EXIM_USER as a name, this is looked up at build time, and the +# uid number is built into the binary. However, you can specify that this +# lookup is deferred until runtime. In this case, it is the name that is built +# into the binary. You can do this by a setting of the form: + +# EXIM_USER=ref:exim + +# In other words, put "ref:" in front of the user name. If you set EXIM_USER +# like this, any value specified for EXIM_GROUP is also passed "by reference". +# Although this costs a bit of resource at runtime, it is convenient to use +# this feature when building binaries that are to be run on multiple systems +# where the name may refer to different uids. It also allows you to build Exim +# on a system where there is no Exim user defined. + +# If the setting of EXIM_USER is numeric (e.g. EXIM_USER=42), there must +# also be a setting of EXIM_GROUP. If, on the other hand, you use a name +# for EXIM_USER (e.g. EXIM_USER=exim), you don't need to set EXIM_GROUP unless +# you want to use a group other than the default group for the given user. + +# EXIM_GROUP= + +# Many sites define a user called "exim", with an appropriate default group, +# and use +# +# EXIM_USER=exim +# +# while leaving EXIM_GROUP unspecified (commented out). + + +#------------------------------------------------------------------------------ +# SPOOL_DIRECTORY defines the directory where all the data for messages in +# transit is kept. It is strongly recommended that you define it here, though +# it is possible to leave this till the run time configuration. + +# Exim creates the spool directory if it does not exist. The owner and group +# will be those defined by EXIM_USER and EXIM_GROUP, and this also applies to +# all the files and directories that are created in the spool directory. + +# Almost all installations choose this: + +SPOOL_DIRECTORY=/var/spool/exim + + + +############################################################################### +# THESE ARE THINGS YOU PROBABLY WANT TO SPECIFY # +############################################################################### + +# You need to specify some routers and transports if you want the Exim that you +# are building to be capable of delivering mail. You almost certainly need at +# least one type of lookup. You should consider whether you want to build +# the Exim monitor or not. + + +#------------------------------------------------------------------------------ +# These settings determine which individual router drivers are included in the +# Exim binary. There are no defaults in the code; those routers that are wanted +# must be defined here by setting the appropriate variables to the value "yes". +# Including a router in the binary does not cause it to be used automatically. +# It has also to be configured in the run time configuration file. By +# commenting out those you know you don't want to use, you can make the binary +# a bit smaller. If you are unsure, leave all of these included for now. + +ROUTER_ACCEPT=yes +ROUTER_DNSLOOKUP=yes +ROUTER_IPLITERAL=yes +ROUTER_MANUALROUTE=yes +ROUTER_QUERYPROGRAM=yes +ROUTER_REDIRECT=yes + +# This one is very special-purpose, so is not included by default. + +# ROUTER_IPLOOKUP=yes + + +#------------------------------------------------------------------------------ +# These settings determine which individual transport drivers are included in +# the Exim binary. There are no defaults; those transports that are wanted must +# be defined here by setting the appropriate variables to the value "yes". +# Including a transport in the binary does not cause it to be used +# automatically. It has also to be configured in the run time configuration +# file. By commenting out those you know you don't want to use, you can make +# the binary a bit smaller. If you are unsure, leave all of these included for +# now. + +TRANSPORT_APPENDFILE=yes +TRANSPORT_AUTOREPLY=yes +TRANSPORT_PIPE=yes +TRANSPORT_SMTP=yes + +# This one is special-purpose, and commonly not required, so it is not +# included by default. + +# TRANSPORT_LMTP=yes + + +#------------------------------------------------------------------------------ +# The appendfile transport can write messages to local mailboxes in a number +# of formats. The code for three specialist formats, maildir, mailstore, and +# MBX, is included only when requested. If you do not know what this is about, +# leave these settings commented out. + +# SUPPORT_MAILDIR=yes +# SUPPORT_MAILSTORE=yes +# SUPPORT_MBX=yes + + +#------------------------------------------------------------------------------ +# These settings determine which file and database lookup methods are included +# in the binary. See the manual chapter entitled "File and database lookups" +# for discussion. DBM and lsearch (linear search) are included by default. If +# you are unsure about the others, leave them commented out for now. +# LOOKUP_DNSDB does *not* refer to general mail routing using the DNS. It is +# for the specialist case of using the DNS as a general database facility (not +# common). + +LOOKUP_DBM=yes +LOOKUP_LSEARCH=yes + +# LOOKUP_CDB=yes +# LOOKUP_DNSDB=yes +# LOOKUP_DSEARCH=yes +# LOOKUP_IBASE=yes +# LOOKUP_LDAP=yes +# LOOKUP_MYSQL=yes +# LOOKUP_NIS=yes +# LOOKUP_NISPLUS=yes +# LOOKUP_ORACLE=yes +# LOOKUP_PASSWD=yes +# LOOKUP_PGSQL=yes +# LOOKUP_WHOSON=yes + +# These two settings are obsolete; all three lookups are compiled when +# LOOKUP_LSEARCH is enabled. However, we retain these for backward +# compatibility. Setting one forces LOOKUP_LSEARCH if it is not set. + +# LOOKUP_WILDLSEARCH=yes +# LOOKUP_NWILDLSEARCH=yes + + +#------------------------------------------------------------------------------ +# If you have set LOOKUP_LDAP=yes, you should set LDAP_LIB_TYPE to indicate +# which LDAP library you have. Unfortunately, though most of their functions +# are the same, there are minor differences. Currently Exim knows about four +# LDAP libraries: the one from the University of Michigan (also known as +# OpenLDAP 1), OpenLDAP 2, the Netscape SDK library, and the library that comes +# with Solaris 7 onwards. Uncomment whichever of these you are using. + +# LDAP_LIB_TYPE=OPENLDAP1 +# LDAP_LIB_TYPE=OPENLDAP2 +# LDAP_LIB_TYPE=NETSCAPE +# LDAP_LIB_TYPE=SOLARIS + +# If you don't set any of these, Exim assumes the original University of +# Michigan (OpenLDAP 1) library. + + +#------------------------------------------------------------------------------ +# Additional libraries and include directories may be required for some +# lookup styles (e.g. LDAP, MYSQL or PGSQL). LOOKUP_LIBS is included only on +# the command for linking Exim itself, not on any auxiliary programs. You +# don't need to set LOOKUP_INCLUDE if the relevant directories are already +# specified in INCLUDE. The settings below are just examples; -lpq is for +# PostgreSQL, -lgds is for Interbase. + +# LOOKUP_INCLUDE=-I /usr/local/ldap/include -I /usr/local/mysql/include -I /usr/local/pgsql/include +# LOOKUP_LIBS=-L/usr/local/lib -lldap -llber -lmysqlclient -lpq -lgds + +#------------------------------------------------------------------------------ +# Compiling the Exim monitor: If you want to compile the Exim monitor, a +# program that requires an X11 display, then EXIM_MONITOR should be set to the +# value "eximon.bin". Comment out this setting to disable compilation of the +# monitor. The locations of various X11 directories for libraries and include +# files are defaulted in the OS/Makefile-Default file, but can be overridden in +# local OS-specific make files. + +EXIM_MONITOR=eximon.bin + + + +############################################################################### +# THESE ARE THINGS YOU MIGHT WANT TO SPECIFY # +############################################################################### + +# The items in this section are those that are commonly changed according to +# the sysadmin's preferences, but whose defaults are often acceptable. The +# first five are concerned with security issues, where differing levels of +# paranoia are appropriate in different environments. Sysadmins also vary in +# their views on appropriate levels of defence in these areas. If you do not +# understand these issues, go with the defaults, which are used by many sites. + + +#------------------------------------------------------------------------------ +# Although Exim is normally a setuid program, owned by root, it refuses to run +# local deliveries as root by default. There is a runtime option called +# "never_users" which lists the users that must never be used for local +# deliveries. There is also the setting below, which provides a list that +# cannot be overridden at runtime. This guards against problems caused by +# unauthorized changes to the runtime configuration. You are advised not to +# remove "root" from this option, but you can add other users if you want. The +# list is colon-separated. + +FIXED_NEVER_USERS=root + + +#------------------------------------------------------------------------------ +# By default, Exim insists that its configuration file be owned either by root +# or by the Exim user. You can specify one additional permitted owner here. + +# CONFIGURE_OWNER= + +# If you specify CONFIGURE_OWNER as a name, this is looked up at build time, +# and the uid number is built into the binary. However, you can specify that +# this lookup is deferred until runtime. In this case, it is the name that is +# built into the binary. You can do this by a setting of the form: + +# CONFIGURE_OWNER=ref:mail + +# In other words, put "ref:" in front of the user name. Although this costs a +# bit of resource at runtime, it is convenient to use this feature when +# building binaries that are to be run on multiple systems where the name may +# refer to different uids. It also allows you to build Exim on a system where +# the relevant user is not defined. + + +#------------------------------------------------------------------------------ +# The -C option allows Exim to be run with an alternate runtime configuration +# file. When this is used by root or the Exim user, root privilege is retained +# by the binary (for any other caller, it is dropped). You can restrict the +# location of alternate configurations by defining a prefix below. Any file +# used with -C must then start with this prefix (except that /dev/null is also +# permitted if the caller is root, because that is used in the install script). +# If the prefix specifies a directory that is owned by root, a compromise of +# the Exim account does not permit arbitrary alternate configurations to be +# used. The prefix can be more restrictive than just a directory (the second +# example). + +# ALT_CONFIG_PREFIX=/some/directory/ +# ALT_CONFIG_PREFIX=/some/directory/exim.conf- + + +#------------------------------------------------------------------------------ +# If you uncomment the following line, only root may use the -C or -D options +# without losing root privilege. The -C option specifies an alternate runtime +# configuration file, and the -D option changes macro values in the runtime +# configuration. Uncommenting this line restricts what can be done with these +# options. A call to receive a message (either one-off or via a daemon) cannot +# successfully continue to deliver it, because the re-exec of Exim to regain +# root privilege will fail, owing to the use of -C or -D by the Exim user. +# However, you can still use -C for testing (as root) if you do separate Exim +# calls for receiving a message and subsequently delivering it. + +# ALT_CONFIG_ROOT_ONLY=yes + + +#------------------------------------------------------------------------------ +# Uncommenting this option disables the use of the -D command line option, +# which changes the values of macros in the runtime configuration file. +# This is another protection against somebody breaking into the Exim account. + +# DISABLE_D_OPTION=yes + + +#------------------------------------------------------------------------------ +# Exim has support for the AUTH (authentication) extension of the SMTP +# protocol, as defined by RFC 2554. If you don't know what SMTP authentication +# is, you probably won't want to include this code, so you should leave these +# settings commented out. If you do want to make use of SMTP authentication, +# you must uncomment at least one of the following, so that appropriate code is +# included in the Exim binary. You will then need to set up the run time +# configuration to make use of the mechanism(s) selected. + +# AUTH_CRAM_MD5=yes +# AUTH_CYRUS_SASL=yes +# AUTH_PLAINTEXT=yes +# AUTH_SPA=yes + + +#------------------------------------------------------------------------------ +# If you specified AUTH_CYRUS_SASL above, you should ensure that you have the +# Cyrus SASL library installed before trying to build Exim, and you probably +# want to uncomment the following line: + +# AUTH_LIBS=-lsasl2 + + +#------------------------------------------------------------------------------ +# When Exim is decoding MIME "words" in header lines, most commonly for use +# in the $header_xxx expansion, it converts any foreign character sets to the +# one that is set in the headers_charset option. The default setting is +# defined by this setting: + +HEADERS_CHARSET="ISO-8859-1" + +# If you are going to make use of $header_xxx expansions in your configuration +# file, or if your users are going to use them in filter files, and the normal +# character set on your host is something other than ISO-8859-1, you might +# like to specify a different default here. This value can be overridden in +# the runtime configuration, and it can also be overridden in individual filter +# files. +# +# IMPORTANT NOTE: The iconv() function is needed for character code +# conversions. Please see the next item... + + +#------------------------------------------------------------------------------ +# Character code conversions are possible only if the iconv() function is +# installed on your operating system. There are two places in Exim where this +# is relevant: (a) The $header_xxx expansion (see the previous item), and (b) +# the Sieve filter support. For those OS where iconv() is known to be installed +# as standard, the file in OS/Makefile-xxxx contains +# +# HAVE_ICONV=yes +# +# If you are not using one of those systems, but have installed iconv(), you +# need to uncomment that line above. In some cases, you may find that iconv() +# and its header file are not in the default places. You might need to use +# something like this: +# +# HAVE_ICONV=yes +# CFLAGS=-O -I/usr/local/include +# EXTRALIBS_EXIM=-L/usr/local/lib -liconv +# +# but of course there may need to be other things in CFLAGS and EXTRALIBS_EXIM +# as well. + + +#------------------------------------------------------------------------------ +# The passwords for user accounts are normally encrypted with the crypt() +# function. Comparisons with encrypted passwords can be done using Exim's +# "crypteq" expansion operator. (This is commonly used as part of the +# configuration of an authenticator for use with SMTP AUTH.) At least one +# operating system has an extended function called crypt16(), which uses up to +# 16 characters of a password (the normal crypt() uses only the first 8). Exim +# supports the use of crypt16() as well as crypt(). + +# You can always indicate a crypt16-encrypted password by preceding it with +# "{crypt16}". If you want the default handling (without any preceding +# indicator) to use crypt16(), uncomment the following line: + +# DEFAULT_CRYPT=crypt16 + +# If you do that, you can still access the basic crypt() function by preceding +# an encrypted password with "{crypt}". For more details, see the description +# of the "crypteq" condition in the manual chapter on string expansions. + +# Since most operating systems do not include a crypt16() function (yet?), Exim +# has one of its own, which it uses unless HAVE_CRYPT16 is defined. Normally, +# that will be set in an OS-specific Makefile for the OS that have such a +# function, so you should not need to bother with it. + + +#------------------------------------------------------------------------------ +# Exim can be built to support the SMTP STARTTLS command, which implements +# Transport Layer Security using SSL (Secure Sockets Layer). To do this, you +# must install the OpenSSL library package or the GnuTLS library. Exim contains +# no cryptographic code of its own. Uncomment the following lines if you want +# to build Exim with TLS support. If you don't know what this is all about, +# leave these settings commented out. + +# This setting is required for any TLS support (either OpenSSL or GnuTLS) +# SUPPORT_TLS=yes + +# Uncomment this setting if you are using OpenSSL +# TLS_LIBS=-lssl -lcrypto + +# Uncomment these settings if you are using GnuTLS +# USE_GNUTLS=yes +# TLS_LIBS=-lgnutls -ltasn1 -lgcrypt + +# If you are running Exim as a server, note that just building it with TLS +# support is not all you need to do. You also need to set up a suitable +# certificate, and tell Exim about it by means of the tls_certificate +# and tls_privatekey run time options. You also need to set tls_advertise_hosts +# to specify the hosts to which Exim advertises TLS support. On the other hand, +# if you are running Exim only as a client, building it with TLS support +# is all you need to do. + +# Additional libraries and include files are required for both OpenSSL and +# GnuTLS. The TLS_LIBS settings above assume that the libraries are installed +# with all your other libraries. If they are in a special directory, you may +# need something like + +# TLS_LIBS=-L/usr/local/openssl/lib -lssl -lcrypto +# or +# TLS_LIBS=-L/opt/gnu/lib -lgnutls -ltasn1 -lgcrypt + +# TLS_LIBS is included only on the command for linking Exim itself, not on any +# auxiliary programs. If the include files are not in a standard place, you can +# set TLS_INCLUDE to specify where they are, for example: + +# TLS_INCLUDE=-I/usr/local/openssl/include/ +# or +# TLS_INCLUDE=-I/opt/gnu/include + +# You don't need to set TLS_INCLUDE if the relevant directories are already +# specified in INCLUDE. + + +#------------------------------------------------------------------------------ +# The default distribution of Exim contains only the plain text form of the +# documentation. Other forms are available separately. If you want to install +# the documentation in "info" format, first fetch the Texinfo documentation +# sources from the ftp directory and unpack them, which should create files +# with the extension "texinfo" in the doc directory. You may find that the +# version number of the texinfo files is different to your Exim version number, +# because the main documentation isn't updated as often as the code. For +# example, if you have Exim version 4.43, the source tarball upacks into a +# directory called exim-4.43, but the texinfo tarball unpacks into exim-4.40. +# In this case, move the contents of exim-4.40/doc into exim-4.43/doc after you +# have unpacked them. Then set INFO_DIRECTORY to the location of your info +# directory. This varies from system to system, but is often /usr/share/info. +# Once you have done this, "make install" will build the info files and +# install them in the directory you have defined. + +# INFO_DIRECTORY=/usr/share/info + + +#------------------------------------------------------------------------------ +# Exim log directory and files: Exim creates several log files inside a +# single log directory. You can define the directory and the form of the +# log file name here. If you do not set anything, Exim creates a directory +# called "log" inside its spool directory (see SPOOL_DIRECTORY above) and uses +# the filenames "mainlog", "paniclog", and "rejectlog". If you want to change +# this, you can set LOG_FILE_PATH to a path name containing one occurrence of +# %s. This will be replaced by one of the strings "main", "panic", or "reject" +# to form the final file names. Some installations may want something like this: + +# LOG_FILE_PATH=/var/log/exim_%slog + +# which results in files with names /var/log/exim_mainlog, etc. The directory +# in which the log files are placed must exist; Exim does not try to create +# it for itself. It is also your responsibility to ensure that Exim is capable +# of writing files using this path name. The Exim user (see EXIM_USER above) +# must be able to create and update files in the directory you have specified. + +# You can also configure Exim to use syslog, instead of or as well as log +# files, by settings such as these + +# LOG_FILE_PATH=syslog +# LOG_FILE_PATH=syslog:/var/log/exim_%slog + +# The first of these uses only syslog; the second uses syslog and also writes +# to log files. Do not include white space in such a setting as it messes up +# the building process. + + +#------------------------------------------------------------------------------ +# When logging to syslog, the following option caters for syslog replacements +# that are able to accept log entries longer than the 1024 characters allowed +# by RFC 3164. It is up to you to make sure your syslog daemon can handle this. +# Non-printable characters are usually unacceptable regardless, so log entries +# are still split on newline characters. + +# SYSLOG_LONG_LINES=yes + +# If you are not interested in the process identifier (pid) of the Exim that is +# making the call to syslog, then comment out the following line. + +SYSLOG_LOG_PID=yes + + +#------------------------------------------------------------------------------ +# Cycling log files: this variable specifies the maximum number of old +# log files that are kept by the exicyclog log-cycling script. You don't have +# to use exicyclog. If your operating system has other ways of cycling log +# files, you can use them instead. The exicyclog script isn't run by default; +# you have to set up a cron job for it if you want it. + +EXICYCLOG_MAX=10 + + +#------------------------------------------------------------------------------ +# The compress command is used by the exicyclog script to compress old log +# files. Both the name of the command and the suffix that it adds to files +# need to be defined here. See also the EXICYCLOG_MAX configuration. + +COMPRESS_COMMAND=/usr/bin/gzip +COMPRESS_SUFFIX=gz + + +#------------------------------------------------------------------------------ +# If the exigrep utility is fed compressed log files, it tries to uncompress +# them using this command. + +ZCAT_COMMAND=/usr/bin/zcat + + +#------------------------------------------------------------------------------ +# Compiling in support for embedded Perl: If you want to be able to +# use Perl code in Exim's string manipulation language and you have Perl +# (version 5.004 or later) installed, set EXIM_PERL to perl.o. Using embedded +# Perl costs quite a lot of resources. Only do this if you really need it. + +# EXIM_PERL=perl.o + + +#------------------------------------------------------------------------------ +# Exim has support for PAM (Pluggable Authentication Modules), a facility +# which is available in the latest releases of Solaris and in some GNU/Linux +# distributions (see http://ftp.kernel.org/pub/linux/libs/pam/). The Exim +# support, which is intended for use in conjunction with the SMTP AUTH +# facilities, is included only when requested by the following setting: + +# SUPPORT_PAM=yes + +# You probably need to add -lpam to EXTRALIBS, and in some releases of +# GNU/Linux -ldl is also needed. + + +#------------------------------------------------------------------------------ +# Support for authentication via Radius is also available. The Exim support, +# which is intended for use in conjunction with the SMTP AUTH facilities, +# is included only when requested by setting the following parameter to the +# location of your Radius configuration file: + +# RADIUS_CONFIG_FILE=/etc/radiusclient/radiusclient.conf +# RADIUS_CONFIG_FILE=/etc/radius.conf + +# If you have set RADIUS_CONFIG_FILE, you should also set one of these to +# indicate which RADIUS library is used: +# +# RADIUSCLIENT is the radiusclient library; you probably need to add +# -libradiusclient to EXTRALIBS +# +# RADLIB is the Radius library that comes with FreeBSD (the header file is +# called radlib.h); you probably need to add -lradius to EXTRALIBS + +# RADIUS_LIB_TYPE=RADIUSCLIENT +# RADIUS_LIB_TYPE=RADLIB + +# If you don't set one of these, Exim assumes the radiusclient library. + + +#------------------------------------------------------------------------------ +# Support for authentication via the Cyrus SASL pwcheck daemon is available. +# Note, however, that pwcheck is now deprecated in favour of saslauthd (see +# next item). The Exim support for pwcheck, which is intented for use in +# conjunction with the SMTP AUTH facilities, is included only when requested by +# setting the following parameter to the location of the pwcheck daemon's +# socket. +# +# There is no need to install all of SASL on your system. You just need to run +# ./configure --with-pwcheck, cd to the pwcheck directory within the sources, +# make and make install. You must create the socket directory (default +# /var/pwcheck) and chown it to exim's user and group. Once you have installed +# pwcheck, you should arrange for it to be started by root at boot time. + +# CYRUS_PWCHECK_SOCKET=/var/pwcheck/pwcheck + + +#------------------------------------------------------------------------------ +# Support for authentication via the Cyrus SASL saslauthd daemon is available. +# The Exim support, which is intented for use in conjunction with the SMTP AUTH +# facilities, is included only when requested by setting the following +# parameter to the location of the saslauthd daemon's socket. +# +# There is no need to install all of SASL on your system. You just need to run +# ./configure --with-saslauthd (and any other options you need, for example, to +# select or deselect authentication mechanisms), cd to the saslauthd directory +# within the sources, make and make install. You must create the socket +# directory (default /var/state/saslauthd) and chown it to exim's user and +# group. Once you have installed saslauthd, you should arrange for it to be +# started by root at boot time. + +# CYRUS_SASLAUTHD_SOCKET=/var/state/saslauthd/mux + + +#------------------------------------------------------------------------------ +# TCP wrappers: If you want to use tcpwrappers from within Exim, uncomment +# this setting. See the manual section entitled "Use of tcpwrappers" in the +# chapter on building and installing Exim. +# +# USE_TCP_WRAPPERS=yes +# +# You may well also have to specify a local "include" file and an additional +# library for TCP wrappers, so you probably need something like this: +# +# USE_TCP_WRAPPERS=yes +# CFLAGS=-O -I/usr/local/include +# EXTRALIBS_EXIM=-L/usr/local/lib -lwrap +# +# but of course there may need to be other things in CFLAGS and EXTRALIBS_EXIM +# as well. + + +#------------------------------------------------------------------------------ +# The default action of the exim_install script (which is run by "make +# install") is to install the Exim binary with a unique name such as +# exim-4.43-1, and then set up a symbolic link called "exim" to reference it, +# moving the symbolic link from any previous version. If you define NO_SYMLINK +# (the value doesn't matter), the symbolic link is not created or moved. You +# will then have to "turn Exim on" by setting up the link manually. + +# NO_SYMLINK=yes + + +#------------------------------------------------------------------------------ +# Another default action of the install script is to install a default runtime +# configuration file if one does not exist. This configuration has a router for +# expanding system aliases. The default assumes that these aliases are kept +# in the traditional file called /etc/aliases. If such a file does not exist, +# the installation script creates one that contains just comments (no actual +# aliases). The following setting can be changed to specify a different +# location for the system alias file. + +SYSTEM_ALIASES_FILE=/etc/aliases + + +#------------------------------------------------------------------------------ +# There are some testing options (-be, -bt, -bv) that read data from the +# standard input when no arguments are supplied. By default, the input lines +# are read using the standard fgets() function. This does not support line +# editing during interactive input (though the terminal's "erase" character +# works as normal). If your operating system has the readline() function, and +# in addition supports dynamic loading of library functions, you can cause +# Exim to use readline() for the -be testing option (only) by uncommenting the +# following setting. Dynamic loading is used so that the library is loaded only +# when the -be testing option is given; by the time the loading occurs, +# Exim has given up its root privilege and is running as the calling user. This +# is the reason why readline() is NOT supported for -bt and -bv, because Exim +# runs as root or as exim, respectively, for those options. When USE_READLINE +# is "yes", as well as supporting line editing, a history of input lines in the +# current run is maintained. + +# USE_READLINE=yes + + + +############################################################################### +# THINGS YOU ALMOST NEVER NEED TO MENTION # +############################################################################### + +# The settings in this section are available for use in special circumstances. +# In the vast majority of installations you need not change anything below. + + +#------------------------------------------------------------------------------ +# The following commands live in different places in some OS. Either the +# ultimate default settings, or the OS-specific files should already point to +# the right place, but they can be overridden here if necessary. These settings +# are used when building various scripts to ensure that the correct paths are +# used when the scripts are run. They are not used in the Makefile itself. Perl +# is not necessary for running Exim unless you set EXIM_PERL (see above) to get +# it embedded, but there are some utilities that are Perl scripts. If you +# haven't got Perl, Exim will still build and run; you just won't be able to +# use those utilities. + +# CHOWN_COMMAND=/usr/bin/chown +# CHGRP_COMMAND=/usr/bin/chgrp +# MV_COMMAND=/bin/mv +# RM_COMMAND=/bin/rm +# PERL_COMMAND=/usr/bin/perl + + +#------------------------------------------------------------------------------ +# The following macro can be used to change the command for building a library +# of functions. By default the "ar" command is used, with options "cq". +# Only in rare circumstances should you need to change this. + +# AR=ar cq + + +#------------------------------------------------------------------------------ +# In some operating systems, the value of the TMPDIR environment variable +# controls where temporary files are created. Exim does not make use of +# temporary files, except when delivering to MBX mailboxes. However, if Exim +# calls any external libraries (e.g. DBM libraries), they may use temporary +# files, and thus be influenced by the value of TMPDIR. For this reason, when +# Exim starts, it checks the environment for TMPDIR, and if it finds it is set, +# it replaces the value with what is defined here. Commenting this setting +# suppresses the check altogether. + +TMPDIR="/tmp" + + +#------------------------------------------------------------------------------ +# The following macros can be used to change the default modes that are used +# by the appendfile transport. In most installations the defaults are just +# fine, and in any case, you can change particular instances of the transport +# at run time if you want. + +# APPENDFILE_MODE=0600 +# APPENDFILE_DIRECTORY_MODE=0700 +# APPENDFILE_LOCKFILE_MODE=0600 + + +#------------------------------------------------------------------------------ +# In some installations there may be multiple machines sharing file systems, +# where a different configuration file is required for Exim on the different +# machines. If CONFIGURE_FILE_USE_NODE is defined, then Exim will first look +# for a configuration file whose name is that defined by CONFIGURE_FILE, +# with the node name obtained by uname() tacked on the end, separated by a +# period (for example, /usr/exim/configure.host.in.some.domain). If this file +# does not exist, then the bare configuration file name is tried. + +# CONFIGURE_FILE_USE_NODE=yes + + +#------------------------------------------------------------------------------ +# In some esoteric configurations two different versions of Exim are run, +# with different setuid values, and different configuration files are required +# to handle the different cases. If CONFIGURE_FILE_USE_EUID is defined, then +# Exim will first look for a configuration file whose name is that defined +# by CONFIGURE_FILE, with the effective uid tacked on the end, separated by +# a period (for eximple, /usr/exim/configure.0). If this file does not exist, +# then the bare configuration file name is tried. In the case when both +# CONFIGURE_FILE_USE_EUID and CONFIGURE_FILE_USE_NODE are set, four files +# are tried: .., ., ., and . + +# CONFIGURE_FILE_USE_EUID=yes + + +#------------------------------------------------------------------------------ +# The size of the delivery buffers: These specify the sizes (in bytes) of +# the buffers that are used when copying a message from the spool to a +# destination. There is rarely any need to change these values. + +# DELIVER_IN_BUFFER_SIZE=8192 +# DELIVER_OUT_BUFFER_SIZE=8192 + + +#------------------------------------------------------------------------------ +# The mode of the database directory: Exim creates a directory called "db" +# in its spool directory, to hold its databases of hints. This variable +# determines the mode of the created directory. The default value in the +# source is 0750. + +# EXIMDB_DIRECTORY_MODE=0750 + + +#------------------------------------------------------------------------------ +# Database file mode: The mode of files created in the "db" directory defaults +# to 0640 in the source, and can be changed here. + +# EXIMDB_MODE=0640 + + +#------------------------------------------------------------------------------ +# Database lock file mode: The mode of zero-length files created in the "db" +# directory to use for locking purposes defaults to 0640 in the source, and +# can be changed here. + +# EXIMDB_LOCKFILE_MODE=0640 + + +#------------------------------------------------------------------------------ +# This parameter sets the maximum length of the header portion of a message +# that Exim is prepared to process. The default setting is one megabyte. The +# limit exists in order to catch rogue mailers that might connect to your SMTP +# port, start off a header line, and then just pump junk at it for ever. The +# message_size_limit option would also catch this, but it may not be set. +# The value set here is the default; it can be changed at runtime. + +# HEADER_MAXSIZE="(1024*1024)" + + +#------------------------------------------------------------------------------ +# The mode of the input directory: The input directory is where messages are +# kept while awaiting delivery. Exim creates it if necessary, using a mode +# which can be defined here (default 0750). + +# INPUT_DIRECTORY_MODE=0750 + + +#------------------------------------------------------------------------------ +# The mode of Exim's log directory, when it is created by Exim inside the spool +# directory, defaults to 0750 but can be changed here. + +# LOG_DIRECTORY_MODE=0750 + + +#------------------------------------------------------------------------------ +# The log files themselves are created as required, with a mode that defaults +# to 0640, but which can be changed here. + +# LOG_MODE=0640 + + +#------------------------------------------------------------------------------ +# The TESTDB lookup is for performing tests on the handling of lookup results, +# and is not useful for general running. It should be included only when +# debugging the code of Exim. + +# LOOKUP_TESTDB=yes + + +#------------------------------------------------------------------------------ +# /bin/sh is used by default as the shell in which to run commands that are +# defined in the makefiles. This can be changed if necessary, by uncommenting +# this line and specifying another shell, but note that a Bourne-compatible +# shell is expected. + +# MAKE_SHELL=/bin/sh + + +#------------------------------------------------------------------------------ +# The maximum number of named lists of each type (address, domain, host, and +# local part) can be increased by changing this value. It should be set to +# a multiple of 16. + +# MAX_NAMED_LIST=16 + + +#------------------------------------------------------------------------------ +# Network interfaces: Unless you set the local_interfaces option in the runtime +# configuration file to restrict Exim to certain interfaces only, it will run +# code to find all the interfaces there are on your host. Unfortunately, +# the call to the OS that does this requires a buffer large enough to hold +# data for all the interfaces - it was designed in the days when a host rarely +# had more than three or four interfaces. Nowadays hosts can have very many +# virtual interfaces running on the same hardware. If you have more than 250 +# virtual interfaces, you will need to uncomment this setting and increase the +# value. + +# MAXINTERFACES=250 + + +#------------------------------------------------------------------------------ +# Per-message logs: While a message is in the process of being delivered, +# comments on its progress are written to a message log, for the benefit of +# human administrators. These logs are held in a directory called "msglog" +# in the spool directory. Its mode defaults to 0750, but can be changed here. +# The message log directory is also used for storing files that are used by +# transports for returning data to a message's sender (see the "return_output" +# option for transports). + +# MSGLOG_DIRECTORY_MODE=0750 + + +#------------------------------------------------------------------------------ +# There are three options which are used when compiling the Perl interface and +# when linking with Perl. The default values for these are placed automatically +# at the head of the Makefile by the script which builds it. However, if you +# want to override them, you can do so here. + +# PERL_CC= +# PERL_CCOPTS= +# PERL_LIBS= + + +#------------------------------------------------------------------------------ +# Identifying the daemon: When an Exim daemon starts up, it writes its pid +# (process id) to a file so that it can easily be identified. The path of the +# file can be specified here. Some installations may want something like this: + +# PID_FILE_PATH=/var/lock/exim.pid + +# If PID_FILE_PATH is not defined, Exim writes a file in its spool directory +# using the name "exim-daemon.pid". + +# If you start up a daemon without the -bd option (for example, with just +# the -q15m option), a pid file is not written. Also, if you override the +# configuration file with the -oX option, no pid file is written. In other +# words, the pid file is written only for a "standard" daemon. + + +#------------------------------------------------------------------------------ +# If Exim creates the spool directory, it is given this mode, defaulting in the +# source to 0750. + +# SPOOL_DIRECTORY_MODE=0750 + + +#------------------------------------------------------------------------------ +# The mode of files on the input spool which hold the contents of messages can +# be changed here. The default is 0640 so that information from the spool is +# available to anyone who is a member of the Exim group. + +# SPOOL_MODE=0640 + + +#------------------------------------------------------------------------------ +# Moving frozen messages: If the following is uncommented, Exim is compiled +# with support for automatically moving frozen messages out of the main spool +# directory, a facility that is found useful by some large installations. A +# run time option is required to cause the moving actually to occur. Such +# messages become "invisible" to the normal management tools. + +# SUPPORT_MOVE_FROZEN_MESSAGES=yes + +# End of EDITME for Exim 4. diff --git a/src/src/acl.c b/src/src/acl.c new file mode 100644 index 000000000..d6a354cf2 --- /dev/null +++ b/src/src/acl.c @@ -0,0 +1,2151 @@ +/* $Cambridge: exim/src/src/acl.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Code for handling Access Control Lists (ACLs) */ + +#include "exim.h" + + +/* Default callout timeout */ + +#define CALLOUT_TIMEOUT_DEFAULT 30 + +/* ACL verb codes - keep in step with the table of verbs that follows */ + +enum { ACL_ACCEPT, ACL_DEFER, ACL_DENY, ACL_DISCARD, ACL_DROP, ACL_REQUIRE, + ACL_WARN }; + +/* ACL verbs */ + +static uschar *verbs[] = + { US"accept", US"defer", US"deny", US"discard", US"drop", US"require", + US"warn" }; + +/* For each verb, the condition for which "message" is used */ + +static int msgcond[] = { FAIL, OK, OK, FAIL, OK, FAIL, OK }; + +/* ACL condition and modifier codes - keep in step with the table that +follows. */ + +enum { ACLC_ACL, ACLC_AUTHENTICATED, ACLC_CONDITION, ACLC_CONTROL, ACLC_DELAY, + ACLC_DNSLISTS, ACLC_DOMAINS, ACLC_ENCRYPTED, ACLC_ENDPASS, ACLC_HOSTS, + ACLC_LOCAL_PARTS, ACLC_LOG_MESSAGE, ACLC_LOGWRITE, ACLC_MESSAGE, + ACLC_RECIPIENTS, ACLC_SENDER_DOMAINS, ACLC_SENDERS, ACLC_SET, ACLC_VERIFY }; + +/* ACL conditions/modifiers: "delay", "control", "endpass", "message", +"log_message", "logwrite", and "set" are modifiers that look like conditions +but always return TRUE. They are used for their side effects. */ + +static uschar *conditions[] = { US"acl", US"authenticated", US"condition", + US"control", US"delay", US"dnslists", US"domains", US"encrypted", + US"endpass", US"hosts", US"local_parts", US"log_message", US"logwrite", + US"message", US"recipients", US"sender_domains", US"senders", US"set", + US"verify" }; + +/* Flags to indicate for which conditions /modifiers a string expansion is done +at the outer level. In the other cases, expansion already occurs in the +checking functions. */ + +static uschar cond_expand_at_top[] = { + TRUE, /* acl */ + FALSE, /* authenticated */ + TRUE, /* condition */ + TRUE, /* control */ + TRUE, /* delay */ + TRUE, /* dnslists */ + FALSE, /* domains */ + FALSE, /* encrypted */ + TRUE, /* endpass */ + FALSE, /* hosts */ + FALSE, /* local_parts */ + TRUE, /* log_message */ + TRUE, /* logwrite */ + TRUE, /* message */ + FALSE, /* recipients */ + FALSE, /* sender_domains */ + FALSE, /* senders */ + TRUE, /* set */ + TRUE /* verify */ +}; + +/* Flags to identify the modifiers */ + +static uschar cond_modifiers[] = { + FALSE, /* acl */ + FALSE, /* authenticated */ + FALSE, /* condition */ + TRUE, /* control */ + TRUE, /* delay */ + FALSE, /* dnslists */ + FALSE, /* domains */ + FALSE, /* encrypted */ + TRUE, /* endpass */ + FALSE, /* hosts */ + FALSE, /* local_parts */ + TRUE, /* log_message */ + TRUE, /* log_write */ + TRUE, /* message */ + FALSE, /* recipients */ + FALSE, /* sender_domains */ + FALSE, /* senders */ + TRUE, /* set */ + FALSE /* verify */ +}; + +/* Bit map of which conditions are not allowed at certain times. For each +condition, there's a bitmap of dis-allowed times. */ + +static unsigned int cond_forbids[] = { + 0, /* acl */ + (1<next); + this->next = NULL; + this->verb = v; + this->condition = NULL; + condp = &(this->condition); + if (*s == 0) continue; /* No condition on this line */ + if (*s == '!') + { + negated = TRUE; + s++; + } + s = readconf_readname(name, sizeof(name), s); /* Condition name */ + } + + /* Handle a condition or modifier. */ + + c = acl_checkname(name, conditions, sizeof(conditions)/sizeof(char *)); + if (c < 0) + { + *error = string_sprintf("unknown ACL condition/modifier in \"%s\"", + saveline); + return NULL; + } + + /* The modifiers may not be negated */ + + if (negated && cond_modifiers[c]) + { + *error = string_sprintf("ACL error: negation is not allowed with " + "\"%s\"", conditions[c]); + return NULL; + } + + /* ENDPASS may occur only with ACCEPT or DISCARD. */ + + if (c == ACLC_ENDPASS && + this->verb != ACL_ACCEPT && + this->verb != ACL_DISCARD) + { + *error = string_sprintf("ACL error: \"%s\" is not allowed with \"%s\"", + conditions[c], verbs[this->verb]); + return NULL; + } + + cond = store_get(sizeof(acl_condition_block)); + cond->next = NULL; + cond->type = c; + cond->u.negated = negated; + + *condp = cond; + condp = &(cond->next); + + /* The "set" modifier is different in that its argument is "name=value" + rather than just a value, and we can check the validity of the name, which + gives us a variable number to insert into the data block. */ + + if (c == ACLC_SET) + { + if (Ustrncmp(s, "acl_", 4) != 0 || (s[4] != 'c' && s[4] != 'm') || + !isdigit(s[5]) || (!isspace(s[6]) && s[6] != '=')) + { + *error = string_sprintf("unrecognized name after \"set\" in ACL " + "modifier \"set %s\"", s); + return NULL; + } + + cond->u.varnumber = s[5] - '0'; + if (s[4] == 'm') cond->u.varnumber += ACL_C_MAX; + s += 6; + while (isspace(*s)) s++; + } + + /* For "set", we are now positioned for the data. For the others, only + "endpass" has no data */ + + if (c != ACLC_ENDPASS) + { + if (*s++ != '=') + { + *error = string_sprintf("\"=\" missing after ACL \"%s\" %s", name, + cond_modifiers[c]? US"modifier" : US"condition"); + return NULL; + } + while (isspace(*s)) s++; + cond->arg = string_copy(s); + } + } + +return yield; +} + + + +/************************************************* +* Handle warnings * +*************************************************/ + +/* This function is called when a WARN verb's conditions are true. It adds to +the message's headers, and/or writes information to the log. In each case, this +only happens once (per message for headers, per connection for log). + +Arguments: + where ACL_WHERE_xxxx indicating which ACL this is + user_message message for adding to headers + log_message message for logging, if different + +Returns: nothing +*/ + +static void +acl_warn(int where, uschar *user_message, uschar *log_message) +{ +int hlen; + +if (log_message != NULL && log_message != user_message) + { + uschar *text; + string_item *logged; + + text = string_sprintf("%s Warning: %s", host_and_ident(TRUE), + string_printing(log_message)); + + /* If a sender verification has failed, and the log message is "sender verify + failed", add the failure message. */ + + if (sender_verified_failed != NULL && + sender_verified_failed->message != NULL && + strcmpic(log_message, US"sender verify failed") == 0) + text = string_sprintf("%s: %s", text, sender_verified_failed->message); + + /* Search previously logged warnings. They are kept in malloc store so they + can be freed at the start of a new message. */ + + for (logged = acl_warn_logged; logged != NULL; logged = logged->next) + if (Ustrcmp(logged->text, text) == 0) break; + + if (logged == NULL) + { + int length = Ustrlen(text) + 1; + log_write(0, LOG_MAIN, "%s", text); + logged = store_malloc(sizeof(string_item) + length); + logged->text = (uschar *)logged + sizeof(string_item); + memcpy(logged->text, text, length); + logged->next = acl_warn_logged; + acl_warn_logged = logged; + } + } + +/* If there's no user message, we are done. */ + +if (user_message == NULL) return; + +/* If this isn't a message ACL, we can't do anything with a user message. +Log an error. */ + +if (where > ACL_WHERE_NOTSMTP) + { + log_write(0, LOG_MAIN|LOG_PANIC, "ACL \"warn\" with \"message\" setting " + "found in a non-message (%s) ACL: cannot specify header lines here: " + "message ignored", acl_wherenames[where]); + return; + } + +/* Treat the user message as a sequence of one or more header lines. */ + +hlen = Ustrlen(user_message); +if (hlen > 0) + { + uschar *text, *p, *q; + + /* Add a final newline if not present */ + + text = ((user_message)[hlen-1] == '\n')? user_message : + string_sprintf("%s\n", user_message); + + /* Loop for multiple header lines, taking care about continuations */ + + for (p = q = text; *p != 0; ) + { + uschar *s; + int newtype = htype_add_bot; + header_line **hptr = &acl_warn_headers; + + /* Find next header line within the string */ + + for (;;) + { + q = Ustrchr(q, '\n'); + if (*(++q) != ' ' && *q != '\t') break; + } + + /* If the line starts with a colon, interpret the instruction for where to + add it. This temporarily sets up a new type. */ + + if (*p == ':') + { + if (strncmpic(p, US":after_received:", 16) == 0) + { + newtype = htype_add_rec; + p += 16; + } + else if (strncmpic(p, US":at_start:", 10) == 0) + { + newtype = htype_add_top; + p += 10; + } + else if (strncmpic(p, US":at_end:", 8) == 0) + { + newtype = htype_add_bot; + p += 8; + } + while (*p == ' ' || *p == '\t') p++; + } + + /* See if this line starts with a header name, and if not, add X-ACL-Warn: + to the front of it. */ + + for (s = p; s < q - 1; s++) + { + if (*s == ':' || !isgraph(*s)) break; + } + + s = string_sprintf("%s%.*s", (*s == ':')? "" : "X-ACL-Warn: ", q - p, p); + hlen = Ustrlen(s); + + /* See if this line has already been added */ + + while (*hptr != NULL) + { + if (Ustrncmp((*hptr)->text, s, hlen) == 0) break; + hptr = &((*hptr)->next); + } + + /* Add if not previously present */ + + if (*hptr == NULL) + { + header_line *h = store_get(sizeof(header_line)); + h->text = s; + h->next = NULL; + h->type = newtype; + h->slen = hlen; + *hptr = h; + hptr = &(h->next); + } + + /* Advance for next header line within the string */ + + p = q; + } + } +} + + + +/************************************************* +* Verify and check reverse DNS * +*************************************************/ + +/* Called from acl_verify() below. We look up the host name(s) of the client IP +address if this has not yet been done. The host_name_lookup() function checks +that one of these names resolves to an address list that contains the client IP +address, so we don't actually have to do the check here. + +Arguments: + user_msgptr pointer for user message + log_msgptr pointer for log message + +Returns: OK verification condition succeeded + FAIL verification failed + DEFER there was a problem verifying +*/ + +static int +acl_verify_reverse(uschar **user_msgptr, uschar **log_msgptr) +{ +int rc; + +user_msgptr = user_msgptr; /* stop compiler warning */ + +/* Previous success */ + +if (sender_host_name != NULL) return OK; + +/* Previous failure */ + +if (host_lookup_failed) + { + *log_msgptr = string_sprintf("host lookup failed%s", host_lookup_msg); + return FAIL; + } + +/* Need to do a lookup */ + +HDEBUG(D_acl) + debug_printf("looking up host name to force name/address consistency check\n"); + +if ((rc = host_name_lookup()) != OK) + { + *log_msgptr = (rc == DEFER)? + US"host lookup deferred for reverse lookup check" + : + string_sprintf("host lookup failed for reverse lookup check%s", + host_lookup_msg); + return rc; /* DEFER or FAIL */ + } + +host_build_sender_fullhost(); +return OK; +} + + + +/************************************************* +* Handle verification (address & other) * +*************************************************/ + +/* This function implements the "verify" condition. It is called when +encountered in any ACL, because some tests are almost always permitted. Some +just don't make sense, and always fail (for example, an attempt to test a host +lookup for a non-TCP/IP message). Others are restricted to certain ACLs. + +Arguments: + where where called from + addr the recipient address that the ACL is handling, or NULL + arg the argument of "verify" + user_msgptr pointer for user message + log_msgptr pointer for log message + basic_errno where to put verify errno + +Returns: OK verification condition succeeded + FAIL verification failed + DEFER there was a problem verifying + ERROR syntax error +*/ + +static int +acl_verify(int where, address_item *addr, uschar *arg, + uschar **user_msgptr, uschar **log_msgptr, int *basic_errno) +{ +int sep = '/'; +int callout = -1; +int callout_overall = -1; +int verify_options = 0; +int rc; +BOOL verify_header_sender = FALSE; +BOOL defer_ok = FALSE; +BOOL callout_defer_ok = FALSE; +BOOL no_details = FALSE; +address_item *sender_vaddr = NULL; +uschar *verify_sender_address = NULL; +uschar *pm_mailfrom = NULL; +uschar *se_mailfrom = NULL; +uschar *list = arg; +uschar *ss = string_nextinlist(&list, &sep, big_buffer, big_buffer_size); + +if (ss == NULL) goto BAD_VERIFY; + +/* Handle name/address consistency verification in a separate function. */ + +if (strcmpic(ss, US"reverse_host_lookup") == 0) + { + if (sender_host_address == NULL) return OK; + return acl_verify_reverse(user_msgptr, log_msgptr); + } + +/* TLS certificate verification is done at STARTTLS time; here we just +test whether it was successful or not. (This is for optional verification; for +mandatory verification, the connection doesn't last this long.) */ + +if (strcmpic(ss, US"certificate") == 0) + { + if (tls_certificate_verified) return OK; + *user_msgptr = US"no verified certificate"; + return FAIL; + } + +/* We can test the result of optional HELO verification */ + +if (strcmpic(ss, US"helo") == 0) return helo_verified? OK : FAIL; + +/* Handle header verification options - permitted only after DATA or a non-SMTP +message. */ + +if (strncmpic(ss, US"header_", 7) == 0) + { + if (where != ACL_WHERE_DATA && where != ACL_WHERE_NOTSMTP) + { + *log_msgptr = string_sprintf("cannot check header contents in ACL for %s " + "(only possible in ACL for DATA)", acl_wherenames[where]); + return ERROR; + } + + /* Check that all relevant header lines have the correct syntax. If there is + a syntax error, we return details of the error to the sender if configured to + send out full details. (But a "message" setting on the ACL can override, as + always). */ + + if (strcmpic(ss+7, US"syntax") == 0) + { + int rc = verify_check_headers(log_msgptr); + if (rc != OK && smtp_return_error_details && *log_msgptr != NULL) + *user_msgptr = string_sprintf("Rejected after DATA: %s", *log_msgptr); + return rc; + } + + /* Check that there is at least one verifiable sender address in the relevant + header lines. This can be followed by callout and defer options, just like + sender and recipient. */ + + else if (strcmpic(ss+7, US"sender") == 0) verify_header_sender = TRUE; + + /* Unknown verify argument starting with "header_" */ + + else goto BAD_VERIFY; + } + +/* Otherwise, first item in verify argument must be "sender" or "recipient". +In the case of a sender, this can optionally be followed by an address to use +in place of the actual sender (rare special-case requirement). */ + +else if (strncmpic(ss, US"sender", 6) == 0) + { + uschar *s = ss + 6; + if (where > ACL_WHERE_NOTSMTP) + { + *log_msgptr = string_sprintf("cannot verify sender in ACL for %s " + "(only possible for MAIL, RCPT, PREDATA, or DATA)", + acl_wherenames[where]); + return ERROR; + } + if (*s == 0) + verify_sender_address = sender_address; + else + { + while (isspace(*s)) s++; + if (*s++ != '=') goto BAD_VERIFY; + while (isspace(*s)) s++; + verify_sender_address = string_copy(s); + } + } +else + { + if (strcmpic(ss, US"recipient") != 0) goto BAD_VERIFY; + if (addr == NULL) + { + *log_msgptr = string_sprintf("cannot verify recipient in ACL for %s " + "(only possible for RCPT)", acl_wherenames[where]); + return ERROR; + } + } + +/* Remaining items are optional */ + +while ((ss = string_nextinlist(&list, &sep, big_buffer, big_buffer_size)) + != NULL) + { + if (strcmpic(ss, US"defer_ok") == 0) defer_ok = TRUE; + else if (strcmpic(ss, US"no_details") == 0) no_details = TRUE; + + /* These two old options are left for backwards compatibility */ + + else if (strcmpic(ss, US"callout_defer_ok") == 0) + { + callout_defer_ok = TRUE; + if (callout == -1) callout = CALLOUT_TIMEOUT_DEFAULT; + } + + else if (strcmpic(ss, US"check_postmaster") == 0) + { + pm_mailfrom = US""; + if (callout == -1) callout = CALLOUT_TIMEOUT_DEFAULT; + } + + /* The callout option has a number of sub-options, comma separated */ + + else if (strncmpic(ss, US"callout", 7) == 0) + { + callout = CALLOUT_TIMEOUT_DEFAULT; + ss += 7; + if (*ss != 0) + { + while (isspace(*ss)) ss++; + if (*ss++ == '=') + { + int optsep = ','; + uschar *opt; + uschar buffer[256]; + while (isspace(*ss)) ss++; + while ((opt = string_nextinlist(&ss, &optsep, buffer, sizeof(buffer))) + != NULL) + { + if (strcmpic(opt, US"defer_ok") == 0) callout_defer_ok = TRUE; + else if (strcmpic(opt, US"no_cache") == 0) + verify_options |= vopt_callout_no_cache; + else if (strcmpic(opt, US"random") == 0) + verify_options |= vopt_callout_random; + else if (strcmpic(opt, US"use_sender") == 0) + verify_options |= vopt_callout_recipsender; + else if (strcmpic(opt, US"use_postmaster") == 0) + verify_options |= vopt_callout_recippmaster; + else if (strcmpic(opt, US"postmaster") == 0) pm_mailfrom = US""; + + else if (strncmpic(opt, US"mailfrom", 8) == 0) + { + if (!verify_header_sender) + { + *log_msgptr = string_sprintf("\"mailfrom\" is allowed as a " + "callout option only for verify=header_sender (detected in ACL " + "condition \"%s\")", arg); + return ERROR; + } + opt += 8; + while (isspace(*opt)) opt++; + if (*opt++ != '=') + { + *log_msgptr = string_sprintf("'=' expected after " + "\"mailfrom\" in ACL condition \"%s\"", arg); + return ERROR; + } + while (isspace(*opt)) opt++; + se_mailfrom = string_copy(opt); + } + + else if (strncmpic(opt, US"postmaster_mailfrom", 19) == 0) + { + opt += 19; + while (isspace(*opt)) opt++; + if (*opt++ != '=') + { + *log_msgptr = string_sprintf("'=' expected after " + "\"postmaster_mailfrom\" in ACL condition \"%s\"", arg); + return ERROR; + } + while (isspace(*opt)) opt++; + pm_mailfrom = string_copy(opt); + } + + else if (strncmpic(opt, US"maxwait", 7) == 0) + { + opt += 7; + while (isspace(*opt)) opt++; + if (*opt++ != '=') + { + *log_msgptr = string_sprintf("'=' expected after \"maxwait\" in " + "ACL condition \"%s\"", arg); + return ERROR; + } + while (isspace(*opt)) opt++; + callout_overall = readconf_readtime(opt, 0, FALSE); + if (callout_overall < 0) + { + *log_msgptr = string_sprintf("bad time value in ACL condition " + "\"verify %s\"", arg); + return ERROR; + } + } + else /* Plain time is callout connect/command timeout */ + { + callout = readconf_readtime(opt, 0, FALSE); + if (callout < 0) + { + *log_msgptr = string_sprintf("bad time value in ACL condition " + "\"verify %s\"", arg); + return ERROR; + } + } + } + } + else + { + *log_msgptr = string_sprintf("'=' expected after \"callout\" in " + "ACL condition \"%s\"", arg); + return ERROR; + } + } + } + + /* Option not recognized */ + + else + { + *log_msgptr = string_sprintf("unknown option \"%s\" in ACL " + "condition \"verify %s\"", ss, arg); + return ERROR; + } + } + +if ((verify_options & (vopt_callout_recipsender|vopt_callout_recippmaster)) == + (vopt_callout_recipsender|vopt_callout_recippmaster)) + { + *log_msgptr = US"only one of use_sender and use_postmaster can be set " + "for a recipient callout"; + return ERROR; + } + +/* Handle sender-in-header verification. Default the user message to the log +message if giving out verification details. */ + +if (verify_header_sender) + { + rc = verify_check_header_address(user_msgptr, log_msgptr, callout, + callout_overall, se_mailfrom, pm_mailfrom, verify_options); + if (smtp_return_error_details) + { + if (*user_msgptr == NULL && *log_msgptr != NULL) + *user_msgptr = string_sprintf("Rejected after DATA: %s", *log_msgptr); + if (rc == DEFER) acl_temp_details = TRUE; + } + } + +/* Handle a sender address. The default is to verify *the* sender address, but +optionally a different address can be given, for special requirements. If the +address is empty, we are dealing with a bounce message that has no sender, so +we cannot do any checking. If the real sender address gets rewritten during +verification (e.g. DNS widening), set the flag to stop it being rewritten again +during message reception. + +A list of verified "sender" addresses is kept to try to avoid doing to much +work repetitively when there are multiple recipients in a message and they all +require sender verification. However, when callouts are involved, it gets too +complicated because different recipients may require different callout options. +Therefore, we always do a full sender verify when any kind of callout is +specified. Caching elsewhere, for instance in the DNS resolver and in the +callout handling, should ensure that this is not terribly inefficient. */ + +else if (verify_sender_address != NULL) + { + if ((verify_options & (vopt_callout_recipsender|vopt_callout_recippmaster)) + != 0) + { + *log_msgptr = US"use_sender or use_postmaster cannot be used for a " + "sender verify callout"; + return ERROR; + } + + sender_vaddr = verify_checked_sender(verify_sender_address); + if (sender_vaddr != NULL && /* Previously checked */ + callout <= 0) /* No callout needed this time */ + { + /* If the "routed" flag is set, it means that routing worked before, so + this check can give OK (the saved return code value, if set, belongs to a + callout that was done previously). If the "routed" flag is not set, routing + must have failed, so we use the saved return code. */ + + if (testflag(sender_vaddr, af_verify_routed)) rc = OK; else + { + rc = sender_vaddr->special_action; + *basic_errno = sender_vaddr->basic_errno; + } + HDEBUG(D_acl) debug_printf("using cached sender verify result\n"); + } + + /* Do a new verification, and cache the result. The cache is used to avoid + verifying the sender multiple times for multiple RCPTs when callouts are not + specified (see comments above). + + The cache is also used on failure to give details in response to the first + RCPT that gets bounced for this reason. However, this can be suppressed by + the no_details option, which sets the flag that says "this detail has already + been sent". The cache normally contains just one address, but there may be + more in esoteric circumstances. */ + + else + { + BOOL routed = TRUE; + sender_vaddr = deliver_make_addr(verify_sender_address, TRUE); + if (no_details) setflag(sender_vaddr, af_sverify_told); + if (verify_sender_address[0] != 0) + { + /* If this is the real sender address, save the unrewritten version + for use later in receive. Otherwise, set a flag so that rewriting the + sender in verify_address() does not update sender_address. */ + + if (verify_sender_address == sender_address) + sender_address_unrewritten = sender_address; + else + verify_options |= vopt_fake_sender; + + /* The recipient, qualify, and expn options are never set in + verify_options. */ + + rc = verify_address(sender_vaddr, NULL, verify_options, callout, + callout_overall, se_mailfrom, pm_mailfrom, &routed); + + HDEBUG(D_acl) debug_printf("----------- end verify ------------\n"); + + if (rc == OK) + { + if (Ustrcmp(sender_vaddr->address, verify_sender_address) != 0) + { + DEBUG(D_acl) debug_printf("sender %s verified ok as %s\n", + verify_sender_address, sender_vaddr->address); + } + else + { + DEBUG(D_acl) debug_printf("sender %s verified ok\n", + verify_sender_address); + } + } + else *basic_errno = sender_vaddr->basic_errno; + } + else rc = OK; /* Null sender */ + + /* Cache the result code */ + + if (routed) setflag(sender_vaddr, af_verify_routed); + if (callout > 0) setflag(sender_vaddr, af_verify_callout); + sender_vaddr->special_action = rc; + sender_vaddr->next = sender_verified_list; + sender_verified_list = sender_vaddr; + } + } + +/* A recipient address just gets a straightforward verify; again we must handle +the DEFER overrides. */ + +else + { + address_item addr2; + + /* We must use a copy of the address for verification, because it might + get rewritten. */ + + addr2 = *addr; + rc = verify_address(&addr2, NULL, verify_options|vopt_is_recipient, callout, + callout_overall, se_mailfrom, pm_mailfrom, NULL); + HDEBUG(D_acl) debug_printf("----------- end verify ------------\n"); + *log_msgptr = addr2.message; + *user_msgptr = addr2.user_message; + *basic_errno = addr2.basic_errno; + + /* Make $address_data visible */ + deliver_address_data = addr2.p.address_data; + } + +/* We have a result from the relevant test. Handle defer overrides first. */ + +if (rc == DEFER && (defer_ok || + (callout_defer_ok && *basic_errno == ERRNO_CALLOUTDEFER))) + { + HDEBUG(D_acl) debug_printf("verify defer overridden by %s\n", + defer_ok? "defer_ok" : "callout_defer_ok"); + rc = OK; + } + +/* If we've failed a sender, set up a recipient message, and point +sender_verified_failed to the address item that actually failed. */ + +if (rc != OK && verify_sender_address != NULL) + { + if (rc != DEFER) + { + *log_msgptr = *user_msgptr = US"Sender verify failed"; + } + else if (*basic_errno != ERRNO_CALLOUTDEFER) + { + *log_msgptr = *user_msgptr = US"Could not complete sender verify"; + } + else + { + *log_msgptr = US"Could not complete sender verify callout"; + *user_msgptr = smtp_return_error_details? sender_vaddr->user_message : + *log_msgptr; + } + + sender_verified_failed = sender_vaddr; + } + +/* Verifying an address messes up the values of $domain and $local_part, +so reset them before returning if this is a RCPT ACL. */ + +if (addr != NULL) + { + deliver_domain = addr->domain; + deliver_localpart = addr->local_part; + } +return rc; + +/* Syntax errors in the verify argument come here. */ + +BAD_VERIFY: +*log_msgptr = string_sprintf("expected \"sender[=address]\", \"recipient\", " + "\"header_syntax\" or \"header_sender\" at start of ACL condition " + "\"verify %s\"", arg); +return ERROR; +} + + + + +/************************************************* +* Check argument for control= modifier * +*************************************************/ + +/* Called from acl_check_condition() below + +Arguments: + arg the argument string for control= + pptr set to point to the terminating character + where which ACL we are in + log_msgptr for error messages + +Returns: CONTROL_xxx value +*/ + +static int +decode_control(uschar *arg, uschar **pptr, int where, uschar **log_msgptr) +{ +int len; +control_def *d; + +for (d = controls_list; + d < controls_list + sizeof(controls_list)/sizeof(control_def); + d++) + { + len = Ustrlen(d->name); + if (Ustrncmp(d->name, arg, len) == 0) break; + } + +if (d >= controls_list + sizeof(controls_list)/sizeof(control_def) || + (arg[len] != 0 && (!d->has_option || arg[len] != '/'))) + { + *log_msgptr = string_sprintf("syntax error in \"control=%s\"", arg); + return CONTROL_ERROR; + } + +if (where > d->where_max) + { + *log_msgptr = string_sprintf("cannot use \"control=%s\" in %s ACL", + arg, acl_wherenames[where]); + return CONTROL_ERROR; + } + +*pptr = arg + len; +return d->value; +} + + + +/************************************************* +* Handle conditions/modifiers on an ACL item * +*************************************************/ + +/* Called from acl_check() below. + +Arguments: + verb ACL verb + cb ACL condition block - if NULL, result is OK + where where called from + addr the address being checked for RCPT, or NULL + level the nesting level + epp pointer to pass back TRUE if "endpass" encountered + (applies only to "accept" and "discard") + user_msgptr user message pointer + log_msgptr log message pointer + basic_errno pointer to where to put verify error + +Returns: OK - all conditions are met + DISCARD - an "acl" condition returned DISCARD - only allowed + for "accept" or "discard" verbs + FAIL - at least one condition fails + FAIL_DROP - an "acl" condition returned FAIL_DROP + DEFER - can't tell at the moment (typically, lookup defer, + but can be temporary callout problem) + ERROR - ERROR from nested ACL or expansion failure or other + error +*/ + +static int +acl_check_condition(int verb, acl_condition_block *cb, int where, + address_item *addr, int level, BOOL *epp, uschar **user_msgptr, + uschar **log_msgptr, int *basic_errno) +{ +uschar *user_message = NULL; +uschar *log_message = NULL; +uschar *p; +int rc = OK; + +for (; cb != NULL; cb = cb->next) + { + uschar *arg; + + /* The message and log_message items set up messages to be used in + case of rejection. They are expanded later. */ + + if (cb->type == ACLC_MESSAGE) + { + user_message = cb->arg; + continue; + } + + if (cb->type == ACLC_LOG_MESSAGE) + { + log_message = cb->arg; + continue; + } + + /* The endpass "condition" just sets a flag to show it occurred. This is + checked at compile time to be on an "accept" or "discard" item. */ + + if (cb->type == ACLC_ENDPASS) + { + *epp = TRUE; + continue; + } + + /* For other conditions and modifiers, the argument is expanded now for some + of them, but not for all, because expansion happens down in some lower level + checking functions in some cases. */ + + if (cond_expand_at_top[cb->type]) + { + arg = expand_string(cb->arg); + if (arg == NULL) + { + if (expand_string_forcedfail) continue; + *log_msgptr = string_sprintf("failed to expand ACL string \"%s\": %s", + cb->arg, expand_string_message); + return search_find_defer? DEFER : ERROR; + } + } + else arg = cb->arg; + + /* Show condition, and expanded condition if it's different */ + + HDEBUG(D_acl) + { + int lhswidth = 0; + debug_printf("check %s%s %n", + (!cond_modifiers[cb->type] && cb->u.negated)? "!":"", + conditions[cb->type], &lhswidth); + + if (cb->type == ACLC_SET) + { + int n = cb->u.varnumber; + int t = (n < ACL_C_MAX)? 'c' : 'm'; + if (n >= ACL_C_MAX) n -= ACL_C_MAX; + debug_printf("acl_%c%d ", t, n); + lhswidth += 7; + } + + debug_printf("= %s\n", cb->arg); + + if (arg != cb->arg) + debug_printf("%.*s= %s\n", lhswidth, + US" ", CS arg); + } + + /* Check that this condition makes sense at this time */ + + if ((cond_forbids[cb->type] & (1 << where)) != 0) + { + *log_msgptr = string_sprintf("cannot %s %s condition in %s ACL", + cond_modifiers[cb->type]? "use" : "test", + conditions[cb->type], acl_wherenames[where]); + return ERROR; + } + + /* Run the appropriate test for each condition, or take the appropriate + action for the remaining modifiers. */ + + switch(cb->type) + { + /* A nested ACL that returns "discard" makes sense only for an "accept" or + "discard" verb. */ + + case ACLC_ACL: + rc = acl_check_internal(where, addr, arg, level+1, user_msgptr, log_msgptr); + if (rc == DISCARD && verb != ACL_ACCEPT && verb != ACL_DISCARD) + { + *log_msgptr = string_sprintf("nested ACL returned \"discard\" for " + "\"%s\" command (only allowed with \"accept\" or \"discard\")", + verbs[verb]); + return ERROR; + } + break; + + case ACLC_AUTHENTICATED: + rc = (sender_host_authenticated == NULL)? FAIL : + match_isinlist(sender_host_authenticated, &arg, 0, NULL, NULL, MCL_STRING, + TRUE, NULL); + break; + + case ACLC_CONDITION: + if (Ustrspn(arg, "0123456789") == Ustrlen(arg)) /* Digits, or empty */ + rc = (Uatoi(arg) == 0)? FAIL : OK; + else + rc = (strcmpic(arg, US"no") == 0 || + strcmpic(arg, US"false") == 0)? FAIL : + (strcmpic(arg, US"yes") == 0 || + strcmpic(arg, US"true") == 0)? OK : DEFER; + if (rc == DEFER) + *log_msgptr = string_sprintf("invalid \"condition\" value \"%s\"", arg); + break; + + case ACLC_CONTROL: + switch (decode_control(arg, &p, where, log_msgptr)) + { + case CONTROL_ERROR: + return ERROR; + + case CONTROL_CASEFUL_LOCAL_PART: + deliver_localpart = addr->cc_local_part; + break; + + case CONTROL_CASELOWER_LOCAL_PART: + deliver_localpart = addr->lc_local_part; + break; + + case CONTROL_ENFORCE_SYNC: + smtp_enforce_sync = TRUE; + break; + + case CONTROL_NO_ENFORCE_SYNC: + smtp_enforce_sync = FALSE; + break; + + case CONTROL_NO_MULTILINE: + no_multiline_responses = TRUE; + break; + + case CONTROL_FREEZE: + deliver_freeze = TRUE; + deliver_frozen_at = time(NULL); + break; + + case CONTROL_QUEUE_ONLY: + queue_only_policy = TRUE; + break; + + case CONTROL_SUBMISSION: + submission_mode = TRUE; + if (Ustrncmp(p, "/domain=", 8) == 0) + { + submission_domain = string_copy(p+8); + } + else if (*p != 0) + { + *log_msgptr = string_sprintf("syntax error in argument for " + "\"control\" modifier \"%s\"", arg); + return ERROR; + } + break; + } + break; + + case ACLC_DELAY: + { + int delay = readconf_readtime(arg, 0, FALSE); + if (delay < 0) + { + *log_msgptr = string_sprintf("syntax error in argument for \"delay\" " + "modifier: \"%s\" is not a time value", arg); + return ERROR; + } + else + { + HDEBUG(D_acl) debug_printf("delay modifier requests %d-second delay\n", + delay); + if (host_checking) + { + HDEBUG(D_acl) + debug_printf("delay skipped in -bh checking mode\n"); + } + else sleep(delay); + } + } + break; + + case ACLC_DNSLISTS: + rc = verify_check_dnsbl(&arg); + break; + + case ACLC_DOMAINS: + rc = match_isinlist(addr->domain, &arg, 0, &domainlist_anchor, + addr->domain_cache, MCL_DOMAIN, TRUE, &deliver_domain_data); + break; + + /* The value in tls_cipher is the full cipher name, for example, + TLSv1:DES-CBC3-SHA:168, whereas the values to test for are just the + cipher names such as DES-CBC3-SHA. But program defensively. We don't know + what may in practice come out of the SSL library - which at the time of + writing is poorly documented. */ + + case ACLC_ENCRYPTED: + if (tls_cipher == NULL) rc = FAIL; else + { + uschar *endcipher = NULL; + uschar *cipher = Ustrchr(tls_cipher, ':'); + if (cipher == NULL) cipher = tls_cipher; else + { + endcipher = Ustrchr(++cipher, ':'); + if (endcipher != NULL) *endcipher = 0; + } + rc = match_isinlist(cipher, &arg, 0, NULL, NULL, MCL_STRING, TRUE, NULL); + if (endcipher != NULL) *endcipher = ':'; + } + break; + + /* Use verify_check_this_host() instead of verify_check_host() so that + we can pass over &host_data to catch any looked up data. Once it has been + set, it retains its value so that it's still there if another ACL verb + comes through here and uses the cache. However, we must put it into + permanent store in case it is also expected to be used in a subsequent + message in the same SMTP connection. */ + + case ACLC_HOSTS: + rc = verify_check_this_host(&arg, sender_host_cache, NULL, + (sender_host_address == NULL)? US"" : sender_host_address, &host_data); + if (host_data != NULL) host_data = string_copy_malloc(host_data); + break; + + case ACLC_LOCAL_PARTS: + rc = match_isinlist(addr->cc_local_part, &arg, 0, + &localpartlist_anchor, addr->localpart_cache, MCL_LOCALPART, TRUE, + &deliver_localpart_data); + break; + + case ACLC_LOGWRITE: + { + int logbits = 0; + uschar *s = arg; + if (*s == ':') + { + s++; + while (*s != ':') + { + if (Ustrncmp(s, "main", 4) == 0) + { logbits |= LOG_MAIN; s += 4; } + else if (Ustrncmp(s, "panic", 5) == 0) + { logbits |= LOG_PANIC; s += 5; } + else if (Ustrncmp(s, "reject", 6) == 0) + { logbits |= LOG_REJECT; s += 6; } + else + { + logbits = LOG_MAIN|LOG_PANIC; + s = string_sprintf(":unknown log name in \"%s\" in " + "\"logwrite\" in %s ACL", arg, acl_wherenames[where]); + } + if (*s == ',') s++; + } + s++; + } + while (isspace(*s)) s++; + if (logbits == 0) logbits = LOG_MAIN; + log_write(0, logbits, "%s", string_printing(s)); + } + break; + + case ACLC_RECIPIENTS: + rc = match_address_list(addr->address, TRUE, TRUE, &arg, NULL, -1, 0, + &recipient_data); + break; + + case ACLC_SENDER_DOMAINS: + { + uschar *sdomain; + sdomain = Ustrrchr(sender_address, '@'); + sdomain = (sdomain == NULL)? US"" : sdomain + 1; + rc = match_isinlist(sdomain, &arg, 0, &domainlist_anchor, + sender_domain_cache, MCL_DOMAIN, TRUE, NULL); + } + break; + + case ACLC_SENDERS: + rc = match_address_list(sender_address, TRUE, TRUE, &arg, + sender_address_cache, -1, 0, &sender_data); + break; + + /* Connection variables must persist forever */ + + case ACLC_SET: + { + int old_pool = store_pool; + if (cb->u.varnumber < ACL_C_MAX) store_pool = POOL_PERM; + acl_var[cb->u.varnumber] = string_copy(arg); + store_pool = old_pool; + } + break; + + /* If the verb is WARN, discard any user message from verification, because + such messages are SMTP responses, not header additions. The latter come + only from explicit "message" modifiers. */ + + case ACLC_VERIFY: + rc = acl_verify(where, addr, arg, user_msgptr, log_msgptr, basic_errno); + if (verb == ACL_WARN) *user_msgptr = NULL; + break; + + default: + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "internal ACL error: unknown " + "condition %d", cb->type); + break; + } + + /* If a condition was negated, invert OK/FAIL. */ + + if (!cond_modifiers[cb->type] && cb->u.negated) + { + if (rc == OK) rc = FAIL; + else if (rc == FAIL || rc == FAIL_DROP) rc = OK; + } + + if (rc != OK) break; /* Conditions loop */ + } + + +/* If the result is the one for which "message" and/or "log_message" are used, +handle the values of these options. Most verbs have but a single return for +which the messages are relevant, but for "discard", it's useful to have the log +message both when it succeeds and when it fails. Also, for an "accept" that +appears in a QUIT ACL, we want to handle the user message. Since only "accept" +and "warn" are permitted in that ACL, we don't need to test the verb. + +These modifiers act in different ways: + +"message" is a user message that will be included in an SMTP response. Unless +it is empty, it overrides any previously set user message. + +"log_message" is a non-user message, and it adds to any existing non-user +message that is already set. + +If there isn't a log message set, we make it the same as the user message. */ + +if (((rc == FAIL_DROP)? FAIL : rc) == msgcond[verb] || + (verb == ACL_DISCARD && rc == OK) || + (where == ACL_WHERE_QUIT)) + { + uschar *expmessage; + + /* If the verb is "warn", messages generated by conditions (verification or + nested ACLs) are discarded. Only messages specified at this level are used. + However, the value of an existing message is available in $acl_verify_message + during expansions. */ + + uschar *old_user_msgptr = *user_msgptr; + uschar *old_log_msgptr = (*log_msgptr != NULL)? *log_msgptr : old_user_msgptr; + + if (verb == ACL_WARN) *log_msgptr = *user_msgptr = NULL; + + if (user_message != NULL) + { + acl_verify_message = old_user_msgptr; + expmessage = expand_string(user_message); + if (expmessage == NULL) + { + if (!expand_string_forcedfail) + log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand ACL message \"%s\": %s", + user_message, expand_string_message); + } + else if (expmessage[0] != 0) *user_msgptr = expmessage; + } + + if (log_message != NULL) + { + acl_verify_message = old_log_msgptr; + expmessage = expand_string(log_message); + if (expmessage == NULL) + { + if (!expand_string_forcedfail) + log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand ACL message \"%s\": %s", + log_message, expand_string_message); + } + else if (expmessage[0] != 0) + { + *log_msgptr = (*log_msgptr == NULL)? expmessage : + string_sprintf("%s: %s", expmessage, *log_msgptr); + } + } + + /* If no log message, default it to the user message */ + + if (*log_msgptr == NULL) *log_msgptr = *user_msgptr; + } + +acl_verify_message = NULL; +return rc; +} + + + + + +/************************************************* +* Get line from a literal ACL * +*************************************************/ + +/* This function is passed to acl_read() in order to extract individual lines +of a literal ACL, which we access via static pointers. We can destroy the +contents because this is called only once (the compiled ACL is remembered). + +This code is intended to treat the data in the same way as lines in the main +Exim configuration file. That is: + + . Leading spaces are ignored. + + . A \ at the end of a line is a continuation - trailing spaces after the \ + are permitted (this is because I don't believe in making invisible things + significant). Leading spaces on the continued part of a line are ignored. + + . Physical lines starting (significantly) with # are totally ignored, and + may appear within a sequence of backslash-continued lines. + + . Blank lines are ignored, but will end a sequence of continuations. + +Arguments: none +Returns: a pointer to the next line +*/ + + +static uschar *acl_text; /* Current pointer in the text */ +static uschar *acl_text_end; /* Points one past the terminating '0' */ + + +static uschar * +acl_getline(void) +{ +uschar *yield; + +/* This loop handles leading blank lines and comments. */ + +for(;;) + { + while (isspace(*acl_text)) acl_text++; /* Leading spaces/empty lines */ + if (*acl_text == 0) return NULL; /* No more data */ + yield = acl_text; /* Potential data line */ + + while (*acl_text != 0 && *acl_text != '\n') acl_text++; + + /* If we hit the end before a newline, we have the whole logical line. If + it's a comment, there's no more data to be given. Otherwise, yield it. */ + + if (*acl_text == 0) return (*yield == '#')? NULL : yield; + + /* After reaching a newline, end this loop if the physical line does not + start with '#'. If it does, it's a comment, and the loop continues. */ + + if (*yield != '#') break; + } + +/* This loop handles continuations. We know we have some real data, ending in +newline. See if there is a continuation marker at the end (ignoring trailing +white space). We know that *yield is not white space, so no need to test for +cont > yield in the backwards scanning loop. */ + +for(;;) + { + uschar *cont; + for (cont = acl_text - 1; isspace(*cont); cont--); + + /* If no continuation follows, we are done. Mark the end of the line and + return it. */ + + if (*cont != '\\') + { + *acl_text++ = 0; + return yield; + } + + /* We have encountered a continuation. Skip over whitespace at the start of + the next line, and indeed the whole of the next line or lines if they are + comment lines. */ + + for (;;) + { + while (*(++acl_text) == ' ' || *acl_text == '\t'); + if (*acl_text != '#') break; + while (*(++acl_text) != 0 && *acl_text != '\n'); + } + + /* We have the start of a continuation line. Move all the rest of the data + to join onto the previous line, and then find its end. If the end is not a + newline, we are done. Otherwise loop to look for another continuation. */ + + memmove(cont, acl_text, acl_text_end - acl_text); + acl_text_end -= acl_text - cont; + acl_text = cont; + while (*acl_text != 0 && *acl_text != '\n') acl_text++; + if (*acl_text == 0) return yield; + } + +/* Control does not reach here */ +} + + + + + +/************************************************* +* Check access using an ACL * +*************************************************/ + +/* This function is called from address_check. It may recurse via +acl_check_condition() - hence the use of a level to stop looping. The ACL is +passed as a string which is expanded. A forced failure implies no access check +is required. If the result is a single word, it is taken as the name of an ACL +which is sought in the global ACL tree. Otherwise, it is taken as literal ACL +text, complete with newlines, and parsed as such. In both cases, the ACL check +is then run. This function uses an auxiliary function for acl_read() to call +for reading individual lines of a literal ACL. This is acl_getline(), which +appears immediately above. + +Arguments: + where where called from + addr address item when called from RCPT; otherwise NULL + s the input string; NULL is the same as an empty ACL => DENY + level the nesting level + user_msgptr where to put a user error (for SMTP response) + log_msgptr where to put a logging message (not for SMTP response) + +Returns: OK access is granted + DISCARD access is apparently granted... + FAIL access is denied + FAIL_DROP access is denied; drop the connection + DEFER can't tell at the moment + ERROR disaster +*/ + +static int +acl_check_internal(int where, address_item *addr, uschar *s, int level, + uschar **user_msgptr, uschar **log_msgptr) +{ +int fd = -1; +acl_block *acl = NULL; +uschar *acl_name = US"inline ACL"; +uschar *ss; + +/* Catch configuration loops */ + +if (level > 20) + { + *log_msgptr = US"ACL nested too deep: possible loop"; + return ERROR; + } + +if (s == NULL) + { + HDEBUG(D_acl) debug_printf("ACL is NULL: implicit DENY\n"); + return FAIL; + } + +/* At top level, we expand the incoming string. At lower levels, it has already +been expanded as part of condition processing. */ + +if (level == 0) + { + ss = expand_string(s); + if (ss == NULL) + { + if (expand_string_forcedfail) return OK; + *log_msgptr = string_sprintf("failed to expand ACL string \"%s\": %s", s, + expand_string_message); + return ERROR; + } + } +else ss = s; + +while (isspace(*ss))ss++; + +/* If we can't find a named ACL, the default is to parse it as an inline one. +(Unless it begins with a slash; non-existent files give rise to an error.) */ + +acl_text = ss; + +/* Handle the case of a string that does not contain any spaces. Look for a +named ACL among those read from the configuration, or a previously read file. +It is possible that the pointer to the ACL is NULL if the configuration +contains a name with no data. If not found, and the text begins with '/', +read an ACL from a file, and save it so it can be re-used. */ + +if (Ustrchr(ss, ' ') == NULL) + { + tree_node *t = tree_search(acl_anchor, ss); + if (t != NULL) + { + acl = (acl_block *)(t->data.ptr); + if (acl == NULL) + { + HDEBUG(D_acl) debug_printf("ACL \"%s\" is empty: implicit DENY\n", ss); + return FAIL; + } + acl_name = string_sprintf("ACL \"%s\"", ss); + HDEBUG(D_acl) debug_printf("using ACL \"%s\"\n", ss); + } + + else if (*ss == '/') + { + struct stat statbuf; + fd = Uopen(ss, O_RDONLY, 0); + if (fd < 0) + { + *log_msgptr = string_sprintf("failed to open ACL file \"%s\": %s", ss, + strerror(errno)); + return ERROR; + } + + if (fstat(fd, &statbuf) != 0) + { + *log_msgptr = string_sprintf("failed to fstat ACL file \"%s\": %s", ss, + strerror(errno)); + return ERROR; + } + + acl_text = store_get(statbuf.st_size + 1); + acl_text_end = acl_text + statbuf.st_size + 1; + + if (read(fd, acl_text, statbuf.st_size) != statbuf.st_size) + { + *log_msgptr = string_sprintf("failed to read ACL file \"%s\": %s", + ss, strerror(errno)); + return ERROR; + } + acl_text[statbuf.st_size] = 0; + close(fd); + + acl_name = string_sprintf("ACL \"%s\"", ss); + HDEBUG(D_acl) debug_printf("read ACL from file %s\n", ss); + } + } + +/* Parse an ACL that is still in text form. If it came from a file, remember it +in the ACL tree, having read it into the POOL_PERM store pool so that it +persists between multiple messages. */ + +if (acl == NULL) + { + int old_pool = store_pool; + if (fd >= 0) store_pool = POOL_PERM; + acl = acl_read(acl_getline, log_msgptr); + store_pool = old_pool; + if (acl == NULL && *log_msgptr != NULL) return ERROR; + if (fd >= 0) + { + tree_node *t = store_get_perm(sizeof(tree_node) + Ustrlen(ss)); + Ustrcpy(t->name, ss); + t->data.ptr = acl; + (void)tree_insertnode(&acl_anchor, t); + } + } + +/* Now we have an ACL to use. It's possible it may be NULL. */ + +while (acl != NULL) + { + int cond; + int basic_errno = 0; + BOOL endpass_seen = FALSE; + + *log_msgptr = *user_msgptr = NULL; + acl_temp_details = FALSE; + + if (where == ACL_WHERE_QUIT && + acl->verb != ACL_ACCEPT && + acl->verb != ACL_WARN) + { + *log_msgptr = string_sprintf("\"%s\" is not allowed in a QUIT ACL", + verbs[acl->verb]); + return ERROR; + } + + HDEBUG(D_acl) debug_printf("processing \"%s\"\n", verbs[acl->verb]); + + /* Clear out any search error message from a previous check before testing + this condition. */ + + search_error_message = NULL; + cond = acl_check_condition(acl->verb, acl->condition, where, addr, level, + &endpass_seen, user_msgptr, log_msgptr, &basic_errno); + + /* Handle special returns: DEFER causes a return except on a WARN verb; + ERROR always causes a return. */ + + switch (cond) + { + case DEFER: + HDEBUG(D_acl) debug_printf("%s: condition test deferred\n", verbs[acl->verb]); + if (basic_errno != ERRNO_CALLOUTDEFER) + { + if (search_error_message != NULL && *search_error_message != 0) + *log_msgptr = search_error_message; + if (smtp_return_error_details) acl_temp_details = TRUE; + } + else + { + acl_temp_details = TRUE; + } + if (acl->verb != ACL_WARN) return DEFER; + break; + + default: /* Paranoia */ + case ERROR: + HDEBUG(D_acl) debug_printf("%s: condition test error\n", verbs[acl->verb]); + return ERROR; + + case OK: + HDEBUG(D_acl) debug_printf("%s: condition test succeeded\n", + verbs[acl->verb]); + break; + + case FAIL: + HDEBUG(D_acl) debug_printf("%s: condition test failed\n", verbs[acl->verb]); + break; + + /* DISCARD and DROP can happen only from a nested ACL condition, and + DISCARD can happen only for an "accept" or "discard" verb. */ + + case DISCARD: + HDEBUG(D_acl) debug_printf("%s: condition test yielded \"discard\"\n", + verbs[acl->verb]); + break; + + case FAIL_DROP: + HDEBUG(D_acl) debug_printf("%s: condition test yielded \"drop\"\n", + verbs[acl->verb]); + break; + } + + /* At this point, cond for most verbs is either OK or FAIL or (as a result of + a nested ACL condition) FAIL_DROP. However, for WARN, cond may be DEFER, and + for ACCEPT and DISCARD, it may be DISCARD after a nested ACL call. */ + + switch(acl->verb) + { + case ACL_ACCEPT: + if (cond == OK || cond == DISCARD) return cond; + if (endpass_seen) + { + HDEBUG(D_acl) debug_printf("accept: endpass encountered - denying access\n"); + return cond; + } + break; + + case ACL_DEFER: + if (cond == OK) + { + acl_temp_details = TRUE; + return DEFER; + } + break; + + case ACL_DENY: + if (cond == OK) return FAIL; + break; + + case ACL_DISCARD: + if (cond == OK || cond == DISCARD) return DISCARD; + if (endpass_seen) + { + HDEBUG(D_acl) debug_printf("discard: endpass encountered - denying access\n"); + return cond; + } + break; + + case ACL_DROP: + if (cond == OK) return FAIL_DROP; + break; + + case ACL_REQUIRE: + if (cond != OK) return cond; + break; + + case ACL_WARN: + if (cond == OK) + acl_warn(where, *user_msgptr, *log_msgptr); + else if (cond == DEFER) + acl_warn(where, NULL, string_sprintf("ACL \"warn\" statement skipped: " + "condition test deferred: %s", + (*log_msgptr == NULL)? US"" : *log_msgptr)); + *log_msgptr = *user_msgptr = NULL; /* In case implicit DENY follows */ + break; + + default: + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "internal ACL error: unknown verb %d", + acl->verb); + break; + } + + /* Pass to the next ACL item */ + + acl = acl->next; + } + +/* We have reached the end of the ACL. This is an implicit DENY. */ + +HDEBUG(D_acl) debug_printf("end of %s: implicit DENY\n", acl_name); +return FAIL; +} + + +/************************************************* +* Check access using an ACL * +*************************************************/ + +/* This is the external interface for ACL checks. It sets up an address and the +expansions for $domain and $local_part when called after RCPT, then calls +acl_check_internal() to do the actual work. + +Arguments: + where ACL_WHERE_xxxx indicating where called from + data_string RCPT address, or SMTP command argument, or NULL + s the input string; NULL is the same as an empty ACL => DENY + user_msgptr where to put a user error (for SMTP response) + log_msgptr where to put a logging message (not for SMTP response) + +Returns: OK access is granted by an ACCEPT verb + DISCARD access is granted by a DISCARD verb + FAIL access is denied + FAIL_DROP access is denied; drop the connection + DEFER can't tell at the moment + ERROR disaster +*/ + +int +acl_check(int where, uschar *data_string, uschar *s, uschar **user_msgptr, + uschar **log_msgptr) +{ +int rc; +address_item adb; +address_item *addr; + +*user_msgptr = *log_msgptr = NULL; +sender_verified_failed = NULL; + +if (where == ACL_WHERE_RCPT) + { + adb = address_defaults; + addr = &adb; + addr->address = data_string; + if (deliver_split_address(addr) == DEFER) + { + *log_msgptr = US"defer in percent_hack_domains check"; + return DEFER; + } + deliver_domain = addr->domain; + deliver_localpart = addr->local_part; + } +else + { + addr = NULL; + smtp_command_argument = data_string; + } + +rc = acl_check_internal(where, addr, s, 0, user_msgptr, log_msgptr); + +smtp_command_argument = deliver_domain = + deliver_localpart = deliver_address_data = NULL; + +/* A DISCARD response is permitted only for message ACLs, excluding the PREDATA +ACL, which is really in the middle of an SMTP command. */ + +if (rc == DISCARD) + { + if (where > ACL_WHERE_NOTSMTP || where == ACL_WHERE_PREDATA) + { + log_write(0, LOG_MAIN|LOG_PANIC, "\"discard\" verb not allowed in %s " + "ACL", acl_wherenames[where]); + return ERROR; + } + return DISCARD; + } + +/* A DROP response is not permitted from MAILAUTH */ + +if (rc == FAIL_DROP && where == ACL_WHERE_MAILAUTH) + { + log_write(0, LOG_MAIN|LOG_PANIC, "\"drop\" verb not allowed in %s " + "ACL", acl_wherenames[where]); + return ERROR; + } + +/* Before giving an error response, take a look at the length of any user +message, and split it up into multiple lines if possible. */ + +if (rc != OK && *user_msgptr != NULL && Ustrlen(*user_msgptr) > 75) + { + uschar *s = *user_msgptr = string_copy(*user_msgptr); + uschar *ss = s; + + for (;;) + { + int i = 0; + while (i < 75 && *ss != 0 && *ss != '\n') ss++, i++; + if (*ss == 0) break; + if (*ss == '\n') + s = ++ss; + else + { + uschar *t = ss + 1; + uschar *tt = NULL; + while (--t > s + 35) + { + if (*t == ' ') + { + if (t[-1] == ':') { tt = t; break; } + if (tt == NULL) tt = t; + } + } + + if (tt == NULL) /* Can't split behind - try ahead */ + { + t = ss + 1; + while (*t != 0) + { + if (*t == ' ' || *t == '\n') + { tt = t; break; } + t++; + } + } + + if (tt == NULL) break; /* Can't find anywhere to split */ + *tt = '\n'; + s = ss = tt+1; + } + } + } + +return rc; +} + +/* End of acl.c */ diff --git a/src/src/aliases.default b/src/src/aliases.default new file mode 100644 index 000000000..9d2c5058c --- /dev/null +++ b/src/src/aliases.default @@ -0,0 +1,42 @@ +# $Cambridge: exim/src/src/aliases.default,v 1.1 2004/10/07 10:39:01 ph10 Exp $ + +# Default aliases file, installed by Exim. This file contains no real aliases. +# You should edit it to taste. + + +# The following alias is required by the mail RFCs 2821 and 2822. +# Set it to the address of a HUMAN who deals with this system's mail problems. + +# postmaster: someone@your.domain + +# It is also common to set the following alias so that if anybody replies to a +# bounce message from this host, the reply goes to the postmaster. + +# mailer-daemon: postmaster + + +# You should also set up an alias for messages to root, because it is not +# usually a good idea to deliver mail as root. + +# root: postmaster + +# It is a good idea to redirect any messages sent to system accounts so that +# they don't just get ignored. Here are some common examples: + +# bin: root +# daemon: root +# ftp: root +# nobody: root +# operator: root +# uucp: root + +# You should check your /etc/passwd for any others. + + +# Other commonly enountered aliases are: +# +# abuse: the person dealing with network and mail abuse +# hostmaster: the person dealing with DNS problems +# webmaster: the person dealing with your web site + +#### diff --git a/src/src/buildconfig.c b/src/src/buildconfig.c new file mode 100644 index 000000000..6dd0d5112 --- /dev/null +++ b/src/src/buildconfig.c @@ -0,0 +1,748 @@ +/* $Cambridge: exim/src/src/buildconfig.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + + +/************************************************* +* Build configuration header for Exim * +*************************************************/ + +/* This auxiliary program builds the file config.h by the following +process: + +First it reads Makefile, looking for certain OS-specific definitions which it +uses to define macros. Then it reads the defaults file config.h.defaults. + +The defaults file contains normal C #define statements for various macros; if +the name of a macro is found in the environment, the environment value replaces +the default. If the default #define does not contain any value, then that macro +is not copied to the created file unless there is some value in the +environment. + +This program is compiled and run as part of the Make process and is not +normally called independently. */ + + +#include +#include +#include +#include +#include +#include +#include + +typedef struct { + char *name; + int *flag; +} have_item; + +typedef struct { + char *name; + char *data; +} save_item; + +static char *db_opts[] = { "", "USE_DB", "USE_GDBM", "USE_TDB" }; + +static int have_ipv6 = 0; +static int have_iconv = 0; + +static char errno_quota[256]; +static char ostype[256]; +static char cc[256]; + +/* If any entry is an initial substring of another, the longer one must +appear first. */ + +static have_item have_list[] = { + { "HAVE_IPV6", &have_ipv6 }, + { "HAVE_ICONV", &have_iconv }, + { NULL, NULL} +}; + +static save_item save_list[] = { + { "ERRNO_QUOTA", errno_quota }, + { "OSTYPE", ostype }, + { "CC", cc }, + { NULL, NULL} +}; + + +/* Subroutine to check a string for precisely one instance of "%s". If not, +bomb out. */ + +void +check_percent_ess(char *value, char *name) +{ +int OK = 0; +char *p = strstr(value, "%s"); +if (p != NULL) OK = strstr(p+2, "%s") == NULL; +if (!OK) + { + printf("\n*** \"%s\" (%s) must contain precisely one occurrence of\n" + "*** \"%%s\". Please review your build-time configuration.\n\n/", value, + name); + exit(1); + } +} + + +/* Main program */ + +int +main(int argc, char **argv) +{ +FILE *base; +FILE *new; +int last_initial = 'A'; +int linecount = 0; +int have_auth = 0; +int in_local_makefile = 0; +int use_which_db = 0; +int use_which_db_in_local_makefile = 0; +int support_crypteq = 0; +char buffer[1024]; + +if (argc != 1) + { + printf("*** Buildconfig: called with incorrect arguments\n"); + exit(1); + } + +new = fopen("config.h", "wb"); +if (new == NULL) + { + printf("*** Buildconfig: failed to open config.h for output\n"); + exit(1); + } + +printf("Building configuration file config.h\n"); + +fprintf(new, "/*************************************************\n"); +fprintf(new, "* Configuration header for Exim *\n"); +fprintf(new, "*************************************************/\n\n"); + +fprintf(new, "/* This file was automatically generated from Makefile and " + "config.h.defaults,\n"); +fprintf(new, "using values specified in the configuration file Local/Makefile.\n"); +fprintf(new, "Do not edit it. Instead, edit Local/Makefile and " + "rerun make. */\n\n"); + +/* First, search the makefile for certain settings */ + +base = fopen("Makefile", "rb"); +if (base == NULL) + { + printf("*** Buildconfig: failed to open Makefile\n"); + fclose(new); + exit(1); + } + +errno_quota[0] = 0; /* no over-riding value set */ +ostype[0] = 0; /* just in case */ +cc[0] = 0; + +while (fgets(buffer, sizeof(buffer), base) != NULL) + { + int i; + have_item *h; + save_item *s; + char *p = buffer + (int)strlen(buffer); + linecount++; + while (p > buffer && isspace((unsigned char)p[-1])) p--; + *p = 0; + p = buffer; + while (isspace((unsigned char)*p)) p++; + + /* Notice when we hit the user's makefile */ + + if (strcmp(p, "# From Local/Makefile") == 0) + { + in_local_makefile = 1; + continue; + } + + /* Remember the last DB option setting. If we hit two in the user's + Makefile, complain. */ + + for (i = 1; i < sizeof(db_opts)/sizeof(char *); i++) + { + int len = (int)strlen(db_opts[i]); + if (strncmp(p, db_opts[i], len) == 0 && (p[len] == ' ' || p[len] == '=')) + { + if (in_local_makefile) + { + if (use_which_db_in_local_makefile) + { + printf("*** Only one of USE_DB, USE_GDBM, or USE_TDB should be " + "defined in Local/Makefile\n"); + exit(1); + } + use_which_db_in_local_makefile = 1; + } + use_which_db = i; + break; + } + } + if (i < sizeof(db_opts)/sizeof(char *)) continue; + + /* Items where we just save a boolean */ + + for (h = have_list; h->name != NULL; h++) + { + int len = (int)strlen(h->name); + if (strncmp(p, h->name, len) == 0) + { + p += len; + while (isspace((unsigned char)*p)) p++; + if (*p++ != '=') + { + printf("*** Buildconfig: syntax error in Makefile line %d\n", linecount); + exit(1); + } + while (isspace((unsigned char)*p)) p++; + if (strcmp(p, "YES") == 0 || strcmp(p, "yes") == 0) *(h->flag) = 1; + else *(h->flag) = 0; /* Must reset in case multiple instances */ + break; + } + } + + if (h->name != NULL) continue; + + /* Items where we save the complete string */ + + for (s = save_list; s->name != NULL; s++) + { + int len = (int)strlen(s->name); + if (strncmp(p, s->name, len) == 0) + { + p += len; + while (isspace((unsigned char)*p)) p++; + if (*p++ != '=') + { + printf("*** Buildconfig: syntax error in Makefile line %d\n", linecount); + exit(1); + } + while (isspace((unsigned char)*p)) p++; + strcpy(s->data, p); + } + } + } + +fprintf(new, "#define HAVE_IPV6 %s\n", + have_ipv6? "TRUE" : "FALSE"); + +fprintf(new, "#define HAVE_ICONV %s\n", + have_iconv? "TRUE" : "FALSE"); + +if (errno_quota[0] != 0) + fprintf(new, "\n#define ERRNO_QUOTA %s\n", errno_quota); + +if (strcmp(cc, "gcc") == 0 && strstr(ostype, "IRIX") != NULL) + { + fprintf(new, "\n/* This switch includes the code to fix the inet_ntoa() */"); + fprintf(new, "\n/* bug when using gcc on an IRIX system. */"); + fprintf(new, "\n#define USE_INET_NTOA_FIX"); + } + +fprintf(new, "\n"); +fclose(base); + + +/* Now handle the macros listed in the defaults */ + +base = fopen("../src/config.h.defaults", "rb"); +if (base == NULL) + { + printf("*** Buildconfig: failed to open ../src/config.h.defaults\n"); + fclose(new); + exit(1); + } + +while (fgets(buffer, sizeof(buffer), base) != NULL) + { + int i; + char name[256]; + char *value; + char *p = buffer; + char *q = name; + + while (*p == ' ' || *p == '\t') p++; + + if (strncmp(p, "#define ", 8) != 0) continue; + + p += 8; + while (*p == ' ' || *p == '\t') p++; + + if (*p < last_initial) fprintf(new, "\n"); + last_initial = *p; + + while (*p && (isalnum((unsigned char)*p) || *p == '_')) *q++ = *p++; + *q = 0; + + /* USE_DB, USE_GDBM, and USE_TDB are special cases. We want to have only + one of them set. The scan of the Makefile has saved which was the last one + encountered. */ + + for (i = 1; i < sizeof(db_opts)/sizeof(char *); i++) + { + if (strcmp(name, db_opts[i]) == 0) + { + if (use_which_db == i) + fprintf(new, "#define %s %.*syes\n", db_opts[i], + 21 - (int)strlen(db_opts[i]), " "); + else + fprintf(new, "/* %s not set */\n", name); + break; + } + } + if (i < sizeof(db_opts)/sizeof(char *)) continue; + + /* EXIM_USER is a special case. We look in the environment for EXIM_USER or + EXIM_UID (the latter for backward compatibility with Exim 3). If the value is + not numeric, we look up the user, and default the GID if found. Otherwise, + EXIM_GROUP or EXIM_GID must be in the environment. */ + + if (strcmp(name, "EXIM_UID") == 0) + { + uid_t uid = 0; + gid_t gid = 0; + int gid_set = 0; + char *username = NULL; + char *groupname = NULL; + char *s; + char *user = getenv("EXIM_USER"); + char *group = getenv("EXIM_GROUP"); + + if (user == NULL) user = getenv("EXIM_UID"); + if (group == NULL) group = getenv("EXIM_GID"); + + if (user == NULL) + { + printf("\n*** EXIM_USER has not been defined in any of the Makefiles in " + "the\n \"Local\" directory. Please review your build-time " + "configuration.\n\n"); + return 1; + } + + while (isspace((unsigned char)(*user))) user++; + if (*user == 0) + { + printf("\n*** EXIM_USER is defined as an empty string in one of the " + "files\n in the \"Local\" directory. Please review your build-time" + "\n configuration.\n\n"); + return 1; + } + + for (s = user; *s != 0; s++) + { + if (iscntrl((unsigned char)(*s))) + { + printf("\n*** EXIM_USER contains the control character 0x%02X in one " + "of the files\n in the \"Local\" directory. Please review your " + "build-time\n configuration.\n\n", *s); + return 1; + } + } + + /* Numeric uid given */ + + if (user[strspn(user, "0123456789")] == 0) + { + uid = (uid_t)atoi(user); + } + + /* User name given. Normally, we look up the uid right away. However, + people building binary distributions sometimes want to retain the name till + runtime. This is supported if the name begins "ref:". */ + + else if (strncmp(user, "ref:", 4) == 0) + { + user += 4; + while (isspace(*user)) user++; + username = user; + gid_set = 1; + } + + else + { + struct passwd *pw = getpwnam(user); + if (pw == NULL) + { + printf("\n*** User \"%s\" (specified in one of the Makefiles) does not " + "exist.\n Please review your build-time configuration.\n\n", + user); + return 1; + } + + uid = pw->pw_uid; + gid = pw->pw_gid; + gid_set = 1; + } + + /* Use explicit group if set. */ + + if (group != NULL) + { + while (isspace((unsigned char)(*group))) group++; + if (*group == 0) + { + printf("\n*** EXIM_GROUP is defined as an empty string in one of " + "the files in the\n \"Local\" directory. "); + if (gid_set) + { + printf("If you want the Exim group to be taken from the\n " + "password data for the Exim user, just remove the EXIM_GROUP " + "setting.\n Otherwise, p"); + } + else printf("EXIM_USER is defined numerically, so there is no" + "\n default for EXIM_GROUP and you must set it explicitly.\n P"); + printf("lease review your build-time configuration.\n\n"); + return 1; + } + + for (s = group; *s != 0; s++) + { + if (iscntrl((unsigned char)(*s))) + { + printf("\n*** EXIM_GROUP contains the control character 0x%02X in one " + "of the files\n in the \"Local\" directory. Please review your " + "build-time\n configuration.\n\n", *s); + return 1; + } + } + + /* Group name given. This may be by reference or to be looked up now, + as for user. */ + + if (strncmp(group, "ref:", 4) == 0) + { + group += 4; + while (isspace(*group)) group++; + groupname = group; + } + + else if (username != NULL) + { + groupname = group; + } + + else if (group[strspn(group, "0123456789")] == 0) + { + gid = (gid_t)atoi(group); + } + + else + { + struct group *gr = getgrnam(group); + if (gr == NULL) + { + printf("\n*** Group \"%s\" (specified in one of the Makefiles) does " + "not exist.\n Please review your build-time configuration.\n\n", + group); + return 1; + } + gid = gr->gr_gid; + } + } + + /* Else trouble unless found in passwd file with user */ + + else if (!gid_set) + { + printf("\n*** No group set for Exim. Please review your build-time " + "configuration.\n\n"); + return 1; + } + + /* Output user and group names or uid/gid. When names are set, uid/gid + are set to zero but will be replaced at runtime. */ + + if (username != NULL) + fprintf(new, "#define EXIM_USERNAME \"%s\"\n", username); + if (groupname != NULL) + fprintf(new, "#define EXIM_GROUPNAME \"%s\"\n", groupname); + + fprintf(new, "#define EXIM_UID %d\n", (int)uid); + fprintf(new, "#define EXIM_GID %d\n", (int)gid); + continue; + } + + /* CONFIGURE_OWNER is a special case. We look in the environment for + CONFIGURE_OWNER. If the value is not numeric, we look up the user. A lot of + this code is similar to that for EXIM_USER, but we aren't interested in a gid + here, and it's all optional, so just keep it separate. */ + + if (strcmp(name, "CONFIGURE_OWNER") == 0) + { + uid_t uid = 0; + char *s; + char *username = NULL; + char *user = getenv("CONFIGURE_OWNER"); + + if (user == NULL) user = ""; + while (isspace((unsigned char)(*user))) user++; + if (*user == 0) + { + fprintf(new, "/* %s not set */\n", name); + continue; + } + + for (s = user; *s != 0; s++) + { + if (iscntrl((unsigned char)(*s))) + { + printf("\n*** CONFIGURE_OWNER contains the control character 0x%02X in " + "one of the files\n in the \"Local\" directory. Please review " + "your build-time\n configuration.\n\n", *s); + return 1; + } + } + + /* Numeric uid given */ + + if (user[strspn(user, "0123456789")] == 0) + { + uid = (uid_t)atoi(user); + } + + /* User name given. Normally, we look up the uid right away. However, + people building binary distributions sometimes want to retain the name till + runtime. This is supported if the name begins "ref:". */ + + else if (strncmp(user, "ref:", 4) == 0) + { + user += 4; + while (isspace(*user)) user++; + username = user; + } + + else + { + struct passwd *pw = getpwnam(user); + if (pw == NULL) + { + printf("\n*** User \"%s\" (specified in one of the Makefiles) does not " + "exist.\n Please review your build-time configuration.\n\n", + user); + return 1; + } + + uid = pw->pw_uid; + } + + /* Output user and group names or uid/gid. When names are set, uid/gid + are set to zero but will be replaced at runtime. */ + + if (username != NULL) + fprintf(new, "#define CONFIGURE_OWNERNAME \"%s\"\n", username); + fprintf(new, "#define CONFIGURE_OWNER %d\n", (int)uid); + continue; + } + + /* FIXED_NEVER_USERS is another special case. Look up the uid values and + create suitable initialization data for a vector. */ + + if (strcmp(name, "FIXED_NEVER_USERS") == 0) + { + char *list = getenv("FIXED_NEVER_USERS"); + if (list == NULL) + { + fprintf(new, "#define FIXED_NEVER_USERS 0\n"); + } + else + { + int count = 1; + int i; + uid_t *vector; + char *p = list; + while (*p != 0) if (*p++ == ':') count++; + + vector = malloc((count+1) * sizeof(uid_t)); + vector[0] = (uid_t)count; + + for (i = 1; i <= count; list++, i++) + { + char name[64]; + p = list; + while (*list != 0 && *list != ':') list++; + strncpy(name, p, list-p); + name[list-p] = 0; + + if (name[strspn(name, "0123456789")] == 0) + { + vector[i] = (uid_t)atoi(name); + } + else + { + struct passwd *pw = getpwnam(name); + if (pw == NULL) + { + printf("\n*** User \"%s\" (specified for FIXED_NEVER_USERS in one of the Makefiles) does not " + "exist.\n Please review your build-time configuration.\n\n", + name); + return 1; + } + vector[i] = pw->pw_uid; + } + } + fprintf(new, "#define FIXED_NEVER_USERS %d, ", count); + for (i = 1; i <= count - 1; i++) + fprintf(new, "%d, ", (unsigned int)vector[i]); + fprintf(new, "%d\n", (unsigned int)vector[i]); + } + continue; + } + + /* Otherwise, check whether a value exists in the environment. Remember if + it is an AUTH setting or SUPPORT_CRYPTEQ. */ + + if ((value = getenv(name)) != NULL) + { + int len; + len = 21 - (int)strlen(name); + + if (strncmp(name, "AUTH_", 5) == 0) have_auth = 1; + if (strncmp(name, "SUPPORT_CRYPTEQ", 15) == 0) support_crypteq = 1; + + /* The text value of LDAP_LIB_TYPE refers to a macro that gets set. */ + + if (strcmp(name, "LDAP_LIB_TYPE") == 0) + { + if (strcmp(value, "NETSCAPE") == 0 || + strcmp(value, "UMICHIGAN") == 0 || + strcmp(value, "OPENLDAP1") == 0 || + strcmp(value, "OPENLDAP2") == 0 || + strcmp(value, "SOLARIS") == 0 || + strcmp(value, "SOLARIS7") == 0) /* Compatibility */ + { + fprintf(new, "#define LDAP_LIB_%s\n", value); + } + else + { + printf("\n*** LDAP_LIB_TYPE=%s is not a recognized LDAP library type." + "\n*** Please review your build-time configuration.\n\n", value); + return 1; + } + } + + else if (strcmp(name, "RADIUS_LIB_TYPE") == 0) + { + if (strcmp(value, "RADIUSCLIENT") == 0 || + strcmp(value, "RADLIB") == 0) + { + fprintf(new, "#define RADIUS_LIB_%s\n", value); + } + else + { + printf("\n*** RADIUS_LIB_TYPE=%s is not a recognized RADIUS library type." + "\n*** Please review your build-time configuration.\n\n", value); + return 1; + } + } + + /* Other macros get set to the environment value. */ + + else + { + fprintf(new, "#define %s ", name); + while(len-- > 0) fputc(' ', new); + + /* LOG_FILE_PATH is now messy because it can be a path containing %s or + it can be "syslog" or ":syslog" or "syslog:path" or even "path:syslog". */ + + if (strcmp(name, "LOG_FILE_PATH") == 0) + { + char *ss = value; + for(;;) + { + char *pp; + char *sss = strchr(ss, ':'); + if (sss != NULL) + { + strncpy(buffer, ss, sss-ss); + buffer[sss-ss] = 0; /* For empty case */ + } + else strcpy(buffer, ss); + pp = buffer + (int)strlen(buffer); + while (pp > buffer && isspace((unsigned char)pp[-1])) pp--; + *pp = 0; + if (buffer[0] != 0 && strcmp(buffer, "syslog") != 0) + check_percent_ess(buffer, name); + if (sss == NULL) break; + ss = sss + 1; + while (isspace((unsigned char)*ss)) ss++; + } + fprintf(new, "\"%s\"\n", value); + } + + /* Timezone values and HEADERS_CHARSET get quoted */ + + else if (strcmp(name, "TIMEZONE_DEFAULT") == 0|| + strcmp(name, "HEADERS_CHARSET") == 0) + fprintf(new, "\"%s\"\n", value); + + /* For others, quote any paths and don't quote anything else */ + + else + { + if (value[0] == '/') fprintf(new, "\"%s\"\n", value); + else fprintf(new, "%s\n", value); + } + } + } + + /* Value not defined in the environment; use the default */ + + else + { + char *t = p; + while (*p == ' ' || *p == '\t') p++; + if (*p != '\n') fputs(buffer, new); else + { + *t = 0; + if (strcmp(name, "BIN_DIRECTORY") == 0 || + strcmp(name, "CONFIGURE_FILE") == 0) + { + printf("\n*** %s has not been defined in any of the Makefiles in the\n" + " \"Local\" directory. " + "Please review your build-time configuration.\n\n", name); + return 1; + } + + if (strcmp(name, "TIMEZONE_DEFAULT") == 0) + { + char *tz = getenv("TZ"); + fprintf(new, "#define TIMEZONE_DEFAULT "); + if (tz == NULL) fprintf(new, "NULL\n"); else + fprintf(new, "\"%s\"\n", tz); + } + + else fprintf(new, "/* %s not set */\n", name); + } + } + } + +fclose(base); + +/* If any AUTH macros were defined, ensure that SUPPORT_CRYPTEQ is also +defined. */ + +if (have_auth) + { + if (!support_crypteq) fprintf(new, "/* Force SUPPORT_CRYPTEQ for AUTH */\n" + "#define SUPPORT_CRYPTEQ\n"); + } + +/* End off */ + +fprintf(new, "\n/* End of config.h */\n"); +fclose(new); +return 0; +} + +/* End of buildconfig.c */ diff --git a/src/src/child.c b/src/src/child.c new file mode 100644 index 000000000..1c48a4e4c --- /dev/null +++ b/src/src/child.c @@ -0,0 +1,454 @@ +/* $Cambridge: exim/src/src/child.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + + +#include "exim.h" + +static void (*oldsignal)(int); + + +/************************************************* +* Ensure an fd has a given value * +*************************************************/ + +/* This function is called when we want to ensure that a certain fd has a +specific value (one of 0, 1, 2). If it hasn't got it already, close the value +we want, duplicate the fd, then close the old one. + +Arguments: + oldfd original fd + newfd the fd we want + +Returns: nothing +*/ + +static void +force_fd(int oldfd, int newfd) +{ +if (oldfd == newfd) return; +close(newfd); +dup2(oldfd, newfd); +close(oldfd); +} + + + +/************************************************* +* Build argv list and optionally re-exec Exim * +*************************************************/ + +/* This function is called when Exim wants to re-exec (overlay) itself in the +current process. This is different to child_open_exim(), which runs another +Exim process in parallel (but it then calls this function). The function's +basic job is to build the argv list according to the values of current options +settings. There is a basic list that all calls require, and an additional list +that some do not require. Further additions can be given as additional +arguments. An option specifies whether the exec() is actually to happen, and if +so, what is to be done if it fails. + +Arguments: + exec_type CEE_RETURN_ARGV => don't exec; return the argv list + CEE_EXEC_EXIT => just exit() on exec failure + CEE_EXEC_PANIC => panic-die on exec failure + kill_v if TRUE, don't pass on the D_v flag + pcount if not NULL, points to extra size of argv required, and if + CEE_RETURN_ARGV is specified, it is updated to give the + number of slots used + minimal TRUE if only minimal argv is required + acount number of additional arguments + ... further values to add to argv + +Returns: if CEE_RETURN_ARGV is given, returns a pointer to argv; + otherwise, does not return +*/ + +uschar ** +child_exec_exim(int exec_type, BOOL kill_v, int *pcount, BOOL minimal, + int acount, ...) +{ +int first_special = -1; +int n = 0; +int extra = (pcount != NULL)? *pcount : 0; +uschar **argv = + store_get((extra + acount + MAX_CLMACROS + 16) * sizeof(char *)); + +/* In all case, the list starts out with the path, any macros, and a changed +config file. */ + +argv[n++] = exim_path; +if (clmacro_count > 0) + { + memcpy(argv + n, clmacros, clmacro_count * sizeof(uschar *)); + n += clmacro_count; + } +if (config_changed) + { + argv[n++] = US"-C"; + argv[n++] = config_main_filename; + } + +/* These values are added only for non-minimal cases. If debug_selector is +precisely D_v, we have to assume this was started by a non-admin user, and +we suppress the flag when requested. (This happens when passing on an SMTP +connection, and after ETRN.) If there's more debugging going on, an admin user +was involved, so we do pass it on. */ + +if (!minimal) + { + if (debug_selector == D_v) + { + if (!kill_v) argv[n++] = US"-v"; + } + else + { + if (debug_selector != 0) + argv[n++] = string_sprintf("-d=0x%x", debug_selector); + } + if (dont_deliver) argv[n++] = US"-N"; + if (queue_smtp) argv[n++] = US"-odqs"; + if (synchronous_delivery) argv[n++] = US"-odi"; + if (connection_max_messages >= 0) + argv[n++] = string_sprintf("-oB%d", connection_max_messages); + } + +/* Now add in any others that are in the call. Remember which they were, +for more helpful diagnosis on failure. */ + +if (acount > 0) + { + va_list ap; + va_start(ap, acount); + first_special = n; + while (acount-- > 0) + argv[n++] = va_arg(ap, uschar *); + va_end(ap); + } + +/* Terminate the list, and return it, if that is what is wanted. */ + +argv[n] = NULL; +if (exec_type == CEE_RETURN_ARGV) + { + if (pcount != NULL) *pcount = n; + return argv; + } + +/* Otherwise, do the exec() here, and handle the consequences of an unexpected +failure. We know that there will always be at least one extra option in the +call when exec() is done here, so it can be used to add to the panic data. */ + +DEBUG(D_exec) debug_print_argv(argv); +exim_nullstd(); /* Make sure std{in,out,err} exist */ +execv(CS argv[0], (char *const *)argv); + +log_write(0, + LOG_MAIN | ((exec_type == CEE_EXEC_EXIT)? LOG_PANIC : LOG_PANIC_DIE), + "re-exec of exim (%s) with %s failed: %s", exim_path, argv[first_special], + strerror(errno)); + +/* Get here if exec_type == CEE_EXEC_EXIT. +Note: this must be _exit(), not exit(). */ + +_exit(EX_EXECFAILED); + +return NULL; /* To keep compilers happy */ +} + + + + +/************************************************* +* Create a child Exim process * +*************************************************/ + +/* This function is called when Exim wants to run a parallel instance of itself +in order to inject a message via the standard input. The function creates a +child process and runs Exim in it. It sets up a pipe to the standard input of +the new process, and returns that to the caller via fdptr. The function returns +the pid of the new process, or -1 if things go wrong. If debug_fd is +non-negative, it is passed as stderr. + +Argument: fdptr pointer to int for the stdin fd +Returns: pid of the created process or -1 if anything has gone wrong +*/ + +pid_t +child_open_exim(int *fdptr) +{ +int pfd[2]; +int save_errno; +pid_t pid; + +/* Create the pipe and fork the process. Ensure that SIGCHLD is set to +SIG_DFL before forking, so that the child process can be waited for. We +sometimes get here with it set otherwise. Save the old state for resetting +on the wait. */ + +if (pipe(pfd) != 0) return (pid_t)(-1); +oldsignal = signal(SIGCHLD, SIG_DFL); +pid = fork(); + +/* Child process: make the reading end of the pipe into the standard input and +close the writing end. If debugging, pass debug_fd as stderr. Then re-exec +Exim. Failure is signalled with EX_EXECFAILED, but this shouldn't occur! */ + +if (pid == 0) + { + force_fd(pfd[pipe_read], 0); + close(pfd[pipe_write]); + if (debug_fd > 0) force_fd(debug_fd, 2); + if (bounce_sender_authentication != NULL) + child_exec_exim(CEE_EXEC_EXIT, FALSE, NULL, FALSE, 8, + US"-t", US"-oem", US"-oi", US"-f", US"<>", US"-oMas", + bounce_sender_authentication, message_id_option); + else + child_exec_exim(CEE_EXEC_EXIT, FALSE, NULL, FALSE, 6, + US"-t", US"-oem", US"-oi", US"-f", US"<>", message_id_option); + /* Control does not return here. */ + } + +/* Parent process. Save fork() errno and close the reading end of the stdin +pipe. */ + +save_errno = errno; +close(pfd[pipe_read]); + +/* Fork succeeded */ + +if (pid > 0) + { + *fdptr = pfd[pipe_write]; /* return writing end of stdin pipe */ + return pid; /* and pid of new process */ + } + +/* Fork failed */ + +close(pfd[pipe_write]); +errno = save_errno; +return (pid_t)(-1); +} + + + + +/************************************************* +* Create a non-Exim child process * +*************************************************/ + +/* This function creates a child process and runs the given command in it. It +sets up pipes to the standard input and output of the new process, and returns +them to the caller. The standard error is cloned to the output. If there are +any file descriptors "in the way" in the new process, they are closed. A new +umask is supplied for the process, and an optional new uid and gid are also +available. These are used by the queryprogram router to set an unprivileged id. +The function returns the pid of the new process, or -1 if things go wrong. + +Arguments: + argv the argv for exec in the new process + envp the envp for exec in the new process + newumask umask to set in the new process + newuid point to uid for the new process or NULL for no change + newgid point to gid for the new process or NULL for no change + infdptr pointer to int into which the fd of the stdin of the new process + is placed + outfdptr pointer to int into which the fd of the stdout/stderr of the new + process is placed + wd if not NULL, a path to be handed to chdir() in the new process + make_leader if TRUE, make the new process a process group leader + +Returns: the pid of the created process or -1 if anything has gone wrong +*/ + +pid_t +child_open_uid(uschar **argv, uschar **envp, int newumask, uid_t *newuid, + gid_t *newgid, int *infdptr, int *outfdptr, uschar *wd, BOOL make_leader) +{ +int save_errno; +int inpfd[2], outpfd[2]; +pid_t pid; + +/* Create the pipes. */ + +if (pipe(inpfd) != 0) return (pid_t)(-1); +if (pipe(outpfd) != 0) + { + close(inpfd[pipe_read]); + close(inpfd[pipe_write]); + return (pid_t)(-1); + } + +/* Fork the process. Ensure that SIGCHLD is set to SIG_DFL before forking, so +that the child process can be waited for. We sometimes get here with it set +otherwise. Save the old state for resetting on the wait. */ + +oldsignal = signal(SIGCHLD, SIG_DFL); +pid = fork(); + +/* The child process becomes a process group leader if requested, and then +organizes the pipes. Any unexpected failure is signalled with EX_EXECFAILED; +these are all "should never occur" failures, except perhaps for exec failing +because the command doesn't exist. */ + +if (pid == 0) + { + if (make_leader && setpgid(0,0) < 0) goto CHILD_FAILED; + + close(inpfd[pipe_write]); + force_fd(inpfd[pipe_read], 0); + + close(outpfd[pipe_read]); + force_fd(outpfd[pipe_write], 1); + + close(2); + dup2(1, 2); + + /* Set the required environment. If changing uid, ensure that + SIGUSR1 is ignored, as the process won't have the privilege to + write to the process log. */ + + if (newgid != NULL && setgid(*newgid) < 0) goto CHILD_FAILED; + if (newuid != NULL) + { + signal(SIGUSR1, SIG_IGN); + if (setuid(*newuid) < 0) goto CHILD_FAILED; + } + (void)umask(newumask); + + /* Set the working directory if required */ + + if (wd != NULL && Uchdir(wd) < 0) goto CHILD_FAILED; + + /* Now do the exec */ + + if (envp == NULL) execv(CS argv[0], (char *const *)argv); + else execve(CS argv[0], (char *const *)argv, (char *const *)envp); + + /* Failed to execv. Signal this failure using EX_EXECFAILED. We are + losing the actual errno we got back, because there is no way to return + this. */ + + CHILD_FAILED: + _exit(EX_EXECFAILED); /* Note: must be _exit(), NOT exit() */ + } + +/* Parent process. Save any fork failure code, and close the reading end of the +stdin pipe, and the writing end of the stdout pipe. */ + +save_errno = errno; +close(inpfd[pipe_read]); +close(outpfd[pipe_write]); + +/* Fork succeeded; return the input/output pipes and the pid */ + +if (pid > 0) + { + *infdptr = inpfd[pipe_write]; + *outfdptr = outpfd[pipe_read]; + return pid; + } + +/* Fork failed; reset fork errno before returning */ + +close(inpfd[pipe_write]); +close(outpfd[pipe_read]); +errno = save_errno; +return (pid_t)(-1); +} + + + + +/************************************************* +* Create child process without uid change * +*************************************************/ + +/* This function is a wrapper for child_open_uid() that doesn't have the uid, +gid, and working directory changing arguments. It is provided so as to have a +clean interface for use from local_scan(), but also saves writing NULL +arguments in other calls. + +Arguments: + argv the argv for exec in the new process + envp the envp for exec in the new process + newumask umask to set in the new process + infdptr pointer to int into which the fd of the stdin of the new process + is placed + outfdptr pointer to int into which the fd of the stdout/stderr of the new + process is placed + make_leader if TRUE, make the new process a process group leader + +Returns: the pid of the created process or -1 if anything has gone wrong +*/ + +pid_t +child_open(uschar **argv, uschar **envp, int newumask, int *infdptr, + int *outfdptr, BOOL make_leader) +{ +return child_open_uid(argv, envp, newumask, NULL, NULL, infdptr, outfdptr, + NULL, make_leader); +} + + + + +/************************************************* +* Close down child process * +*************************************************/ + +/* Wait for the given process to finish, with optional timeout. + +Arguments + pid: the pid to wait for + timeout: maximum time to wait; 0 means for as long as it takes + +Returns: >= 0 process terminated by exiting; value is process + ending status; if an execve() failed, the value + is typically 127 (defined as EX_EXECFAILED) + < 0 & > -256 process was terminated by a signal; value is the + negation of the signal number + -256 timed out + -257 other error in wait(); errno still set +*/ + +int +child_close(pid_t pid, int timeout) +{ +int yield; + +if (timeout > 0) + { + sigalrm_seen = FALSE; + alarm(timeout); + } + +for(;;) + { + int status; + pid_t rc = waitpid(pid, &status, 0); + if (rc == pid) + { + int lowbyte = status & 255; + if (lowbyte == 0) yield = (status >> 8) & 255; + else yield = -lowbyte; + break; + } + if (rc < 0) + { + yield = (errno == EINTR && sigalrm_seen)? -256 : -257; + break; + } + } + +if (timeout > 0) alarm(0); + +signal(SIGCHLD, oldsignal); /* restore */ +return yield; +} + +/* End of child.c */ diff --git a/src/src/config.h.defaults b/src/src/config.h.defaults new file mode 100644 index 000000000..39a17ae17 --- /dev/null +++ b/src/src/config.h.defaults @@ -0,0 +1,147 @@ +/* $Cambridge: exim/src/src/config.h.defaults,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* The default settings for Exim configuration variables. A #define without +any data just defines the existence of the variable; it won't get included +in config.h unless some value is defined in Local/Makefile. */ + +#define ALT_CONFIG_PREFIX +#define ALT_CONFIG_ROOT_ONLY + +#define APPENDFILE_MODE 0600 +#define APPENDFILE_DIRECTORY_MODE 0700 +#define APPENDFILE_LOCKFILE_MODE 0600 + +#define AUTH_CRAM_MD5 +#define AUTH_CYRUS_SASL +#define AUTH_PLAINTEXT +#define AUTH_SPA + +#define BIN_DIRECTORY + +#define CONFIGURE_FILE +#define CONFIGURE_FILE_USE_EUID +#define CONFIGURE_FILE_USE_NODE +#define CONFIGURE_OWNER +#define CYRUS_PWCHECK_SOCKET +#define CYRUS_SASLAUTHD_SOCKET + +#define DEFAULT_CRYPT crypt +#define DELIVER_IN_BUFFER_SIZE 8192 +#define DELIVER_OUT_BUFFER_SIZE 8192 +#define DISABLE_D_OPTION + +#define EXIMDB_DIRECTORY_MODE 0750 +#define EXIMDB_LOCK_TIMEOUT 60 +#define EXIMDB_LOCKFILE_MODE 0640 +#define EXIMDB_MODE 0640 + +#define EXIM_PERL + +/* Both uid and gid are triggered by this */ +#define EXIM_UID + +#define FIXED_NEVER_USERS "root" + +#define HAVE_CRYPT16 +#define HAVE_SA_LEN +#define HEADERS_CHARSET "ISO-8859-1" +#define HEADER_ADD_BUFFER_SIZE 8192 +#define HEADER_MAXSIZE (1024*1024) + +#define INPUT_DIRECTORY_MODE 0750 +#define IPV6_USE_INET_PTON + +#define LDAP_LIB_TYPE +#define LOCAL_SCAN_HAS_OPTIONS + +#define LOG_DIRECTORY_MODE 0750 +#define LOG_FILE_PATH +#define LOG_MODE 0640 + +#define LOOKUP_CDB +#define LOOKUP_DBM +#define LOOKUP_DNSDB +#define LOOKUP_DSEARCH +#define LOOKUP_IBASE +#define LOOKUP_LDAP +#define LOOKUP_LSEARCH +#define LOOKUP_MYSQL +#define LOOKUP_NIS +#define LOOKUP_NISPLUS +#define LOOKUP_ORACLE +#define LOOKUP_PASSWD +#define LOOKUP_PGSQL +#define LOOKUP_TESTDB +#define LOOKUP_WHOSON +#define LOOKUP_WILDLSEARCH +#define LOOKUP_NWILDLSEARCH + +#define MAX_FILTER_SIZE (1024*1024) +#define MAX_LOCALHOST_NUMBER 256 +#define MAX_INCLUDE_SIZE (1024*1024) +#define MAX_INTERFACES 250 +#define MAX_NAMED_LIST 16 +#define MSGLOG_DIRECTORY_MODE 0750 + +#define PID_FILE_PATH + +#define RADIUS_CONFIG_FILE +#define RADIUS_LIB_TYPE + +#define ROUTER_ACCEPT +#define ROUTER_DNSLOOKUP +#define ROUTER_IPLITERAL +#define ROUTER_IPLOOKUP +#define ROUTER_MANUALROUTE +#define ROUTER_QUERYPROGRAM +#define ROUTER_REDIRECT + +#define SPOOL_DIRECTORY +#define SPOOL_DIRECTORY_MODE 0750 +#define SPOOL_MODE 0640 +#define STRING_SPRINTF_BUFFER_SIZE 8192 + +#define SUPPORT_A6 +#define SUPPORT_CRYPTEQ +#define SUPPORT_MAILDIR +#define SUPPORT_MAILSTORE +#define SUPPORT_MBX +#define SUPPORT_MOVE_FROZEN_MESSAGES +#define SUPPORT_PAM +#define SUPPORT_TLS +#define SUPPORT_TRANSLATE_IP_ADDRESS + +#define SYSLOG_LOG_PID +#define SYSLOG_LONG_LINES + +#define TIMEZONE_DEFAULT +#define TMPDIR + +#define TRANSPORT_APPENDFILE +#define TRANSPORT_AUTOREPLY +#define TRANSPORT_LMTP +#define TRANSPORT_PIPE +#define TRANSPORT_SMTP + +#define USE_DB +#define USE_GDBM +#define USE_GNUTLS +#define USE_READLINE +#define USE_TCP_WRAPPERS +#define USE_TDB + +/* Things that are not routinely changed but are nevertheless configurable +just in case. */ + +#define DNS_MAXNAME 256 +#define EXPAND_MAXN 20 +#define ROOT_UID 0 + +/* End of config.h.defaults */ diff --git a/src/src/configure.default b/src/src/configure.default new file mode 100644 index 000000000..6a29ec50d --- /dev/null +++ b/src/src/configure.default @@ -0,0 +1,598 @@ +# $Cambridge: exim/src/src/configure.default,v 1.1 2004/10/07 10:39:01 ph10 Exp $ + +###################################################################### +# Runtime configuration file for Exim # +###################################################################### + + +# This is a default configuration file which will operate correctly in +# uncomplicated installations. Please see the manual for a complete list +# of all the runtime configuration options that can be included in a +# configuration file. There are many more than are mentioned here. The +# manual is in the file doc/spec.txt in the Exim distribution as a plain +# ASCII file. Other formats (PostScript, Texinfo, HTML, PDF) are available +# from the Exim ftp sites. The manual is also online at the Exim web sites. + + +# This file is divided into several parts, all but the first of which are +# headed by a line starting with the word "begin". Only those parts that +# are required need to be present. Blank lines, and lines starting with # +# are ignored. + + +########### IMPORTANT ########## IMPORTANT ########### IMPORTANT ########### +# # +# Whenever you change Exim's configuration file, you *must* remember to # +# HUP the Exim daemon, because it will not pick up the new configuration # +# until you do. However, any other Exim processes that are started, for # +# example, a process started by an MUA in order to send a message, will # +# see the new configuration as soon as it is in place. # +# # +# You do not need to HUP the daemon for changes in auxiliary files that # +# are referenced from this file. They are read every time they are used. # +# # +# It is usually a good idea to test a new configuration for syntactic # +# correctness before installing it (for example, by running the command # +# "exim -C /config/file.new -bV"). # +# # +########### IMPORTANT ########## IMPORTANT ########### IMPORTANT ########### + + + +###################################################################### +# MAIN CONFIGURATION SETTINGS # +###################################################################### + +# Specify your host's canonical name here. This should normally be the fully +# qualified "official" name of your host. If this option is not set, the +# uname() function is called to obtain the name. In many cases this does +# the right thing and you need not set anything explicitly. + +# primary_hostname = + + +# The next three settings create two lists of domains and one list of hosts. +# These lists are referred to later in this configuration using the syntax +# +local_domains, +relay_to_domains, and +relay_from_hosts, respectively. They +# are all colon-separated lists: + +domainlist local_domains = @ +domainlist relay_to_domains = +hostlist relay_from_hosts = 127.0.0.1 + +# Most straightforward access control requirements can be obtained by +# appropriate settings of the above options. In more complicated situations, you +# may need to modify the Access Control List (ACL) which appears later in this +# file. + +# The first setting specifies your local domains, for example: +# +# domainlist local_domains = my.first.domain : my.second.domain +# +# You can use "@" to mean "the name of the local host", as in the default +# setting above. This is the name that is specified by primary_hostname, +# as specified above (or defaulted). If you do not want to do any local +# deliveries, remove the "@" from the setting above. If you want to accept mail +# addressed to your host's literal IP address, for example, mail addressed to +# "user@[192.168.23.44]", you can add "@[]" as an item in the local domains +# list. You also need to uncomment "allow_domain_literals" below. This is not +# recommended for today's Internet. + +# The second setting specifies domains for which your host is an incoming relay. +# If you are not doing any relaying, you should leave the list empty. However, +# if your host is an MX backup or gateway of some kind for some domains, you +# must set relay_to_domains to match those domains. For example: +# +# domainlist relay_to_domains = *.myco.com : my.friend.org +# +# This will allow any host to relay through your host to those domains. +# See the section of the manual entitled "Control of relaying" for more +# information. + +# The third setting specifies hosts that can use your host as an outgoing relay +# to any other host on the Internet. Such a setting commonly refers to a +# complete local network as well as the localhost. For example: +# +# hostlist relay_from_hosts = 127.0.0.1 : 192.168.0.0/16 +# +# The "/16" is a bit mask (CIDR notation), not a number of hosts. Note that you +# have to include 127.0.0.1 if you want to allow processes on your host to send +# SMTP mail by using the loopback address. A number of MUAs use this method of +# sending mail. + + +# All three of these lists may contain many different kinds of item, including +# wildcarded names, regular expressions, and file lookups. See the reference +# manual for details. The lists above are used in the access control list for +# incoming messages. The name of this ACL is defined here: + +acl_smtp_rcpt = acl_check_rcpt + +# You should not change that setting until you understand how ACLs work. + + +# Specify the domain you want to be added to all unqualified addresses +# here. An unqualified address is one that does not contain an "@" character +# followed by a domain. For example, "caesar@rome.example" is a fully qualified +# address, but the string "caesar" (i.e. just a login name) is an unqualified +# email address. Unqualified addresses are accepted only from local callers by +# default. See the recipient_unqualified_hosts option if you want to permit +# unqualified addresses from remote sources. If this option is not set, the +# primary_hostname value is used for qualification. + +# qualify_domain = + + +# If you want unqualified recipient addresses to be qualified with a different +# domain to unqualified sender addresses, specify the recipient domain here. +# If this option is not set, the qualify_domain value is used. + +# qualify_recipient = + + +# The following line must be uncommented if you want Exim to recognize +# addresses of the form "user@[10.11.12.13]" that is, with a "domain literal" +# (an IP address) instead of a named domain. The RFCs still require this form, +# but it makes little sense to permit mail to be sent to specific hosts by +# their IP address in the modern Internet. This ancient format has been used +# by those seeking to abuse hosts by using them for unwanted relaying. If you +# really do want to support domain literals, uncomment the following line, and +# see also the "domain_literal" router below. + +# allow_domain_literals + + +# No deliveries will ever be run under the uids of these users (a colon- +# separated list). An attempt to do so causes a panic error to be logged, and +# the delivery to be deferred. This is a paranoic safety catch. There is an +# even stronger safety catch in the form of the FIXED_NEVER_USERS setting +# in the configuration for building Exim. The list of users that it specifies +# is built into the binary, and cannot be changed. The option below just adds +# additional users to the list. The default for FIXED_NEVER_USERS is "root", +# but just to be absolutely sure, the default here is also "root". + +# Note that the default setting means you cannot deliver mail addressed to root +# as if it were a normal user. This isn't usually a problem, as most sites have +# an alias for root that redirects such mail to a human administrator. + +never_users = root + + +# The setting below causes Exim to do a reverse DNS lookup on all incoming +# IP calls, in order to get the true host name. If you feel this is too +# expensive, you can specify the networks for which a lookup is done, or +# remove the setting entirely. + +host_lookup = * + + +# The settings below, which are actually the same as the defaults in the +# code, cause Exim to make RFC 1413 (ident) callbacks for all incoming SMTP +# calls. You can limit the hosts to which these calls are made, and/or change +# the timeout that is used. If you set the timeout to zero, all RFC 1413 calls +# are disabled. RFC 1413 calls are cheap and can provide useful information +# for tracing problem messages, but some hosts and firewalls have problems +# with them. This can result in a timeout instead of an immediate refused +# connection, leading to delays on starting up an SMTP session. + +rfc1413_hosts = * +rfc1413_query_timeout = 30s + + +# By default, Exim expects all envelope addresses to be fully qualified, that +# is, they must contain both a local part and a domain. If you want to accept +# unqualified addresses (just a local part) from certain hosts, you can specify +# these hosts by setting one or both of +# +# sender_unqualified_hosts = +# recipient_unqualified_hosts = +# +# to control sender and recipient addresses, respectively. When this is done, +# unqualified addresses are qualified using the settings of qualify_domain +# and/or qualify_recipient (see above). + + +# If you want Exim to support the "percent hack" for certain domains, +# uncomment the following line and provide a list of domains. The "percent +# hack" is the feature by which mail addressed to x%y@z (where z is one of +# the domains listed) is locally rerouted to x@y and sent on. If z is not one +# of the "percent hack" domains, x%y is treated as an ordinary local part. This +# hack is rarely needed nowadays; you should not enable it unless you are sure +# that you really need it. +# +# percent_hack_domains = +# +# As well as setting this option you will also need to remove the test +# for local parts containing % in the ACL definition below. + + +# When Exim can neither deliver a message nor return it to sender, it "freezes" +# the delivery error message (aka "bounce message"). There are also other +# circumstances in which messages get frozen. They will stay on the queue for +# ever unless one of the following options is set. + +# This option unfreezes frozen bounce messages after two days, tries +# once more to deliver them, and ignores any delivery failures. + +ignore_bounce_errors_after = 2d + +# This option cancels (removes) frozen messages that are older than a week. + +timeout_frozen_after = 7d + + + +###################################################################### +# ACL CONFIGURATION # +# Specifies access control lists for incoming SMTP mail # +###################################################################### + +begin acl + +# This access control list is used for every RCPT command in an incoming +# SMTP message. The tests are run in order until the address is either +# accepted or denied. + +acl_check_rcpt: + + # Accept if the source is local SMTP (i.e. not over TCP/IP). We do this by + # testing for an empty sending host field. + + accept hosts = : + + ############################################################################# + # The following section of the ACL is concerned with local parts that contain + # @ or % or ! or / or | or dots in unusual places. + # + # The characters other than dots are rarely found in genuine local parts, but + # are often tried by people looking to circumvent relaying restrictions. + # Therefore, although they are valid in local parts, these rules lock them + # out, as a precaution. + # + # Empty components (two dots in a row) are not valid in RFC 2822, but Exim + # allows them because they have been encountered. (Consider local parts + # constructed as "firstinitial.secondinitial.familyname" when applied to + # someone like me, who has no second initial.) However, a local part starting + # with a dot or containing /../ can cause trouble if it is used as part of a + # file name (e.g. for a mailing list). This is also true for local parts that + # contain slashes. A pipe symbol can also be troublesome if the local part is + # incorporated unthinkingly into a shell command line. + # + # Two different rules are used. The first one is stricter, and is applied to + # messages that are addressed to one of the local domains handled by this + # host. It blocks local parts that begin with a dot or contain @ % ! / or |. + # If you have local accounts that include these characters, you will have to + # modify this rule. + + deny message = Restricted characters in address + domains = +local_domains + local_parts = ^[.] : ^.*[@%!/|] + + # The second rule applies to all other domains, and is less strict. This + # allows your own users to send outgoing messages to sites that use slashes + # and vertical bars in their local parts. It blocks local parts that begin + # with a dot, slash, or vertical bar, but allows these characters within the + # local part. However, the sequence /../ is barred. The use of @ % and ! is + # blocked, as before. The motivation here is to prevent your users (or + # your users' viruses) from mounting certain kinds of attack on remote sites. + + deny message = Restricted characters in address + domains = !+local_domains + local_parts = ^[./|] : ^.*[@%!] : ^.*/\\.\\./ + ############################################################################# + + # Accept mail to postmaster in any local domain, regardless of the source, + # and without verifying the sender. + + accept local_parts = postmaster + domains = +local_domains + + # Deny unless the sender address can be verified. + + require verify = sender + + ############################################################################# + # There are no checks on DNS "black" lists because the domains that contain + # these lists are changing all the time. However, here are two examples of + # how you could get Exim to perform a DNS black list lookup at this point. + # The first one denies, while the second just warns. + # + # deny message = rejected because $sender_host_address is in a black list at $dnslist_domain\n$dnslist_text + # dnslists = black.list.example + # + # warn message = X-Warning: $sender_host_address is in a black list at $dnslist_domain + # log_message = found in $dnslist_domain + # dnslists = black.list.example + ############################################################################# + + # Accept if the address is in a local domain, but only if the recipient can + # be verified. Otherwise deny. The "endpass" line is the border between + # passing on to the next ACL statement (if tests above it fail) or denying + # access (if tests below it fail). + + accept domains = +local_domains + endpass + verify = recipient + + # Accept if the address is in a domain for which we are relaying, but again, + # only if the recipient can be verified. + + accept domains = +relay_to_domains + endpass + verify = recipient + + # If control reaches this point, the domain is neither in +local_domains + # nor in +relay_to_domains. + + # Accept if the message comes from one of the hosts for which we are an + # outgoing relay. Recipient verification is omitted here, because in many + # cases the clients are dumb MUAs that don't cope well with SMTP error + # responses. If you are actually relaying out from MTAs, you should probably + # add recipient verification here. + + accept hosts = +relay_from_hosts + + # Accept if the message arrived over an authenticated connection, from + # any host. Again, these messages are usually from MUAs, so recipient + # verification is omitted. + + accept authenticated = * + + # Reaching the end of the ACL causes a "deny", but we might as well give + # an explicit message. + + deny message = relay not permitted + + + +###################################################################### +# ROUTERS CONFIGURATION # +# Specifies how addresses are handled # +###################################################################### +# THE ORDER IN WHICH THE ROUTERS ARE DEFINED IS IMPORTANT! # +# An address is passed to each router in turn until it is accepted. # +###################################################################### + +begin routers + +# This router routes to remote hosts over SMTP by explicit IP address, +# when an email address is given in "domain literal" form, for example, +# . The RFCs require this facility. However, it is +# little-known these days, and has been exploited by evil people seeking +# to abuse SMTP relays. Consequently it is commented out in the default +# configuration. If you uncomment this router, you also need to uncomment +# allow_domain_literals above, so that Exim can recognize the syntax of +# domain literal addresses. + +# domain_literal: +# driver = ipliteral +# domains = ! +local_domains +# transport = remote_smtp + + +# This router routes addresses that are not in local domains by doing a DNS +# lookup on the domain name. Any domain that resolves to 0.0.0.0 or to a +# loopback interface address (127.0.0.0/8) is treated as if it had no DNS +# entry. Note that 0.0.0.0 is the same as 0.0.0.0/32, which is commonly treated +# as the local host inside the network stack. It is not 0.0.0.0/0, the default +# route. If the DNS lookup fails, no further routers are tried because of +# the no_more setting, and consequently the address is unrouteable. + +dnslookup: + driver = dnslookup + domains = ! +local_domains + transport = remote_smtp + ignore_target_hosts = 0.0.0.0 : 127.0.0.0/8 + no_more + + +# The remaining routers handle addresses in the local domain(s). + + +# This router handles aliasing using a linearly searched alias file with the +# name SYSTEM_ALIASES_FILE. When this configuration is installed automatically, +# the name gets inserted into this file from whatever is set in Exim's +# build-time configuration. The default path is the traditional /etc/aliases. +# If you install this configuration by hand, you need to specify the correct +# path in the "data" setting below. +# +##### NB You must ensure that the alias file exists. It used to be the case +##### NB that every Unix had that file, because it was the Sendmail default. +##### NB These days, there are systems that don't have it. Your aliases +##### NB file should at least contain an alias for "postmaster". +# +# If any of your aliases expand to pipes or files, you will need to set +# up a user and a group for these deliveries to run under. You can do +# this by uncommenting the "user" option below (changing the user name +# as appropriate) and adding a "group" option if necessary. Alternatively, you +# can specify "user" on the transports that are used. Note that the transports +# listed below are the same as are used for .forward files; you might want +# to set up different ones for pipe and file deliveries from aliases. + +system_aliases: + driver = redirect + allow_fail + allow_defer + data = ${lookup{$local_part}lsearch{SYSTEM_ALIASES_FILE}} +# user = exim + file_transport = address_file + pipe_transport = address_pipe + + +# This router handles forwarding using traditional .forward files in users' +# home directories. If you want it also to allow mail filtering when a forward +# file starts with the string "# Exim filter" or "# Sieve filter", uncomment +# the "allow_filter" option. + +# If you want this router to treat local parts with suffixes introduced by "-" +# or "+" characters as if the suffixes did not exist, uncomment the two local_ +# part_suffix options. Then, for example, xxxx-foo@your.domain will be treated +# in the same way as xxxx@your.domain by this router. You probably want to make +# the same change to the localuser router. + +# The no_verify setting means that this router is skipped when Exim is +# verifying addresses. Similarly, no_expn means that this router is skipped if +# Exim is processing an EXPN command. + +# The check_ancestor option means that if the forward file generates an +# address that is an ancestor of the current one, the current one gets +# passed on instead. This covers the case where A is aliased to B and B +# has a .forward file pointing to A. + +# The three transports specified at the end are those that are used when +# forwarding generates a direct delivery to a file, or to a pipe, or sets +# up an auto-reply, respectively. + +userforward: + driver = redirect + check_local_user +# local_part_suffix = +* : -* +# local_part_suffix_optional + file = $home/.forward +# allow_filter + no_verify + no_expn + check_ancestor + file_transport = address_file + pipe_transport = address_pipe + reply_transport = address_reply + + +# This router matches local user mailboxes. If the router fails, the error +# message is "Unknown user". + +# If you want this router to treat local parts with suffixes introduced by "-" +# or "+" characters as if the suffixes did not exist, uncomment the two local_ +# part_suffix options. Then, for example, xxxx-foo@your.domain will be treated +# in the same way as xxxx@your.domain by this router. + +localuser: + driver = accept + check_local_user +# local_part_suffix = +* : -* +# local_part_suffix_optional + transport = local_delivery + cannot_route_message = Unknown user + + + +###################################################################### +# TRANSPORTS CONFIGURATION # +###################################################################### +# ORDER DOES NOT MATTER # +# Only one appropriate transport is called for each delivery. # +###################################################################### + +# A transport is used only when referenced from a router that successfully +# handles an address. + +begin transports + + +# This transport is used for delivering messages over SMTP connections. + +remote_smtp: + driver = smtp + + +# This transport is used for local delivery to user mailboxes in traditional +# BSD mailbox format. By default it will be run under the uid and gid of the +# local user, and requires the sticky bit to be set on the /var/mail directory. +# Some systems use the alternative approach of running mail deliveries under a +# particular group instead of using the sticky bit. The commented options below +# show how this can be done. + +local_delivery: + driver = appendfile + file = /var/mail/$local_part + delivery_date_add + envelope_to_add + return_path_add +# group = mail +# mode = 0660 + + +# This transport is used for handling pipe deliveries generated by alias or +# .forward files. If the pipe generates any standard output, it is returned +# to the sender of the message as a delivery error. Set return_fail_output +# instead of return_output if you want this to happen only when the pipe fails +# to complete normally. You can set different transports for aliases and +# forwards if you want to - see the references to address_pipe in the routers +# section above. + +address_pipe: + driver = pipe + return_output + + +# This transport is used for handling deliveries directly to files that are +# generated by aliasing or forwarding. + +address_file: + driver = appendfile + delivery_date_add + envelope_to_add + return_path_add + + +# This transport is used for handling autoreplies generated by the filtering +# option of the userforward router. + +address_reply: + driver = autoreply + + + +###################################################################### +# RETRY CONFIGURATION # +###################################################################### + +begin retry + +# This single retry rule applies to all domains and all errors. It specifies +# retries every 15 minutes for 2 hours, then increasing retry intervals, +# starting at 1 hour and increasing each time by a factor of 1.5, up to 16 +# hours, then retries every 6 hours until 4 days have passed since the first +# failed delivery. + +# Address or Domain Error Retries +# ----------------- ----- ------- + +* * F,2h,15m; G,16h,1h,1.5; F,4d,6h + + + +###################################################################### +# REWRITE CONFIGURATION # +###################################################################### + +# There are no rewriting specifications in this default configuration file. + +begin rewrite + + + +###################################################################### +# AUTHENTICATION CONFIGURATION # +###################################################################### + +# There are no authenticator specifications in this default configuration file. + +begin authenticators + + + +###################################################################### +# CONFIGURATION FOR local_scan() # +###################################################################### + +# If you have built Exim to include a local_scan() function that contains +# tables for private options, you can define those options here. Remember to +# uncomment the "begin" line. It is commented by default because it provokes +# an error with Exim binaries that are not built with LOCAL_SCAN_HAS_OPTIONS +# set in the Local/Makefile. + +# begin local_scan + + +# End of Exim configuration file diff --git a/src/src/convert4r3.src b/src/src/convert4r3.src new file mode 100755 index 000000000..b972f739f --- /dev/null +++ b/src/src/convert4r3.src @@ -0,0 +1,1369 @@ +#! PERL_COMMAND -w +# $Cambridge: exim/src/src/convert4r3.src,v 1.1 2004/10/07 10:39:01 ph10 Exp $ + +# This is a Perl script that reads an Exim run-time configuration file and +# checks for settings that were valid prior to release 3.00 but which were +# obsoleted by that release. It writes a new file with suggested changes to +# the standard output, and commentary about what it has done to stderr. + +# It is assumed that the input is a valid Exim configuration file. + + +################################################## +# Analyse one line # +################################################## + +# This is called for the main and the driver sections, not for retry +# or rewrite sections (which are unmodified). + +sub checkline{ +my($line) = $_[0]; + +return "comment" if $line =~ /^\s*(#|$)/; +return "end" if $line =~ /^\s*end\s*$/; + +# Macros are recognized only in the first section of the file. + +return "macro" if $prefix eq "" && $line =~ /^\s*[A-Z]/; + +# Pick out the name at the start and the rest of the line (into global +# variables) and return whether the start of a driver or not. + +($i1,$name,$i2,$rest) = $line =~ /^(\s*)([a-z0-9_]+)(\s*)(.*?)\s*$/; +return ($rest =~ /^:/)? "driver" : "option"; +} + + + + +################################################## +# Add transport setting to a director # +################################################## + +# This function adds a transport setting to an aliasfile or forwardfile +# director if a global setting exists and a local one does not. If neither +# exist, it adds file/pipe/reply, but not the directory ones. + +sub add_transport{ +my($option) = @_; + +my($key) = "$prefix$driver.${option}_transport"; +if (!exists $o{$key}) + { + if (exists $o{"address_${option}_transport"}) + { + print STDOUT "# >> Option added by convert4r3\n"; + printf STDOUT "${i1}${option}_transport = %s\n", + $o{"address_${option}_transport"}; + printf STDERR + "\n%03d ${option}_transport added to $driver director.\n", + ++$count; + } + else + { + if ($option eq "pipe" || $option eq "file" || $option eq "reply") + { + print STDOUT "# >> Option added by convert4r3\n"; + printf STDOUT "${i1}${option}_transport = address_${option}\n"; + printf STDERR + "\n%03d ${option}_transport added to $driver director.\n", + ++$count; + } + } + } +} + + + + +################################################## +# Negate a list of things # +################################################## + +sub negate { +my($list) = $_[0]; + +return $list if ! defined $list; + +($list) = $list =~ /^"?(.*?)"?\s*$/s; + +# Under Perl 5.005 we can split very nicely at colons, ignoring double +# colons, like this: +# +# @split = split /\s*(?%%%%%%%%> Option rewritten by convert4r3\n"; +print STDOUT "${i1}$name = \""; + +if (defined $reject_except) + { + print STDOUT "$reject_except"; + $join = " : \\\n "; + $last_was_negated = ($_[4] ne "+"); + } +if (defined $reject) + { + print STDOUT "$join$reject"; + $join = " : \\\n "; + $last_was_negated = ($_[4] eq "+"); + } +if (defined $accept) + { + print STDOUT "$join$accept"; + $last_was_negated = ($_[4] ne "+"); + $join = " : \\\n "; + } + +print STDOUT "$join*" if $last_was_negated; + +print STDOUT "\"\n"; + +my($driver_name); +my($driver_type) = ""; + +if ($_[0] =~ /^(d|r|t)\.([^.]+)\./ || + $_[1] =~ /^(d|r|t)\.([^.]+)\./ || + $_[2] =~ /^(d|r|t)\.([^.]+)\./) + { + $driver_type = ($1 eq 'd')? "director" : ($1 eq 'r')? "router" : "transport"; + $driver_name = $2; + } + +my($x) = ($driver_type ne "")? " in \"$driver_name\" $driver_type" : ""; + +my($l0) = &base($_[0]); +my($l1) = &base($_[1]); +my($l2) = &base($_[2]); + + +if ($l2 eq "") + { + if ($l0 eq "") + { + printf STDERR "\n%03d $l1 converted to $name$x.\n", ++$count; + } + else + { + printf STDERR "\n%03d $l0 and $l1\n amalgamated into $name$x.\n", + ++$count; + } + } +else + { + if ($l1 eq "") + { + printf STDERR "\n%03d $l0 and $l2\n amalgamated into $name$x.\n", + ++$count; + } + else + { + printf STDERR "\n%03d $l0, $l1 and $l2\n amalgamated into " . + "$name$x.\n", ++$count; + } + } +} + + + + +################################################## +# Join two lists, if they exist # +################################################## + +sub pair{ +my($l1) = $o{"$_[0]"}; +my($l2) = $o{"$_[1]"}; + +return $l2 if (!defined $l1); +return $l1 if (!defined $l2); + +($l1) = $l1 =~ /^"?(.*?)"?\s*$/s; +($l2) = $l2 =~ /^"?(.*?)"?\s*$/s; + +return "$l1 : $l2"; +} + + + + +################################################## +# Amalgamate accept/reject/reject_except pairs # +################################################## + +# This is like amalgamate, but it combines pairs of arguments, and +# doesn't output commentary (easier to write a generic one for the few +# cases). + +sub amalgamatepairs { +my($accept) = &pair($_[0], $_[1]); +my($reject) = &pair($_[2], $_[3]); +my($reject_except) = &pair($_[4], $_[5]); +my($last_was_negated) = 0; +my($join) = ""; + +if ($_[7] eq "+") + { + ($accept) = $accept =~ /^"?(.*?)"?\s*$/s if defined $accept; + $reject = &negate($reject) if defined $reject; + ($reject_except) = $reject_except =~ /^"?(.*?)"?\s*$/s if defined $reject_except; + } +else + { + $accept = &negate($accept) if defined $accept; + ($reject) = $reject =~ /^"?(.*?)"?$/s if defined $reject; + $reject_except = &negate($reject_except) if defined $reject_except; + } + +print STDOUT "# >> Option rewritten by convert4r3\n"; +print STDOUT "${i1}$_[6] = \""; + +if (defined $reject_except) + { + print STDOUT "$reject_except"; + $join = " : \\\n "; + $last_was_negated = ($_[7] ne "+"); + } +if (defined $reject) + { + print STDOUT "$join$reject"; + $join = " : \\\n "; + $last_was_negated = ($_[7] eq "+"); + } +if (defined $accept) + { + print STDOUT "$join$accept"; + $last_was_negated = ($_[7] ne "+"); + $join = " : \\\n "; + } + +print STDOUT "$join*" if $last_was_negated; +print STDOUT "\"\n"; +} + + + +################################################## +# Amalgamate boolean and exception list(s) # +################################################## + +sub amalgboolandlist { +my($name,$bool,$e1,$e2) = @_; + +print STDOUT "# >> Option rewritten by convert4r3\n"; +if ($bool eq "false") + { + printf STDOUT "$i1$name =\n"; + } +else + { + printf STDOUT "$i1$name = "; + my($n1) = &negate($o{$e1}); + my($n2) = &negate($o{$e2}); + if (!defined $n1 && !defined $n2) + { + print STDOUT "*\n"; + } + elsif (!defined $n1) + { + print STDOUT "\"$n2 : \\\n *\"\n"; + } + elsif (!defined $n2) + { + print STDOUT "\"$n1 : \\\n *\"\n"; + } + else + { + print STDOUT "\"$n1 : \\\n $n2 : \\\n *\"\n"; + } + } +} + + + +################################################## +# Convert mask format # +################################################## + +# This function converts an address and mask in old-fashioned dotted-quad +# format into an address plus a new format mask. + +@byte_list = (0, 128, 192, 224, 240, 248, 252, 254, 255); + +sub mask { +my($address,$mask) = @_; +my($length) = 0; +my($i, $j); + +my(@bytes) = split /\./, $mask; + +for ($i = 0; $i < 4; $i++) + { + for ($j = 0; $j <= 8; $j++) + { + if ($bytes[$i] == $byte_list[$j]) + { + $length += $j; + if ($j != 8) + { + for ($i++; $i < 4; $i++) + { + $j = 9 if ($bytes[$i] != 0); + } + } + last; + } + } + + if ($j > 8) + { + print STDERR "*** IP mask $mask cannot be converted to /n format. ***\n"; + return "$address/$mask"; + } + } + +if (!defined $masks{$mask}) + { + printf STDERR "\n%03d IP address mask $mask converted to /$length\n", + ++$count, $mask, $length; + $masks{$mask} = 1; + } + +return sprintf "$address/%d", $length; +} + + + + + +################################################## +# Main program # +################################################## + +print STDERR "Exim pre-release 3.00 configuration file converter.\n"; + +$count = 0; +$seen_helo_accept_junk = 0; +$seen_hold_domains = 0; +$seen_receiver_unqualified = 0; +$seen_receiver_verify_except = 0; +$seen_receiver_verify_senders = 0; +$seen_rfc1413_except = 0; +$seen_sender_accept = 0; +$seen_sender_accept_recipients = 0; +$seen_sender_host_accept = 0; +$seen_sender_host_accept_recipients = 0; +$seen_sender_host_accept_relay = 0; +$seen_sender_unqualified = 0; +$seen_sender_verify_except_hosts = 0; +$seen_smtp_etrn = 0; +$seen_smtp_expn = 0; +$seen_smtp_reserve = 0; +$semicomma = 0; + +# Read the entire file into an array + +chomp(@c = ); + +# First, go through the input and covert any net masks in the old dotted-quad +# style into the new /n style. + +for ($i = 0; $i < scalar(@c); $i++) + { + $c[$i] =~ + s"((?:\d{1,3}\.){3}\d{1,3})/((?:\d{1,3}\.){3}\d{1,3})"&mask($1,$2)"eg; + } + +# We now make two more passes over the input. In the first pass, we place all +# the option values into an associative array. Main options are keyed by their +# names; options for drivers are keyed by a driver type letter, the driver +# name, and the option name, dot-separated. In the second pass we modify +# the options if necessary, and write the output file. + +for ($pass = 1; $pass < 3; $pass++) + { + $prefix = ""; + $driver = ""; + $last_was_blank = 0; + + for ($i = 0; $i < scalar(@c); $i++) + { + # Everything after the router section is just copied in pass 2 and + # ignored in pass 1. + + if ($prefix eq "end") + { + print STDOUT "$c[$i]\n" if $pass == 2; + next; + } + + # Analyze the line + + $type = &checkline($c[$i]); + + # Skip comments in pass 1; copy in pass 2 + + if ($type eq "comment") + { + $last_was_blank = ($c[$i] =~ /^\s*$/)? 1 : 0; + print STDOUT "$c[$i]\n" if $pass == 2; + next; + } + + # Skip/copy macro definitions, but must handle continuations + + if ($type eq "macro") + { + print STDOUT "$c[$i]\n" if $pass == 2; + while ($c[$i] =~ /\\\s*$/) + { + $i++; + print STDOUT "$c[$i]\n" if $pass == 2; + } + $last_was_blank = 0; + next; + } + + # Handle end of section + + if ($type eq "end") + { + $prefix = "end"if $prefix eq "r."; + $prefix = "r." if $prefix eq "d."; + $prefix = "d." if $prefix eq "t."; + $prefix = "t." if $prefix eq ""; + print STDOUT "$c[$i]\n" if $pass == 2; + $last_was_blank = 0; + next; + } + + # Handle start of a new driver + + if ($type eq "driver") + { + $driver = $name; + print STDOUT "$c[$i]\n" if $pass == 2; + $last_was_blank = 0; + $seen_domains = 0; + $seen_local_parts = 0; + $seen_senders = 0; + $seen_mx_domains = 0; + $seen_serialize = 0; + next; + } + + # Handle definition of an option + + if ($type eq "option") + { + # Handle continued strings + + if ($rest =~ /^=\s*".*\\$/) + { + for (;;) + { + $rest .= "\n$c[++$i]"; + last unless $c[$i] =~ /(\\\s*$|^\s*#)/; + } + } + + # Remove any terminating commas and semicolons in pass 2 + + if ($pass == 2 && $rest =~ /[;,]\s*$/) + { + $rest =~ s/\s*[;,]\s*$//; + if (!$semicomma) + { + printf STDERR + "\n%03d Terminating semicolons and commas removed from driver " . + "options.\n", ++$count; + $semicomma = 1; + } + } + + # Convert all booleans to "x = true/false" format, but save the + # original so that it can be reproduced unchanged for options that + # are not of interest. + + $origname = $name; + $origrest = $rest; + + if ($name =~ /^not?_(.*)/) + { + $name = $1; + $rest = "= false"; + } + elsif ($rest !~ /^=/) + { + $rest = "= true"; + } + + # Set up the associative array key, and get rid of the = on the data + + $key = ($prefix eq "")? "$name" : "$prefix$driver.$name"; + ($rest) = $rest =~ /^=\s*(.*)/s; + + # Create the associative array of values in pass 1 + + if ($pass == 1) + { + $o{$key} = $rest; + } + + # In pass 2, test for interesting options and do the necessary; copy + # all the rest. + + else + { + ########## Global configuration ########## + + # These global options are abolished + + if ($name eq "address_directory_transport" || + $name eq "address_directory2_transport" || + $name eq "address_file_transport" || + $name eq "address_pipe_transport" || + $name eq "address_reply_transport") + { + ($n2) = $name =~ /^address_(.*)/; + printf STDERR "\n%03d $name option deleted.\n", ++$count; + printf STDERR " $n2 will be added to appropriate directors.\n"; + $i = &skipblanks($i); + next; + } + + # This debugging option is abolished + + elsif ($name eq "sender_verify_log_details") + { + printf STDERR "\n%03d $name option deleted.\n", ++$count; + printf STDERR " (Little used facility abolished.)\n"; + } + + # This option has been renamed + + elsif ($name eq "check_dns_names") + { + $origname =~ s/check_dns/dns_check/; + print STDOUT "# >> Option rewritten by convert4r3\n"; + print STDOUT "$i1$origname$i2$origrest\n"; + printf STDERR "\n%03d check_dns_names renamed as dns_check_names.\n", + ++$count; + } + + # helo_accept_junk_nets is abolished + + elsif ($name eq "helo_accept_junk_nets" || + $name eq "helo_accept_junk_hosts") + { + if (!$seen_helo_accept_junk) + { + &amalgamate("helo_accept_junk_nets", "", + "helo_accept_junk_hosts", "helo_accept_junk_hosts", "+"); + $seen_helo_accept_junk = 1; + } + else + { + $i = &skipblanks($i); + next; + } + } + + # helo_verify_except_{hosts,nets} are abolished, and helo_verify + # is now a host list instead of a boolean. + + elsif ($name eq "helo_verify") + { + &amalgboolandlist("helo_verify", $rest, "helo_verify_except_hosts", + "helo_verify_except_nets"); + printf STDERR "\n%03d helo_verify converted to host list.\n", + ++$count; + } + elsif ($name eq "helo_verify_except_hosts" || + $name eq "helo_verify_except_nets") + { + $i = &skipblanks($i); + next; + } + + # helo_verify_nets was an old synonym for host_lookup_nets; only + # one of them will be encountered. Change to a new name. + + elsif ($name eq "helo_verify_nets" || + $name eq "host_lookup_nets") + { + print STDOUT "# >> Option rewritten by convert4r3\n"; + print STDOUT "${i1}host_lookup$i2$origrest\n"; + printf STDERR "\n%03d $name renamed as host_lookup.\n", ++$count; + } + + # hold_domains_except is abolished; add as negated items to + # hold_domains. + + elsif ($name eq "hold_domains_except" || + $name eq "hold_domains") + { + if ($seen_hold_domains) # If already done with these + { # omit, and following blanks. + $i = &skipblanks($i); + next; + } + $seen_hold_domains = 1; + + if (exists $o{"hold_domains_except"}) + { + &amalgamate("hold_domains", "hold_domains_except", "", + "hold_domains", "+"); + } + else + { + print STDOUT "$i1$origname$i2$origrest\n"; + } + } + + # ignore_fromline_nets is renamed as ignore_fromline_hosts + + elsif ($name eq "ignore_fromline_nets") + { + $origname =~ s/_nets/_hosts/; + print STDOUT "# >> Option rewritten by convert4r3\n"; + print STDOUT "$i1$origname$i2$origrest\n"; + printf STDERR + "\n%03d ignore_fromline_nets renamed as ignore_fromline_hosts.\n", + ++$count; + } + + # Output a warning for message filters with no transports set + + elsif ($name eq "message_filter") + { + print STDOUT "$i1$origname$i2$origrest\n"; + + if (!exists $o{"message_filter_directory_transport"} && + !exists $o{"message_filter_directory2_transport"} && + !exists $o{"message_filter_file_transport"} && + !exists $o{"message_filter_pipe_transport"} && + !exists $o{"message_filter_reply_transport"}) + { + printf STDERR + "\n%03d message_filter is set, but no message_filter transports " + . "are defined.\n" + . " If your filter generates file or pipe deliveries, or " + . "auto-replies,\n" + . " you will need to define " + . "message_filter_{file,pipe,reply}_transport\n" + . " options, as required.\n", ++$count; + } + } + + # queue_remote_except is abolished, and queue_remote is replaced by + # queue_remote_domains, which is a host list. + + elsif ($name eq "queue_remote") + { + &amalgboolandlist("queue_remote_domains", $rest, + "queue_remote_except", ""); + printf STDERR + "\n%03d queue_remote converted to domain list queue_remote_domains.\n", + ++$count; + } + elsif ($name eq "queue_remote_except") + { + $i = &skipblanks($i); + next; + } + + # queue_smtp_except is abolished, and queue_smtp is replaced by + # queue_smtp_domains, which is a host list. + + elsif ($name eq "queue_smtp") + { + &amalgboolandlist("queue_smtp_domains", $rest, + "queue_smtp_except", ""); + printf STDERR + "\n%03d queue_smtp converted to domain list queue_smtp_domains.\n", + ++$count; + } + elsif ($name eq "queue_smtp_except") + { + $i = &skipblanks($i); + next; + } + + # rbl_except_nets is replaced by rbl_hosts + + elsif ($name eq "rbl_except_nets") + { + &amalgamate("", "rbl_except_nets", "", "rbl_hosts", "+"); + } + + # receiver_unqualified_nets is abolished + + elsif ($name eq "receiver_unqualified_nets" || + $name eq "receiver_unqualified_hosts") + { + if (!$seen_receiver_unqualified) + { + &amalgamate("receiver_unqualified_nets", "", + "receiver_unqualified_hosts", "receiver_unqualified_hosts", "+"); + $seen_receiver_unqualified = 1; + } + else + { + $i = &skipblanks($i); + next; + } + } + + # receiver_verify_except_{hosts,nets} are replaced by + # receiver_verify_hosts. + + elsif ($name eq "receiver_verify_except_hosts" || + $name eq "receiver_verify_except_nets") + { + if (!$seen_receiver_verify_except) + { + &amalgboolandlist("receiver_verify_hosts", "true", + "receiver_verify_except_hosts", "receiver_verify_except_nets"); + printf STDERR + "\n%03d receiver_verify_except_{hosts,nets} converted to " . + "receiver_verify_hosts.\n", + ++$count; + $seen_receiver_verify_except = 1; + } + else + { + $i = &skipblanks($i); + next; + } + } + + # receiver_verify_senders_except is abolished + + elsif ($name eq "receiver_verify_senders" || + $name eq "receiver_verify_senders_except") + { + if (defined $o{"receiver_verify_senders_except"}) + { + if (!$seen_receiver_verify_senders) + { + &amalgamate("receiver_verify_senders", + "receiver_verify_senders_except", "", + "receiver_verify_senders", "+"); + $seen_receiver_verify_senders = 1; + } + else + { + $i = &skipblanks($i); + next; + } + } + else + { + print STDOUT "$i1$origname$i2$origrest\n"; + } + } + + # rfc1413_except_{hosts,nets} are replaced by rfc1413_hosts. + + elsif ($name eq "rfc1413_except_hosts" || + $name eq "rfc1413_except_nets") + { + if (!$seen_rfc1413_except) + { + &amalgboolandlist("rfc1413_hosts", "true", + "rfc1413_except_hosts", "rfc1413_except_nets"); + printf STDERR + "\n%03d rfc1413_except_{hosts,nets} converted to rfc1413_hosts.\n", + ++$count; + $seen_rfc1413_except = 1; + } + else + { + $i = &skipblanks($i); + next; + } + } + + # sender_accept and sender_reject_except are abolished + + elsif ($name eq "sender_accept" || + $name eq "sender_reject") + { + if (!$seen_sender_accept) + { + &amalgamate("sender_accept", "sender_reject", + "sender_reject_except", "sender_reject", "-"); + $seen_sender_accept = 1; + } + else + { + $i = &skipblanks($i); + next; + } + } + + # sender_accept_recipients is also abolished; sender_reject_except + # also used to apply to this, so we include it here as well. + + elsif ($name eq "sender_accept_recipients" || + $name eq "sender_reject_recipients") + { + if (!$seen_sender_accept_recipients) + { + &amalgamate("sender_accept_recipients", "sender_reject_recipients", + "sender_reject_except", "sender_reject_recipients", "-"); + $seen_sender_accept_recipients = 1; + } + else + { + $i = &skipblanks($i); + next; + } + } + + # sender_reject_except must be removed + + elsif ($name eq "sender_reject_except") + { + $i = &skipblanks($i); + next; + } + + # sender_{host,net}_{accept,reject}[_except] all collapse into + # host_reject. + + elsif ($name eq "sender_host_accept" || + $name eq "sender_net_accept" || + $name eq "sender_host_reject" || + $name eq "sender_net_reject") + { + if (!$seen_sender_host_accept) + { + &amalgamatepairs("sender_host_accept", "sender_net_accept", + "sender_host_reject", "sender_net_reject", + "sender_host_reject_except", "sender_net_reject_except", + "host_reject", "-"); + printf STDERR "\n%03d sender_{host,net}_{accept,reject} and " . + "sender_{host_net}_reject_except\n" . + " amalgamated into host_reject.\n", ++$count; + $seen_sender_host_accept = 1; + } + else + { + $i = &skipblanks($i); + next; + } + } + + # sender_{host,net}_{accept,reject}_recipients all collapse into + # host_reject_recipients. + + elsif ($name eq "sender_host_accept_recipients" || + $name eq "sender_net_accept_recipients" || + $name eq "sender_host_reject_recipients" || + $name eq "sender_net_reject_recipients") + { + if (!$seen_sender_host_accept_recipients) + { + &amalgamatepairs("sender_host_accept_recipients", + "sender_net_accept_recipients", + "sender_host_reject_recipients", + "sender_net_reject_recipients", + "sender_host_reject_except", "sender_net_reject_except", + "host_reject_recipients", "-"); + printf STDERR "\n%03d sender_{host,net}_{accept,reject}_recipients" + . "\n and sender_{host_net}_reject_except" + . "\n amalgamated into host_reject_recipients.\n", ++$count; + $seen_sender_host_accept_recipients = 1; + } + else + { + $i = &skipblanks($i); + next; + } + } + + # sender_{host,net}_reject_except must be removed + + elsif ($name eq "sender_host_reject_except" || + $name eq "sender_net_reject_except") + { + $i = &skipblanks($i); + next; + } + + # sender_{host,net}_{accept,reject}_relay all collapse into + # host_accept_relay. + + elsif ($name eq "sender_host_accept_relay" || + $name eq "sender_net_accept_relay" || + $name eq "sender_host_reject_relay" || + $name eq "sender_net_reject_relay") + { + if (!$seen_sender_host_accept_relay) + { + &amalgamatepairs("sender_host_accept_relay", + "sender_net_accept_relay", + "sender_host_reject_relay", + "sender_net_reject_relay", + "sender_host_reject_relay_except", + "sender_net_reject_relay_except", + "host_accept_relay", "+"); + printf STDERR "\n%03d sender_{host,net}_{accept,reject}_relay" + . "\n and sender_{host_net}_reject_relay_except" + . "\n amalgamated into host_accept_relay.\n", ++$count; + $seen_sender_host_accept_relay = 1; + } + else + { + $i = &skipblanks($i); + next; + } + } + + # sender_{host,net}_reject_relay_except must be removed + + elsif ($name eq "sender_host_reject_relay_except" || + $name eq "sender_net_reject_relay_except") + { + $i = &skipblanks($i); + next; + } + + + # sender_unqualified_nets is abolished + + elsif ($name eq "sender_unqualified_nets" || + $name eq "sender_unqualified_hosts") + { + if (!$seen_sender_unqualified) + { + &amalgamate("sender_unqualified_nets", "", + "sender_unqualified_hosts", "sender_unqualified_hosts", "+"); + $seen_sender_unqualified = 1; + } + else + { + $i = &skipblanks($i); + next; + } + } + + # sender_verify_except_{hosts,nets} are replaced by sender_verify_hosts. + + elsif ($name eq "sender_verify_except_hosts" || + $name eq "sender_verify_except_nets") + { + if (!$seen_sender_verify_except_hosts) + { + &amalgboolandlist("sender_verify_hosts", "true", + "sender_verify_except_hosts", "sender_verify_except_nets"); + printf STDERR + "\n%03d sender_verify_except_{hosts,nets} converted to " . + "sender_verify_hosts.\n", + ++$count; + $seen_sender_verify_except_hosts = 1; + } + else + { + $i = &skipblanks($i); + next; + } + } + + # smtp_etrn_nets is abolished + + elsif ($name eq "smtp_etrn_nets" || + $name eq "smtp_etrn_hosts") + { + if (!$seen_smtp_etrn) + { + &amalgamate("smtp_etrn_nets", "", + "smtp_etrn_hosts", "smtp_etrn_hosts", "+"); + $seen_smtp_etrn = 1; + } + else + { + $i = &skipblanks($i); + next; + } + } + + # smtp_expn_nets is abolished + + elsif ($name eq "smtp_expn_nets" || + $name eq "smtp_expn_hosts") + { + if (!$seen_smtp_expn) + { + &amalgamate("smtp_expn_nets", "", + "smtp_expn_hosts", "smtp_expn_hosts", "+"); + $seen_smtp_expn = 1; + } + else + { + $i = &skipblanks($i); + next; + } + } + + # This option has been renamed + + elsif ($name eq "smtp_log_connections") + { + $origname =~ s/smtp_log/log_smtp/; + print STDOUT "# >> Option rewritten by convert4r3\n"; + print STDOUT "$i1$origname$i2$origrest\n"; + printf STDERR "\n%03d smtp_log_connections renamed as " . + "log_smtp_connections.\n", + ++$count; + } + + # smtp_reserve_nets is abolished + + elsif ($name eq "smtp_reserve_nets" || + $name eq "smtp_reserve_hosts") + { + if (!$seen_smtp_reserve) + { + &amalgamate("smtp_reserve_nets", "", + "smtp_reserve_hosts", "smtp_reserve_hosts", "+"); + $seen_smtp_reserve = 1; + } + else + { + $i = &skipblanks($i); + next; + } + } + + ########### Driver configurations ########## + + # For aliasfile and forwardfile directors, add file, pipe, and + # reply transports - copying from the globals if they are set. + + elsif ($name eq "driver") + { + $driver_type = $rest; + print STDOUT "$i1$origname$i2$origrest\n"; + if ($rest eq "aliasfile" || $rest eq "forwardfile") + { + &add_transport("directory"); + &add_transport("directory2"); + &add_transport("file"); + &add_transport("pipe"); + &add_transport("reply") if $rest eq "forwardfile"; + } + } + + # except_domains is abolished; add as negated items to domains. + + elsif ($name eq "except_domains" || + $name eq "domains") + { + if ($seen_domains) # If already done with these + { # omit, and following blanks. + $i = &skipblanks($i); + next; + } + $seen_domains = 1; + + if (exists $o{"$prefix$driver.except_domains"}) + { + &amalgamate("$prefix$driver.domains", + "$prefix$driver.except_domains", "", + "domains", "+"); + } + else + { + print STDOUT "$i1$origname$i2$origrest\n"; + } + } + + # except_local_parts is abolished; add as negated items to + # local_parts. + + elsif ($name eq "except_local_parts" || + $name eq "local_parts") + { + if ($seen_local_parts) # If already done with these + { # omit, and following blanks. + $i = &skipblanks($i); + next; + } + $seen_local_parts = 1; + + if (exists $o{"$prefix$driver.except_local_parts"}) + { + &amalgamate("$prefix$driver.local_parts", + "$prefix$driver.except_local_parts", "", + "local_parts", "+"); + } + else + { + print STDOUT "$i1$origname$i2$origrest\n"; + } + } + + # except_senders is abolished; add as negated items to senders + + elsif ($name eq "except_senders" || + $name eq "senders") + { + if ($seen_senders) # If already done with these + { # omit, and following blanks. + $i = &skipblanks($i); + next; + } + $seen_senders = 1; + + if (exists $o{"$prefix$driver.except_senders"}) + { + &amalgamate("$prefix$driver.senders", + "$prefix$driver.except_senders", "", + "senders", "+"); + } + else + { + print STDOUT "$i1$origname$i2$origrest\n"; + } + } + + # This option has been renamed + + elsif ($name eq "directory" && $driver_type eq "aliasfile") + { + $origname =~ s/directory/home_directory/; + print STDOUT "# >> Option rewritten by convert4r3\n"; + print STDOUT "$i1$origname$i2$origrest\n"; + printf STDERR "\n%03d directory renamed as " . + "home_directory in \"$driver\" director.\n", + ++$count; + } + + # This option has been renamed + + elsif ($name eq "directory" && $driver_type eq "forwardfile") + { + $origname =~ s/directory/file_directory/; + print STDOUT "# >> Option rewritten by convert4r3\n"; + print STDOUT "$i1$origname$i2$origrest\n"; + printf STDERR "\n%03d directory renamed as " . + "file_directory in \"$driver\" director.\n", + ++$count; + } + + # This option has been renamed + + elsif ($name eq "forbid_filter_log" && $driver_type eq "forwardfile") + { + $origname =~ s/log/logwrite/; + print STDOUT "# >> Option rewritten by convert4r3\n"; + print STDOUT "$i1$origname$i2$origrest\n"; + printf STDERR "\n%03d forbid_filter_log renamed as " . + "forbid_filter_logwrite in \"$driver\" director.\n", + ++$count; + } + + # This option has been renamed + + elsif ($name eq "directory" && $driver_type eq "localuser") + { + $origname =~ s/directory/match_directory/; + print STDOUT "# >> Option rewritten by convert4r3\n"; + print STDOUT "$i1$origname$i2$origrest\n"; + printf STDERR "\n%03d directory renamed as " . + "match_directory in \"$driver\" director.\n", + ++$count; + } + + # mx_domains_except (and old synonym non_mx_domains) are abolished + # (both lookuphost router and smtp transport) + + elsif ($name eq "mx_domains" || + $name eq "mx_domains_except" || + $name eq "non_mx_domains") + { + if ($seen_mx_domains) # If already done with these + { # omit, and following blanks. + $i = &skipblanks($i); + next; + } + $seen_mx_domains = 1; + + if (exists $o{"$prefix$driver.mx_domains_except"} || + exists $o{"$prefix$driver.non_mx_domains"}) + { + $o{"$prefix$driver.mx_domains_except"} = + &pair("$prefix$driver.mx_domains_except", + "$prefix$driver.non_mx_domains"); + + &amalgamate("$prefix$driver.mx_domains", + "$prefix$driver.mx_domains_except", "", + "mx_domains", "+"); + } + else + { + print STDOUT "$i1$origname$i2$origrest\n"; + } + } + + # This option has been renamed + + elsif ($name eq "directory" && $driver_type eq "pipe") + { + $origname =~ s/directory/home_directory/; + print STDOUT "# >> Option rewritten by convert4r3\n"; + print STDOUT "$i1$origname$i2$origrest\n"; + printf STDERR "\n%03d directory renamed as " . + "home_directory in \"$driver\" director.\n", + ++$count; + } + + # serialize_nets is abolished + + elsif ($name eq "serialize_nets" || + $name eq "serialize_hosts") + { + if (!$seen_serialize) + { + &amalgamate("$prefix$driver.serialize_nets", "", + "$prefix$driver.serialize_hosts", "serialize_hosts", "+"); + $seen_serialize = 1; + } + else + { + $i = &skipblanks($i); + next; + } + } + + + # Option not of interest; reproduce verbatim + + else + { + print STDOUT "$i1$origname$i2$origrest\n"; + } + + + $last_was_blank = 0; + } + } + } + + } + +# Debugging: show the associative array +# foreach $key (sort keys %o) { print STDERR "$key = $o{$key}\n"; } + +print STDERR "\nEnd of configuration file conversion.\n"; +print STDERR "\n*******************************************************\n"; +print STDERR "***** Please review the generated file carefully. *****\n"; +print STDERR "*******************************************************\n\n"; + +print STDERR "In particular:\n\n"; + +print STDERR "(1) If you use regular expressions in any options that have\n"; +print STDERR " been rewritten by this script, they might have been put\n"; +print STDERR " inside quotes, when then were not previously quoted. This\n"; +print STDERR " means that any backslashes in them must now be escaped.\n\n"; + +print STDERR "(2) If your configuration refers to any external files that\n"; +print STDERR " contain lists of network addresses, check that the masks\n"; +print STDERR " are specified as single numbers, e.g. /24 and NOT as dotted\n"; +print STDERR " quads (e.g. 255.255.255.0) because Exim release 3.00 does\n"; +print STDERR " not recognize the dotted quad form.\n\n"; + +print STDERR "(3) If your configuration uses macros for lists of domains or\n"; +print STDERR " hosts or addresses, check to see if any of the references\n"; +print STDERR " have been negated. If so, you will have to rework things,\n"; +print STDERR " because the negation will apply only to the first item in\n"; +print STDERR " the macro-generated list.\n\n"; + +print STDERR "(4) If you do not generate deliveries to pipes, files, or\n"; +print STDERR " auto-replies in your aliasfile and forwardfile directors,\n"; +print STDERR " you can remove the added transport settings.\n\n"; + +# End of convert4r3 diff --git a/src/src/convert4r4.src b/src/src/convert4r4.src new file mode 100755 index 000000000..09b653c73 --- /dev/null +++ b/src/src/convert4r4.src @@ -0,0 +1,2514 @@ +#! PERL_COMMAND -w +# $Cambridge: exim/src/src/convert4r4.src,v 1.1 2004/10/07 10:39:01 ph10 Exp $ + +# This is a Perl script that reads an Exim run-time configuration file for +# Exim 3. It makes what changes it can for Exim 4, and also output commentary +# on what it has done, and on things it cannot do. + +# It is assumed that the input is a valid Exim 3 configuration file. + + +# These are lists of main options which are abolished in Exim 4. +# The first contains options that are used to construct new options. + +@skipped_options = ( +"auth_hosts", +"auth_over_tls_hosts", +"errors_address", +"headers_check_syntax", +"headers_checks_fail", +"headers_sender_verify", +"headers_sender_verify_errmsg", +"host_accept_relay", +"host_auth_accept_relay", +"host_reject_recipients", +"local_domains", +"local_domains_include_host", +"local_domains_include_host_literals", +"log_all_parents", +"log_arguments", +"log_incoming_port", +"log_interface", +"log_level", +"log_received_sender", +"log_received_recipients", +"log_rewrites", +"log_sender_on_delivery", +"log_smtp_confirmation", +"log_smtp_connections", +"log_smtp_syntax_errors", +"log_subject", +"log_queue_run_level", +"rbl_domains", +"rbl_hosts", +"rbl_reject_recipients", +"receiver_verify", +"receiver_verify_addresses", +"receiver_verify_hosts", +"receiver_verify_senders", +"recipients_reject_except", +"recipients_reject_except_senders", +"relay_domains", +"relay_domains_include_local_mx", +"sender_address_relay", +"sender_address_relay_hosts", +"sender_reject_recipients", +"sender_verify", +"sender_verify_hosts_callback", +"sender_verify_callback_domains", +"sender_verify_callback_timeout", +"sender_verify_hosts", +"smtp_etrn_hosts", +"smtp_expn_hosts", +"smtp_verify", +"tls_host_accept_relay", +"tls_hosts", +"tls_log_cipher", +"tls_log_peerdn", +"tls_verify_ciphers" +); + +# The second contains options that are completely abolished and have +# no equivalent. + +@abolished_options = ( +"always_bcc", +"debug_level", +"helo_strict_syntax", +"kill_ip_options", +"log_ip_options", +"log_refused_recipients", +"message_size_limit_count_recipients", +"rbl_log_headers", +"rbl_log_rcpt_count", +"receiver_try_verify", +"refuse_ip_options", +"relay_match_host_or_sender", +"sender_try_verify", +"sender_verify_batch", +"sender_verify_fixup", +"sender_verify_reject", +"sender_verify_max_retry_rate", +); + +# This is a list of options that are not otherwise handled, but which +# contain domain or host lists that have to be processed so that any +# regular expressions are marked "not for expansion". + +@list_options = ( +"dns_again_means_nonexist", +"hold_domains", +"hosts_treat_as_local", +"percent_hack_domains", +"queue_smtp_domains", +"helo_accept_junk_hosts", +"host_lookup", +"ignore_fromline_hosts", +"rfc1413_hosts", +"sender_unqualified_hosts", +"smtp_reserve_hosts", +"tls_advertise_hosts", +"tls_verify_hosts", +); + + + +################################################## +# Output problem rubric once # +################################################## + +sub rubric { +return if $rubric_output; +$rubric_output = 1; +print STDERR "\n" . +"** The following comments describe problems that have been encountered\n" . +" while converting an Exim 3 runtime file for Exim 4. More detail can\n" . +" be found in the file doc/Exim4.upgrade.\n"; +} + + +################################################## +# Analyse one line # +################################################## + +sub checkline{ +my($line) = $_[0]; + +return "comment" if $line =~ /^\s*(#|$)/; +return "end" if $line =~ /^\s*end\s*$/i; + +# Macros are recognized only in the first section of the file. + +return "macro" if $prefix eq "" && $line =~ /^\s*[A-Z]/; + +# In retry and rewrite sections, the type is always "other" + +return "other" if $prefix eq "=retry" || $prefix eq "=rewrite"; + +# Pick out the name at the start and the rest of the line (into global +# variables) and return whether the start of a driver or not. + +($hide,$name,$rest) = $line =~ /^\s*(hide\s+|)([a-z0-9_]+)\s*(.*?)\s*$/; + +# If $rest begins with a colon, this is a driver name + +return "driver" if $rest =~ /^:/; + +# If $rest begins with an = the value of the option is given explicitly; +# remove the = from the start. Turn "yes"/"no" into "true"/"false". + +if ($rest =~ /^=/) + { + $rest =~ s/^=\s*//; + $rest = "true" if $rest eq "yes"; + $rest = "false" if $rest eq "no"; + } + +# Otherwise we have a boolean option. Set up a "true"/"false" value. + +else + { + if ($name =~ /^not?_/) # Recognize "no_" or "not_" + { + $rest = "false"; + $name =~ s/^not?_//; + } + else + { + $rest = "true"; + } + } + +return "option"; +} + + + +################################################## +# Negate a list of things # +################################################## + +# Can be tricky, because there may be comment lines in the list. +# Also, lists may have different delimiters. + +sub negate { +my($list) = $_[0]; +my($delim) = ":"; +my($leadin) = ""; + +return $list if ! defined $list; + +($list) = $list =~ /^"?(.*?)"?\s*$/s; # Remove surrounding quotes +$list =~ s/\\\s*\n\s*//g; # Remove continuation markers + +if ($list =~ /^(\s*<(\S)\s*)(.*)/s) + { + $leadin = $1; + $delim = $2; + $list = $3; + } + +$list =~ s/^\s+//; +$list =~ s/\Q$delim$delim/>%%%%%%%%%%%%%%%%%%%%%%%%); +$clen = scalar @c; + +# Remove the standard comment that appears at the end of the default + +if ($clen > 0 && $c[$clen-1] =~ /^#\s*End of Exim configuration file\s*/i) + { + pop @c; + $clen--; + } + +# The first pass over the input fishes out all the options settings in the +# main, transport, director, and router sections, and places their values in +# associative arrays. It also notes the starting position of all the sections. + +$prefix = ""; +%main = (); +$hash = \%main; + +for ($i = 0; $i < $clen; $i++) + { + # Change references to +allow_unknown and +warn_unknown into +include_unknown + + if ($c[$i] =~ /\+(?:allow|warn)_unknown/) + { + if (!$unk_output) + { + &rubric(); + print STDERR "\n" . +"** You have used '+allow_unknown' or '+warn_unknown' in a configuration\n" . +" option. This has been converted to '+include_unknown', but the action\n" . +" is different in Exim 4, so you should review all the relevant options.\n"; + $unk_output = 1; + } + $c[$i] =~ s/\+(?:allow|warn)_unknown/+include_unknown/g; + } + + # Any reference to $errmsg_recipient is changed to $bounce_recipient + + if ($c[$i] =~ /\$errmsg_recipient/) + { + if (!$errmsg_output) + { + &rubric(); + print STDERR "\n" . +"** References to \$errmsg_recipient have been changed to \$bounce_recipient\n"; + $errmsg_output = 1; + } + $c[$i] =~ s/\$errmsg_recipient/\$bounce_recipient/g; + } + + + # Analyse the type of line + + $type = &checkline($c[$i]); + next if $type eq "comment"; + + # Output a warning if $key is used + + if ($c[$i] =~ /\$key/ && !$key_output) + { + &rubric(); + print STDERR "\n" . +"** You have used '\$key' in a configuration option. This variable does not\n" . +" exist in Exim 4. Instead, the value you need for your lookup will be\n" . +" in one of the other variables such as '\$domain' or '\$host'. You will\n" . +" need to edit the new configuration to sort this out.\n"; + $key_output = 1; + } + + # Save macro definitions so we can output them first; must handle + # continuations. + + if ($type eq "macro") + { + $macro_output .= "$c[$i++]\n" while $c[$i] =~ /\\\s*$|^\s*#/; + $macro_output .= "$c[$i]\n"; + } + + # Handle end of section + + elsif ($type eq "end") + { + if ($prefix eq "=rewrite") + { + $prefix = "a."; + $auth_start = $i + 1; + last; + } + elsif ($prefix eq "=retry") + { + $prefix = "=rewrite"; + $rewrite_start = $i + 1; + } + elsif ($prefix eq "r.") + { + $prefix = "=retry"; + $retry_start = $i + 1; + } + elsif ($prefix eq "d.") + { + $prefix = "r."; + $router_start = $i + 1; + } + elsif ($prefix eq "t.") + { + $prefix = "d."; + $director_start = $i + 1; + } + elsif ($prefix eq "") + { + $prefix = "t."; + $transport_start = $i + 1; + } + } + + # Handle start of a new director, router or transport driver + + elsif ($type eq "driver" && $prefix !~ /^=/) + { + $hash = {}; + if (defined $driverlist{"$prefix$name"}) + { + die "*** There are two drivers with the name \"$name\"\n"; + } + $driverlist{"$prefix$name"} = $hash; + $first_director = $name if !defined $first_director && $prefix eq "d."; + } + + # Handle definition of an option; we must pull in any continuation + # strings, and save the value in the current hash. Note if the option + # is hidden. + + elsif ($type eq "option") + { + my($nextline) = ""; + + while ($i < $clen - 1 && ($rest =~ /\\\s*$/s || $nextline =~ /^\s*#/)) + { + $nextline = $c[++$i]; + $rest .= "\n$nextline"; + } + + $$hash{$name} = $rest; + $$hash{"$name-hide"} = 1 if $hide ne ""; + } + } + + +# Generate the new configuration. Start with a warning rubric. + +print STDOUT "#!!# This file is output from the convert4r4 script, which tries\n"; +print STDOUT "#!!# to convert Exim 3 configurations into Exim 4 configurations.\n"; +print STDOUT "#!!# However, it is not perfect, especially with non-simple\n"; +print STDOUT "#!!# configurations. You must check it before running it.\n"; +print STDOUT "\n\n"; + +# Output the macro definitions + +if ($macro_output ne "") + { + print STDOUT "#!!# All macro definitions have been gathered here to ensure\n"; + print STDOUT "#!!# they precede any references to them.\n\n"; + print STDOUT "$macro_output\n"; + } + +# Output some default pointers to ACLs for RCPT and DATA time. If no Exim 3 +# options that apply are set, non-restricting ACLs are generated. + +print STDOUT "#!!# These options specify the Access Control Lists (ACLs) that\n"; +print STDOUT "#!!# are used for incoming SMTP messages - after the RCPT and DATA\n"; +print STDOUT "#!!# commands, respectively.\n\n"; + +print STDOUT "acl_smtp_rcpt = check_recipient\n"; +print STDOUT "acl_smtp_data = check_message\n\n"; + +if (defined $main{"auth_over_tls_hosts"}) + { + print STDOUT "#!!# This option specifies the Access Control List (ACL) that\n"; + print STDOUT "#!!# is used after an AUTH command.\n\n"; + print STDOUT "acl_smtp_auth = check_auth\n\n"; + } + +if (&bool("smtp_verify") || + defined $main{"smtp_etrn_hosts"} || + defined $main{"smtp_expn_hosts"}) + { + print STDOUT "#!!# These options specify the Access Control Lists (ACLs) that\n"; + print STDOUT "#!!# are used to control the ETRN, EXPN, and VRFY commands.\n"; + print STDOUT "#!!# Where no ACL is defined, the command is locked out.\n\n"; + + print STDOUT "acl_smtp_etrn = check_etrn\n" if defined $main{"smtp_etrn_hosts"}; + print STDOUT "acl_smtp_expn = check_expn\n" if defined $main{"smtp_expn_hosts"}; + print STDOUT "acl_smtp_vrfy = check_vrfy\n" if &bool("smtp_verify"); + print STDOUT "\n"; + } + +# If local_domains was set, get its value; otherwise set to "@". Add into it +# appropriate magic for local_domains_include_host[_literals]. + +$local_domains = (defined $main{"local_domains"})? $main{"local_domains"} : "@"; + +$ldsep = ":"; +if ($local_domains =~ /^\s*<(.)\s*(.*)/s) + { + $ldsep = $1; + $local_domains = $2; + } + +$local_domains = "\@[] $ldsep " . $local_domains + if defined $main{"local_domains_include_host_literals"} && + $main{"local_domains_include_host_literals"} eq "true"; + +$local_domains = "\@ $ldsep " . $local_domains + if defined $main{"local_domains_include_host"} && + $main{"local_domains_include_host"} eq "true"; + +$local_domains = "<$ldsep " . $local_domains if $ldsep ne ":"; + +# Output a domain list setting for these domains, provided something is defined + +if ($local_domains !~ /^\s*$/) + { + print STDOUT "#!!# This setting defines a named domain list called\n"; + print STDOUT "#!!# local_domains, created from the old options that\n"; + print STDOUT "#!!# referred to local domains. It will be referenced\n"; + print STDOUT "#!!# later on by the syntax \"+local_domains\".\n"; + print STDOUT "#!!# Other domain and host lists may follow.\n\n"; + + printf STDOUT ("domainlist local_domains = %s\n\n", + &no_expand_regex($local_domains)); + } + +$relay_domains = (defined $main{"relay_domains"})? $main{"relay_domains"} : ""; + +$ldsep = ":"; +if ($relay_domains =~ /^\s*<(.)\s*(.*)/s) + { + $ldsep = $1; + } + +if (defined $main{"relay_domains_include_local_mx"}) + { + $relay_domains .= ($relay_domains =~ /^\s*$/)? "\@mx_any" : + " $ldsep \@mx_any"; + } + +printf STDOUT ("domainlist relay_domains = %s\n", + &no_expand_regex($relay_domains)) + if $relay_domains !~ /^\s*$/; + + +# If ignore_errmsg_errors is set, we are going to force 0s as the value +# for ignore_errmsg_errors_after, so arrange to skip any other value. + +push @skipped_options, "ignore_errmsg_errors_after" + if &bool("ignore_errmsg_errors"); + + +# If rbl_domains is set, split it up and generate six lists: +# rbl_warn_domains, rbl_warn_domains_skiprelay +# rbl_reject_domains, rbl_reject_domains_skiprelay +# rbl_accept_domains, rbl_accept_domains_skiprelay + +if (defined $main{"rbl_domains"}) + { + my($s) = &unquote($main{"rbl_domains"}); + $s =~ s/\s*\\\s*\n\s*/ /g; + my(@list) = split /\s*:\s*/, $s; + + foreach $d (@list) + { + my(@sublist) = split /\//, $d; + my($name) = shift @sublist; + my($warn) = 0; + if (defined $main{"rbl_reject_recipients"}) + { + $warn = $main{"rbl_reject_recipients"} ne "true"; + } + + foreach $o (@sublist) + { + $warn = 1 if $o eq "warn"; + $warn = 0 if $o eq "reject"; + $warn = 2 if $o eq "accept"; + $skiprelay = 1 if $o eq "skiprelay"; + } + + if ($skiprelay) + { + if ($warn == 0) + { + $rbl_reject_skiprelay .= ((defined $rbl_reject_skiprelay)? ":":"").$name; + } + elsif ($warn == 1) + { + $rbl_warn_skiprelay .= ((defined $rbl_warn_skiprelay)? ":":"").$name; + } + elsif ($warn == 2) + { + $rbl_accept_skiprelay .= ((defined $rbl_accept_skiprelay)? ":":"").$name; + } + } + else + { + if ($warn == 0) + { + $rbl_reject_domains .= ((defined $rbl_reject_domains)? ":":"").$name; + } + elsif ($warn == 1) + { + $rbl_warn_domains .= ((defined $rbl_warn_domains)? ":":"").$name; + } + elsif ($warn == 2) + { + $rbl_accept_domains .= ((defined $rbl_accept_domains)? ":":"").$name; + } + } + } + } + + +# Output host list settings + +printf STDOUT ("hostlist auth_hosts = %s\n", + &no_expand_regex($main{"auth_hosts"})) + if defined $main{"auth_hosts"}; +printf STDOUT ("hostlist rbl_hosts = %s\n", + &no_expand_regex($main{"rbl_hosts"})) + if defined $main{"rbl_hosts"}; +printf STDOUT ("hostlist relay_hosts = %s\n", + &no_expand_regex($main{"host_accept_relay"})) + if defined $main{"host_accept_relay"}; +printf STDOUT ("hostlist auth_relay_hosts = %s\n", + &no_expand_regex($main{"host_auth_accept_relay"})) + if defined $main{"host_auth_accept_relay"}; + +printf STDOUT ("hostlist auth_over_tls_hosts = %s\n", + &no_expand_regex($main{"auth_over_tls_hosts"})) + if defined $main{"auth_over_tls_hosts"}; +printf STDOUT ("hostlist tls_hosts = %s\n", + &no_expand_regex($main{"tls_hosts"})) + if defined $main{"tls_hosts"}; +printf STDOUT ("hostlist tls_relay_hosts = %s\n", + &no_expand_regex($main{"tls_host_accept_relay"})) + if defined $main{"tls_host_accept_relay"}; + +print STDOUT "\n"; + + +# Convert various logging options + +$log_selector = ""; +$sep = " \\\n "; + +if (defined $main{"log_level"}) + { + my($level) = $main{"log_level"}; + $log_selector .= "$sep -retry_defer$sep -skip_delivery" if $level < 5; + $log_selector .= "$sep -lost_incoming_connection$sep -smtp_syntax_error" . + "$sep -delay_delivery" if $level < 4; + $log_selector .= "$sep -size_reject" if $level < 2; + } + +$log_selector .= "$sep -queue_run" + if defined $main{"log_queue_run_level"} && + defined $main{"log_level"} && + $main{"log_queue_run_level"} > $main{"log_level"}; + +$log_selector .= "$sep +address_rewrite" if &bool("log_rewrites"); +$log_selector .= "$sep +all_parents" if &bool("log_all_parents"); +$log_selector .= "$sep +arguments" if &bool("log_arguments"); +$log_selector .= "$sep +incoming_port" if &bool("log_incoming_port"); +$log_selector .= "$sep +incoming_interface" if &bool("log_interface"); +$log_selector .= "$sep +received_sender" if &bool("log_received_sender"); +$log_selector .= "$sep +received_recipients" if &bool("log_received_recipients"); +$log_selector .= "$sep +sender_on_delivery" if &bool("log_sender_on_delivery"); +$log_selector .= "$sep +smtp_confirmation" if &bool("log_smtp_confirmation"); +$log_selector .= "$sep +smtp_connection" if &bool("log_smtp_connections"); +$log_selector .= "$sep +smtp_syntax_error" if &bool("log_smtp_syntax_errors"); +$log_selector .= "$sep +subject" if &bool("log_subject"); +$log_selector .= "$sep +tls_cipher" if &bool("tls_log_cipher"); +$log_selector .= "$sep +tls_peerdn" if &bool("tls_log_peerdn"); + + +if ($log_selector ne "") + { + print STDOUT "#!!# All previous logging options are combined into a single\n" + . "#!!# option in Exim 4. This setting is an approximation to\n" + . "#!!# the previous state - some logging has changed.\n\n"; + print STDOUT "log_selector = $log_selector\n\n"; + } + +# If deliver_load_max is set, replace it with queue_only_load (taking the +# lower value if both set) and also set deliver_queue_load_max if it is +# not already set. When scanning for output, deliver_load_max is skipped. + +if (defined $main{"deliver_load_max"}) + { + &rubric(); + print STDERR "\n" . +"** deliver_load_max is abolished in Exim 4.\n"; + + if (defined $main{"queue_only_load"}) + { + $queue_only_load_was_present = 1; + if ($main{"queue_only_load"} < $main{"deliver_load_max"}) + { + print STDERR +" As queue_only_load was set lower, deliver_load_max is just removed.\n"; + } + else + { + print STDERR +" As queue_only_load was set higher, it's value has been replaced by\n" . +" the value of deliver_load_max.\n"; + $main{"queue_only_load"} = $main{"deliver_load_max"}; + } + } + else + { + print STDERR +" queue_only_load has been set to the load value.\n"; + $main{"queue_only_load"} = $main{"deliver_load_max"}; + } + + if (!defined $main{"deliver_queue_load_max"}) + { + print STDERR +" deliver_queue_load_max has been set to the value of queue_only_load.\n"; + $main{"deliver_queue_load_max"} = $main{"queue_only_load"}; + } + else + { + $deliver_queue_load_max_was_present = 1; + } + } + + +# Now we scan through the various parts of the file again, making changes +# as necessary. + +# -------- The main configuration -------- + +$prefix = ""; +MainLine: for ($i = 0; $i < $clen; $i++) + { + my($nextline) = ""; + $type = &checkline($c[$i]); + last if $type eq "end"; + + if ($type eq "macro") + { + $i++ while $c[$i] =~ /\\\s*$|^\s*#/; + next; + } + + if ($type eq "comment") { print STDOUT "$c[$i]\n"; next; } + + # Collect any continuation lines for an option setting + + while ($rest =~ /\\\s*$/s || $nextline =~ /^\s*#/) + { + $nextline = $c[++$i]; + $rest .= "\n$nextline"; + } + + $rest =~ s/^=\s*//; + + # Deal with main options that are skipped (they are used in other + # options in other places). + + for $skipped (@skipped_options) + { + next MainLine if $name eq $skipped; + } + + # Deal with main options that are totally abolished + + for $abolished (@abolished_options) + { + if ($name eq $abolished) + { + &rubric(); + print STDERR "\n" . +"** The $name option no longer exists, and has no equivalent\n" . +" in Exim 4.\n"; + next MainLine; + } + } + + # There is a special case for rbl_warn_header + + if ($name eq "rbl_warn_header") + { + &rubric(); + print STDERR "\n" . +"** The $name option no longer exists. In Exim 4 you can achieve the\n" . +" effect by adding a suitable \"message\" statement in the ACL.\n"; + } + + # There is a special case for sender_reject and host_reject + + elsif ($name eq "sender_reject" || $name eq "host_reject") + { + &rubric(); + print STDERR "\n" . +"** The $name option no longer exists. Its data has been used in\n" . +" an Access Control List as if it were in ${name}_recipients.\n"; + } + + # And a special message for prohibition_message + + elsif ($name eq "prohibition_message") + { + &rubric(); + print STDERR "\n" . +"** The prohibition_message option no longer exists. The facility is\n" . +" provided in a different way in Exim 4, via the \"message\" keyword\n" . +" in Access Control Lists. It isn't possible to do an automatic conversion,\n" . +" so the value of prohibition_message has been ignored. You will have to\n" . +" modify the ACLs if you want to reinstate the feature.\n"; + } + + # auth_always_advertise gets converted to auth_advertise_hosts + + elsif ($name eq "auth_always_advertise") + { + print STDOUT "#!!# auth_always_advertise converted to auth_advertise_hosts\n"; + if (&bool("auth_always_advertise")) + { + print STDOUT "auth_advertise_hosts = *\n"; + } + else + { + $sep = ""; + print STDOUT "auth_advertise_hosts ="; + if (defined $main{"auth_hosts"}) + { + print STDOUT "$sep +auth_hosts"; + $sep = " :"; + } + if (defined $main{"host_accept_relay"}) + { + print STDOUT "$sep !+relay_hosts"; + $sep = " :"; + } + if (defined $main{"host_auth_accept_relay"}) + { + print STDOUT "$sep +auth_relay_hosts"; + } + print STDOUT "\n"; + } + } + + # Deal with main options that have to be rewritten + + elsif ($name eq "accept_timeout") + { + print STDOUT "#!!# accept_timeout renamed receive_timeout\n"; + print STDOUT "receive_timeout = $rest\n"; + } + + elsif ($name eq "collapse_source_routes") + { + print STDOUT "#!!# collapse_source_routes removed\n"; + print STDOUT "#!!# It has been a no-op since 3.10.\n"; + } + + elsif ($name eq "daemon_smtp_service") + { + print STDOUT "#!!# daemon_smtp_service renamed daemon_smtp_port\n"; + print STDOUT "daemon_smtp_port = $rest\n"; + } + + elsif ($name eq "dns_check_names" || $name eq "dns_check_names_pattern") + { + if (!$done_dns_check_names) + { + if (&bool("dns_check_names")) + { + if (defined $main{"dns_check_names_pattern"}) + { + &outopt(\%main, "dns_check_names_pattern", 0); + } + } + + else + { + print STDOUT "#!!# dns_check_names has been abolished\n"; + print STDOUT "#!!# setting dns_check_pattern empty to turn off check\n"; + print STDOUT "dns_check_names_pattern =\n"; + } + + $done_dns_check_names = 1; + } + } + + elsif ($name eq "deliver_load_max") + { + print STDOUT "deliver_queue_load_max = $main{'deliver_queue_load_max'}\n" + if !$deliver_queue_load_max_was_present; + print STDOUT "queue_only_load = $main{'queue_only_load'}\n" + if !$queue_only_load_was_present; + } + + elsif ($name eq "errmsg_file") + { + print STDOUT "#!!# errmsg_file renamed bounce_message_file\n"; + print STDOUT "bounce_message_file = $rest\n"; + } + + elsif ($name eq "errmsg_text") + { + print STDOUT "#!!# errmsg_text renamed bounce_message_text\n"; + print STDOUT "bounce_message_text = $rest\n"; + } + + elsif ($name eq "forbid_domain_literals") + { + print STDOUT "#!!# forbid_domain_literals replaced by allow_domain_literals\n"; + print STDOUT "allow_domain_literals = ", + &bool("forbid_domain_literals")? "false" : "true", "\n"; + } + + elsif ($name eq "freeze_tell_mailmaster") + { + print STDOUT "#!!# freeze_tell_mailmaster replaced by freeze_tell\n"; + if (&bool("freeze_tell_mailmaster")) + { + print STDOUT "freeze_tell = ", + ((defined $main{"errors_address"})? + $main{"errors_address"} : "postmaster"), "\n"; + } + else + { + print STDOUT "#!!# freeze_tell is unset by default\n"; + } + } + + elsif ($name eq "helo_verify") + { + print STDOUT "#!!# helo_verify renamed helo_verify_hosts\n"; + printf STDOUT ("helo_verify_hosts = %s\n", &no_expand_regex($rest)); + } + + elsif ($name eq "ignore_errmsg_errors") + { + print STDOUT "ignore_bounce_errors_after = 0s\n"; + } + + elsif ($name eq "ignore_errmsg_errors_after") + { + print STDOUT "#!!# ignore_errmsg_errors_after renamed ignore_bounce_errors_after\n"; + print STDOUT "ignore_bounce_errors_after = $rest\n"; + } + + elsif ($name eq "ipv4_address_lookup" || $name eq "dns_ipv4_lookup") + { + print STDOUT "#!!# $name changed to dns_ipv4_lookup\n" + if $name eq "ipv4_address_lookup"; + print STDOUT "#!!# dns_ipv4_lookup is now a domain list\n"; + if (&bool($name)) + { + print STDOUT "dns_ipv4_lookup = *\n"; + } + else + { + print STDOUT "#!!# default for dns_ipv4_lookup is unset\n"; + } + } + + elsif ($name eq "locally_caseless") + { + print STDOUT "#!!# locally_caseless removed\n"; + print STDOUT "#!!# caseful_local_part will be added to ex-directors\n"; + $add_caseful_local_part = 1; + } + + elsif ($name eq "message_filter_directory2_transport") + { + print STDOUT "#!!# message_filter_directory2_transport removed\n"; + } + + elsif ($name =~ /^message_filter(.*)/) + { + print STDOUT "#!!# $name renamed system_filter$1\n"; + print STDOUT "system_filter$1 = $rest\n"; + } + + elsif ($name eq "queue_remote_domains") + { + print STDOUT "#!!# queue_remote_domains renamed queue_domains\n"; + printf STDOUT ("queue_domains = %s\n", &no_expand_regex($rest)); + } + + elsif ($name eq "receiver_unqualified_hosts") + { + print STDOUT "#!!# receiver_unqualified_hosts renamed recipient_unqualified_hosts\n"; + printf STDOUT ("recipient_unqualified_hosts = %s\n", + &no_expand_regex($rest)); + } + + elsif ($name eq "remote_sort") + { + print STDOUT "#!!# remote_sort renamed remote_sort_domains\n"; + printf STDOUT ("remote_sort_domains = %s\n", &no_expand_regex($rest)); + } + + elsif ($name eq "security") + { + if ($rest eq "unprivileged") + { + print STDOUT "#!!# security=unprivileged changed to deliver_drop_privilege\n"; + print STDOUT "deliver_drop_privilege\n"; + } + else + { + &rubric(); + print STDERR "\n" . +"** The 'security' option no longer exists.\n"; + } + } + + elsif ($name eq "timestamps_utc") + { + print STDOUT "#!!# timestamps_utc changed to use timezone\n"; + print STDOUT "timezone = utc\n"; + } + + elsif ($name eq "untrusted_set_sender") + { + print STDOUT "#!!# untrusted_set_sender is now a list of what can be set\n"; + print STDOUT "#!!# The default is an empty list.\n"; + if (&bool("untrusted_set_sender")) + { + print STDOUT "untrusted_set_sender = *\n"; + } + } + + elsif ($name eq "warnmsg_file") + { + print STDOUT "#!!# warnmsg_file renamed warn_message_file\n"; + print STDOUT "warn_message_file = $rest\n"; + } + + # Remaining options just get copied unless they are one of those that's + # a list where any regular expressions have to be escaped. + + else + { + my($no_expand) = 0; + foreach $o (@list_options) + { + if ($name eq $o) + { + $no_expand = 1; + last; + } + } + &outopt(\%main, $name, $no_expand); + } + } + + +# -------- The ACL configuration -------- + +print STDOUT "\n"; +print STDOUT "#!!#######################################################!!#\n"; +print STDOUT "#!!# This new section of the configuration contains ACLs #!!#\n"; +print STDOUT "#!!# (Access Control Lists) derived from the Exim 3 #!!#\n"; +print STDOUT "#!!# policy control options. #!!#\n"; +print STDOUT "#!!#######################################################!!#\n"; + +print STDOUT "\n"; +print STDOUT "#!!# These ACLs are crudely constructed from Exim 3 options.\n"; +print STDOUT "#!!# They are almost certainly not optimal. You should study\n"; +print STDOUT "#!!# them and rewrite as necessary.\n"; + +print STDOUT "\nbegin acl\n\n"; + + +# Output an ACL for use after the RCPT command. This combines all the previous +# policy checking options. + +print STDOUT "#!!# ACL that is used after the RCPT command\n"; +print STDOUT "check_recipient:\n"; + +print STDOUT " # Exim 3 had no checking on -bs messages, so for compatibility\n"; +print STDOUT " # we accept if the source is local SMTP (i.e. not over TCP/IP).\n"; +print STDOUT " # We do this by testing for an empty sending host field.\n"; +print STDOUT " accept hosts = :\n"; + +if (defined $main{"tls_verify_ciphers"}) + { + print STDOUT " deny "; + print STDOUT "hosts = $main{'tls_verify_hosts'}\n " + if defined $main{"tls_verify_hosts"}; + print STDOUT " encrypted = *\n "; + print STDOUT "!encrypted = $main{'tls_verify_ciphers'}\n"; + } + +print STDOUT " deny hosts = +auth_hosts\n" . + " message = authentication required\n" . + " !authenticated = *\n" + if defined $main{"auth_hosts"}; + +print STDOUT " deny hosts = +tls_hosts\n" . + " message = encryption required\n" . + " !encrypted = *\n" + if defined $main{"tls_hosts"}; + +printf STDOUT (" accept recipients = %s\n", + &acl_quote(&sort_address_list($main{"recipients_reject_except"}, + "recipients_reject_except"))) + if defined $main{"recipients_reject_except"}; + +printf STDOUT (" accept senders = %s\n", + &acl_quote(&sort_address_list($main{"recipients_reject_except_senders"}, + "recipients_reject_except_senders"))) + if defined $main{"recipients_reject_except_senders"}; + +printf STDOUT (" deny hosts = %s\n", &acl_quote($main{"host_reject"})) + if defined $main{"host_reject"}; + +printf STDOUT (" deny hosts = %s\n", + &acl_quote($main{"host_reject_recipients"})) + if defined $main{"host_reject_recipients"}; + +if (defined $main{"rbl_domains"}) + { + my($msg) = "message = host is listed in \$dnslist_domain\n "; + my($hlist) = (defined $main{"rbl_hosts"})? + "hosts = +rbl_hosts\n " : ""; + + print STDOUT " accept ${hlist}dnslists = $rbl_accept_domains\n" + if defined $rbl_accept_domains; + print STDOUT " deny ${hlist}${msg}dnslists = $rbl_reject_domains\n" + if defined $rbl_reject_domains; + print STDOUT " warn ${hlist}" . + "message = X-Warning: \$sender_host_address is listed at \$dnslist_domain\n" . + " dnslists = $rbl_warn_domains\n" + if defined $rbl_warn_domains; + + if (defined $main{"host_accept_relay"}) + { + $hlist .= "hosts = !+relay_hosts\n "; + print STDOUT " accept ${hlist}dnslists = $rbl_accept_skiprelay\n" + if defined $rbl_accept_skiprelay; + print STDOUT " deny ${hlist}${msg}dnslists = $rbl_reject_skiprelay\n" + if defined $rbl_reject_skiprelay; + print STDOUT " warn ${hlist}" . + "message = X-Warning: \$sender_host_address is listed at \$dnslist_domain\n" . + " dnslists = $rbl_warn_skiprelay\n" + if defined $rbl_warn_skiprelay; + } + } + +printf STDOUT (" deny senders = %s\n", + &acl_quote(&sort_address_list($main{"sender_reject"}, "sender_reject"))) + if defined $main{"sender_reject"}; + +printf STDOUT (" deny senders = %s\n", + &acl_quote(&sort_address_list($main{"sender_reject_recipients"}, + "sender_reject_recipients"))) + if defined $main{"sender_reject_recipients"}; + +if (&bool("sender_verify")) + { + if (defined $main{"sender_verify_hosts_callback"} && + defined $main{"sender_verify_callback_domains"}) + { + printf STDOUT (" deny hosts = %s\n", + &acl_quote($main{"sender_verify_hosts_callback"})); + printf STDOUT (" sender_domains = %s\n", + &acl_quote($main{"sender_verify_callback_domains"})); + print STDOUT " !verify = sender/callout"; + print STDOUT "=$main{\"sender_verify_callback_timeout\"}" + if defined $main{"sender_verify_callback_timeout"}; + print STDOUT "\n"; + } + + if (defined $main{"sender_verify_hosts"}) + { + printf STDOUT (" deny hosts = %s\n", + &acl_quote($main{"sender_verify_hosts"})); + print STDOUT " !verify = sender\n"; + } + else + { + print STDOUT " require verify = sender\n"; + } + } + +if (&bool("receiver_verify")) + { + print STDOUT " deny message = unrouteable address\n"; + printf STDOUT (" recipients = %s\n", + &acl_quote(&sort_address_list($main{"receiver_verify_addresses"}, + "receiver_verify_addresses"))) + if defined $main{"receiver_verify_addresses"}; + printf STDOUT (" hosts = %s\n", + &acl_quote($main{"receiver_verify_hosts"})) + if defined $main{"receiver_verify_hosts"}; + printf STDOUT (" senders = %s\n", + &acl_quote(&sort_address_list($main{"receiver_verify_senders"}, + "receiver_verify_senders"))) + if defined $main{"receiver_verify_senders"}; + print STDOUT " !verify = recipient\n"; + } + +print STDOUT " accept domains = +local_domains\n" + if $local_domains !~ /^\s*$/; + +print STDOUT " accept domains = +relay_domains\n" + if $relay_domains !~ /^\s*$/; + +if (defined $main{"host_accept_relay"}) + { + if (defined $main{"sender_address_relay"}) + { + if (defined $main{"sender_address_relay_hosts"}) + { + printf STDOUT (" accept hosts = %s\n", + &acl_quote($main{"sender_address_relay_hosts"})); + print STDOUT " endpass\n"; + print STDOUT " message = invalid sender\n"; + printf STDOUT (" senders = %s\n", + &acl_quote(&sort_address_list($main{"sender_address_relay"}, + "sender_address_relay"))); + print STDOUT " accept hosts = +relay_hosts\n"; + } + else + { + print STDOUT " accept hosts = +relay_hosts\n"; + print STDOUT " endpass\n"; + print STDOUT " message = invalid sender\n"; + printf STDOUT (" senders = %s\n", + &acl_quote(&sort_address_list($main{"sender_address_relay"}, + "sender_address_relay"))); + } + } + else + { + print STDOUT " accept hosts = +relay_hosts\n"; + } + } + +print STDOUT " accept hosts = +auth_relay_hosts\n" . + " endpass\n" . + " message = authentication required\n" . + " authenticated = *\n" + if defined $main{"host_auth_accept_relay"}; + +print STDOUT " accept hosts = +tls_relay_hosts\n" . + " endpass\n" . + " message = encryption required\n" . + " encrypted = *\n" + if defined $main{"tls_host_accept_relay"}; + +print STDOUT " deny message = relay not permitted\n\n"; + + +# Output an ACL for use after the DATA command. This is concerned with +# header checking. + +print STDOUT "#!!# ACL that is used after the DATA command\n"; +print STDOUT "check_message:\n"; + +# Default for headers_checks_fail is true + +if (!defined $main{"headers_checks_fail"} || + $main{"headers_checks_fail"} eq "true") + { + print STDOUT " require verify = header_syntax\n" + if &bool("headers_check_syntax"); + print STDOUT " require verify = header_sender\n" + if &bool("headers_sender_verify"); + print STDOUT " accept senders = !:\n require verify = header_sender\n" + if &bool("headers_sender_verify_errmsg"); + } +else + { + print STDOUT " warn !verify = header_syntax\n" + if &bool("headers_check_syntax"); + print STDOUT " warn !verify = header_sender\n" + if &bool("headers_sender_verify"); + print STDOUT " accept senders = !:\n warn !verify = header_sender\n" + if &bool("headers_sender_verify_errmsg"); + } + +print STDOUT " accept\n\n"; + + +# Output an ACL for AUTH if required + +if (defined $main{"auth_over_tls_hosts"}) + { + print STDOUT "#!!# ACL that is used after the AUTH command\n" . + "check_auth:\n" . + " accept hosts = +auth_over_tls_hosts\n" . + " endpass\n" . + " message = STARTTLS required before AUTH\n" . + " encrypted = *\n" . + " accept\n"; + } + + +# Output ACLs for ETRN, EXPN, and VRFY if required + +if (defined $main{"smtp_etrn_hosts"}) + { + print STDOUT "#!!# ACL that is used after the ETRN command\n" . + "check_etrn:\n"; + print STDOUT " deny hosts = +auth_hosts\n" . + " message = authentication required\n" . + " !authenticated = *\n" + if defined $main{"auth_hosts"}; + print STDOUT " accept hosts = $main{\"smtp_etrn_hosts\"}\n\n"; + } + +if (defined $main{"smtp_expn_hosts"}) + { + print STDOUT "#!!# ACL that is used after the EXPN command\n" . + "check_expn:\n"; + print STDOUT " deny hosts = +auth_hosts\n" . + " message = authentication required\n" . + " !authenticated = *\n" + if defined $main{"auth_hosts"}; + print STDOUT " accept hosts = $main{\"smtp_expn_hosts\"}\n\n"; + } + +if (&bool("smtp_verify")) + { + print STDOUT "#!!# ACL that is used after the VRFY command\n" . + "check_vrfy:\n"; + print STDOUT " deny hosts = +auth_hosts\n" . + " message = authentication required\n" . + " !authenticated = *\n" + if defined $main{"auth_hosts"}; + print STDOUT " accept\n\n"; + } + +# -------- The authenticators -------- + +$started = 0; +for ($i = $auth_start; $i < $clen; $i++) + { + if (!$started) + { + if ($c[$i] !~ /^\s*(#|$)/) + { + print STDOUT "\nbegin authenticators\n\n"; + $started = 1; + } + } + print STDOUT "$c[$i]\n"; + } + + +# -------- Rewrite section -------- + +$started = 0; +for ($i = $rewrite_start; $i < $clen && $i < $auth_start - 1; $i++) + { + if (!$started) + { + if ($c[$i] !~ /^\s*(#|$)/) + { + print STDOUT "\nbegin rewrite\n\n"; + $started = 1; + } + } + &print_no_expand($c[$i]); + } + + +# -------- The routers configuration -------- + +# The new routers configuration is created out of the old directors and routers +# configuration. We put the old routers first, adding a "domains" option to +# any that don't have one, to make them select the domains that do not match +# the original local_domains. The routers get modified as necessary, and the +# final one has "no_more" set, unless it has conditions. In that case we have +# to add an extra router to be sure of failing all non-local addresses that +# fall through. We do this also if there are no routers at all. The old +# directors follow, modified as required. + +$prefix = "r."; +undef @comments; + +print STDOUT "\n"; +print STDOUT "#!!#######################################################!!#\n"; +print STDOUT "#!!# Here follow routers created from the old routers, #!!#\n"; +print STDOUT "#!!# for handling non-local domains. #!!#\n"; +print STDOUT "#!!#######################################################!!#\n"; + +print STDOUT "\nbegin routers\n\n"; + +for ($i = $router_start; $i < $clen; $i++) + { + $type = &checkline($c[$i]); + last if $type eq "end"; + + if ($type eq "comment") { push(@comments, "$c[$i]\n"); next; } + + # When we hit the start of a driver, modify its options as necessary, + # and then output it from the stored option settings, having first output + # and previous comments. + + if ($type eq "driver") + { + print STDOUT shift @comments while scalar(@comments) > 0; + + $hash = $driverlist{"$prefix$name"}; + $driver = $$hash{"driver"}; + print STDOUT "$name:\n"; + + $add_no_more = + ! defined $$hash{"domains"} && + ! defined $$hash{"local_parts"} && + ! defined $$hash{"senders"} && + ! defined $$hash{"condition"} && + ! defined $$hash{"require_files"} && + (!defined $$hash{"verify_only"} || $$hash{"verify_only"} eq "false") && + (!defined $$hash{"verify"} || $$hash{"verify"} eq "true"); + + # Create a "domains" setting if there isn't one, unless local domains + # was explicitly empty. + + $$hash{"domains"} = "! +local_domains" + if !defined $$hash{"domains"} && $local_domains !~ /^\s*$/; + + # If the router had a local_parts setting, add caseful_local_part + + $$hash{"caseful_local_part"} = "true" if defined $$hash{"local_parts"}; + + # If the router has "self=local" set, change it to "self=pass", and + # set pass_router to the router that was the first director. Change the + # obsolete self settings of "fail_hard" and "fail_soft" to "fail" and + # "pass". + + if (defined $$hash{"self"}) + { + if ($$hash{"self"} eq "local") + { + $$hash{"self"} = "pass"; + $$hash{"pass_router"} = $first_director; + } + elsif ($$hash{"self"} eq "fail_hard") + { + $$hash{"self"} = "fail"; + } + elsif ($$hash{"self"} eq "fail_soft") + { + $$hash{"self"} = "pass"; + } + } + + # If the router had a require_files setting, check it for user names + # and colons that are part of expansion items + + if (defined $$hash{"require_files"}) + { + &check_require($$hash{"require_files"}, "'$name' router"); + if (($$hash{"require_files"} =~ s/(\$\{\w+):/$1::/g) > 0 || + ($$hash{"require_files"} =~ s/ldap:/ldap::/g) > 0) + { + &rubric(); + print STDERR "\n" . +"*** A setting of require_files in the $name router contains\n" . +" a colon in what appears to be an expansion item. In Exim 3, the\n" . +" whole string was expanded before splitting the list, but in Exim 4\n" . +" each item is expanded separately, so colons that are not list\n" . +" item separators have to be doubled. One or more such colons in this\n" . +" list have been doubled as a precaution. Please check the result.\n"; + } + } + + # If the router had a "senders" setting, munge the address list + + $$hash{"senders"} = &sort_address_list($$hash{"senders"}, "senders") + if defined $$hash{"senders"}; + + # ---- Changes to domainlist router ---- + + if ($driver eq "domainlist") + { + &abolished($hash, "A domainlist router", + "modemask", "owners", "owngroups", + "qualify_single", "search_parents"); + + # The name has changed + + $$hash{"driver"} = "manualroute"; + + # Turn "route_file", "route_query" and "route_queries" into lookups for + # route_data. + + if (defined $$hash{"route_file"}) + { + $$hash{"route_data"} = "\${lookup\{\$domain\}$$hash{'search_type'}" . + "\{$$hash{'route_file'}\}\}"; + } + elsif (defined $$hash{"route_query"}) + { + $$hash{"route_data"} = "\${lookup $$hash{'search_type'}" . + "\{" . &unquote($$hash{'route_query'}) . "\}\}"; + } + elsif (defined $$hash{"route_queries"}) + { + $endkets = 0; + $$hash{"route_data"} = ""; + $route_queries = $$hash{'route_queries'}; + $route_queries =~ s/^"(.*)"$/$1/s; + $route_queries =~ s/::/++colons++/g; + @qq = split(/:/, $route_queries); + + foreach $q (@qq) + { + $q =~ s/\+\+colons\+\+/:/g; + $q =~ s/^\s+//; + $q =~ s/\s+$//; + if ($endkets > 0) + { + $$hash{"route_data"} .= "\\\n {"; + $endkets++; + } + $$hash{"route_data"} .= "\${lookup $$hash{'search_type'} \{$q\}\{\$value\}"; + $endkets++; + } + + $$hash{"route_data"} .= "}" x $endkets; + } + + delete $$hash{"route_file"}; + delete $$hash{"route_query"}; + delete $$hash{"route_queries"}; + delete $$hash{"search_type"}; + + # But we can't allow both route_data and route_list + + if (defined $$hash{"route_data"} && defined $$hash{"route_list"}) + { + &rubric(); + print STDERR "\n" . +"** An Exim 3 'domainlist' router called '$name' contained a 'route_list'\n" . +" option as well as a setting of 'route_file', 'route_query', or\n" . +" 'route_queries'. The latter has been turned into a 'route_data' setting,\n". +" but in Exim 4 you can't have both 'route_data' and 'route_list'. You'll\n" . +" have to rewrite this router; in the meantime, 'route_list' has been\n" . +" omitted.\n"; + print STDOUT "#!!# route_list option removed\n"; + delete $$hash{"route_list"}; + } + + # Change bydns_a into bydns in a route_list; also bydns_mx, but that + # works differently. + + if (defined $$hash{"route_list"}) + { + $$hash{"route_list"} =~ s/bydns_a/bydns/g; + if ($$hash{"route_list"} =~ /bydns_mx/) + { + $$hash{"route_list"} =~ s/bydns_mx/bydns/g; + &rubric(); + print STDERR "\n" . +"*** An Exim 3 'domainlist' router called '$name' contained a 'route_list'\n" . +" option which used 'bydns_mx'. This feature no longer exists in Exim 4.\n" . +" It has been changed to 'bydns', but it won't have the same effect,\n" . +" because it will look for A rather than MX records. Use the 'dnslookup'\n" . +" router to do MX lookups - if you want to override the hosts found from\n" . +" MX records, you should route to a special 'smtp' transport which has\n" . +" both 'hosts' and 'hosts_override' set.\n"; + } + } + + # Arrange to not expand regex + + $$hash{"route_list"} = &no_expand_regex($$hash{"route_list"}, ";") + if (defined $$hash{"route_list"}) + } + + + # ---- Changes to iplookup router ---- + + elsif ($driver eq "iplookup") + { + &renamed($hash, "service", "port"); + } + + + # ---- Changes to lookuphost router ---- + + elsif ($driver eq "lookuphost") + { + $$hash{"driver"} = "dnslookup"; + + if (defined $$hash{"gethostbyname"}) + { + &rubric(); + print STDERR "\n" . +"** An Exim 3 'lookuphost' router called '$name' used the 'gethostbyname'\n" . +" option, which no longer exists. You will have to rewrite it.\n"; + print STDOUT "#!!# gethostbyname option removed\n"; + delete $$hash{"gethostbyname"}; + } + + $$hash{"mx_domains"} = &no_expand_regex($$hash{"mx_domains"}) + if defined $$hash{"mx_domains"}; + } + + + # ---- Changes to the queryprogram router ---- + + elsif ($driver eq "queryprogram") + { + &rubric(); + print STDERR "\n" . +"** The configuration contains a 'queryprogram' router. Please note that\n" . +" the specification for the text that is returned by the program run\n" . +" by this router has changed in Exim 4. You will need to modify your\n" . +" program.\n"; + + if (!defined $$hash{'command_user'}) + { + &rubric(); + print STDERR "\n" . +"** The 'queryprogram' router called '$name' does not have a setting for\n" . +" the 'command_user' option. This is mandatory in Exim 4. A setting of\n" . +" 'nobody' has been created.\n"; + $$hash{"command_user"} = "nobody"; + } + } + + + # ------------------------------------- + + # Output the router's option settings + + &outdriver($hash); + next; + } + + # Skip past any continuation lines for an option setting + while ($c[$i] =~ /\\\s*$/s && $i < $clen - 1) + { + $i++; + $i++ while ($c[$i] =~ /^\s*#/); + } + } + +# Add "no_more" to the final driver from the old routers, provided it had no +# conditions. Otherwise, or if there were no routers, make up one to fail all +# non-local domains. + +if ($add_no_more) + { + print STDOUT " no_more\n"; + print STDOUT shift @comments while scalar(@comments) > 0; + } +else + { + print STDOUT shift @comments while scalar(@comments) > 0; + print STDOUT "\n#!!# This new router is put here to fail all domains that\n"; + print STDOUT "#!!# were not in local_domains in the Exim 3 configuration.\n\n"; + print STDOUT "fail_remote_domains:\n"; + print STDOUT " driver = redirect\n"; + print STDOUT " domains = ! +local_domains\n"; + print STDOUT " allow_fail\n"; + print STDOUT " data = :fail: unrouteable mail domain \"\$domain\"\n\n"; + } + +# Now copy the directors, making appropriate changes + +print STDOUT "\n"; +print STDOUT "#!!#######################################################!!#\n"; +print STDOUT "#!!# Here follow routers created from the old directors, #!!#\n"; +print STDOUT "#!!# for handling local domains. #!!#\n"; +print STDOUT "#!!#######################################################!!#\n"; + +$prefix = "d."; +for ($i = $director_start; $i < $clen; $i++) + { + $type = &checkline($c[$i]); + last if $type eq "end"; + + if ($type eq "comment") { print STDOUT "$c[$i]\n"; next; } + + undef $second_router; + + if ($type eq "driver") + { + $hash = $driverlist{"$prefix$name"}; + $driver = $$hash{"driver"}; + print STDOUT "$name:\n"; + + $$hash{"caseful_local_part"} = "true" if $add_caseful_local_part; + + if (defined $$hash{"local_parts"} && + (defined $$hash{"prefix"} || defined $hash{"suffix"})) + { + &rubric(); + print STDERR "\n" . +"** The Exim 3 configuration contains a director called '$name' which has\n" . +" 'local_parts' set, together with either or both of 'prefix' and 'suffix'\n". +" This combination has a different effect in Exim 4, where the affix\n" . +" is removed *before* 'local_parts' is tested. You will probably need\n" . +" to make changes to this driver.\n"; + } + + &renamed($hash, "prefix", "local_part_prefix"); + &renamed($hash, "prefix_optional", "local_part_prefix_optional"); + &renamed($hash, "suffix", "local_part_suffix"); + &renamed($hash, "suffix_optional", "local_part_suffix_optional"); + &renamed($hash, "new_director", "redirect_router"); + + &handle_current_and_home_directory($hash, $driver, $name); + + # If the director had a require_files setting, check it for user names + # and colons that are part of expansion items + + if (defined $$hash{"require_files"}) + { + &check_require($$hash{"require_files"}, "'$name' director"); + if (($$hash{"require_files"} =~ s/(\$\{\w+):/$1::/g) > 0 || + ($$hash{"require_files"} =~ s/ldap:/ldap::/g) > 0) + { + &rubric(); + print STDERR "\n" . +"*** A setting of require_files in the $name director contains\n" . +" a colon in what appears to be an expansion item. In Exim 3, the\n" . +" whole string was expanded before splitting the list, but in Exim 4\n" . +" each item is expanded separately, so colons that are not list\n" . +" item separators have to be doubled. One or more such colons in this\n" . +" list have been doubled as a precaution. Please check the result.\n"; + } + } + + # If the director had a "senders" setting, munge the address list + + $$hash{"senders"} = &sort_address_list($$hash{"senders"}, "senders") + if defined $$hash{"senders"}; + + # ---- Changes to aliasfile director ---- + + if ($driver eq "aliasfile") + { + &abolished($hash, "An aliasfile director", + "directory2_transport", "freeze_missing_include", + "modemask", "owners", "owngroups"); + + $$hash{"driver"} = "redirect"; + + $key = "\$local_part"; + $key = "\$local_part\@\$domain" + if defined $$hash{"include_domain"} && + $$hash{"include_domain"} eq "true"; + delete $$hash{"include_domain"}; + + if (defined $$hash{"forbid_special"} && $$hash{"forbid_special"} eq "true") + { + $$hash{"forbid_blackhole"} = "true"; + } + else + { + $$hash{"allow_defer"} = "true"; + $$hash{"allow_fail"} = "true"; + } + delete $$hash{"forbid_special"}; + + # Deal with "file", "query", or "queries" + + if (defined $$hash{"file"}) + { + $$hash{"data"} = + "\$\{lookup\{$key\}$$hash{'search_type'}\{$$hash{'file'}\}\}"; + if (defined $$hash{"optional"} && $$hash{"optional"} eq "true") + { + $$hash{"data"} = + "\$\{if exists\{$$hash{'file'}\}\{$$hash{'data'}\}\}"; + } + delete $$hash{"optional"}; + } + elsif (defined $$hash{"query"}) + { + &abolished($hash, "An aliasfile director", "optional"); + $$hash{"data"} = "\${lookup $$hash{'search_type'} " . + "\{" . &unquote($$hash{'query'}) . "\}\}"; + } + else # Must be queries + { + &abolished($hash, "An aliasfile director", "optional"); + $endkets = 0; + $$hash{"data"} = ""; + $queries = $$hash{'queries'}; + $queries =~ s/^"(.*)"$/$1/s; + $queries =~ s/::/++colons++/g; + @qq = split(/:/, $queries); + + foreach $q (@qq) + { + $q =~ s/\+\+colons\+\+/:/g; + $q =~ s/^\s+//; + $q =~ s/\s+$//; + if ($endkets > 0) + { + $$hash{"data"} .= "\\\n {"; + $endkets++; + } + $$hash{"data"} .= "\${lookup $$hash{'search_type'} \{$q\}\{\$value\}"; + $endkets++; + } + + $$hash{"data"} .= "}" x $endkets; + } + + $$hash{"data"} = "\${expand:$$hash{'data'}\}" + if (defined $$hash{"expand"} && $$hash{"expand"} eq "true"); + + delete $$hash{"expand"}; + delete $$hash{"file"}; + delete $$hash{"query"}; + delete $$hash{"queries"}; + delete $$hash{"search_type"}; + + # Turn aliasfile + transport into accept + condition + + if (defined $$hash{'transport'}) + { + &rubric(); + if (!defined $$hash{'condition'}) + { + print STDERR "\n" . +"** The Exim 3 configuration contains an aliasfile director called '$name',\n". +" which has 'transport' set. This has been turned into an 'accept' router\n". +" with a 'condition' setting, but should be carefully checked.\n"; + $$hash{'driver'} = "accept"; + $$hash{'condition'} = + "\$\{if eq \{\}\{$$hash{'data'}\}\{no\}\{yes\}\}"; + delete $$hash{'data'}; + delete $$hash{'allow_defer'}; + delete $$hash{'allow_fail'}; + } + else + { + print STDERR "\n" . +"** The Exim 3 configuration contains an aliasfile director called '$name',\n". +" which has 'transport' set. This cannot be turned into an 'accept' router\n". +" with a 'condition' setting, because there is already a 'condition'\n" . +" setting. It has been left as 'redirect' with a transport, which is\n" . +" invalid - you must sort this one out.\n"; + } + } + } + + + # ---- Changes to forwardfile director ---- + + elsif ($driver eq "forwardfile") + { + &abolished($hash, "A forwardfile director", + "check_group", "directory2_transport", + "freeze_missing_include", "match_directory", + "seteuid"); + + &renamed($hash, "filter", "allow_filter"); + + $$hash{"driver"} = "redirect"; + $$hash{"check_local_user"} = "true" + if !defined $$hash{"check_local_user"}; + + if (defined $$hash{"forbid_pipe"} && $$hash{"forbid_pipe"} eq "true") + { + print STDOUT "#!!# forbid_filter_run added because forbid_pipe is set\n"; + $$hash{"forbid_filter_run"} = "true"; + } + + if (defined $$hash{'allow_system_actions'} && + $$hash{'allow_system_actions'} eq 'true') + { + $$hash{'allow_freeze'} = "true"; + } + delete $$hash{'allow_system_actions'}; + + # If file_directory is defined, use it to qualify relative paths; if not, + # and check_local_user is defined, use $home. Remove file_directory from + # the output. + + $dir = ""; + if (defined $$hash{"file_directory"}) + { + $dir = $$hash{"file_directory"} . "/"; + delete $$hash{"file_directory"}; + } + elsif ($$hash{"check_local_user"} eq "true") + { + $dir = "\$home/"; + } + + # If it begins with an upper case letter, guess that this is really + # a macro. + + if (defined $$hash{"file"} && $$hash{"file"} !~ /^[\/A-Z]/) + { + $$hash{"file"} = $dir . $$hash{"file"}; + } + } + + + # ---- Changes to localuser director ---- + + elsif ($driver eq "localuser") + { + &abolished($hash, "A localuser director", "match_directory"); + $$hash{"driver"} = "accept"; + $$hash{"check_local_user"} = "true"; + } + + + # ---- Changes to smartuser director ---- + + elsif ($driver eq "smartuser") + { + &abolished($hash, "A smartuser director", "panic_expansion_fail"); + + $transport = $$hash{"transport"}; + $new_address = $$hash{"new_address"}; + + if (defined $transport && defined $new_address) + { + &rubric(); + print STDERR "\n" . +"** The Exim 3 configuration contains a smartuser director called '$name',\n". +" which has both 'transport' and 'new_address' set. This has been turned\n". +" into two routers for Exim 4. However, if the new address contains a\n" . +" reference to \$local_part, this won't work correctly. In any case, you\n". +" may be able to make it tidier by rewriting.\n"; + $$hash{"driver"} = "redirect"; + $$hash{"data"} = $new_address; + $$hash{"redirect_router"} = "${name}_part2"; + + $second_router = "\n". + "#!!# This router is invented to go with the previous one because\n". + "#!!# in Exim 4 you can't have a change of address and a transport\n". + "#!!# setting in the same router as you could in Exim 3.\n\n" . + "${name}_part2:\n". + " driver = accept\n". + " condition = \$\{if eq\{\$local_part@\$domain\}" . + "\{$new_address\}\{yes\}\{no\}\}\n". + " transport = $$hash{'transport'}\n"; + + delete $$hash{"new_address"}; + delete $$hash{"transport"}; + } + elsif (defined $new_address) + { + $$hash{"driver"} = "redirect"; + $$hash{"data"} = $new_address; + $$hash{"allow_defer"} = "true"; + $$hash{"allow_fail"} = "true"; + delete $$hash{"new_address"}; + } + else # Includes the case of neither set (verify_only) + { + $$hash{"driver"} = "accept"; + if (defined $$hash{"rewrite"}) + { + &rubric(); + print STDERR "\n" . +"** The Exim 3 configuration contains a setting of the 'rewrite' option on\n". +" a smartuser director called '$name', but this director does not have\n". +" a setting of 'new_address', so 'rewrite' has no effect. The director\n". +" has been turned into an 'accept' router, and 'rewrite' has been discarded."; + delete $$hash{"rewrite"}; + } + } + } + + + # ------------------------------------- + + # For ex-directors that don't have check_local_user set, add + # retry_use_local_part to imitate what Exim 3 would have done. + + $$hash{"retry_use_local_part"} = "true" + if (!defined $$hash{"check_local_user"} || + $$hash{"check_local_user"} eq "false") ; + + # Output the router's option settings + + &outdriver($hash); + + # Output an auxiliary router if one is needed + + print STDOUT $second_router if defined $second_router; + + next; + } + + # Skip past any continuation lines for an option setting + while ($c[$i] =~ /\\\s*$/s) + { + $i++; + $i++ while ($c[$i] =~ /^\s*#/); + } + } + + + +# -------- The transports configuration -------- + +$started = 0; +$prefix = "t."; +for ($i = $transport_start; $i < $clen; $i++) + { + $type = &checkline($c[$i]); + last if $type eq "end"; + + if ($type eq "comment") { print STDOUT "$c[$i]\n"; next; } + + if (!$started) + { + print STDOUT "begin transports\n\n"; + $started = 1; + } + + if ($type eq "driver") + { + $hash = $driverlist{"$prefix$name"}; + $driver = $$hash{"driver"}; + print STDOUT "$name:\n"; + + # ---- Changes to the appendfile transport ---- + + if ($driver eq "appendfile") + { + &renamed($hash, "prefix", "message_prefix"); + &renamed($hash, "suffix", "message_suffix"); + &abolished($hash, "An appendfile transport", + "require_lockfile"); + &handle_batch_and_bsmtp($hash); + if (defined $$hash{"from_hack"} && $$hash{"from_hack"} eq "false") + { + print STDOUT "#!!# no_from_hack replaced by check_string\n"; + $$hash{"check_string"} = ""; + } + delete $$hash{"from_hack"}; + } + + # ---- Changes to the lmtp transport ---- + + elsif ($driver eq "lmtp") + { + if (defined $$hash{"batch"} && $$hash{"batch"} ne "none") + { + $$hash{"batch_max"} = "100" if !defined $$hash{"batch_max"}; + $$hash{"batch_id"} = "\$domain" if $$hash{"batch"} eq "domain"; + } + else + { + $$hash{"batch_max"} = "1" if defined $$hash{"batch_max"}; + } + delete $$hash{"batch"}; + } + + # ---- Changes to the pipe transport ---- + + elsif ($driver eq "pipe") + { + &renamed($hash, "prefix", "message_prefix"); + &renamed($hash, "suffix", "message_suffix"); + &handle_batch_and_bsmtp($hash); + if (defined $$hash{"from_hack"} && $$hash{"from_hack"} eq "false") + { + print STDOUT "#!!# no_from_hack replaced by check_string\n"; + $$hash{"check_string"} = ""; + } + delete $$hash{"from_hack"}; + } + + # ---- Changes to the smtp transport ---- + + elsif ($driver eq "smtp") + { + &abolished($hash, "An smtp transport", "mx_domains"); + &renamed($hash, "service", "port"); + &renamed($hash, "tls_verify_ciphers", "tls_require_ciphers"); + &renamed($hash, "authenticate_hosts", "hosts_try_auth"); + + if (defined $$hash{"batch_max"}) + { + print STDOUT "#!!# batch_max renamed connection_max_messages\n"; + $$hash{"connection_max_messages"} = $$hash{"batch_max"}; + delete $$hash{"batch_max"}; + } + + foreach $o ("hosts_try_auth", "hosts_avoid_tls", "hosts_require_tls", + "mx_domains", "serialize_hosts") + { + $$hash{$o} = &no_expand_regex($$hash{$o}) if defined $$hash{$o}; + } + } + + &outdriver($driverlist{"$prefix$name"}); + next; + } + + # Skip past any continuation lines for an option setting + while ($c[$i] =~ /\\\s*$/s) + { + $i++; + $i++ while ($c[$i] =~ /^\s*#/); + } + } + + +# -------- The retry configuration -------- + +$started = 0; +for ($i = $retry_start; $i < $clen && $i < $rewrite_start - 1; $i++) + { + if (!$started) + { + if ($c[$i] !~ /^\s*(#|$)/) + { + print STDOUT "\nbegin retry\n\n"; + $started = 1; + } + } + &print_no_expand($c[$i]); + } + +print STDOUT "\n# End of Exim 4 configuration\n"; + +print STDERR "\n*******************************************************\n"; +print STDERR "***** Please review the generated file carefully. *****\n"; +print STDERR "*******************************************************\n\n"; + +# End of convert4r4 + diff --git a/src/src/crypt16.c b/src/src/crypt16.c new file mode 100644 index 000000000..434135669 --- /dev/null +++ b/src/src/crypt16.c @@ -0,0 +1,77 @@ +/* $Cambridge: exim/src/src/crypt16.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/* + * Copyright (c) 2000-2002 + * Chris Adams + * written for HiWAAY Internet Services + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +/* + * Adapted for Exim by Tamas TEVESZ + * Further adapted by Philip Hazel to cut out this function for operating + * systems that have a built-in version. + */ + +/* The OS has a built-in crypt16(). Some compilers don't like compiling empty +modules, so keep them happy with a dummy when skipping the rest. */ + +#include "config.h" + +#ifdef HAVE_CRYPT16 +static void dummy(int x) { dummy(x-1); } +#else + +/* The OS doesn't have a built-in crypt16(). Compile this one. */ + +#include +#include +#include "os.h" + +#ifdef CRYPT_H +#include +#endif + +char *crypt16(char *key, char *salt) +{ + static char res[25]; + static char s2[3]; + char *p; + + /* Clear the string of any previous data */ + memset (res, 0, sizeof (res)); + + /* crypt the first part */ + p = crypt (key, salt); + strncpy (res, p, 13); + + if (strlen (key) > 8) + { + /* crypt the rest + * the first two characters of the first block (not counting + * the salt) make up the new salt */ + strncpy (s2, &(res[2]), 2); + p = crypt (&(key[8]), s2); + strncpy (&(res[13]), &(p[2]), 11); + memset (s2, 0, sizeof (s2)); + } + + return (res); +} +#endif + +/* End of crypt16.c */ diff --git a/src/src/daemon.c b/src/src/daemon.c new file mode 100644 index 000000000..d9375eabf --- /dev/null +++ b/src/src/daemon.c @@ -0,0 +1,1802 @@ +/* $Cambridge: exim/src/src/daemon.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Functions concerned with running Exim as a daemon */ + + +#include "exim.h" + + +/* Structure for holding data for each SMTP connection */ + +typedef struct smtp_slot { + pid_t pid; /* pid of the spawned reception process */ + uschar *host_address; /* address of the client host */ +} smtp_slot; + +/* An empty slot for initializing (Standard C does not allow constructor +expressions in assigments except as initializers in declarations). */ + +static smtp_slot empty_smtp_slot = { 0, NULL }; + + + +/************************************************* +* Local static variables * +*************************************************/ + +static volatile BOOL sigchld_seen; +static volatile BOOL sighup_seen; + +static int accept_retry_count = 0; +static int accept_retry_errno; +static BOOL accept_retry_select_failed; + +static int queue_run_count = 0; +static pid_t *queue_pid_slots; +static smtp_slot *smtp_slots; + +static BOOL write_pid = TRUE; + + + +/************************************************* +* SIGHUP Handler * +*************************************************/ + +/* All this handler does is to set a flag and re-enable the signal. + +Argument: the signal number +Returns: nothing +*/ + +static void +sighup_handler(int sig) +{ +sig = sig; /* Keep picky compilers happy */ +sighup_seen = TRUE; +signal(SIGHUP, sighup_handler); +} + + + +/************************************************* +* SIGCHLD handler for main daemon process * +*************************************************/ + +/* Don't re-enable the handler here, since we aren't doing the +waiting here. If the signal is re-enabled, there will just be an +infinite sequence of calls to this handler. The SIGCHLD signal is +used just as a means of waking up the daemon so that it notices +terminated subprocesses as soon as possible. + +Argument: the signal number +Returns: nothing +*/ + +static void +main_sigchld_handler(int sig) +{ +sig = sig; /* Keep picky compilers happy */ +sigchld_seen = TRUE; +signal(SIGCHLD, SIG_DFL); +} + + + + +/************************************************* +* Unexpected errors in SMTP calls * +*************************************************/ + +/* This function just saves a bit of repetitious coding. + +Arguments: + log_msg Text of message to be logged + smtp_msg Text of SMTP error message + was_errno The failing errno + +Returns: nothing +*/ + +static void +never_error(uschar *log_msg, uschar *smtp_msg, int was_errno) +{ +uschar *emsg = (was_errno <= 0)? US"" : + string_sprintf(": %s", strerror(was_errno)); +log_write(0, LOG_MAIN|LOG_PANIC, "%s%s", log_msg, emsg); +if (smtp_out != NULL) smtp_printf("421 %s\r\n", smtp_msg); +} + + + + +/************************************************* +* Handle a connected SMTP call * +*************************************************/ + +/* This function is called when an SMTP connection has been accepted. +If there are too many, give an error message and close down. Otherwise +spin off a sub-process to handle the call. The list of listening sockets +is required so that they can be closed in the sub-process. Take care not to +leak store in this process - reset the stacking pool at the end. + +Arguments: + listen_sockets sockets which are listening for incoming calls + listen_socket_count count of listening sockets + accept_socket socket of the current accepted call + accepted socket information about the current call + +Returns: nothing +*/ + +static void +handle_smtp_call(int *listen_sockets, int listen_socket_count, + int accept_socket, struct sockaddr *accepted) +{ +pid_t pid; +union sockaddr_46 interface_sockaddr; +SOCKLEN_T ifsize = sizeof(interface_sockaddr); +int dup_accept_socket = -1; +int max_for_this_host = 0; +int wfsize = 0; +int wfptr = 0; +int use_log_write_selector = log_write_selector; +uschar *whofrom = NULL; + +void *reset_point = store_get(0); + +/* Make the address available in ASCII representation, and also fish out +the remote port. */ + +sender_host_address = host_ntoa(-1, accepted, NULL, &sender_host_port); +DEBUG(D_any) debug_printf("Connection request from %s port %d\n", + sender_host_address, sender_host_port); + +/* Set up the output stream, check the socket has duplicated, and set up the +input stream. These operations fail only the exceptional circumstances. Note +that never_error() won't use smtp_out if it is NULL. */ + +smtp_out = fdopen(accept_socket, "wb"); +if (smtp_out == NULL) + { + never_error(US"daemon: fdopen() for smtp_out failed", US"", errno); + goto ERROR_RETURN; + } + +dup_accept_socket = dup(accept_socket); +if (dup_accept_socket < 0) + { + never_error(US"daemon: couldn't dup socket descriptor", + US"Connection setup failed", errno); + goto ERROR_RETURN; + } + +smtp_in = fdopen(dup_accept_socket, "rb"); +if (smtp_in == NULL) + { + never_error(US"daemon: fdopen() for smtp_in failed", + US"Connection setup failed", errno); + goto ERROR_RETURN; + } + +/* Get the data for the local interface address. */ + +if (getsockname(accept_socket, (struct sockaddr *)(&interface_sockaddr), + &ifsize) < 0) + { + log_write(0, LOG_MAIN|LOG_PANIC, "getsockname() failed: %s", + strerror(errno)); + smtp_printf("421 Local problem: getsockname() failed; please try again later\r\n"); + goto ERROR_RETURN; + } + +interface_address = host_ntoa(-1, &interface_sockaddr, NULL, &interface_port); +DEBUG(D_interface) debug_printf("interface address=%s port=%d\n", + interface_address, interface_port); + +/* Build a string identifying the remote host and, if requested, the port and +the local interface data. This is for logging; at the end of this function the +memory is reclaimed. */ + +whofrom = string_append(whofrom, &wfsize, &wfptr, 3, "[", sender_host_address, "]"); + +if ((log_extra_selector & LX_incoming_port) != 0) + whofrom = string_append(whofrom, &wfsize, &wfptr, 2, ":", string_sprintf("%d", + sender_host_port)); + +if ((log_extra_selector & LX_incoming_interface) != 0) + whofrom = string_append(whofrom, &wfsize, &wfptr, 4, " I=[", + interface_address, "]:", string_sprintf("%d", interface_port)); + +whofrom[wfptr] = 0; /* Terminate the newly-built string */ + +/* Check maximum number of connections. We do not check for reserved +connections or unacceptable hosts here. That is done in the subprocess because +it might take some time. */ + +if (smtp_accept_max > 0 && smtp_accept_count >= smtp_accept_max) + { + DEBUG(D_any) debug_printf("rejecting SMTP connection: count=%d max=%d\n", + smtp_accept_count, smtp_accept_max); + smtp_printf("421 Too many concurrent SMTP connections; " + "please try again later.\r\n"); + log_write(L_connection_reject, + LOG_MAIN, "Connection from %s refused: too many connections", + whofrom); + goto ERROR_RETURN; + } + +/* If a load limit above which only reserved hosts are acceptable is defined, +get the load average here, and if there are in fact no reserved hosts, do +the test right away (saves a fork). If there are hosts, do the check in the +subprocess because it might take time. */ + +if (smtp_load_reserve >= 0) + { + load_average = os_getloadavg(); + if (smtp_reserve_hosts == NULL && load_average > smtp_load_reserve) + { + DEBUG(D_any) debug_printf("rejecting SMTP connection: load average = %.2f\n", + (double)load_average/1000.0); + smtp_printf("421 Too much load; please try again later.\r\n"); + log_write(L_connection_reject, + LOG_MAIN, "Connection from %s refused: load average = %.2f", + whofrom, (double)load_average/1000.0); + goto ERROR_RETURN; + } + } + +/* Check that one specific host (strictly, IP address) is not hogging +resources. This is done here to prevent a denial of service attack by someone +forcing you to fork lots of times before denying service. The value of +smtp_accept_max_per_host is a string which is expanded. This makes it possible +to provide host-specific limits according to $sender_host address, but because +this is in the daemon mainline, only fast expansions (such as inline address +checks) should be used. The documentation is full of warnings. */ + +if (smtp_accept_max_per_host != NULL) + { + uschar *expanded = expand_string(smtp_accept_max_per_host); + if (expanded == NULL) + { + if (!expand_string_forcedfail) + log_write(0, LOG_MAIN|LOG_PANIC, "expansion of smtp_accept_max_per_host " + "failed for %s: %s", whofrom, expand_string_message); + } + /* For speed, interpret a decimal number inline here */ + else + { + uschar *s = expanded; + while (isdigit(*s)) + max_for_this_host = max_for_this_host * 10 + *s++ - '0'; + if (*s != 0) + log_write(0, LOG_MAIN|LOG_PANIC, "expansion of smtp_accept_max_per_host " + "for %s contains non-digit: %s", whofrom, expanded); + } + } + +/* If we have fewer connections than max_for_this_host, we can skip the tedious +per host_address checks. Note that at this stage smtp_accept_count contains the +count of *other* connections, not including this one. */ + +if ((max_for_this_host > 0) && + (smtp_accept_count >= max_for_this_host)) + { + int i; + int host_accept_count = 0; + int other_host_count = 0; /* keep a count of non matches to optimise */ + + for (i = 0; i < smtp_accept_max; ++i) + { + if (smtp_slots[i].host_address != NULL) + { + if (Ustrcmp(sender_host_address, smtp_slots[i].host_address) == 0) + host_accept_count++; + else + other_host_count++; + + /* Testing all these strings is expensive - see if we can drop out + early, either by hitting the target, or finding there are not enough + connections left to make the target. */ + + if ((host_accept_count >= max_for_this_host) || + ((smtp_accept_count - other_host_count) < max_for_this_host)) + break; + } + } + + if (host_accept_count >= max_for_this_host) + { + DEBUG(D_any) debug_printf("rejecting SMTP connection: too many from this " + "IP address: count=%d max=%d\n", + host_accept_count, max_for_this_host); + smtp_printf("421 Too many concurrent SMTP connections " + "from this IP address; please try again later.\r\n"); + log_write(L_connection_reject, + LOG_MAIN, "Connection from %s refused: too many connections " + "from that IP address", whofrom); + goto ERROR_RETURN; + } + } + +/* OK, the connection count checks have been passed. Before we can fork the +accepting process, we must first log the connection if requested. This logging +used to happen in the subprocess, but doing that means that the value of +smtp_accept_count can be out of step by the time it is logged. So we have to do +the logging here and accept the performance cost. Note that smtp_accept_count +hasn't yet been incremented to take account of this connection. + +In order to minimize the cost (because this is going to happen for every +connection), do a preliminary selector test here. This saves ploughing through +the generalized logging code each time when the selector is false. If the +selector is set, check whether the host is on the list for logging. If not, +arrange to unset the selector in the subprocess. */ + +if ((log_write_selector & L_smtp_connection) != 0) + { + uschar *list = hosts_connection_nolog; + if (list != NULL && verify_check_host(&list) == OK) + use_log_write_selector &= ~L_smtp_connection; + else + log_write(L_smtp_connection, LOG_MAIN, "SMTP connection from %s " + "(TCP/IP connection count = %d)", whofrom, smtp_accept_count + 1); + } + +/* Now we can fork the accepting process; do a lookup tidy, just in case any +expansion above did a lookup. */ + +search_tidyup(); +pid = fork(); + +/* Handle the child process */ + +if (pid == 0) + { + int i; + int queue_only_reason = 0; + int old_pool = store_pool; + BOOL local_queue_only; + #ifdef SA_NOCLDWAIT + struct sigaction act; + #endif + + /* May have been modified for the subprocess */ + + log_write_selector = use_log_write_selector; + + /* Get the local interface address into permanent store */ + + store_pool = POOL_PERM; + interface_address = string_copy(interface_address); + store_pool = old_pool; + + /* Check for a tls-on-connect port */ + + if (host_is_tls_on_connect_port(interface_port)) tls_on_connect = TRUE; + + /* Expand smtp_active_hostname if required. We do not do this any earlier, + because it may depend on the local interface address (indeed, that is most + likely what it depends on.) */ + + smtp_active_hostname = primary_hostname; + if (raw_active_hostname != NULL) + { + uschar *nah = expand_string(raw_active_hostname); + if (nah == NULL) + { + if (!expand_string_forcedfail) + { + log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand \"%s\" " + "(smtp_active_hostname): %s", raw_active_hostname, + expand_string_message); + smtp_printf("421 Local configuration error; " + "please try again later.\r\n"); + mac_smtp_fflush(); + search_tidyup(); + _exit(EXIT_FAILURE); + } + } + else if (nah[0] != 0) smtp_active_hostname = nah; + } + + /* Initialize the queueing flags */ + + queue_check_only(); + local_queue_only = queue_only; + + /* Close the listening sockets, and set the SIGCHLD handler to SIG_IGN. + We also attempt to set things up so that children are automatically reaped, + but just in case this isn't available, there's a paranoid waitpid() in the + loop too (except for systems where we are sure it isn't needed). See the more + extensive comment before the reception loop in exim.c for a fuller + explanation of this logic. */ + + for (i = 0; i < listen_socket_count; i++) close(listen_sockets[i]); + + #ifdef SA_NOCLDWAIT + act.sa_handler = SIG_IGN; + sigemptyset(&(act.sa_mask)); + act.sa_flags = SA_NOCLDWAIT; + sigaction(SIGCHLD, &act, NULL); + #else + signal(SIGCHLD, SIG_IGN); + #endif + + /* Attempt to get an id from the sending machine via the RFC 1413 + protocol. We do this in the sub-process in order not to hold up the + main process if there is any delay. Then set up the fullhost information + in case there is no HELO/EHLO. */ + + verify_get_ident(IDENT_PORT); + host_build_sender_fullhost(); + + DEBUG(D_any) + debug_printf("Process %d is handling incoming connection from %s\n", + (int)getpid(), sender_fullhost); + + /* If there are too many child processes for immediate delivery, + set the local_queue_only flag, which is initialized from the + configured value and may therefore already be TRUE. Leave logging + till later so it will have a message id attached. */ + + if (smtp_accept_queue > 0 && smtp_accept_count >= smtp_accept_queue) + { + local_queue_only = TRUE; + queue_only_reason = 1; + } + + /* Handle the start of the SMTP session, then loop, accepting incoming + messages from the SMTP connection. The end will come at the QUIT command, + when smtp_setup_msg() returns 0. A break in the connection causes the + process to die (see accept.c). */ + + if (!smtp_start_session()) + { + mac_smtp_fflush(); + search_tidyup(); + _exit(EXIT_SUCCESS); + } + + for (;;) + { + int rc; + message_id[0] = 0; /* Clear out any previous message_id */ + reset_point = store_get(0); /* Save current store high water point */ + + DEBUG(D_any) + debug_printf("Process %d is ready for new message\n", (int)getpid()); + + /* Smtp_setup_msg() returns 0 on QUIT or if the call is from an + unacceptable host or if an ACL "drop" command was triggered, -1 on + connection lost, and +1 on validly reaching DATA. Receive_msg() almost + always returns TRUE when smtp_input is true; just retry if no message was + accepted (can happen for invalid message parameters). However, it can yield + FALSE if the connection was forcibly dropped by the DATA ACL. */ + + if ((rc = smtp_setup_msg()) > 0) + { + BOOL ok = receive_msg(FALSE); + search_tidyup(); /* Close cached databases */ + if (!ok) /* Connection was dropped */ + { + mac_smtp_fflush(); + _exit(EXIT_SUCCESS); + } + if (message_id[0] == 0) continue; /* No message was accepted */ + } + else + { + mac_smtp_fflush(); + search_tidyup(); + _exit((rc == 0)? EXIT_SUCCESS : EXIT_FAILURE); + } + + /* Show the recipients when debugging */ + + DEBUG(D_receive) + { + int i; + if (sender_address != NULL) + debug_printf("Sender: %s\n", sender_address); + if (recipients_list != NULL) + { + debug_printf("Recipients:\n"); + for (i = 0; i < recipients_count; i++) + debug_printf(" %s\n", recipients_list[i].address); + } + } + + /* A message has been accepted. Clean up any previous delivery processes + that have completed and are defunct, on systems where they don't go away + by themselves (see comments when setting SIG_IGN above). On such systems + (if any) these delivery processes hang around after termination until + the next message is received. */ + + #ifndef SIG_IGN_WORKS + while (waitpid(-1, NULL, WNOHANG) > 0); + #endif + + /* Reclaim up the store used in accepting this message */ + + store_reset(reset_point); + + /* If queue_only is set or if there are too many incoming connections in + existence, local_queue_only will be TRUE. If it is not, check whether we + have received too many messages in this session for immediate delivery. If + not, and queue_only_load is set, check that the load average is below it. + Note that, once set, local_queue_only remains set for any subsequent + messages on the same SMTP connection. This is a deliberate choice; even + though the load average may fall, it doesn't seem right to deliver later + messages on the same call when not delivering earlier ones. */ + + if (!local_queue_only) + { + if (smtp_accept_queue_per_connection > 0 && + receive_messagecount > smtp_accept_queue_per_connection) + { + local_queue_only = TRUE; + queue_only_reason = 2; + } + else if (queue_only_load >= 0) + { + local_queue_only = (load_average = os_getloadavg()) > queue_only_load; + if (local_queue_only) queue_only_reason = 3; + } + } + + /* Log the queueing here, when it will get a message id attached, but + not if queue_only is set (case 0). */ + + if (local_queue_only) switch(queue_only_reason) + { + case 1: + log_write(L_delay_delivery, + LOG_MAIN, "no immediate delivery: too many connections " + "(%d, max %d)", smtp_accept_count, smtp_accept_queue); + break; + + case 2: + log_write(L_delay_delivery, + LOG_MAIN, "no immediate delivery: more than %d messages " + "received in one connection", smtp_accept_queue_per_connection); + break; + + case 3: + log_write(L_delay_delivery, + LOG_MAIN, "no immediate delivery: load average %.2f", + (double)load_average/1000.0); + break; + } + + /* If a delivery attempt is required, spin off a new process to handle it. + If we are not root, we have to re-exec exim unless deliveries are being + done unprivileged. */ + + else if (!queue_only_policy && !deliver_freeze) + { + pid_t dpid; + + /* Before forking, ensure that the C output buffer is flushed. Otherwise + anything that it in it will get duplicated, leading to duplicate copies + of the pending output. */ + + mac_smtp_fflush(); + + if ((dpid = fork()) == 0) + { + fclose(smtp_in); + fclose(smtp_out); + + /* Don't ever molest the parent's SSL connection, but do clean up + the data structures if necessary. */ + + #ifdef SUPPORT_TLS + tls_close(FALSE); + #endif + + /* Reset SIGHUP and SIGCHLD in the child in both cases. */ + + signal(SIGHUP, SIG_DFL); + signal(SIGCHLD, SIG_DFL); + + if (geteuid() != root_uid && !deliver_drop_privilege) + { + signal(SIGALRM, SIG_DFL); + (void)child_exec_exim(CEE_EXEC_PANIC, FALSE, NULL, FALSE, 2, US"-Mc", + message_id); + /* Control does not return here. */ + } + + /* No need to re-exec; SIGALRM remains set to the default handler */ + + (void)deliver_message(message_id, FALSE, FALSE); + search_tidyup(); + _exit(EXIT_SUCCESS); + } + + if (dpid > 0) + { + DEBUG(D_any) debug_printf("forked delivery process %d\n", (int)dpid); + } + else + { + log_write(0, LOG_MAIN|LOG_PANIC, "daemon: delivery process fork " + "failed: %s", strerror(errno)); + } + } + } + } + + +/* Carrying on in the parent daemon process... Can't do much if the fork +failed. Otherwise, keep count of the number of accepting processes and +remember the pid for ticking off when the child completes. */ + +if (pid < 0) + { + never_error(US"daemon: accept process fork failed", US"Fork failed", errno); + } +else + { + int i; + for (i = 0; i < smtp_accept_max; ++i) + { + if (smtp_slots[i].pid <= 0) + { + smtp_slots[i].pid = pid; + if (smtp_accept_max_per_host != NULL) + smtp_slots[i].host_address = string_copy_malloc(sender_host_address); + smtp_accept_count++; + break; + } + } + DEBUG(D_any) debug_printf("%d SMTP accept process%s running\n", + smtp_accept_count, (smtp_accept_count == 1)? "" : "es"); + } + +/* Get here via goto in error cases */ + +ERROR_RETURN: + +/* Close the streams associated with the socket which will also close the +socket fds in this process. We can't do anything if fclose() fails, but +logging brings it to someone's attention. However, "connection reset by peer" +isn't really a problem, so skip that one. If the streams don't exist, something +went wrong while setting things up. Make sure the socket descriptors are +closed, in order to drop the connection. */ + +if (smtp_out != NULL) + { + if (fclose(smtp_out) != 0 && errno != ECONNRESET) + log_write(0, LOG_MAIN|LOG_PANIC, "daemon: fclose(smtp_out) failed: %s", + strerror(errno)); + smtp_out = NULL; + } +else close(accept_socket); + +if (smtp_in != NULL) + { + if (fclose(smtp_in) != 0 && errno != ECONNRESET) + log_write(0, LOG_MAIN|LOG_PANIC, "daemon: fclose(smtp_in) failed: %s", + strerror(errno)); + smtp_in = NULL; + } +else close(dup_accept_socket); + +/* Release any store used in this process, including the store used for holding +the incoming host address and an expanded active_hostname. */ + +store_reset(reset_point); +sender_host_address = NULL; +} + + + + +/************************************************* +* Check wildcard listen special cases * +*************************************************/ + +/* This function is used when binding and listening on lists of addresses and +ports. It tests for special cases of wildcard listening, when IPv4 and IPv6 +sockets may interact in different ways in different operating systems. It is +passed an error number, the list of listening addresses, and the current +address. Two checks are available: for a previous wildcard IPv6 address, or for +a following wildcard IPv4 address, in both cases on the same port. + +In practice, pairs of wildcard addresses should be adjacent in the address list +because they are sorted that way below. + +Arguments: + eno the error number + addresses the list of addresses + ipa the current IP address + back if TRUE, check for previous wildcard IPv6 address + if FALSE, check for a following wildcard IPv4 address + +Returns: TRUE or FALSE +*/ + +static BOOL +check_special_case(int eno, ip_address_item *addresses, ip_address_item *ipa, + BOOL back) +{ +ip_address_item *ipa2; + +/* For the "back" case, if the failure was "address in use" for a wildcard IPv4 +address, seek a previous IPv6 wildcard address on the same port. As it is +previous, it must have been successfully bound and be listening. Flag it as a +"6 including 4" listener. */ + +if (back) + { + if (eno != EADDRINUSE || ipa->address[0] != 0) return FALSE; + for (ipa2 = addresses; ipa2 != ipa; ipa2 = ipa2->next) + { + if (ipa2->address[1] == 0 && ipa2->port == ipa->port) + { + ipa2->v6_include_v4 = TRUE; + return TRUE; + } + } + } + +/* For the "forward" case, if the current address is a wildcard IPv6 address, +we seek a following wildcard IPv4 address on the same port. */ + +else + { + if (ipa->address[0] != ':' || ipa->address[1] != 0) return FALSE; + for (ipa2 = ipa->next; ipa2 != NULL; ipa2 = ipa2->next) + if (ipa2->address[0] == 0 && ipa->port == ipa2->port) return TRUE; + } + +return FALSE; +} + + + + + + +/************************************************* +* Exim Daemon Mainline * +*************************************************/ + +/* The daemon can do two jobs, either of which is optional: + +(1) Listens for incoming SMTP calls and spawns off a sub-process to handle +each one. This is requested by the -bd option, with -oX specifying the SMTP +port on which to listen (for testing). + +(2) Spawns a queue-running process every so often. This is controlled by the +-q option with a an interval time. (If no time is given, a single queue run +is done from the main function, and control doesn't get here.) + +Root privilege is required in order to attach to port 25. Some systems require +it when calling socket() rather than bind(). To cope with all cases, we run as +root for both socket() and bind(). Some systems also require root in order to +write to the pid file directory. This function must therefore be called as root +if it is to work properly in all circumstances. Once the socket is bound and +the pid file written, root privilege is given up if there is an exim uid. + +There are no arguments to this function, and it never returns. */ + +void +daemon_go(void) +{ +int *listen_sockets = NULL; +int listen_socket_count = 0; +ip_address_item *addresses = NULL; + +/* If any debugging options are set, turn on the D_pid bit so that all +debugging lines get the pid added. */ + +DEBUG(D_any|D_v) debug_selector |= D_pid; + + +/* Do the preparation for setting up a listener on one or more interfaces, and +possible on various ports. This is controlled by the combination of +local_interfaces (which can set IP addresses and ports) and daemon_smtp_port +(which is a list of default ports to use for those items in local_interfaces +that do not specify a port). The -oX command line option can be used to +override one or both of these options. + +If local_interfaces is not set, the default is to listen on all interfaces. +When it is set, it can include "all IPvx interfaces" as an item. This is useful +when different ports are in use. + +It turns out that listening on all interfaces is messy in an IPv6 world, +because several different implementation approaches have been taken. This code +is now supposed to work with all of them. The point of difference is whether an +IPv6 socket that is listening on all interfaces will receive incoming IPv4 +calls or not. We also have to cope with the case when IPv6 libraries exist, but +there is no IPv6 support in the kernel. + +. On Solaris, an IPv6 socket will accept IPv4 calls, and give them as mapped + addresses. However, if an IPv4 socket is also listening on all interfaces, + calls are directed to the appropriate socket. + +. On (some versions of) Linux, an IPv6 socket will accept IPv4 calls, and + give them as mapped addresses, but an attempt also to listen on an IPv4 + socket on all interfaces causes an error. + +. On OpenBSD, an IPv6 socket will not accept IPv4 calls. You have to set up + two sockets if you want to accept both kinds of call. + +. FreeBSD is like OpenBSD, but it has the IPV6_V6ONLY socket option, which + can be turned off, to make it behave like the versions of Linux described + above. + +. I heard a report that the USAGI IPv6 stack for Linux has implemented + IPV6_V6ONLY. + +So, what we do when IPv6 is supported is as follows: + + (1) After it is set up, the list of interfaces is scanned for wildcard + addresses. If an IPv6 and an IPv4 wildcard are both found for the same + port, the list is re-arranged so that they are together, with the IPv6 + wildcard first. + + (2) If the creation of a wildcard IPv6 socket fails, we just log the error and + carry on if an IPv4 wildcard socket for the same port follows later in the + list. This allows Exim to carry on in the case when the kernel has no IPv6 + support. + + (3) Having created an IPv6 wildcard socket, we try to set IPV6_V6ONLY if that + option is defined. However, if setting fails, carry on regardless (but log + the incident). + + (4) If binding or listening on an IPv6 wildcard socket fails, it is a serious + error. + + (5) If binding or listening on an IPv4 wildcard socket fails with the error + EADDRINUSE, and a previous interface was an IPv6 wildcard for the same + port (which must have succeeded or we wouldn't have got this far), we + assume we are in the situation where just a single socket is permitted, + and ignore the error. + +Phew! + +The preparation code decodes options and sets up the relevant data. We do this +first, so that we can return non-zero if there are any syntax errors, and also +write to stderr. */ + +if (daemon_listen) + { + int *default_smtp_port; + int sep; + int pct = 0; + uschar *s; + uschar *list; + uschar *local_iface_source = US"local_interfaces"; + ip_address_item *ipa; + ip_address_item **pipa; + + /* If any option requiring a load average to be available during the + reception of a message is set, call os_getloadavg() while we are root + for those OS for which this is necessary the first time it is called (in + order to perform an "open" on the kernel memory file). */ + + #ifdef LOAD_AVG_NEEDS_ROOT + if (queue_only_load >= 0 || smtp_load_reserve >= 0) (void)os_getloadavg(); + #endif + + /* If -oX was used, disable the writing of a pid file unless -oP was + explicitly used to force it. Then scan the string given to -oX. Any items + that contain neither a dot nor a colon are used to override daemon_smtp_port. + Any other items are used to override local_interfaces. */ + + if (override_local_interfaces != NULL) + { + uschar *new_smtp_port = NULL; + uschar *new_local_interfaces = NULL; + int portsize = 0; + int portptr = 0; + int ifacesize = 0; + int ifaceptr = 0; + + if (override_pid_file_path == NULL) write_pid = FALSE; + + list = override_local_interfaces; + sep = 0; + while ((s = string_nextinlist(&list,&sep,big_buffer,big_buffer_size)) + != NULL) + { + uschar joinstr[4]; + uschar **ptr; + int *sizeptr; + int *ptrptr; + + if (Ustrpbrk(s, ".:") == NULL) + { + ptr = &new_smtp_port; + sizeptr = &portsize; + ptrptr = &portptr; + } + else + { + ptr = &new_local_interfaces; + sizeptr = &ifacesize; + ptrptr = &ifaceptr; + } + + if (*ptr == NULL) + { + joinstr[0] = sep; + joinstr[1] = ' '; + *ptr = string_cat(*ptr, sizeptr, ptrptr, US"<", 1); + } + + *ptr = string_cat(*ptr, sizeptr, ptrptr, joinstr, 2); + *ptr = string_cat(*ptr, sizeptr, ptrptr, s, Ustrlen(s)); + } + + if (new_smtp_port != NULL) + { + new_smtp_port[portptr] = 0; + daemon_smtp_port = new_smtp_port; + DEBUG(D_any) debug_printf("daemon_smtp_port overridden by -oX:\n %s\n", + daemon_smtp_port); + } + + if (new_local_interfaces != NULL) + { + new_local_interfaces[ifaceptr] = 0; + local_interfaces = new_local_interfaces; + local_iface_source = US"-oX data"; + DEBUG(D_any) debug_printf("local_interfaces overridden by -oX:\n %s\n", + local_interfaces); + } + } + + /* Create a list of default SMTP ports, to be used if local_interfaces + contains entries without explict ports. First count the number of ports, then + build a translated list in a vector. */ + + list = daemon_smtp_port; + sep = 0; + while ((s = string_nextinlist(&list,&sep,big_buffer,big_buffer_size)) != NULL) + pct++; + default_smtp_port = store_get((pct+1) * sizeof(int)); + list = daemon_smtp_port; + sep = 0; + for (pct = 0; + (s = string_nextinlist(&list,&sep,big_buffer,big_buffer_size)) != NULL; + pct++) + { + if (isdigit(*s)) + { + uschar *end; + default_smtp_port[pct] = Ustrtol(s, &end, 0); + if (end != s + Ustrlen(s)) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG, "invalid SMTP port: %s", s); + } + else + { + struct servent *smtp_service = getservbyname(CS s, "tcp"); + if (smtp_service == NULL) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG, "TCP port \"%s\" not found", s); + default_smtp_port[pct] = ntohs(smtp_service->s_port); + } + } + default_smtp_port[pct] = 0; + + /* Create the list of local interfaces, possibly with ports included. This + list may contain references to 0.0.0.0 and ::0 as wildcards. These special + values are converted below. */ + + addresses = host_build_ifacelist(local_interfaces, local_iface_source); + + /* In the list of IP addresses, convert 0.0.0.0 into an empty string, and ::0 + into the string ":". We use these to recognize wildcards in IPv4 and IPv6. In + fact, many IP stacks recognize 0.0.0.0 and ::0 and handle them as wildcards + anyway, but we need to know which are the wildcard addresses, and the shorter + strings are neater. + + In the same scan, fill in missing port numbers from the default list. When + there is more than one item in the list, extra items are created. */ + + for (ipa = addresses; ipa != NULL; ipa = ipa->next) + { + int i; + + if (Ustrcmp(ipa->address, "0.0.0.0") == 0) ipa->address[0] = 0; + else if (Ustrcmp(ipa->address, "::0") == 0) + { + ipa->address[0] = ':'; + ipa->address[1] = 0; + } + + if (ipa->port > 0) continue; + + if (daemon_smtp_port[0] <= 0) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "no port specified for interface " + "%s and daemon_smtp_port is unset; cannot start daemon", + (ipa->address[0] == 0)? US"\"all IPv4\"" : + (ipa->address[1] == 0)? US"\"all IPv6\"" : ipa->address); + ipa->port = default_smtp_port[0]; + for (i = 1; default_smtp_port[i] > 0; i++) + { + ip_address_item *new = store_get(sizeof(ip_address_item)); + memcpy(new->address, ipa->address, Ustrlen(ipa->address) + 1); + new->port = default_smtp_port[i]; + new->next = ipa->next; + ipa->next = new; + ipa = new; + } + } + + /* Scan the list of addresses for wildcards. If we find an IPv4 and an IPv6 + wildcard for the same port, ensure that (a) they are together and (b) the + IPv6 address comes first. This makes handling the messy features easier, and + also simplifies the construction of the "daemon started" log line. */ + + pipa = &addresses; + for (ipa = addresses; ipa != NULL; pipa = &(ipa->next), ipa = ipa->next) + { + ip_address_item *ipa2; + + /* Handle an IPv4 wildcard */ + + if (ipa->address[0] == 0) + { + for (ipa2 = ipa; ipa2->next != NULL; ipa2 = ipa2->next) + { + ip_address_item *ipa3 = ipa2->next; + if (ipa3->address[0] == ':' && + ipa3->address[1] == 0 && + ipa3->port == ipa->port) + { + ipa2->next = ipa3->next; + ipa3->next = ipa; + *pipa = ipa3; + break; + } + } + } + + /* Handle an IPv6 wildcard. */ + + else if (ipa->address[0] == ':' && ipa->address[1] == 0) + { + for (ipa2 = ipa; ipa2->next != NULL; ipa2 = ipa2->next) + { + ip_address_item *ipa3 = ipa2->next; + if (ipa3->address[0] == 0 && ipa3->port == ipa->port) + { + ipa2->next = ipa3->next; + ipa3->next = ipa->next; + ipa->next = ipa3; + ipa = ipa3; + break; + } + } + } + } + + /* Get a vector to remember all the sockets in */ + + for (ipa = addresses; ipa != NULL; ipa = ipa->next) + listen_socket_count++; + listen_sockets = store_get(sizeof(int *) * listen_socket_count); + + /* Do a sanity check on the max connects value just to save us from getting + a huge amount of store. */ + + if (smtp_accept_max > 4095) smtp_accept_max = 4096; + + /* There's no point setting smtp_accept_queue unless it is less than the max + connects limit. The configuration reader ensures that the max is set if the + queue-only option is set. */ + + if (smtp_accept_queue > smtp_accept_max) smtp_accept_queue = 0; + + /* Get somewhere to keep the list of SMTP accepting pids if we are keeping + track of them for total number and queue/host limits. */ + + if (smtp_accept_max > 0) + { + int i; + smtp_slots = store_get(smtp_accept_max * sizeof(smtp_slot)); + for (i = 0; i < smtp_accept_max; i++) smtp_slots[i] = empty_smtp_slot; + } + } + +/* We now close all open file descriptors that we know about, and disconnect +from the controlling terminal, unless background_daemon is unset. This is +always unset when debugging, but can also be forced. Most modern Unixes seem to +have setsid() for getting rid of the controlling terminal. For any OS that +doesn't, setsid() can be #defined as a no-op, or as something else. */ + +if (background_daemon) + { + log_close_all(); /* Just in case anything was logged earlier */ + search_tidyup(); /* Just in case any were used in reading the config. */ + close(0); /* Get rid of stdin/stdout/stderr */ + close(1); + close(2); + log_stderr = NULL; /* So no attempt to copy paniclog output */ + + /* If the parent process of this one has pid == 1, we are re-initializing the + daemon as the result of a SIGHUP. In this case, there is no need to do any + forking, because the controlling terminal has long gone. Otherwise, fork, + in case current process is a process group leader (see 'man setsid' for an + explanation). */ + + if (getppid() != 1) + { + pid_t pid = fork(); + if (pid < 0) log_write(0, LOG_MAIN|LOG_PANIC_DIE, + "fork() failed when starting daemon: %s", strerror(errno)); + if (pid > 0) exit(EXIT_SUCCESS); /* in parent process, just exit */ + (void)setsid(); /* release controlling terminal */ + } + } + +/* We are now in the disconnected, daemon process (unless debugging). Set up +the listening sockets if required. */ + +if (daemon_listen) + { + int sk; + int on = 1; + ip_address_item *ipa; + + /* For each IP address, create a socket, bind it to the appropriate port, and + start listening. See comments above about IPv6 sockets that may or may not + accept IPv4 calls when listening on all interfaces. We also have to cope with + the case of a system with IPv6 libraries, but no IPv6 support in the kernel. + listening, provided a wildcard IPv4 socket for the same port follows. */ + + for (ipa = addresses, sk = 0; sk < listen_socket_count; ipa = ipa->next, sk++) + { + BOOL wildcard; + ip_address_item *ipa2; + int retries = 9; + int af; + + if (Ustrchr(ipa->address, ':') != NULL) + { + af = AF_INET6; + wildcard = ipa->address[1] == 0; + } + else + { + af = AF_INET; + wildcard = ipa->address[0] == 0; + } + + listen_sockets[sk] = ip_socket(SOCK_STREAM, af); + if (listen_sockets[sk] < 0) + { + if (check_special_case(0, addresses, ipa, FALSE)) + { + log_write(0, LOG_MAIN, "Failed to create IPv6 socket for wildcard " + "listening (%s): will use IPv4", strerror(errno)); + goto SKIP_SOCKET; + } + log_write(0, LOG_PANIC_DIE, "IPv%c socket creation failed: %s", + (af == AF_INET6)? '6' : '4', strerror(errno)); + } + + /* If this is an IPv6 wildcard socket, set IPV6_V6ONLY if that option is + available. Just log failure (can get protocol not available, just like + socket creation can). */ + + #ifdef IPV6_V6ONLY + if (af == AF_INET6 && wildcard && + setsockopt(listen_sockets[sk], IPPROTO_IPV6, IPV6_V6ONLY, (char *)(&on), + sizeof(on)) < 0) + log_write(0, LOG_MAIN, "Setting IPV6_V6ONLY on daemon's IPv6 wildcard " + "socket failed (%s): carrying on without it", strerror(errno)); + #endif /* IPV6_V6ONLY */ + + /* Set SO_REUSEADDR so that the daemon can be restarted while a connection + is being handled. Without this, a connection will prevent reuse of the + smtp port for listening. */ + + if (setsockopt(listen_sockets[sk], SOL_SOCKET, SO_REUSEADDR, + (uschar *)(&on), sizeof(on)) < 0) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "setting SO_REUSEADDR on socket " + "failed when starting daemon: %s", strerror(errno)); + + /* Set TCP_NODELAY; Exim does its own buffering. There is a switch to + disable this because it breaks some broken clients. */ + + if (tcp_nodelay) setsockopt(listen_sockets[sk], IPPROTO_TCP, TCP_NODELAY, + (uschar *)(&on), sizeof(on)); + + /* Now bind the socket to the required port; if Exim is being restarted + it may not always be possible to bind immediately, even with SO_REUSEADDR + set, so try 10 times, waiting between each try. After 10 failures, we give + up. In an IPv6 environment, if bind () fails with the error EADDRINUSE and + we are doing wildcard IPv4 listening and there was a previous IPv6 wildcard + address for the same port, ignore the error on the grounds that we must be + in a system where the IPv6 socket accepts both kinds of call. This is + necessary for (some release of) USAGI Linux; other IP stacks fail at the + listen() stage instead. */ + + for(;;) + { + uschar *msg, *addr; + if (ip_bind(listen_sockets[sk], af, ipa->address, ipa->port) >= 0) break; + if (check_special_case(errno, addresses, ipa, TRUE)) + { + DEBUG(D_any) debug_printf("wildcard IPv4 bind() failed after IPv6 " + "listen() success; EADDRINUSE ignored\n"); + close(listen_sockets[sk]); + goto SKIP_SOCKET; + } + msg = US strerror(errno); + addr = wildcard? ((af == AF_INET6)? US"(any IPv6)" : US"(any IPv4)") : + ipa->address; + if (retries-- <= 0) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, + "socket bind() to port %d for address %s failed: %s: " + "daemon abandoned", ipa->port, addr, msg); + log_write(0, LOG_MAIN, "socket bind() to port %d for address %s " + "failed: %s: waiting before trying again", ipa->port, addr, msg); + sleep(30); + } + + DEBUG(D_any) + { + if (wildcard) + debug_printf("listening on all interfaces (IPv%c) port %d\n", + (af == AF_INET6)? '6' : '4', ipa->port); + else + debug_printf("listening on %s port %d\n", ipa->address, ipa->port); + } + + /* Start listening on the bound socket, establishing the maximum backlog of + connections that is allowed. On success, continue to the next address. */ + + if (listen(listen_sockets[sk], smtp_connect_backlog) >= 0) continue; + + /* Listening has failed. In an IPv6 environment, as for bind(), if listen() + fails with the error EADDRINUSE and we are doing IPv4 wildcard listening + and there was a previous successful IPv6 wildcard listen on the same port, + we want to ignore the error on the grounds that we must be in a system + where the IPv6 socket accepts both kinds of call. */ + + if (!check_special_case(errno, addresses, ipa, TRUE)) + log_write(0, LOG_PANIC_DIE, "listen() failed on interface %s: %s", + wildcard? ((af == AF_INET6)? US"(any IPv6)" : US"(any IPv4)") : + ipa->address, + strerror(errno)); + + DEBUG(D_any) debug_printf("wildcard IPv4 listen() failed after IPv6 " + "listen() success; EADDRINUSE ignored\n"); + close(listen_sockets[sk]); + + /* Come here if there has been a problem with the socket which we + are going to ignore. We remove the address from the chain, and back up the + counts. */ + + SKIP_SOCKET: + sk--; /* Back up the count */ + listen_socket_count--; /* Reduce the total */ + if (ipa == addresses) addresses = ipa->next; else + { + for (ipa2 = addresses; ipa2->next != ipa; ipa2 = ipa2->next); + ipa2->next = ipa->next; + ipa = ipa2; + } + } /* End of bind/listen loop for each address */ + } /* End of setup for listening */ + + +/* If we are not listening, we want to write a pid file only if -oP was +explicitly given. */ + +else if (override_pid_file_path == NULL) write_pid = FALSE; + +/* Write the pid to a known file for assistance in identification, if required. +We do this before giving up root privilege, because on some systems it is +necessary to be root in order to write into the pid file directory. There's +nothing to stop multiple daemons running, as long as no more than one listens +on a given TCP/IP port on the same interface(s). However, in these +circumstances it gets far too complicated to mess with pid file names +automatically. Consequently, Exim 4 writes a pid file only + + (a) When running in the test harness, or + (b) When -bd is used and -oX is not used, or + (c) When -oP is used to supply a path. + +The variable daemon_write_pid is used to control this. */ + +if (running_in_test_harness || write_pid) + { + FILE *f; + + if (override_pid_file_path != NULL) + pid_file_path = override_pid_file_path; + + if (pid_file_path[0] == 0) + pid_file_path = string_sprintf("%s/exim-daemon.pid", spool_directory); + + f = Ufopen(pid_file_path, "wb"); + if (f != NULL) + { + fprintf(f, "%d\n", (int)getpid()); + fchmod(fileno(f), 0644); + fclose(f); + DEBUG(D_any) debug_printf("pid written to %s\n", pid_file_path); + } + else + { + DEBUG(D_any) + debug_printf("%s\n", string_open_failed(errno, "pid file %s", + pid_file_path)); + } + } + +/* Set up the handler for SIGHUP, which causes a restart of the daemon. */ + +sighup_seen = FALSE; +signal(SIGHUP, sighup_handler); + +/* Give up root privilege at this point (assuming that exim_uid and exim_gid +are not root). The third argument controls the running of initgroups(). +Normally we do this, in order to set up the groups for the Exim user. However, +if we are not root at this time - some odd installations run that way - we +cannot do this. */ + +exim_setugid(exim_uid, exim_gid, geteuid()==root_uid, US"running as a daemon"); + +/* Get somewhere to keep the list of queue-runner pids if we are keeping track +of them (and also if we are doing queue runs). */ + +if (queue_interval > 0 && queue_run_max > 0) + { + int i; + queue_pid_slots = store_get(queue_run_max * sizeof(pid_t)); + for (i = 0; i < queue_run_max; i++) queue_pid_slots[i] = 0; + } + +/* Set up the handler for termination of child processes. */ + +sigchld_seen = FALSE; +signal(SIGCHLD, main_sigchld_handler); + +/* If we are to run the queue periodically, pretend the alarm has just gone +off. This will cause the first queue-runner to get kicked off straight away. */ + +sigalrm_seen = (queue_interval > 0); + +/* Log the start up of a daemon - at least one of listening or queue running +must be set up. */ + +if (daemon_listen) + { + int i, j; + int smtp_ports = 0; + int smtps_ports = 0; + ip_address_item *ipa; + uschar *p = big_buffer; + uschar *qinfo = (queue_interval > 0)? + string_sprintf("-q%s", readconf_printtime(queue_interval)) + : + US"no queue runs"; + + /* Build a list of listening addresses in big_buffer, but limit it to 10 + items. The style is for backwards compatibility. + + It is now possible to have some ports listening for SMTPS (the old, + deprecated protocol that starts TLS without using STARTTLS), and others + listening for standard SMTP. Keep their listings separate. */ + + for (j = 0; j < 2; j++) + { + for (i = 0, ipa = addresses; i < 10 && ipa != NULL; i++, ipa = ipa->next) + { + /* First time round, look for SMTP ports; second time round, look for + SMTPS ports. For the first one of each, insert leading text. */ + + if (host_is_tls_on_connect_port(ipa->port) == (j > 0)) + { + if (j == 0) + { + if (smtp_ports++ == 0) + { + memcpy(p, "SMTP on", 8); + p += 7; + } + } + else + { + if (smtps_ports++ == 0) + { + (void)sprintf(CS p, "%sSMTPS on", + (smtp_ports == 0)? "":" and for "); + while (*p != 0) p++; + } + } + + /* Now the information about the port (and sometimes interface) */ + + if (ipa->address[0] == ':' && ipa->address[1] == 0) + { + if (ipa->next != NULL && ipa->next->address[0] == 0 && + ipa->next->port == ipa->port) + { + (void)sprintf(CS p, " port %d (IPv6 and IPv4)", ipa->port); + ipa = ipa->next; + } + else if (ipa->v6_include_v4) + (void)sprintf(CS p, " port %d (IPv6 with IPv4)", ipa->port); + else + (void)sprintf(CS p, " port %d (IPv6)", ipa->port); + } + else if (ipa->address[0] == 0) + (void)sprintf(CS p, " port %d (IPv4)", ipa->port); + else + (void)sprintf(CS p, " [%s]:%d", ipa->address, ipa->port); + while (*p != 0) p++; + } + } + + if (ipa != NULL) + { + memcpy(p, " ...", 5); + p += 4; + } + } + + log_write(0, LOG_MAIN, + "exim %s daemon started: pid=%d, %s, listening for %s", + version_string, getpid(), qinfo, big_buffer); + set_process_info("daemon: %s, listening for %s", qinfo, big_buffer); + } + +else + { + log_write(0, LOG_MAIN, + "exim %s daemon started: pid=%d, -q%s, not listening for SMTP", + version_string, getpid(), readconf_printtime(queue_interval)); + set_process_info("daemon: -q%s, not listening", + readconf_printtime(queue_interval)); + } + + +/* Close the log so it can be renamed and moved. In the few cases below where +this long-running process writes to the log (always exceptional conditions), it +closes the log afterwards, for the same reason. */ + +log_close_all(); + +DEBUG(D_any) debug_print_ids(US"daemon running with"); + +/* Any messages accepted via this route are going to be SMTP. */ + +smtp_input = TRUE; + +/* Enter the never-ending loop... */ + +for (;;) + { + #if HAVE_IPV6 + struct sockaddr_in6 accepted; + #else + struct sockaddr_in accepted; + #endif + + SOCKLEN_T len = sizeof(accepted); + int status; + pid_t pid; + + /* This code is placed first in the loop, so that it gets obeyed at the + start, before the first wait. This causes the first queue-runner to be + started immediately. */ + + if (sigalrm_seen) + { + DEBUG(D_any) debug_printf("SIGALRM received\n"); + + /* Do a full queue run in a child process, if required, unless we already + have enough queue runners on the go. If we are not running as root, a + re-exec is required. */ + + if (queue_interval > 0 && + (queue_run_max <= 0 || queue_run_count < queue_run_max)) + { + if ((pid = fork()) == 0) + { + int sk; + DEBUG(D_any) debug_printf("Starting queue-runner: pid %d\n", + (int)getpid()); + + /* Close any open listening sockets in the child */ + + for (sk = 0; sk < listen_socket_count; sk++) close(listen_sockets[sk]); + + /* Reset SIGHUP and SIGCHLD in the child in both cases. */ + + signal(SIGHUP, SIG_DFL); + signal(SIGCHLD, SIG_DFL); + + /* Re-exec if privilege has been given up, unless deliver_drop_ + privilege is set. Reset SIGALRM before exec(). */ + + if (geteuid() != root_uid && !deliver_drop_privilege) + { + uschar opt[8]; + uschar *p = opt; + + signal(SIGALRM, SIG_DFL); + *p++ = '-'; + *p++ = 'q'; + if (queue_2stage) *p++ = 'q'; + if (queue_run_first_delivery) *p++ = 'i'; + if (queue_run_force) *p++ = 'f'; + if (deliver_force_thaw) *p++ = 'f'; + if (queue_run_local) *p++ = 'l'; + *p = 0; + + (void)child_exec_exim(CEE_EXEC_PANIC, FALSE, NULL, TRUE, 1, opt); + /* Control never returns here. */ + } + + /* No need to re-exec; SIGALRM remains set to the default handler */ + + queue_run(NULL, NULL, FALSE); + _exit(EXIT_SUCCESS); + } + + if (pid < 0) + { + log_write(0, LOG_MAIN|LOG_PANIC, "daemon: fork of queue-runner " + "process failed: %s", strerror(errno)); + log_close_all(); + } + else + { + int i; + for (i = 0; i < queue_run_max; ++i) + { + if (queue_pid_slots[i] <= 0) + { + queue_pid_slots[i] = pid; + queue_run_count++; + break; + } + } + DEBUG(D_any) debug_printf("%d queue-runner process%s running\n", + queue_run_count, (queue_run_count == 1)? "" : "es"); + } + } + + /* Reset the alarm clock */ + + sigalrm_seen = FALSE; + alarm(queue_interval); + } + + + /* Sleep till a connection happens if listening, and handle the connection if + that is why we woke up. The FreeBSD operating system requires the use of + select() before accept() because the latter function is not interrupted by + a signal, and we want to wake up for SIGCHLD and SIGALRM signals. Some other + OS do notice signals in accept() but it does no harm to have the select() + in for all of them - and it won't then be a lurking problem for ports to + new OS. In fact, the later addition of listening on specific interfaces only + requires this way of working anyway. */ + + if (daemon_listen) + { + int sk, lcount; + int max_socket = 0; + BOOL select_failed = FALSE; + fd_set select_listen; + + FD_ZERO(&select_listen); + for (sk = 0; sk < listen_socket_count; sk++) + { + FD_SET(listen_sockets[sk], &select_listen); + if (listen_sockets[sk] > max_socket) max_socket = listen_sockets[sk]; + } + + DEBUG(D_any) debug_printf("Listening...\n"); + + if ((lcount = select(max_socket + 1, (SELECT_ARG2_TYPE *)&select_listen, + NULL, NULL, NULL)) < 0) + { + select_failed = TRUE; + lcount = 1; + } + + /* Loop for all the sockets that are currently ready to go. If select + actually failed, we have set the count to 1 and a flag, so as to use the + common error code for select/accept below. */ + + while (lcount-- > 0) + { + int accept_socket = -1; + if (!select_failed) + { + for (sk = 0; sk < listen_socket_count; sk++) + { + if (FD_ISSET(listen_sockets[sk], &select_listen)) + { + accept_socket = accept(listen_sockets[sk], + (struct sockaddr *)&accepted, &len); + FD_CLR(listen_sockets[sk], &select_listen); + break; + } + } + } + + /* If select or accept has failed and this was not caused by an + interruption, log the incident and try again. With asymmetric TCP/IP + routing errors such as "No route to network" have been seen here. Also + "connection reset by peer" has been seen. These cannot be classed as + disastrous errors, but they could fill up a lot of log. The code in smail + crashes the daemon after 10 successive failures of accept, on the grounds + that some OS fail continuously. Exim originally followed suit, but this + appears to have caused problems. Now it just keeps going, but instead of + logging each error, it batches them up when they are continuous. */ + + if (accept_socket < 0 && errno != EINTR) + { + if (accept_retry_count == 0) + { + accept_retry_errno = errno; + accept_retry_select_failed = select_failed; + } + else + { + if (errno != accept_retry_errno || + select_failed != accept_retry_select_failed || + accept_retry_count >= 50) + { + log_write(0, LOG_MAIN | ((accept_retry_count >= 50)? LOG_PANIC : 0), + "%d %s() failure%s: %s", + accept_retry_count, + accept_retry_select_failed? "select" : "accept", + (accept_retry_count == 1)? "" : "s", + strerror(accept_retry_errno)); + log_close_all(); + accept_retry_count = 0; + accept_retry_errno = errno; + accept_retry_select_failed = select_failed; + } + } + accept_retry_count++; + } + + else + { + if (accept_retry_count > 0) + { + log_write(0, LOG_MAIN, "%d %s() failure%s: %s", + accept_retry_count, + accept_retry_select_failed? "select" : "accept", + (accept_retry_count == 1)? "" : "s", + strerror(accept_retry_errno)); + log_close_all(); + accept_retry_count = 0; + } + } + + /* If select/accept succeeded, deal with the connection. */ + + if (accept_socket >= 0) + handle_smtp_call(listen_sockets, listen_socket_count, accept_socket, + (struct sockaddr *)&accepted); + } + } + + /* If not listening, then just sleep for the queue interval. If we woke + up early the last time for some other signal, it won't matter because + the alarm signal will wake at the right time. This code originally used + sleep() but it turns out that on the FreeBSD system, sleep() is not inter- + rupted by signals, so it wasn't waking up for SIGALRM or SIGCHLD. Luckily + select() can be used as an interruptible sleep() on all versions of Unix. */ + + else + { + struct timeval tv; + tv.tv_sec = queue_interval; + tv.tv_usec = 0; + select(0, NULL, NULL, NULL, &tv); + } + + /* Handle the termination of a child process. Theoretically, this need + be done only when sigchld_seen is TRUE, but rumour has it that some systems + lose SIGCHLD signals at busy times, so to be on the safe side, just + do it each time round. It shouldn't be too expensive. */ + + while ((pid = waitpid(-1, &status, WNOHANG)) > 0) + { + int i; + DEBUG(D_any) debug_printf("child %d ended: status=0x%x\n", (int)pid, + status); + + /* If it's a listening daemon, deal with an accepting process that has + terminated. */ + + if (daemon_listen) + { + for (i = 0; i < smtp_accept_max; i++) + { + if (smtp_slots[i].pid == pid) + { + if (smtp_slots[i].host_address != NULL) + store_free(smtp_slots[i].host_address); + smtp_slots[i] = empty_smtp_slot; + if (--smtp_accept_count < 0) smtp_accept_count = 0; + DEBUG(D_any) debug_printf("%d SMTP accept process%s now running\n", + smtp_accept_count, (smtp_accept_count == 1)? "" : "es"); + break; + } + } + if (i < smtp_accept_max) continue; /* Found an accepting process */ + } + + /* If it wasn't an accepting process, see if it was a queue-runner + process, if we are keeping track of them. */ + + if (queue_interval > 0) + { + for (i = 0; i < queue_run_max; i++) + { + if (queue_pid_slots[i] == pid) + { + queue_pid_slots[i] = 0; + if (--queue_run_count < 0) queue_run_count = 0; + DEBUG(D_any) debug_printf("%d queue-runner process%s now running\n", + queue_run_count, (queue_run_count == 1)? "" : "es"); + break; + } + } + } + } + + /* Re-enable the SIGCHLD handler if it has been run. It can't do it + for itself, because it isn't doing the waiting itself. */ + + if (sigchld_seen) + { + sigchld_seen = FALSE; + signal(SIGCHLD, main_sigchld_handler); + } + + /* Handle being woken by SIGHUP. We know at this point that the result + of accept() has been dealt with, so we can re-exec exim safely, first + closing the listening sockets so that they can be reused. Cancel any pending + alarm in case it is just about to go off, and set SIGHUP to be ignored so + that another HUP in quick succession doesn't clobber the new daemon before it + gets going. All log files get closed by the close-on-exec flag; however, if + the exec fails, we need to close the logs. */ + + if (sighup_seen) + { + int sk; + log_write(0, LOG_MAIN, "pid %d: SIGHUP received: re-exec daemon", + getpid()); + for (sk = 0; sk < listen_socket_count; sk++) close(listen_sockets[sk]); + alarm(0); + signal(SIGHUP, SIG_IGN); + sighup_argv[0] = exim_path; + exim_nullstd(); + execv(CS exim_path, (char *const *)sighup_argv); + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "pid %d: exec of %s failed: %s", + getpid(), exim_path, strerror(errno)); + log_close_all(); + } + + } /* End of main loop */ + +/* Control never reaches here */ +} + +/* End of exim_daemon.c */ + diff --git a/src/src/dbfn.c b/src/src/dbfn.c new file mode 100644 index 000000000..7baf232f3 --- /dev/null +++ b/src/src/dbfn.c @@ -0,0 +1,673 @@ +/* $Cambridge: exim/src/src/dbfn.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + + +#include "exim.h" + + +/* Functions for accessing Exim's hints database, which consists of a number of +different DBM files. This module does not contain code for reading DBM files +for (e.g.) alias expansion. That is all contained within the general search +functions. As Exim now has support for several DBM interfaces, all the relevant +functions are called as macros. + +All the data in Exim's database is in the nature of *hints*. Therefore it +doesn't matter if it gets destroyed by accident. These functions are not +supposed to implement a "safe" database. + +Keys are passed in as C strings, and the terminating zero *is* used when +building the dbm files. This just makes life easier when scanning the files +sequentially. + +Synchronization is required on the database files, and this is achieved by +means of locking on independent lock files. (Earlier attempts to lock on the +DBM files themselves were never completely successful.) Since callers may in +general want to do more than one read or write while holding the lock, there +are separate open and close functions. However, the calling modules should +arrange to hold the locks for the bare minimum of time. */ + + + +/************************************************* +* Berkeley DB error callback * +*************************************************/ + +/* For Berkeley DB >= 2, we can define a function to be called in case of DB +errors. This should help with debugging strange DB problems, e.g. getting "File +exists" when you try to open a db file. */ + +#if defined(USE_DB) && defined(DB_VERSION_STRING) +void +dbfn_bdb_error_callback(const char *pfx, char *msg) +{ +pfx = pfx; +log_write(0, LOG_MAIN, "Berkeley DB error: %s", msg); +} +#endif + + + + +/************************************************* +* Open and lock a database file * +*************************************************/ + +/* Used for accessing Exim's hints databases. + +Arguments: + name The single-component name of one of Exim's database files. + flags Either O_RDONLY or O_RDWR, indicating the type of open required; + O_RDWR implies "create if necessary" + dbblock Points to an open_db block to be filled in. + lof If TRUE, write to the log for actual open failures (locking failures + are always logged). + +Returns: NULL if the open failed, or the locking failed. After locking + failures, errno is zero. + + On success, dbblock is returned. This contains the dbm pointer and + the fd of the locked lock file. +*/ + +open_db * +dbfn_open(uschar *name, int flags, open_db *dbblock, BOOL lof) +{ +int rc, save_errno; +BOOL read_only = flags == O_RDONLY; +BOOL created = FALSE; +flock_t lock_data; +uschar buffer[256]; + +/* The first thing to do is to open a separate file on which to lock. This +ensures that Exim has exclusive use of the database before it even tries to +open it. Early versions tried to lock on the open database itself, but that +gave rise to mysterious problems from time to time - it was suspected that some +DB libraries "do things" on their open() calls which break the interlocking. +The lock file is never written to, but we open it for writing so we can get a +write lock if required. If it does not exist, we create it. This is done +separately so we know when we have done it, because when running as root we +need to change the ownership - see the bottom of this function. We also try to +make the directory as well, just in case. We won't be doing this many times +unnecessarily, because usually the lock file will be there. If the directory +exists, there is no error. */ + +sprintf(CS buffer, "%s/db/%s.lockfile", spool_directory, name); + +if ((dbblock->lockfd = Uopen(buffer, O_RDWR, EXIMDB_LOCKFILE_MODE)) < 0) + { + created = TRUE; + (void)directory_make(spool_directory, US"db", EXIMDB_DIRECTORY_MODE, TRUE); + dbblock->lockfd = Uopen(buffer, O_RDWR|O_CREAT, EXIMDB_LOCKFILE_MODE); + } + +if (dbblock->lockfd < 0) + { + log_write(0, LOG_MAIN, "%s", + string_open_failed(errno, "database lock file %s", buffer)); + errno = 0; /* Indicates locking failure */ + return NULL; + } + +/* Now we must get a lock on the opened lock file; do this with a blocking +lock that times out. */ + +lock_data.l_type = read_only? F_RDLCK : F_WRLCK; +lock_data.l_whence = lock_data.l_start = lock_data.l_len = 0; + +DEBUG(D_hints_lookup|D_retry|D_route|D_deliver) + debug_printf("locking %s\n", buffer); + +sigalrm_seen = FALSE; +alarm(EXIMDB_LOCK_TIMEOUT); +rc = fcntl(dbblock->lockfd, F_SETLKW, &lock_data); +alarm(0); + +if (sigalrm_seen) errno = ETIMEDOUT; +if (rc < 0) + { + log_write(0, LOG_MAIN, "Failed to get %s lock for %s: %s", + ((flags & O_RDONLY) != 0)? "read" : "write", buffer, + (errno == ETIMEDOUT)? "timed out" : strerror(errno)); + close(dbblock->lockfd); + errno = 0; /* Indicates locking failure */ + return NULL; + } + +DEBUG(D_hints_lookup) debug_printf("locked %s\n", buffer); + +/* At this point we have an opened and locked separate lock file, that is, +exclusive access to the database, so we can go ahead and open it. If we are +expected to create it, don't do so at first, again so that we can detect +whether we need to change its ownership (see comments about the lock file +above.) */ + +sprintf(CS buffer, "%s/db/%s", spool_directory, name); +EXIM_DBOPEN(buffer, flags, EXIMDB_MODE, &(dbblock->dbptr)); + +if (dbblock->dbptr == NULL && errno == ENOENT && flags == O_RDWR) + { + DEBUG(D_hints_lookup) + debug_printf("%s appears not to exist: trying to create\n", buffer); + created = TRUE; + EXIM_DBOPEN(buffer, flags|O_CREAT, EXIMDB_MODE, &(dbblock->dbptr)); + } + +save_errno = errno; + +/* If we are running as root and this is the first access to the database, its +files will be owned by root. We want them to be owned by exim. We detect this +situation by noting above when we had to create the lock file or the database +itself. Because the different dbm libraries use different extensions for their +files, I don't know of any easier way of arranging this than scanning the +directory for files with the appropriate base name. At least this deals with +the lock file at the same time. Also, the directory will typically have only +half a dozen files, so the scan will be quick. + +This code is placed here, before the test for successful opening, because there +was a case when a file was created, but the DBM library still returned NULL +because of some problem. It also sorts out the lock file if that was created +but creation of the database file failed. */ + +if (created && geteuid() == root_uid) + { + DIR *dd; + struct dirent *ent; + uschar *lastname = Ustrrchr(buffer, '/') + 1; + int namelen = Ustrlen(name); + + *lastname = 0; + dd = opendir(CS buffer); + + while ((ent = readdir(dd)) != NULL) + { + if (Ustrncmp(ent->d_name, name, namelen) == 0) + { + struct stat statbuf; + Ustrcpy(lastname, ent->d_name); + if (Ustat(buffer, &statbuf) >= 0 && statbuf.st_uid != exim_uid) + { + DEBUG(D_hints_lookup) debug_printf("ensuring %s is owned by exim\n", buffer); + Uchown(buffer, exim_uid, exim_gid); + } + } + } + + closedir(dd); + } + +/* If the open has failed, return NULL, leaving errno set. If lof is TRUE, +log the event - also for debugging - but not if the file just doesn't exist. */ + +if (dbblock->dbptr == NULL) + { + if (save_errno != ENOENT) + { + if (lof) + log_write(0, LOG_MAIN, "%s", string_open_failed(save_errno, "DB file %s", + buffer)); + else + DEBUG(D_hints_lookup) + debug_printf("%s", CS string_open_failed(save_errno, "DB file %s\n", + buffer)); + } + close(dbblock->lockfd); + errno = save_errno; + return NULL; + } + +DEBUG(D_hints_lookup) + debug_printf("opened hints database %s: flags=%x\n", buffer, flags); + +/* Pass back the block containing the opened database handle and the open fd +for the lock. */ + +return dbblock; +} + + + + +/************************************************* +* Unlock and close a database file * +*************************************************/ + +/* Closing a file automatically unlocks it, so after closing the database, just +close the lock file. + +Argument: a pointer to an open database block +Returns: nothing +*/ + +void +dbfn_close(open_db *dbblock) +{ +EXIM_DBCLOSE(dbblock->dbptr); +close(dbblock->lockfd); +} + + + + +/************************************************* +* Read from database file * +*************************************************/ + +/* Passing back the pointer unchanged is useless, because there is +no guarantee of alignment. Since all the records used by Exim need +to be properly aligned to pick out the timestamps, etc., we might as +well do the copying centrally here. + +Most calls don't need the length, so there is a macro called dbfn_read which +has two arguments; it calls this function adding NULL as the third. + +Arguments: + dbblock a pointer to an open database block + key the key of the record to be read + length a pointer to an int into which to return the length, if not NULL + +Returns: a pointer to the retrieved record, or + NULL if the record is not found +*/ + +void * +dbfn_read_with_length(open_db *dbblock, uschar *key, int *length) +{ +void *yield; +EXIM_DATUM key_datum, result_datum; + +DEBUG(D_hints_lookup) debug_printf("dbfn_read: key=%s\n", key); + +EXIM_DATUM_INIT(key_datum); /* Some DBM libraries require the datum */ +EXIM_DATUM_INIT(result_datum); /* to be cleared before use. */ +EXIM_DATUM_DATA(key_datum) = CS key; +EXIM_DATUM_SIZE(key_datum) = Ustrlen(key) + 1; + +if (!EXIM_DBGET(dbblock->dbptr, key_datum, result_datum)) return NULL; + +yield = store_get(EXIM_DATUM_SIZE(result_datum)); +memcpy(yield, EXIM_DATUM_DATA(result_datum), EXIM_DATUM_SIZE(result_datum)); +if (length != NULL) *length = EXIM_DATUM_SIZE(result_datum); + +EXIM_DATUM_FREE(result_datum); /* Some DBM libs require freeing */ +return yield; +} + + + +/************************************************* +* Write to database file * +*************************************************/ + +/* +Arguments: + dbblock a pointer to an open database block + key the key of the record to be written + ptr a pointer to the record to be written + length the length of the record to be written + +Returns: the yield of the underlying dbm or db "write" function. If this + is dbm, the value is zero for OK. +*/ + +int +dbfn_write(open_db *dbblock, uschar *key, void *ptr, int length) +{ +EXIM_DATUM key_datum, value_datum; +dbdata_generic *gptr = (dbdata_generic *)ptr; +gptr->time_stamp = time(NULL); + +DEBUG(D_hints_lookup) debug_printf("dbfn_write: key=%s\n", key); + +EXIM_DATUM_INIT(key_datum); /* Some DBM libraries require the datum */ +EXIM_DATUM_INIT(value_datum); /* to be cleared before use. */ +EXIM_DATUM_DATA(key_datum) = CS key; +EXIM_DATUM_SIZE(key_datum) = Ustrlen(key) + 1; +EXIM_DATUM_DATA(value_datum) = CS ptr; +EXIM_DATUM_SIZE(value_datum) = length; +return EXIM_DBPUT(dbblock->dbptr, key_datum, value_datum); +} + + + +/************************************************* +* Delete record from database file * +*************************************************/ + +/* +Arguments: + dbblock a pointer to an open database block + key the key of the record to be deleted + +Returns: the yield of the underlying dbm or db "delete" function. +*/ + +int +dbfn_delete(open_db *dbblock, uschar *key) +{ +EXIM_DATUM key_datum; +EXIM_DATUM_INIT(key_datum); /* Some DBM libraries require clearing */ +EXIM_DATUM_DATA(key_datum) = CS key; +EXIM_DATUM_SIZE(key_datum) = Ustrlen(key) + 1; +return EXIM_DBDEL(dbblock->dbptr, key_datum); +} + + + +/************************************************* +* Scan the keys of a database file * +*************************************************/ + +/* +Arguments: + dbblock a pointer to an open database block + start TRUE if starting a new scan + FALSE if continuing with the current scan + cursor a pointer to a pointer to a cursor anchor, for those dbm libraries + that use the notion of a cursor + +Returns: the next record from the file, or + NULL if there are no more +*/ + +uschar * +dbfn_scan(open_db *dbblock, BOOL start, EXIM_CURSOR **cursor) +{ +EXIM_DATUM key_datum, value_datum; +uschar *yield; +value_datum = value_datum; /* dummy; not all db libraries use this */ + +/* Some dbm require an initialization */ + +if (start) EXIM_DBCREATE_CURSOR(dbblock->dbptr, cursor); + +EXIM_DATUM_INIT(key_datum); /* Some DBM libraries require the datum */ +EXIM_DATUM_INIT(value_datum); /* to be cleared before use. */ + +yield = (EXIM_DBSCAN(dbblock->dbptr, key_datum, value_datum, start, *cursor))? + US EXIM_DATUM_DATA(key_datum) : NULL; + +/* Some dbm require a termination */ + +if (!yield) EXIM_DBDELETE_CURSOR(*cursor); +return yield; +} + + + +/************************************************* +************************************************** +* Stand-alone test program * +************************************************** +*************************************************/ + +#ifdef STAND_ALONE + +int +main(int argc, char **cargv) +{ +open_db dbblock[8]; +int max_db = sizeof(dbblock)/sizeof(open_db); +int current = -1; +int showtime = 0; +int i; +dbdata_wait *dbwait = NULL; +uschar **argv = USS cargv; +uschar buffer[256]; +uschar structbuffer[1024]; + +if (argc != 2) + { + printf("Usage: test_dbfn directory\n"); + printf("The subdirectory called \"db\" in the given directory is used for\n"); + printf("the files used in this test program.\n"); + return 1; + } + +/* Initialize */ + +spool_directory = argv[1]; +debug_selector = D_all - D_memory; +debug_file = stderr; +big_buffer = malloc(big_buffer_size); + +for (i = 0; i < max_db; i++) dbblock[i].dbptr = NULL; + +printf("\nExim's db functions tester: interface type is %s\n", EXIM_DBTYPE); +printf("DBM library: "); + +#ifdef DB_VERSION_STRING +printf("Berkeley DB: %s\n", DB_VERSION_STRING); +#elif defined(BTREEVERSION) && defined(HASHVERSION) + #ifdef USE_DB + printf("probably Berkeley DB version 1.8x (native mode)\n"); + #else + printf("probably Berkeley DB version 1.8x (compatibility mode)\n"); + #endif +#elif defined(_DBM_RDONLY) || defined(dbm_dirfno) +printf("probably ndbm\n"); +#elif defined(USE_TDB) +printf("using tdb\n"); +#else + #ifdef USE_GDBM + printf("probably GDBM (native mode)\n"); + #else + printf("probably GDBM (compatibility mode)\n"); + #endif +#endif + +/* Test the functions */ + +printf("\nTest the functions\n> "); + +while (Ufgets(buffer, 256, stdin) != NULL) + { + int len = Ustrlen(buffer); + int count = 1; + clock_t start = 1; + clock_t stop = 0; + uschar *cmd = buffer; + while (len > 0 && isspace((uschar)buffer[len-1])) len--; + buffer[len] = 0; + + if (isdigit((uschar)*cmd)) + { + count = Uatoi(cmd); + while (isdigit((uschar)*cmd)) cmd++; + while (isspace((uschar)*cmd)) cmd++; + } + + if (Ustrncmp(cmd, "open", 4) == 0) + { + int i; + open_db *odb; + uschar *s = cmd + 4; + while (isspace((uschar)*s)) s++; + + for (i = 0; i < max_db; i++) + if (dbblock[i].dbptr == NULL) break; + + if (i >= max_db) + { + printf("Too many open databases\n> "); + continue; + } + + start = clock(); + odb = dbfn_open(s, O_RDWR, dbblock + i, TRUE); + stop = clock(); + + if (odb != NULL) + { + current = i; + printf("opened %d\n", current); + } + /* Other error cases will have written messages */ + else if (errno == ENOENT) + { + printf("open failed: %s%s\n", strerror(errno), + #ifdef USE_DB + " (or other Berkeley DB error)" + #else + "" + #endif + ); + } + } + + else if (Ustrncmp(cmd, "write", 5) == 0) + { + int rc = 0; + uschar *key = cmd + 5; + uschar *data; + + if (current < 0) + { + printf("No current database\n"); + continue; + } + + while (isspace((uschar)*key)) key++; + data = key; + while (*data != 0 && !isspace((uschar)*data)) data++; + *data++ = 0; + while (isspace((uschar)*data)) data++; + + dbwait = (dbdata_wait *)(&structbuffer); + Ustrcpy(dbwait->text, data); + + start = clock(); + while (count-- > 0) + rc = dbfn_write(dbblock + current, key, dbwait, + Ustrlen(data) + sizeof(dbdata_wait)); + stop = clock(); + if (rc != 0) printf("Failed: %s\n", strerror(errno)); + } + + else if (Ustrncmp(cmd, "read", 4) == 0) + { + uschar *key = cmd + 4; + if (current < 0) + { + printf("No current database\n"); + continue; + } + while (isspace((uschar)*key)) key++; + start = clock(); + while (count-- > 0) + dbwait = (dbdata_wait *)dbfn_read_with_length(dbblock+ current, key, NULL); + stop = clock(); + printf("%s\n", (dbwait == NULL)? "" : CS dbwait->text); + } + + else if (Ustrncmp(cmd, "delete", 6) == 0) + { + uschar *key = cmd + 6; + if (current < 0) + { + printf("No current database\n"); + continue; + } + while (isspace((uschar)*key)) key++; + dbfn_delete(dbblock + current, key); + } + + else if (Ustrncmp(cmd, "scan", 4) == 0) + { + EXIM_CURSOR *cursor; + BOOL startflag = TRUE; + uschar *key; + uschar keybuffer[256]; + if (current < 0) + { + printf("No current database\n"); + continue; + } + start = clock(); + while ((key = dbfn_scan(dbblock + current, startflag, &cursor)) != NULL) + { + startflag = FALSE; + Ustrcpy(keybuffer, key); + dbwait = (dbdata_wait *)dbfn_read_with_length(dbblock + current, + keybuffer, NULL); + printf("%s: %s\n", keybuffer, dbwait->text); + } + stop = clock(); + printf("End of scan\n"); + } + + else if (Ustrncmp(cmd, "close", 5) == 0) + { + uschar *s = cmd + 5; + while (isspace((uschar)*s)) s++; + i = Uatoi(s); + if (i >= max_db || dbblock[i].dbptr == NULL) printf("Not open\n"); else + { + start = clock(); + dbfn_close(dbblock + i); + stop = clock(); + dbblock[i].dbptr = NULL; + if (i == current) current = -1; + } + } + + else if (Ustrncmp(cmd, "file", 4) == 0) + { + uschar *s = cmd + 4; + while (isspace((uschar)*s)) s++; + i = Uatoi(s); + if (i >= max_db || dbblock[i].dbptr == NULL) printf("Not open\n"); + else current = i; + } + + else if (Ustrncmp(cmd, "time", 4) == 0) + { + showtime = ~showtime; + printf("Timing %s\n", showtime? "on" : "off"); + } + + else if (Ustrcmp(cmd, "q") == 0 || Ustrncmp(cmd, "quit", 4) == 0) break; + + else if (Ustrncmp(cmd, "help", 4) == 0) + { + printf("close [] close file []\n"); + printf("delete remove record from current file\n"); + printf("file make file current\n"); + printf("open open db file\n"); + printf("q[uit] exit program\n"); + printf("read read record from current file\n"); + printf("scan scan current file\n"); + printf("time time display on/off\n"); + printf("write write record to current file\n"); + } + + else printf("Eh?\n"); + + if (showtime && stop >= start) + printf("start=%d stop=%d difference=%d\n", (int)start, (int)stop, + (int)(stop - start)); + + printf("> "); + } + +for (i = 0; i < max_db; i++) + { + if (dbblock[i].dbptr != NULL) + { + printf("\nClosing %d", i); + dbfn_close(dbblock + i); + } + } + +printf("\n"); +return 0; +} + +#endif + +/* End of dbfn.c */ diff --git a/src/src/dbfunctions.h b/src/src/dbfunctions.h new file mode 100644 index 000000000..655877f39 --- /dev/null +++ b/src/src/dbfunctions.h @@ -0,0 +1,30 @@ +/* $Cambridge: exim/src/src/dbfunctions.h,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + + +/* Functions for reading/writing exim database files */ + +void dbfn_close(open_db *); +int dbfn_delete(open_db *, uschar *); +open_db *dbfn_open(uschar *, int, open_db *, BOOL); +void *dbfn_read_with_length(open_db *, uschar *, int *); +uschar *dbfn_scan(open_db *, BOOL, EXIM_CURSOR **); +int dbfn_write(open_db *, uschar *, void *, int); + +/* Macro for the common call to read without wanting to know the length. */ + +#define dbfn_read(a, b) dbfn_read_with_length(a, b, NULL) + +/* Berkeley DB uses a callback function to pass back error details. */ + +#if defined(USE_DB) && defined(DB_VERSION_STRING) +void dbfn_bdb_error_callback(const char *, char *); +#endif + +/* End of dbfunctions.h */ diff --git a/src/src/dbstuff.h b/src/src/dbstuff.h new file mode 100644 index 000000000..7c581ad29 --- /dev/null +++ b/src/src/dbstuff.h @@ -0,0 +1,634 @@ +/* $Cambridge: exim/src/src/dbstuff.h,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* This header file contains macro definitions so that a variety of DBM +libraries can be used by Exim. Nigel Metheringham provided the original set for +Berkeley DB 1.x in native mode and ndbm. Subsequently, versions for Berkeley DB +2.x and 3.x were added. Later still, support for tdb was added, courtesy of +James Antill. Most recently, support for native mode gdbm was added, with code +from Pierre A. Humblet, so Exim could be made to work with Cygwin. + +For convenience, the definitions of the structures used in the various hints +databases are also kept in this file, which is used by the maintenance +utilities as well as the main Exim binary. */ + + +# ifdef USE_TDB + +/* ************************* tdb interface ************************ */ + +#include + +/* Basic DB type */ +#define EXIM_DB TDB_CONTEXT + +/* Cursor type: tdb uses the previous "key" in _nextkey() (really it wants +tdb_traverse to be called) */ +#define EXIM_CURSOR TDB_DATA + +/* The datum type used for queries */ +#define EXIM_DATUM TDB_DATA + +/* Some text for messages */ +#define EXIM_DBTYPE "tdb" + +/* Access functions */ + +/* EXIM_DBOPEN - sets *dbpp to point to an EXIM_DB, NULL if failed */ +#define EXIM_DBOPEN(name, flags, mode, dbpp) \ + *(dbpp) = tdb_open(CS name, 0, TDB_DEFAULT, flags, mode) + +/* EXIM_DBGET - returns TRUE if successful, FALSE otherwise */ +#define EXIM_DBGET(db, key, data) \ + (data = tdb_fetch(db, key), data.dptr != NULL) + +/* EXIM_DBPUT - returns nothing useful, assumes replace mode */ +#define EXIM_DBPUT(db, key, data) \ + tdb_store(db, key, data, TDB_REPLACE) + +/* EXIM_DBPUTB - non-overwriting for use by dbmbuild */ +#define EXIM_DBPUTB(db, key, data) \ + tdb_store(db, key, data, TDB_INSERT) + +/* Returns from EXIM_DBPUTB */ + +#define EXIM_DBPUTB_OK 0 +#define EXIM_DBPUTB_DUP (-1) + +/* EXIM_DBDEL */ +#define EXIM_DBDEL(db, key) tdb_delete(db, key) + +/* EXIM_DBCREATE_CURSOR - initialize for scanning operation */ +#define EXIM_DBCREATE_CURSOR(db, cursor) { \ + *(cursor) = malloc(sizeof(TDB_DATA)); (*(cursor))->dptr = NULL; } + +/* EXIM_DBSCAN - This is complicated because we have to free the last datum +free() must not die when passed NULL */ +#define EXIM_DBSCAN(db, key, data, first, cursor) \ + (key = (first ? tdb_firstkey(db) : tdb_nextkey(db, *(cursor))), \ + free((cursor)->dptr), *(cursor) = key, \ + key.dptr != NULL) + +/* EXIM_DBDELETE_CURSOR - terminate scanning operation. */ +#define EXIM_DBDELETE_CURSOR(cursor) free(cursor) + +/* EXIM_DBCLOSE */ +#define EXIM_DBCLOSE(db) tdb_close(db) + +/* Datum access types - these are intended to be assignable */ + +#define EXIM_DATUM_SIZE(datum) (datum).dsize +#define EXIM_DATUM_DATA(datum) (datum).dptr + +/* Free the stuff inside the datum. */ + +#define EXIM_DATUM_FREE(datum) (free((datum).dptr), (datum).dptr = NULL) + +/* No initialization is needed. */ + +#define EXIM_DATUM_INIT(datum) + + + +/********************* Berkeley db native definitions **********************/ + +#elif defined USE_DB + +#include + + +/* We can distinguish between versions 1.x and 2.x/3.x by looking for a +definition of DB_VERSION_STRING, which is present in versions 2.x onwards. */ + +#ifdef DB_VERSION_STRING + +/* The API changed (again!) between the 2.x and 3.x versions */ + +#if DB_VERSION_MAJOR >= 3 + +/***************** Berkeley db 3.x/4.x native definitions ******************/ + +/* Basic DB type */ +#define EXIM_DB DB + +/* Cursor type, for scanning */ +#define EXIM_CURSOR DBC + +/* The datum type used for queries */ +#define EXIM_DATUM DBT + +/* Some text for messages */ +#define EXIM_DBTYPE "db (v3/4)" + +/* Access functions */ + +/* EXIM_DBOPEN - sets *dbpp to point to an EXIM_DB, NULL if failed. The +API changed for DB 4.1. */ + +#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1) +#define EXIM_DBOPEN(name, flags, mode, dbpp) \ + if (db_create(dbpp, NULL, 0) != 0 || \ + ((*dbpp)->set_errcall(*dbpp, dbfn_bdb_error_callback), \ + (*dbpp)->open(*dbpp, NULL, CS name, NULL, \ + ((flags) == O_RDONLY)? DB_UNKNOWN : DB_HASH, \ + ((flags) == O_RDONLY)? DB_RDONLY : DB_CREATE, \ + mode)) != 0) *(dbpp) = NULL +#else +#define EXIM_DBOPEN(name, flags, mode, dbpp) \ + if (db_create(dbpp, NULL, 0) != 0 || \ + ((*dbpp)->set_errcall(*dbpp, dbfn_bdb_error_callback), \ + (*dbpp)->open(*dbpp, CS name, NULL, \ + ((flags) == O_RDONLY)? DB_UNKNOWN : DB_HASH, \ + ((flags) == O_RDONLY)? DB_RDONLY : DB_CREATE, \ + mode)) != 0) *(dbpp) = NULL +#endif + +/* EXIM_DBGET - returns TRUE if successful, FALSE otherwise */ +#define EXIM_DBGET(db, key, data) \ + ((db)->get(db, NULL, &key, &data, 0) == 0) + +/* EXIM_DBPUT - returns nothing useful, assumes replace mode */ +#define EXIM_DBPUT(db, key, data) \ + (db)->put(db, NULL, &key, &data, 0) + +/* EXIM_DBPUTB - non-overwriting for use by dbmbuild */ +#define EXIM_DBPUTB(db, key, data) \ + (db)->put(db, NULL, &key, &data, DB_NOOVERWRITE) + +/* Return values from EXIM_DBPUTB */ + +#define EXIM_DBPUTB_OK 0 +#define EXIM_DBPUTB_DUP DB_KEYEXIST + +/* EXIM_DBDEL */ +#define EXIM_DBDEL(db, key) (db)->del(db, NULL, &key, 0) + +/* EXIM_DBCREATE_CURSOR - initialize for scanning operation */ + +#define EXIM_DBCREATE_CURSOR(db, cursor) \ + (db)->cursor(db, NULL, cursor, 0) + +/* EXIM_DBSCAN - returns TRUE if data is returned, FALSE at end */ +#define EXIM_DBSCAN(db, key, data, first, cursor) \ + ((cursor)->c_get(cursor, &key, &data, \ + (first? DB_FIRST : DB_NEXT)) == 0) + +/* EXIM_DBDELETE_CURSOR - terminate scanning operation */ +#define EXIM_DBDELETE_CURSOR(cursor) \ + (cursor)->c_close(cursor) + +/* EXIM_DBCLOSE */ +#define EXIM_DBCLOSE(db) (db)->close(db, 0) + +/* Datum access types - these are intended to be assignable. */ + +#define EXIM_DATUM_SIZE(datum) (datum).size +#define EXIM_DATUM_DATA(datum) (datum).data + +/* The whole datum structure contains other fields that must be cleared +before use, but we don't have to free anything after reading data. */ + +#define EXIM_DATUM_INIT(datum) memset(&datum, 0, sizeof(datum)) +#define EXIM_DATUM_FREE(datum) + + +#else /* DB_VERSION_MAJOR >= 3 */ + +/******************* Berkeley db 2.x native definitions ********************/ + +/* Basic DB type */ +#define EXIM_DB DB + +/* Cursor type, for scanning */ +#define EXIM_CURSOR DBC + +/* The datum type used for queries */ +#define EXIM_DATUM DBT + +/* Some text for messages */ +#define EXIM_DBTYPE "db (v2)" + +/* Access functions */ + +/* EXIM_DBOPEN - sets *dbpp to point to an EXIM_DB, NULL if failed */ +#define EXIM_DBOPEN(name, flags, mode, dbpp) \ + if ((errno = db_open(CS name, DB_HASH, \ + ((flags) == O_RDONLY)? DB_RDONLY : DB_CREATE, \ + mode, NULL, NULL, dbpp)) != 0) *(dbpp) = NULL + +/* EXIM_DBGET - returns TRUE if successful, FALSE otherwise */ +#define EXIM_DBGET(db, key, data) \ + ((db)->get(db, NULL, &key, &data, 0) == 0) + +/* EXIM_DBPUT - returns nothing useful, assumes replace mode */ +#define EXIM_DBPUT(db, key, data) \ + (db)->put(db, NULL, &key, &data, 0) + +/* EXIM_DBPUTB - non-overwriting for use by dbmbuild */ +#define EXIM_DBPUTB(db, key, data) \ + (db)->put(db, NULL, &key, &data, DB_NOOVERWRITE) + +/* Return values from EXIM_DBPUTB */ + +#define EXIM_DBPUTB_OK 0 +#define EXIM_DBPUTB_DUP DB_KEYEXIST + +/* EXIM_DBDEL */ +#define EXIM_DBDEL(db, key) (db)->del(db, NULL, &key, 0) + +/* EXIM_DBCREATE_CURSOR - initialize for scanning operation */ + +/* The API of this function was changed between releases 2.4.14 and 2.7.3. I do +not know exactly where the change happened, but the Change Log for 2.5.9 lists +the new option that is available, so I guess that it happened at 2.5.x. */ + +#if DB_VERSION_MINOR >= 5 +#define EXIM_DBCREATE_CURSOR(db, cursor) \ + (db)->cursor(db, NULL, cursor, 0) +#else +#define EXIM_DBCREATE_CURSOR(db, cursor) \ + (db)->cursor(db, NULL, cursor) +#endif + +/* EXIM_DBSCAN - returns TRUE if data is returned, FALSE at end */ +#define EXIM_DBSCAN(db, key, data, first, cursor) \ + ((cursor)->c_get(cursor, &key, &data, \ + (first? DB_FIRST : DB_NEXT)) == 0) + +/* EXIM_DBDELETE_CURSOR - terminate scanning operation */ +#define EXIM_DBDELETE_CURSOR(cursor) \ + (cursor)->c_close(cursor) + +/* EXIM_DBCLOSE */ +#define EXIM_DBCLOSE(db) (db)->close(db, 0) + +/* Datum access types - these are intended to be assignable. */ + +#define EXIM_DATUM_SIZE(datum) (datum).size +#define EXIM_DATUM_DATA(datum) (datum).data + +/* The whole datum structure contains other fields that must be cleared +before use, but we don't have to free anything after reading data. */ + +#define EXIM_DATUM_INIT(datum) memset(&datum, 0, sizeof(datum)) +#define EXIM_DATUM_FREE(datum) + +#endif /* DB_VERSION_MAJOR >= 3 */ + + +/* If DB_VERSION_TYPE is not defined, we have version 1.x */ + +#else /* DB_VERSION_TYPE */ + +/******************* Berkeley db 1.x native definitions ********************/ + +/* Basic DB type */ +#define EXIM_DB DB + +/* Cursor type, not used with DB 1.x: just set up a dummy */ +#define EXIM_CURSOR int + +/* The datum type used for queries */ +#define EXIM_DATUM DBT + +/* Some text for messages */ +#define EXIM_DBTYPE "db (v1)" + +/* Access functions */ + +/* EXIM_DBOPEN - sets *dbpp to point to an EXIM_DB, NULL if failed */ +#define EXIM_DBOPEN(name, flags, mode, dbpp) \ + *(dbpp) = dbopen(CS name, flags, mode, DB_HASH, NULL) + +/* EXIM_DBGET - returns TRUE if successful, FALSE otherwise */ +#define EXIM_DBGET(db, key, data) \ + ((db)->get(db, &key, &data, 0) == 0) + +/* EXIM_DBPUT - returns nothing useful, assumes replace mode */ +#define EXIM_DBPUT(db, key, data) \ + (db)->put(db, &key, &data, 0) + +/* EXIM_DBPUTB - non-overwriting for use by dbmbuild */ +#define EXIM_DBPUTB(db, key, data) \ + (db)->put(db, &key, &data, R_NOOVERWRITE) + +/* Returns from EXIM_DBPUTB */ + +#define EXIM_DBPUTB_OK 0 +#define EXIM_DBPUTB_DUP 1 + +/* EXIM_DBDEL */ +#define EXIM_DBDEL(db, key) (db)->del(db, &key, 0) + +/* EXIM_DBCREATE_CURSOR - initialize for scanning operation (null) */ +#define EXIM_DBCREATE_CURSOR(db, cursor) {} + +/* EXIM_DBSCAN - returns TRUE if data is returned, FALSE at end */ +#define EXIM_DBSCAN(db, key, data, first, cursor) \ + ((db)->seq(db, &key, &data, (first? R_FIRST : 0)) == 0) + +/* EXIM_DBDELETE_CURSOR - terminate scanning operation (null). Make it +refer to cursor, to keep picky compilers happy. */ +#define EXIM_DBDELETE_CURSOR(cursor) { cursor = cursor; } + +/* EXIM_DBCLOSE */ +#define EXIM_DBCLOSE(db) (db)->close(db) + +/* Datum access types - these are intended to be assignable */ + +#define EXIM_DATUM_SIZE(datum) (datum).size +#define EXIM_DATUM_DATA(datum) (datum).data + +/* There's no clearing required before use, and we don't have to free anything +after reading data. */ + +#define EXIM_DATUM_INIT(datum) +#define EXIM_DATUM_FREE(datum) + +#endif /* DB_VERSION_STRING */ + + + +/********************* gdbm interface definitions **********************/ + +#elif defined USE_GDBM + +#include + +/* Basic DB type */ +typedef struct { + GDBM_FILE gdbm; /* Database */ + datum lkey; /* Last key, for scans */ +} EXIM_DB; + +/* Cursor type, not used with gdbm: just set up a dummy */ +#define EXIM_CURSOR int + +/* The datum type used for queries */ +#define EXIM_DATUM datum + +/* Some text for messages */ + +#define EXIM_DBTYPE "gdbm" + +/* Access functions */ + +/* EXIM_DBOPEN - returns a EXIM_DB *, NULL if failed */ +#define EXIM_DBOPEN(name, flags, mode, dbpp) \ + { (*(dbpp)) = (EXIM_DB *) malloc(sizeof(EXIM_DB));\ + if (*(dbpp) != NULL) { \ + (*(dbpp))->lkey.dptr = NULL;\ + (*(dbpp))->gdbm = gdbm_open(CS name, 0, (((flags) & O_CREAT))?GDBM_WRCREAT:(((flags) & (O_RDWR|O_WRONLY))?GDBM_WRITER:GDBM_READER), mode, 0);\ + if ((*(dbpp))->gdbm == NULL) {\ + free(*(dbpp));\ + *(dbpp) = NULL;\ + }\ + }\ + } + +/* EXIM_DBGET - returns TRUE if successful, FALSE otherwise */ +#define EXIM_DBGET(db, key, data) \ + (data = gdbm_fetch(db->gdbm, key), data.dptr != NULL) + +/* EXIM_DBPUT - returns nothing useful, assumes replace mode */ +#define EXIM_DBPUT(db, key, data) \ + gdbm_store(db->gdbm, key, data, GDBM_REPLACE) + +/* EXIM_DBPUTB - non-overwriting for use by dbmbuild */ +#define EXIM_DBPUTB(db, key, data) \ + gdbm_store(db->gdbm, key, data, GDBM_INSERT) + +/* Returns from EXIM_DBPUTB */ + +#define EXIM_DBPUTB_OK 0 +#define EXIM_DBPUTB_DUP 1 + +/* EXIM_DBDEL */ +#define EXIM_DBDEL(db, key) gdbm_delete(db->gdbm, key) + +/* EXIM_DBCREATE_CURSOR - initialize for scanning operation (null) */ +#define EXIM_DBCREATE_CURSOR(db, cursor) {} + +/* EXIM_DBSCAN */ +#define EXIM_DBSCAN(db, key, data, first, cursor) \ + ( key = ((first)? gdbm_firstkey(db->gdbm) : gdbm_nextkey(db->gdbm, db->lkey)), \ + (((db)->lkey.dptr != NULL)? (free((db)->lkey.dptr),1) : 1),\ + db->lkey = key, key.dptr != NULL) + +/* EXIM_DBDELETE_CURSOR - terminate scanning operation (null). Make it +refer to cursor, to keep picky compilers happy. */ +#define EXIM_DBDELETE_CURSOR(cursor) { cursor = cursor; } + +/* EXIM_DBCLOSE */ +#define EXIM_DBCLOSE(db) \ +{ gdbm_close((db)->gdbm);\ + if ((db)->lkey.dptr != NULL) free((db)->lkey.dptr);\ + free(db); } + +/* Datum access types - these are intended to be assignable */ + +#define EXIM_DATUM_SIZE(datum) (datum).dsize +#define EXIM_DATUM_DATA(datum) (datum).dptr + +/* There's no clearing required before use, but we have to free the dptr +after reading data. */ + +#define EXIM_DATUM_INIT(datum) +#define EXIM_DATUM_FREE(datum) free(datum.dptr) + +#else /* USE_GDBM */ + + +/* If none of USE_DB, USG_GDBM, or USE_TDB are set, the default is the NDBM +interface */ + + +/********************* ndbm interface definitions **********************/ + +#include + +/* Basic DB type */ +#define EXIM_DB DBM + +/* Cursor type, not used with ndbm: just set up a dummy */ +#define EXIM_CURSOR int + +/* The datum type used for queries */ +#define EXIM_DATUM datum + +/* Some text for messages */ + +#define EXIM_DBTYPE "ndbm" + +/* Access functions */ + +/* EXIM_DBOPEN - returns a EXIM_DB *, NULL if failed */ +#define EXIM_DBOPEN(name, flags, mode, dbpp) \ + *(dbpp) = dbm_open(CS name, flags, mode) + +/* EXIM_DBGET - returns TRUE if successful, FALSE otherwise */ +#define EXIM_DBGET(db, key, data) \ + (data = dbm_fetch(db, key), data.dptr != NULL) + +/* EXIM_DBPUT - returns nothing useful, assumes replace mode */ +#define EXIM_DBPUT(db, key, data) \ + dbm_store(db, key, data, DBM_REPLACE) + +/* EXIM_DBPUTB - non-overwriting for use by dbmbuild */ +#define EXIM_DBPUTB(db, key, data) \ + dbm_store(db, key, data, DBM_INSERT) + +/* Returns from EXIM_DBPUTB */ + +#define EXIM_DBPUTB_OK 0 +#define EXIM_DBPUTB_DUP 1 + +/* EXIM_DBDEL */ +#define EXIM_DBDEL(db, key) dbm_delete(db, key) + +/* EXIM_DBCREATE_CURSOR - initialize for scanning operation (null) */ +#define EXIM_DBCREATE_CURSOR(db, cursor) {} + +/* EXIM_DBSCAN */ +#define EXIM_DBSCAN(db, key, data, first, cursor) \ + (key = (first? dbm_firstkey(db) : dbm_nextkey(db)), key.dptr != NULL) + +/* EXIM_DBDELETE_CURSOR - terminate scanning operation (null). Make it +refer to cursor, to keep picky compilers happy. */ +#define EXIM_DBDELETE_CURSOR(cursor) { cursor = cursor; } + +/* EXIM_DBCLOSE */ +#define EXIM_DBCLOSE(db) dbm_close(db) + +/* Datum access types - these are intended to be assignable */ + +#define EXIM_DATUM_SIZE(datum) (datum).dsize +#define EXIM_DATUM_DATA(datum) (datum).dptr + +/* There's no clearing required before use, and we don't have to free anything +after reading data. */ + +#define EXIM_DATUM_INIT(datum) +#define EXIM_DATUM_FREE(datum) + +#endif /* USE_GDBM */ + +/********************* End of dbm library definitions **********************/ + + +/* Structure for carrying around an open DBM file, and an open locking file +that relates to it. */ + +typedef struct { + EXIM_DB *dbptr; + int lockfd; +} open_db; + + +/* Structures for records stored in exim database dbm files. They all +start with the same fields, described in the generic type. */ + + +typedef struct { + time_t time_stamp; /* Timestamp of writing */ +} dbdata_generic; + + +/* This structure keeps track of retry information for a host or a local +address. */ + +typedef struct { + time_t time_stamp; + /*************/ + time_t first_failed; /* Time of first failure */ + time_t last_try; /* Time of last try */ + time_t next_try; /* Time of next try */ + BOOL expired; /* Retry time has expired */ + int basic_errno; /* Errno of last failure */ + int more_errno; /* Additional information */ + uschar text[1]; /* Text message for last failure */ +} dbdata_retry; + +/* These structures keep track of addresses that have had callout verification +performed on them. There are two groups of records: + +1. keyed by localpart@domain - + Full address was tested and record holds result + +2. keyed by domain - + Domain response upto MAIL FROM:<>, postmaster, random local part; + +If a record exists, the result field is either ccache_accept or ccache_reject. +The other fields, however, (which are only relevant to domain records) may also +contain ccache_unknown if that particular test has not been done. + +Originally, there was only one structure, used for both types. However, it got +expanded for domain records, so it got split. To make it possible for Exim to +handle the old type of record, we retain the old definition. The different +kinds of record can be distinguised by their different lengths. */ + +typedef struct { + time_t time_stamp; + /*************/ + int result; + int postmaster_result; /* Postmaster is accepted */ + int random_result; /* Random local part was accepted */ +} dbdata_callout_cache_obs; + +typedef struct { + time_t time_stamp; /* Timestamp of last address check */ + /*************/ + int result; /* accept or reject */ +} dbdata_callout_cache_address; + +/* For this new layout, we put the additional fields (the timestamps) +last so that if somebody reverts to an older Exim, the new records will +still make sense because they match the old layout. */ + +typedef struct { + time_t time_stamp; /* Time stamp of last connection */ + /*************/ + int result; /* Domain reject or accept */ + int postmaster_result; /* Postmaster result */ + int random_result; /* Random result */ + time_t postmaster_stamp; /* Timestamp of postmaster check */ + time_t random_stamp; /* Timestamp of random check */ +} dbdata_callout_cache; + +/* This structure keeps track of messages that are waiting for a particular +host for a particular transport. */ + +typedef struct { + time_t time_stamp; + /*************/ + int count; /* Count of message ids */ + int sequence; /* Sequence for continued records */ + uschar text[1]; /* One long character string */ +} dbdata_wait; + + +/* The contents of the "misc" database are a mixture of different kinds of +record, as defined below. The keys used for a specific type all start with a +given string such as "etrn-" or "host-serialize-". */ + + +/* This structure records a connection to a particular host, for the +purpose of serializing access to certain hosts. For possible future extension, +a field is defined for holding the count of connections, but it is not +at present in use. The same structure is used for recording a running ETRN +process. */ + +typedef struct { + time_t time_stamp; + /*************/ + int count; /* Reserved for possible connection count */ +} dbdata_serialize; + + +/* End of dbstuff.h */ diff --git a/src/src/debug.c b/src/src/debug.c new file mode 100644 index 000000000..3128b545f --- /dev/null +++ b/src/src/debug.c @@ -0,0 +1,237 @@ +/* $Cambridge: exim/src/src/debug.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + + +#include "exim.h" + +static uschar debug_buffer[1024]; +static uschar *debug_ptr = debug_buffer; +static int debug_prefix_length = 0; + + + +/************************************************* +* Print tree * +*************************************************/ + +/* Recursive tree-printing subroutine. It uses a static vector of uschar to +hold the line-drawing characters that need to be printed on every line as it +moves down the page. This function is used only in debugging circumstances. The +output is done via debug_printf(). */ + +#define tree_printlinesize 132 /* line size for printing */ +static uschar tree_printline[tree_printlinesize]; + +/* Internal recursive subroutine. + +Arguments: + p tree node + pos amount of indenting & vertical bars to pring + barswitch if TRUE print | at the pos value + +Returns: nothing +*/ + +static void +tree_printsub(tree_node *p, int pos, int barswitch) +{ +int i; +if (p->right != NULL) tree_printsub(p->right, pos+2, 1); +for (i = 0; i <= pos-1; i++) debug_printf("%c", tree_printline[i]); +debug_printf("-->%s [%d]\n", p->name, p->balance); +tree_printline[pos] = barswitch? '|' : ' '; +if (p->left != NULL) + { + tree_printline[pos+2] = '|'; + tree_printsub(p->left, pos+2, 0); + } +} + +/* The external function, with just a tree node argument. */ + +void +debug_print_tree(tree_node *p) +{ +int i; +for (i = 0; i < tree_printlinesize; i++) tree_printline[i] = ' '; +if (p == NULL) debug_printf("Empty Tree\n"); else tree_printsub(p, 0, 0); +debug_printf("---- End of tree ----\n"); +} + + + +/************************************************* +* Print an argv vector * +*************************************************/ + +/* Called when about to obey execv(). + +Argument: the argv vector +Returns: nothing +*/ + +void +debug_print_argv(uschar **argv) +{ +debug_printf("exec"); +while (*argv != NULL) debug_printf(" %.256s", *argv++); +debug_printf("\n"); +} + + + +/************************************************* +* Expand and print debugging string * +*************************************************/ + +/* The string is expanded and written as debugging output. If +expansion fails, a message is written instead. + +Argument: the string +Returns: nothing +*/ + +void +debug_print_string(uschar *debug_string) +{ +if (debug_string == NULL) return; +HDEBUG(D_any|D_v) + { + uschar *s = expand_string(debug_string); + if (s == NULL) + debug_printf("failed to expand debug_output \"%s\": %s\n", debug_string, + expand_string_message); + else if (s[0] != 0) + debug_printf("%s%s", s, (s[Ustrlen(s)-1] == '\n')? "" : "\n"); + } +} + + + +/************************************************* +* Print current uids and gids * +*************************************************/ + +/* +Argument: an introductory string +Returns: nothing +*/ + +void +debug_print_ids(uschar *s) +{ +debug_printf("%s uid=%ld gid=%ld euid=%ld egid=%ld\n", s, + (long int)getuid(), (long int)getgid(), (long int)geteuid(), + (long int)getegid()); +} + + + + +/************************************************* +* Print debugging message * +*************************************************/ + +/* There are two entries, one for use when being called directly from a +function with a variable argument list. + +If debug_pid is nonzero, print the pid at the start of each line. This is for +tidier output when running parallel remote deliveries with debugging turned on. +Must do the whole thing with a single printf and flush, as otherwise output may +get interleaved. Since some calls to debug_printf() don't end with newline, +we save up the text until we do get the newline. */ + +void +debug_printf(char *format, ...) +{ +va_list ap; +va_start(ap, format); +debug_vprintf(format, ap); +va_end(ap); +} + +void +debug_vprintf(char *format, va_list ap) +{ +if (debug_file == NULL) return; + +/* Various things can be inserted at the start of a line. */ + +if (debug_ptr == debug_buffer) + { + DEBUG(D_timestamp) + { + uschar *ts = tod_stamp(tod_log_bare); + sprintf(CS debug_ptr, "%s ", ts + 11); + while(*debug_ptr != 0) debug_ptr++; + } + + DEBUG(D_pid) + { + sprintf(CS debug_ptr, "%5d ", (int)getpid()); + while(*debug_ptr != 0) debug_ptr++; + } + + /* Set up prefix if outputting for host checking and not debugging */ + + if (host_checking && debug_selector == 0) + { + Ustrcpy(debug_ptr, ">>> "); + debug_ptr += 4; + } + + debug_prefix_length = debug_ptr - debug_buffer; + } + +/* Use the checked formatting routine to ensure that the buffer +does not overflow. Ensure there's space for a newline at the end. */ + +if (!string_vformat(debug_ptr, + sizeof(debug_buffer) - (debug_ptr - debug_buffer) - 1, format, ap)) + { + uschar *s = US"**** debug string too long - truncated ****\n"; + uschar *p = debug_buffer + Ustrlen(debug_buffer); + int maxlen = sizeof(debug_buffer) - Ustrlen(s) - 3; + if (p > debug_buffer + maxlen) p = debug_buffer + maxlen; + if (p > debug_buffer && p[-1] != '\n') *p++ = '\n'; + Ustrcpy(p, s); + } + +while(*debug_ptr != 0) debug_ptr++; + +/* Output the line if it is complete. If we added any prefix data and there +are internal newlines, make sure the prefix is on the continuation lines, +as long as there is room in the buffer. We want to do just a single fprintf() +so as to avoid interleaving. */ + +if (debug_ptr[-1] == '\n') + { + if (debug_prefix_length > 0) + { + uschar *p = debug_buffer; + int left = sizeof(debug_buffer) - (debug_ptr - debug_buffer) - 1; + while ((p = Ustrchr(p, '\n') + 1) != debug_ptr && + left >= debug_prefix_length) + { + int len = debug_ptr - p; + memmove(p + debug_prefix_length, p, len + 1); + memmove(p, debug_buffer, debug_prefix_length); + debug_ptr += debug_prefix_length; + left -= debug_prefix_length; + } + } + + fprintf(debug_file, "%s", CS debug_buffer); + fflush(debug_file); + debug_ptr = debug_buffer; + debug_prefix_length = 0; + } +} + +/* End of debug.c */ diff --git a/src/src/deliver.c b/src/src/deliver.c new file mode 100644 index 000000000..741d7b79c --- /dev/null +++ b/src/src/deliver.c @@ -0,0 +1,6738 @@ +/* $Cambridge: exim/src/src/deliver.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* The main code for delivering a message. */ + + +#include "exim.h" + + +/* Data block for keeping track of subprocesses for parallel remote +delivery. */ + +typedef struct pardata { + address_item *addrlist; /* chain of addresses */ + address_item *addr; /* next address data expected for */ + pid_t pid; /* subprocess pid */ + int fd; /* pipe fd for getting result from subprocess */ + int transport_count; /* returned transport count value */ + BOOL done; /* no more data needed */ + uschar *msg; /* error message */ + uschar *return_path; /* return_path for these addresses */ +} pardata; + +/* Values for the process_recipients variable */ + +enum { RECIP_ACCEPT, RECIP_IGNORE, RECIP_DEFER, + RECIP_FAIL, RECIP_FAIL_FILTER, RECIP_FAIL_TIMEOUT, + RECIP_FAIL_LOOP}; + +/* Mutually recursive functions for marking addresses done. */ + +static void child_done(address_item *, uschar *); +static void address_done(address_item *, uschar *); + +/* Table for turning base-62 numbers into binary */ + +static uschar tab62[] = + {0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, /* 0-9 */ + 0,10,11,12,13,14,15,16,17,18,19,20, /* A-K */ + 21,22,23,24,25,26,27,28,29,30,31,32, /* L-W */ + 33,34,35, 0, 0, 0, 0, 0, /* X-Z */ + 0,36,37,38,39,40,41,42,43,44,45,46, /* a-k */ + 47,48,49,50,51,52,53,54,55,56,57,58, /* l-w */ + 59,60,61}; /* x-z */ + + +/************************************************* +* Local static variables * +*************************************************/ + +/* addr_duplicate is global because it needs to be seen from the Envelope-To +writing code. */ + +static address_item *addr_defer = NULL; +static address_item *addr_failed = NULL; +static address_item *addr_fallback = NULL; +static address_item *addr_local = NULL; +static address_item *addr_new = NULL; +static address_item *addr_remote = NULL; +static address_item *addr_route = NULL; +static address_item *addr_succeed = NULL; + +static FILE *message_log = NULL; +static BOOL update_spool; +static BOOL remove_journal; +static int parcount = 0; +static pardata *parlist = NULL; +static int return_count; +static uschar *frozen_info = US""; +static uschar *used_return_path = NULL; + +static uschar spoolname[PATH_MAX]; + + + +/************************************************* +* Make a new address item * +*************************************************/ + +/* This function gets the store and initializes with default values. The +transport_return value defaults to DEFER, so that any unexpected failure to +deliver does not wipe out the message. The default unique string is set to a +copy of the address, so that its domain can be lowercased. + +Argument: + address the RFC822 address string + copy force a copy of the address + +Returns: a pointer to an initialized address_item +*/ + +address_item * +deliver_make_addr(uschar *address, BOOL copy) +{ +address_item *addr = store_get(sizeof(address_item)); +*addr = address_defaults; +if (copy) address = string_copy(address); +addr->address = address; +addr->unique = string_copy(address); +return addr; +} + + + + +/************************************************* +* Set expansion values for an address * +*************************************************/ + +/* Certain expansion variables are valid only when handling an address or +address list. This function sets them up or clears the values, according to its +argument. + +Arguments: + addr the address in question, or NULL to clear values +Returns: nothing +*/ + +void +deliver_set_expansions(address_item *addr) +{ +if (addr == NULL) + { + uschar ***p = address_expansions; + while (*p != NULL) **p++ = NULL; + return; + } + +/* Exactly what gets set depends on whether there is one or more addresses, and +what they contain. These first ones are always set, taking their values from +the first address. */ + +if (addr->host_list == NULL) + { + deliver_host = deliver_host_address = US""; + } +else + { + deliver_host = addr->host_list->name; + deliver_host_address = addr->host_list->address; + } + +deliver_recipients = addr; +deliver_address_data = addr->p.address_data; +deliver_domain_data = addr->p.domain_data; +deliver_localpart_data = addr->p.localpart_data; + +/* These may be unset for multiple addresses */ + +deliver_domain = addr->domain; +self_hostname = addr->self_hostname; + +/* If there's only one address we can set everything. */ + +if (addr->next == NULL) + { + address_item *addr_orig; + + deliver_localpart = addr->local_part; + deliver_localpart_prefix = addr->prefix; + deliver_localpart_suffix = addr->suffix; + + for (addr_orig = addr; addr_orig->parent != NULL; + addr_orig = addr_orig->parent); + deliver_domain_orig = addr_orig->domain; + + /* Re-instate any prefix and suffix in the original local part. In all + normal cases, the address will have a router associated with it, and we can + choose the caseful or caseless version accordingly. However, when a system + filter sets up a pipe, file, or autoreply delivery, no router is involved. + In this case, though, there won't be any prefix or suffix to worry about. */ + + deliver_localpart_orig = (addr_orig->router == NULL)? addr_orig->local_part : + addr_orig->router->caseful_local_part? + addr_orig->cc_local_part : addr_orig->lc_local_part; + + /* If there's a parent, make its domain and local part available, and if + delivering to a pipe or file, or sending an autoreply, get the local + part from the parent. For pipes and files, put the pipe or file string + into address_pipe and address_file. */ + + if (addr->parent != NULL) + { + deliver_domain_parent = addr->parent->domain; + deliver_localpart_parent = (addr->parent->router == NULL)? + addr->parent->local_part : + addr->parent->router->caseful_local_part? + addr->parent->cc_local_part : addr->parent->lc_local_part; + + /* File deliveries have their own flag because they need to be picked out + as special more often. */ + + if (testflag(addr, af_pfr)) + { + if (testflag(addr, af_file)) address_file = addr->local_part; + else if (deliver_localpart[0] == '|') address_pipe = addr->local_part; + deliver_localpart = addr->parent->local_part; + deliver_localpart_prefix = addr->parent->prefix; + deliver_localpart_suffix = addr->parent->suffix; + } + } + } + +/* For multiple addresses, don't set local part, and leave the domain and +self_hostname set only if it is the same for all of them. */ + +else + { + address_item *addr2; + for (addr2 = addr->next; addr2 != NULL; addr2 = addr2->next) + { + if (deliver_domain != NULL && + Ustrcmp(deliver_domain, addr2->domain) != 0) + deliver_domain = NULL; + if (self_hostname != NULL && (addr2->self_hostname == NULL || + Ustrcmp(self_hostname, addr2->self_hostname) != 0)) + self_hostname = NULL; + if (deliver_domain == NULL && self_hostname == NULL) break; + } + } +} + + + + +/************************************************* +* Open a msglog file * +*************************************************/ + +/* This function is used both for normal message logs, and for files in the +msglog directory that are used to catch output from pipes. Try to create the +directory if it does not exist. From release 4.21, normal message logs should +be created when the message is received. + +Argument: + filename the file name + mode the mode required + error used for saying what failed + +Returns: a file descriptor, or -1 (with errno set) +*/ + +static int +open_msglog_file(uschar *filename, int mode, uschar **error) +{ +int fd = Uopen(filename, O_WRONLY|O_APPEND|O_CREAT, mode); + +if (fd < 0 && errno == ENOENT) + { + uschar temp[16]; + sprintf(CS temp, "msglog/%s", message_subdir); + if (message_subdir[0] == 0) temp[6] = 0; + (void)directory_make(spool_directory, temp, MSGLOG_DIRECTORY_MODE, TRUE); + fd = Uopen(filename, O_WRONLY|O_APPEND|O_CREAT, mode); + } + +/* Set the close-on-exec flag and change the owner to the exim uid/gid (this +function is called as root). Double check the mode, because the group setting +doesn't always get set automatically. */ + +if (fd >= 0) + { + fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); + if (fchown(fd, exim_uid, exim_gid) < 0) + { + *error = US"chown"; + return -1; + } + if (fchmod(fd, mode) < 0) + { + *error = US"chmod"; + return -1; + } + } +else *error = US"create"; + +return fd; +} + + + + +/************************************************* +* Write to msglog if required * +*************************************************/ + +/* Write to the message log, if configured. This function may also be called +from transports. + +Arguments: + format a string format + +Returns: nothing +*/ + +void +deliver_msglog(const char *format, ...) +{ +va_list ap; +if (!message_logs) return; +va_start(ap, format); +vfprintf(message_log, format, ap); +fflush(message_log); +va_end(ap); +} + + + + +/************************************************* +* Replicate status for batch * +*************************************************/ + +/* When a transport handles a batch of addresses, it may treat them +individually, or it may just put the status in the first one, and return FALSE, +requesting that the status be copied to all the others externally. This is the +replication function. As well as the status, it copies the transport pointer, +which may have changed if appendfile passed the addresses on to a different +transport. + +Argument: pointer to the first address in a chain +Returns: nothing +*/ + +static void +replicate_status(address_item *addr) +{ +address_item *addr2; +for (addr2 = addr->next; addr2 != NULL; addr2 = addr2->next) + { + addr2->transport = addr->transport; + addr2->transport_return = addr->transport_return; + addr2->basic_errno = addr->basic_errno; + addr2->more_errno = addr->more_errno; + addr2->special_action = addr->special_action; + addr2->message = addr->message; + addr2->user_message = addr->user_message; + } +} + + + +/************************************************* +* Compare lists of hosts * +*************************************************/ + +/* This function is given two pointers to chains of host items, and it yields +TRUE if the lists refer to the same hosts in the same order, except that + +(1) Multiple hosts with the same non-negative MX values are permitted to appear + in different orders. Round-robinning nameservers can cause this to happen. + +(2) Multiple hosts with the same negative MX values less than MX_NONE are also + permitted to appear in different orders. This is caused by randomizing + hosts lists. + +This enables Exim to use a single SMTP transaction for sending to two entirely +different domains that happen to end up pointing at the same hosts. + +Arguments: + one points to the first host list + two points to the second host list + +Returns: TRUE if the lists refer to the same host set +*/ + +static BOOL +same_hosts(host_item *one, host_item *two) +{ +while (one != NULL && two != NULL) + { + if (Ustrcmp(one->name, two->name) != 0) + { + int mx = one->mx; + host_item *end_one = one; + host_item *end_two = two; + + /* Batch up only if there was no MX and the list was not randomized */ + + if (mx == MX_NONE) return FALSE; + + /* Find the ends of the shortest sequence of identical MX values */ + + while (end_one->next != NULL && end_one->next->mx == mx && + end_two->next != NULL && end_two->next->mx == mx) + { + end_one = end_one->next; + end_two = end_two->next; + } + + /* If there aren't any duplicates, there's no match. */ + + if (end_one == one) return FALSE; + + /* For each host in the 'one' sequence, check that it appears in the 'two' + sequence, returning FALSE if not. */ + + for (;;) + { + host_item *hi; + for (hi = two; hi != end_two->next; hi = hi->next) + if (Ustrcmp(one->name, hi->name) == 0) break; + if (hi == end_two->next) return FALSE; + if (one == end_one) break; + one = one->next; + } + + /* All the hosts in the 'one' sequence were found in the 'two' sequence. + Ensure both are pointing at the last host, and carry on as for equality. */ + + two = end_two; + } + + /* Hosts matched */ + + one = one->next; + two = two->next; + } + +/* True if both are NULL */ + +return (one == two); +} + + + +/************************************************* +* Compare header lines * +*************************************************/ + +/* This function is given two pointers to chains of header items, and it yields +TRUE if they are the same header texts in the same order. + +Arguments: + one points to the first header list + two points to the second header list + +Returns: TRUE if the lists refer to the same header set +*/ + +static BOOL +same_headers(header_line *one, header_line *two) +{ +for (;;) + { + if (one == two) return TRUE; /* Includes the case where both NULL */ + if (one == NULL || two == NULL) return FALSE; + if (Ustrcmp(one->text, two->text) != 0) return FALSE; + one = one->next; + two = two->next; + } +} + + + +/************************************************* +* Compare string settings * +*************************************************/ + +/* This function is given two pointers to strings, and it returns +TRUE if they are the same pointer, or if the two strings are the same. + +Arguments: + one points to the first string + two points to the second string + +Returns: TRUE or FALSE +*/ + +static BOOL +same_strings(uschar *one, uschar *two) +{ +if (one == two) return TRUE; /* Includes the case where both NULL */ +if (one == NULL || two == NULL) return FALSE; +return (Ustrcmp(one, two) == 0); +} + + + +/************************************************* +* Compare uid/gid for addresses * +*************************************************/ + +/* This function is given a transport and two addresses. It yields TRUE if the +uid/gid/initgroups settings for the two addresses are going to be the same when +they are delivered. + +Arguments: + tp the transort + addr1 the first address + addr2 the second address + +Returns: TRUE or FALSE +*/ + +static BOOL +same_ugid(transport_instance *tp, address_item *addr1, address_item *addr2) +{ +if (!tp->uid_set && tp->expand_uid == NULL && !tp->deliver_as_creator) + { + if (testflag(addr1, af_uid_set) != testflag(addr2, af_gid_set) || + (testflag(addr1, af_uid_set) && + (addr1->uid != addr2->uid || + testflag(addr1, af_initgroups) != testflag(addr2, af_initgroups)))) + return FALSE; + } + +if (!tp->gid_set && tp->expand_gid == NULL) + { + if (testflag(addr1, af_gid_set) != testflag(addr2, af_gid_set) || + (testflag(addr1, af_gid_set) && addr1->gid != addr2->gid)) + return FALSE; + } + +return TRUE; +} + + + + +/************************************************* +* Record that an address is complete * +*************************************************/ + +/* This function records that an address is complete. This is straightforward +for most addresses, where the unique address is just the full address with the +domain lower cased. For homonyms (addresses that are the same as one of their +ancestors) their are complications. Their unique addresses have \x\ prepended +(where x = 0, 1, 2...), so that de-duplication works correctly for siblings and +cousins. + +Exim used to record the unique addresses of homonyms as "complete". This, +however, fails when the pattern of redirection varies over time (e.g. if taking +unseen copies at only some times of day) because the prepended numbers may vary +from one delivery run to the next. This problem is solved by never recording +prepended unique addresses as complete. Instead, when a homonymic address has +actually been delivered via a transport, we record its basic unique address +followed by the name of the transport. This is checked in subsequent delivery +runs whenever an address is routed to a transport. + +If the completed address is a top-level one (has no parent, which means it +cannot be homonymic) we also add the original address to the non-recipients +tree, so that it gets recorded in the spool file and therefore appears as +"done" in any spool listings. The original address may differ from the unique +address in the case of the domain. + +Finally, this function scans the list of duplicates, marks as done any that +match this address, and calls child_done() for their ancestors. + +Arguments: + addr address item that has been completed + now current time as a string + +Returns: nothing +*/ + +static void +address_done(address_item *addr, uschar *now) +{ +address_item *dup; + +update_spool = TRUE; /* Ensure spool gets updated */ + +/* Top-level address */ + +if (addr->parent == NULL) + { + tree_add_nonrecipient(addr->unique); + tree_add_nonrecipient(addr->address); + } + +/* Homonymous child address */ + +else if (testflag(addr, af_homonym)) + { + if (addr->transport != NULL) + { + tree_add_nonrecipient( + string_sprintf("%s/%s", addr->unique + 3, addr->transport->name)); + } + } + +/* Non-homonymous child address */ + +else tree_add_nonrecipient(addr->unique); + +/* Check the list of duplicate addresses and ensure they are now marked +done as well. */ + +for (dup = addr_duplicate; dup != NULL; dup = dup->next) + { + if (Ustrcmp(addr->unique, dup->unique) == 0) + { + tree_add_nonrecipient(dup->address); + child_done(dup, now); + } + } +} + + + + +/************************************************* +* Decrease counts in parents and mark done * +*************************************************/ + +/* This function is called when an address is complete. If there is a parent +address, its count of children is decremented. If there are still other +children outstanding, the function exits. Otherwise, if the count has become +zero, address_done() is called to mark the parent and its duplicates complete. +Then loop for any earlier ancestors. + +Arguments: + addr points to the completed address item + now the current time as a string, for writing to the message log + +Returns: nothing +*/ + +static void +child_done(address_item *addr, uschar *now) +{ +address_item *aa; +while (addr->parent != NULL) + { + addr = addr->parent; + if ((addr->child_count -= 1) > 0) return; /* Incomplete parent */ + address_done(addr, now); + + /* Log the completion of all descendents only when there is no ancestor with + the same original address. */ + + for (aa = addr->parent; aa != NULL; aa = aa->parent) + if (Ustrcmp(aa->address, addr->address) == 0) break; + if (aa != NULL) continue; + + deliver_msglog("%s %s: children all complete\n", now, addr->address); + DEBUG(D_deliver) debug_printf("%s: children all complete\n", addr->address); + } +} + + + + +/************************************************* +* Actions at the end of handling an address * +*************************************************/ + +/* This is a function for processing a single address when all that can be done +with it has been done. + +Arguments: + addr points to the address block + result the result of the delivery attempt + logflags flags for log_write() (LOG_MAIN and/or LOG_PANIC) + driver_type indicates which type of driver (transport, or router) was last + to process the address + logchar '=' or '-' for use when logging deliveries with => or -> + +Returns: nothing +*/ + +static void +post_process_one(address_item *addr, int result, int logflags, int driver_type, + int logchar) +{ +uschar *now = tod_stamp(tod_log); +uschar *driver_kind = NULL; +uschar *driver_name = NULL; +uschar *log_address; + +int size = 256; /* Used for a temporary, */ +int ptr = 0; /* expanding buffer, for */ +uschar *s; /* building log lines; */ +void *reset_point; /* released afterwards. */ + + +DEBUG(D_deliver) debug_printf("post-process %s (%d)\n", addr->address, result); + +/* Set up driver kind and name for logging. Disable logging if the router or +transport has disabled it. */ + +if (driver_type == DTYPE_TRANSPORT) + { + if (addr->transport != NULL) + { + driver_name = addr->transport->name; + driver_kind = US" transport"; + disable_logging = addr->transport->disable_logging; + } + else driver_kind = US"transporting"; + } +else if (driver_type == DTYPE_ROUTER) + { + if (addr->router != NULL) + { + driver_name = addr->router->name; + driver_kind = US" router"; + disable_logging = addr->router->disable_logging; + } + else driver_kind = US"routing"; + } + +/* If there's an error message set, ensure that it contains only printing +characters - it should, but occasionally things slip in and this at least +stops the log format from getting wrecked. */ + +if (addr->message != NULL) addr->message = string_printing(addr->message); + +/* If we used a transport that has one of the "return_output" options set, and +if it did in fact generate some output, then for return_output we treat the +message as failed if it was not already set that way, so that the output gets +returned to the sender, provided there is a sender to send it to. For +return_fail_output, do this only if the delivery failed. Otherwise we just +unlink the file, and remove the name so that if the delivery failed, we don't +try to send back an empty or unwanted file. The log_output options operate only +on a non-empty file. + +In any case, we close the message file, because we cannot afford to leave a +file-descriptor for one address while processing (maybe very many) others. */ + +if (addr->return_file >= 0 && addr->return_filename != NULL) + { + BOOL return_output = FALSE; + struct stat statbuf; + fsync(addr->return_file); + + /* If there is no output, do nothing. */ + + if (fstat(addr->return_file, &statbuf) == 0 && statbuf.st_size > 0) + { + transport_instance *tb = addr->transport; + + /* Handle logging options */ + + if (tb->log_output || (result == FAIL && tb->log_fail_output) || + (result == DEFER && tb->log_defer_output)) + { + uschar *s; + FILE *f = Ufopen(addr->return_filename, "rb"); + if (f == NULL) + log_write(0, LOG_MAIN|LOG_PANIC, "failed to open %s to log output " + "from %s transport: %s", addr->return_filename, tb->name, + strerror(errno)); + else + { + s = US Ufgets(big_buffer, big_buffer_size, f); + if (s != NULL) + { + uschar *p = big_buffer + Ustrlen(big_buffer); + while (p > big_buffer && isspace(p[-1])) p--; + *p = 0; + s = string_printing(big_buffer); + log_write(0, LOG_MAIN, "<%s>: %s transport output: %s", + addr->address, tb->name, s); + } + fclose(f); + } + } + + /* Handle returning options, but only if there is an address to return + the text to. */ + + if (sender_address[0] != 0 || addr->p.errors_address != NULL) + { + if (tb->return_output) + { + addr->transport_return = result = FAIL; + if (addr->basic_errno == 0 && addr->message == NULL) + addr->message = US"return message generated"; + return_output = TRUE; + } + else + if (tb->return_fail_output && result == FAIL) return_output = TRUE; + } + } + + /* Get rid of the file unless it might be returned, but close it in + all cases. */ + + if (!return_output) + { + Uunlink(addr->return_filename); + addr->return_filename = NULL; + addr->return_file = -1; + } + + close(addr->return_file); + } + +/* Create the address string for logging. Must not do this earlier, because +an OK result may be changed to FAIL when a pipe returns text. */ + +log_address = string_log_address(addr, + (log_write_selector & L_all_parents) != 0, result == OK); + +/* The sucess case happens only after delivery by a transport. */ + +if (result == OK) + { + addr->next = addr_succeed; + addr_succeed = addr; + + /* Call address_done() to ensure that we don't deliver to this address again, + and write appropriate things to the message log. If it is a child address, we + call child_done() to scan the ancestors and mark them complete if this is the + last child to complete. */ + + address_done(addr, now); + DEBUG(D_deliver) debug_printf("%s delivered\n", addr->address); + + if (addr->parent == NULL) + { + deliver_msglog("%s %s: %s%s succeeded\n", now, addr->address, + driver_name, driver_kind); + } + else + { + deliver_msglog("%s %s <%s>: %s%s succeeded\n", now, addr->address, + addr->parent->address, driver_name, driver_kind); + child_done(addr, now); + } + + /* Log the delivery on the main log. We use an extensible string to build up + the log line, and reset the store afterwards. Remote deliveries should always + have a pointer to the host item that succeeded; local deliveries can have a + pointer to a single host item in their host list, for use by the transport. */ + + s = reset_point = store_get(size); + s[ptr++] = logchar; + + s = string_append(s, &size, &ptr, 2, US"> ", log_address); + + if ((log_extra_selector & LX_sender_on_delivery) != 0) + s = string_append(s, &size, &ptr, 3, US" F=<", sender_address, US">"); + + /* You might think that the return path must always be set for a successful + delivery; indeed, I did for some time, until this statement crashed. The case + when it is not set is for a delivery to /dev/null which is optimised by not + being run at all. */ + + if (used_return_path != NULL && + (log_extra_selector & LX_return_path_on_delivery) != 0) + s = string_append(s, &size, &ptr, 3, US" P=<", used_return_path, US">"); + + /* For a delivery from a system filter, there may not be a router */ + + if (addr->router != NULL) + s = string_append(s, &size, &ptr, 2, US" R=", addr->router->name); + + s = string_append(s, &size, &ptr, 2, US" T=", addr->transport->name); + + if ((log_extra_selector & LX_delivery_size) != 0) + s = string_append(s, &size, &ptr, 2, US" S=", + string_sprintf("%d", transport_count)); + + /* Local delivery */ + + if (addr->transport->info->local) + { + if (addr->host_list != NULL) + s = string_append(s, &size, &ptr, 2, US" H=", addr->host_list->name); + if (addr->shadow_message != NULL) + s = string_cat(s, &size, &ptr, addr->shadow_message, + Ustrlen(addr->shadow_message)); + } + + /* Remote delivery */ + + else + { + if (addr->host_used != NULL) + { + s = string_append(s, &size, &ptr, 5, US" H=", addr->host_used->name, + US" [", addr->host_used->address, US"]"); + if ((log_extra_selector & LX_outgoing_port) != 0) + s = string_append(s, &size, &ptr, 2, US":", string_sprintf("%d", + addr->host_used->port)); + if (continue_sequence > 1) + s = string_cat(s, &size, &ptr, US"*", 1); + } + + #ifdef SUPPORT_TLS + if ((log_extra_selector & LX_tls_cipher) != 0 && addr->cipher != NULL) + s = string_append(s, &size, &ptr, 2, US" X=", addr->cipher); + if ((log_extra_selector & LX_tls_certificate_verified) != 0 && + addr->cipher != NULL) + s = string_append(s, &size, &ptr, 2, US" CV=", + testflag(addr, af_cert_verified)? "yes":"no"); + if ((log_extra_selector & LX_tls_peerdn) != 0 && addr->peerdn != NULL) + s = string_append(s, &size, &ptr, 3, US" DN=\"", addr->peerdn, US"\""); + #endif + + if ((log_extra_selector & LX_smtp_confirmation) != 0 && + addr->message != NULL) + { + int i; + uschar *p = big_buffer; + uschar *ss = addr->message; + *p++ = '\"'; + for (i = 0; i < 100 && ss[i] != 0; i++) + { + if (ss[i] == '\"' || ss[i] == '\\') *p++ = '\\'; + *p++ = ss[i]; + } + *p++ = '\"'; + *p = 0; + s = string_append(s, &size, &ptr, 2, US" C=", big_buffer); + } + } + + /* Time on queue and actual time taken to deliver */ + + if ((log_extra_selector & LX_queue_time) != 0) + { + s = string_append(s, &size, &ptr, 2, US" QT=", + readconf_printtime(time(NULL) - received_time)); + } + + if ((log_extra_selector & LX_deliver_time) != 0) + { + s = string_append(s, &size, &ptr, 2, US" DT=", + readconf_printtime(addr->more_errno)); + } + + /* string_cat() always leaves room for the terminator. Release the + store we used to build the line after writing it. */ + + s[ptr] = 0; + log_write(0, LOG_MAIN, "%s", s); + store_reset(reset_point); + } + + +/* Soft failure, or local delivery process failed; freezing may be +requested. */ + +else if (result == DEFER || result == PANIC) + { + if (result == PANIC) logflags |= LOG_PANIC; + + /* This puts them on the chain in reverse order. Do not change this, because + the code for handling retries assumes that the one with the retry + information is last. */ + + addr->next = addr_defer; + addr_defer = addr; + + /* The only currently implemented special action is to freeze the + message. Logging of this is done later, just before the -H file is + updated. */ + + if (addr->special_action == SPECIAL_FREEZE) + { + deliver_freeze = TRUE; + deliver_frozen_at = time(NULL); + update_spool = TRUE; + } + + /* If doing a 2-stage queue run, we skip writing to either the message + log or the main log for SMTP defers. */ + + if (!queue_2stage || addr->basic_errno != 0) + { + uschar ss[32]; + + /* For errors of the type "retry time not reached" (also remotes skipped + on queue run), logging is controlled by L_retry_defer. Note that this kind + of error number is negative, and all the retry ones are less than any + others. */ + + unsigned int use_log_selector = (addr->basic_errno <= ERRNO_RETRY_BASE)? + L_retry_defer : 0; + + /* Build up the line that is used for both the message log and the main + log. */ + + s = reset_point = store_get(size); + s = string_cat(s, &size, &ptr, log_address, Ustrlen(log_address)); + + /* Either driver_name contains something and driver_kind contains + " router" or " transport" (note the leading space), or driver_name is + a null string and driver_kind contains "routing" without the leading + space, if all routing has been deferred. When a domain has been held, + so nothing has been done at all, both variables contain null strings. */ + + if (driver_name == NULL) + { + if (driver_kind != NULL) + s = string_append(s, &size, &ptr, 2, US" ", driver_kind); + } + else + { + if (driver_kind[1] == 't' && addr->router != NULL) + s = string_append(s, &size, &ptr, 2, US" R=", addr->router->name); + Ustrcpy(ss, " ?="); + ss[1] = toupper(driver_kind[1]); + s = string_append(s, &size, &ptr, 2, ss, driver_name); + } + + sprintf(CS ss, " defer (%d)", addr->basic_errno); + s = string_cat(s, &size, &ptr, ss, Ustrlen(ss)); + + if (addr->basic_errno > 0) + s = string_append(s, &size, &ptr, 2, US": ", + US strerror(addr->basic_errno)); + + if (addr->message != NULL) + s = string_append(s, &size, &ptr, 2, US": ", addr->message); + + s[ptr] = 0; + + /* Log the deferment in the message log, but don't clutter it + up with retry-time defers after the first delivery attempt. */ + + if (deliver_firsttime || addr->basic_errno > ERRNO_RETRY_BASE) + deliver_msglog("%s %s\n", now, s); + + /* Write the main log and reset the store */ + + log_write(use_log_selector, logflags, "== %s", s); + store_reset(reset_point); + } + } + + +/* Hard failure. If there is an address to which an error message can be sent, +put this address on the failed list. If not, put it on the deferred list and +freeze the mail message for human attention. The latter action can also be +explicitly requested by a router or transport. */ + +else + { + /* If this is a delivery error, or a message for which no replies are + wanted, and the message's age is greater than ignore_bounce_errors_after, + force the af_ignore_error flag. This will cause the address to be discarded + later (with a log entry). */ + + if (sender_address[0] == 0 && message_age >= ignore_bounce_errors_after) + setflag(addr, af_ignore_error); + + /* Freeze the message if requested, or if this is a bounce message (or other + message with null sender). However, don't freeze if errors are being ignored. + The actual code to ignore occurs later, instead of sending a message. Logging + of freezing occurs later, just before writing the -H file. */ + + if (!testflag(addr, af_ignore_error) && + (addr->special_action == SPECIAL_FREEZE || sender_address[0] == 0)) + { + frozen_info = (addr->special_action == SPECIAL_FREEZE)? US"" : + (sender_local && !local_error_message)? + US" (message created with -f <>)" : US" (delivery error message)"; + deliver_freeze = TRUE; + deliver_frozen_at = time(NULL); + update_spool = TRUE; + + /* The address is put on the defer rather than the failed queue, because + the message is being retained. */ + + addr->next = addr_defer; + addr_defer = addr; + } + + /* Don't put the address on the nonrecipients tree yet; wait until an + error message has been successfully sent. */ + + else + { + addr->next = addr_failed; + addr_failed = addr; + } + + /* Build up the log line for the message and main logs */ + + s = reset_point = store_get(size); + s = string_cat(s, &size, &ptr, log_address, Ustrlen(log_address)); + + if ((log_extra_selector & LX_sender_on_delivery) != 0) + s = string_append(s, &size, &ptr, 3, US" F=<", sender_address, US">"); + + /* Return path may not be set if no delivery actually happened */ + + if (used_return_path != NULL && + (log_extra_selector & LX_return_path_on_delivery) != 0) + { + s = string_append(s, &size, &ptr, 3, US" P=<", used_return_path, US">"); + } + + if (addr->router != NULL) + s = string_append(s, &size, &ptr, 2, US" R=", addr->router->name); + if (addr->transport != NULL) + s = string_append(s, &size, &ptr, 2, US" T=", addr->transport->name); + + if (addr->host_used != NULL) + s = string_append(s, &size, &ptr, 5, US" H=", addr->host_used->name, + US" [", addr->host_used->address, US"]"); + + if (addr->basic_errno > 0) + s = string_append(s, &size, &ptr, 2, US": ", + US strerror(addr->basic_errno)); + + if (addr->message != NULL) + s = string_append(s, &size, &ptr, 2, US": ", addr->message); + + s[ptr] = 0; + + /* Do the logging. For the message log, "routing failed" for those cases, + just to make it clearer. */ + + if (driver_name == NULL) + deliver_msglog("%s %s failed for %s\n", now, driver_kind, s); + else + deliver_msglog("%s %s\n", now, s); + + log_write(0, LOG_MAIN, "** %s", s); + store_reset(reset_point); + } + +/* Ensure logging is turned on again in all cases */ + +disable_logging = FALSE; +} + + + + +/************************************************* +* Address-independent error * +*************************************************/ + +/* This function is called when there's an error that is not dependent on a +particular address, such as an expansion string failure. It puts the error into +all the addresses in a batch, logs the incident on the main and panic logs, and +clears the expansions. It is mostly called from local_deliver(), but can be +called for a remote delivery via findugid(). + +Arguments: + logit TRUE if (MAIN+PANIC) logging required + addr the first of the chain of addresses + code the error code + format format string for error message, or NULL if already set in addr + ... arguments for the format + +Returns: nothing +*/ + +static void +common_error(BOOL logit, address_item *addr, int code, uschar *format, ...) +{ +address_item *addr2; +addr->basic_errno = code; + +if (format != NULL) + { + va_list ap; + uschar buffer[512]; + va_start(ap, format); + if (!string_vformat(buffer, sizeof(buffer), CS format, ap)) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, + "common_error expansion was longer than %d", sizeof(buffer)); + va_end(ap); + addr->message = string_copy(buffer); + } + +for (addr2 = addr->next; addr2 != NULL; addr2 = addr2->next) + { + addr2->basic_errno = code; + addr2->message = addr->message; + } + +if (logit) log_write(0, LOG_MAIN|LOG_PANIC, "%s", addr->message); +deliver_set_expansions(NULL); +} + + + + +/************************************************* +* Check a "never users" list * +*************************************************/ + +/* This function is called to check whether a uid is on one of the two "never +users" lists. + +Arguments: + uid the uid to be checked + nusers the list to be scanned; the first item in the list is the count + +Returns: TRUE if the uid is on the list +*/ + +static BOOL +check_never_users(uid_t uid, uid_t *nusers) +{ +int i; +if (nusers == NULL) return FALSE; +for (i = 1; i <= (int)(nusers[0]); i++) if (nusers[i] == uid) return TRUE; +return FALSE; +} + + + +/************************************************* +* Find uid and gid for a transport * +*************************************************/ + +/* This function is called for both local and remote deliveries, to find the +uid/gid under which to run the delivery. The values are taken preferentially +from the transport (either explicit or deliver_as_creator), then from the +address (i.e. the router), and if nothing is set, the exim uid/gid are used. If +the resulting uid is on the "never_users" or the "fixed_never_users" list, a +panic error is logged, and the function fails (which normally leads to delivery +deferral). + +Arguments: + addr the address (possibly a chain) + tp the transport + uidp pointer to uid field + gidp pointer to gid field + igfp pointer to the use_initgroups field + +Returns: FALSE if failed - error has been set in address(es) +*/ + +static BOOL +findugid(address_item *addr, transport_instance *tp, uid_t *uidp, gid_t *gidp, + BOOL *igfp) +{ +uschar *nuname = NULL; +BOOL gid_set = FALSE; + +/* Default initgroups flag comes from the transport */ + +*igfp = tp->initgroups; + +/* First see if there's a gid on the transport, either fixed or expandable. +The expanding function always logs failure itself. */ + +if (tp->gid_set) + { + *gidp = tp->gid; + gid_set = TRUE; + } +else if (tp->expand_gid != NULL) + { + if (route_find_expanded_group(tp->expand_gid, tp->name, US"transport", gidp, + &(addr->message))) gid_set = TRUE; + else + { + common_error(FALSE, addr, ERRNO_GIDFAIL, NULL); + return FALSE; + } + } + +/* Pick up a uid from the transport if one is set. */ + +if (tp->uid_set) *uidp = tp->uid; + +/* Otherwise, try for an expandable uid field. If it ends up as a numeric id, +it does not provide a passwd value from which a gid can be taken. */ + +else if (tp->expand_uid != NULL) + { + struct passwd *pw; + if (!route_find_expanded_user(tp->expand_uid, tp->name, US"transport", &pw, + uidp, &(addr->message))) + { + common_error(FALSE, addr, ERRNO_UIDFAIL, NULL); + return FALSE; + } + if (!gid_set && pw != NULL) + { + *gidp = pw->pw_gid; + gid_set = TRUE; + } + } + +/* If the transport doesn't set the uid, test the deliver_as_creator flag. */ + +else if (tp->deliver_as_creator) + { + *uidp = originator_uid; + if (!gid_set) + { + *gidp = originator_gid; + gid_set = TRUE; + } + } + +/* Otherwise see if the address specifies the uid and if so, take its +initgroups flag. The gid from the address is taken only if the transport hasn't +set it. In other words, a gid on the transport overrides the gid on the +address. */ + +else if (testflag(addr, af_uid_set)) + { + *uidp = addr->uid; + *igfp = testflag(addr, af_initgroups); + if (!gid_set) + { + *gidp = addr->gid; + gid_set = TRUE; + } + } + +/* Nothing has specified the uid - default to the Exim user, and group if the +gid is not set. */ + +else + { + *uidp = exim_uid; + if (!gid_set) + { + *gidp = exim_gid; + gid_set = TRUE; + } + } + +/* If no gid is set, it is a disaster. */ + +if (!gid_set) + { + common_error(TRUE, addr, ERRNO_GIDFAIL, US"User set without group for " + "%s transport", tp->name); + return FALSE; + } + +/* Check that the uid is not on the lists of banned uids that may not be used +for delivery processes. */ + +if (check_never_users(*uidp, never_users)) + nuname = US"never_users"; +else if (check_never_users(*uidp, fixed_never_users)) + nuname = US"fixed_never_users"; + +if (nuname != NULL) + { + common_error(TRUE, addr, ERRNO_UIDFAIL, US"User %ld set for %s transport " + "is on the %s list", (long int)(*uidp), tp->name, nuname); + return FALSE; + } + +/* All is well */ + +return TRUE; +} + + + + +/************************************************* +* Check the size of a message for a transport * +*************************************************/ + +/* Checks that the message isn't too big for the selected transport. +This is called only when it is known that the limit is set. + +Arguments: + tp the transport + addr the (first) address being delivered + +Returns: OK + DEFER expansion failed or did not yield an integer + FAIL message too big +*/ + +int +check_message_size(transport_instance *tp, address_item *addr) +{ +int rc = OK; +int size_limit; + +deliver_set_expansions(addr); +size_limit = expand_string_integer(tp->message_size_limit); +deliver_set_expansions(NULL); + +if (size_limit < 0) + { + rc = DEFER; + if (size_limit == -1) + addr->message = string_sprintf("failed to expand message_size_limit " + "in %s transport: %s", tp->name, expand_string_message); + else + addr->message = string_sprintf("invalid message_size_limit " + "in %s transport: %s", tp->name, expand_string_message); + } +else if (size_limit > 0 && message_size > size_limit) + { + rc = FAIL; + addr->message = + string_sprintf("message is too big (transport limit = %d)", + size_limit); + } + +return rc; +} + + + +/************************************************* +* Transport-time check for a previous delivery * +*************************************************/ + +/* Check that this base address hasn't previously been delivered to its routed +transport. The check is necessary at delivery time in order to handle homonymic +addresses correctly in cases where the pattern of redirection changes between +delivery attempts (so the unique fields change). Non-homonymic previous +delivery is detected earlier, at routing time (which saves unnecessary +routing). + +Argument: the address item +Returns: TRUE if previously delivered by the transport +*/ + +static BOOL +previously_transported(address_item *addr) +{ +(void)string_format(big_buffer, big_buffer_size, "%s/%s", + addr->unique + (testflag(addr, af_homonym)? 3:0), addr->transport->name); + +if (tree_search(tree_nonrecipients, big_buffer) != 0) + { + DEBUG(D_deliver|D_route|D_transport) + debug_printf("%s was previously delivered (%s transport): discarded\n", + addr->address, addr->transport->name); + child_done(addr, tod_stamp(tod_log)); + return TRUE; + } + +return FALSE; +} + + + + +/************************************************* +* Perform a local delivery * +*************************************************/ + +/* Each local delivery is performed in a separate process which sets its +uid and gid as specified. This is a safer way than simply changing and +restoring using seteuid(); there is a body of opinion that seteuid() cannot be +used safely. From release 4, Exim no longer makes any use of it. Besides, not +all systems have seteuid(). + +If the uid/gid are specified in the transport_instance, they are used; the +transport initialization must ensure that either both or neither are set. +Otherwise, the values associated with the address are used. If neither are set, +it is a configuration error. + +The transport or the address may specify a home directory (transport over- +rides), and if they do, this is set as $home. If neither have set a working +directory, this value is used for that as well. Otherwise $home is left unset +and the cwd is set to "/" - a directory that should be accessible to all users. + +Using a separate process makes it more complicated to get error information +back. We use a pipe to pass the return code and also an error code and error +text string back to the parent process. + +Arguments: + addr points to an address block for this delivery; for "normal" local + deliveries this is the only address to be delivered, but for + pseudo-remote deliveries (e.g. by batch SMTP to a file or pipe) + a number of addresses can be handled simultaneously, and in this + case addr will point to a chain of addresses with the same + characteristics. + + shadowing TRUE if running a shadow transport; this causes output from pipes + to be ignored. + +Returns: nothing +*/ + +static void +deliver_local(address_item *addr, BOOL shadowing) +{ +BOOL use_initgroups; +uid_t uid; +gid_t gid; +int status, len, rc; +int pfd[2]; +pid_t pid; +uschar *working_directory; +address_item *addr2; +transport_instance *tp = addr->transport; + +/* Set up the return path from the errors or sender address. If the transport +has its own return path setting, expand it and replace the existing value. */ + +return_path = (addr->p.errors_address != NULL)? + addr->p.errors_address : sender_address; + +if (tp->return_path != NULL) + { + uschar *new_return_path = expand_string(tp->return_path); + if (new_return_path == NULL) + { + if (!expand_string_forcedfail) + { + common_error(TRUE, addr, ERRNO_EXPANDFAIL, + US"Failed to expand return path \"%s\" in %s transport: %s", + tp->return_path, tp->name, expand_string_message); + return; + } + } + else return_path = new_return_path; + } + +/* For local deliveries, one at a time, the value used for logging can just be +set directly, once and for all. */ + +used_return_path = return_path; + +/* Sort out the uid, gid, and initgroups flag. If an error occurs, the message +gets put into the address(es), and the expansions are unset, so we can just +return. */ + +if (!findugid(addr, tp, &uid, &gid, &use_initgroups)) return; + +/* See if either the transport or the address specifies a home and/or a current +working directory. Expand it if necessary. If nothing is set, use "/", for the +working directory, which is assumed to be a directory to which all users have +access. It is necessary to be in a visible directory for some operating systems +when running pipes, as some commands (e.g. "rm" under Solaris 2.5) require +this. */ + +deliver_home = (tp->home_dir != NULL)? tp->home_dir : + (addr->home_dir != NULL)? addr->home_dir : NULL; + +if (deliver_home != NULL && !testflag(addr, af_home_expanded)) + { + uschar *rawhome = deliver_home; + deliver_home = NULL; /* in case it contains $home */ + deliver_home = expand_string(rawhome); + if (deliver_home == NULL) + { + common_error(TRUE, addr, ERRNO_EXPANDFAIL, US"home directory \"%s\" failed " + "to expand for %s transport: %s", rawhome, tp->name, + expand_string_message); + return; + } + if (*deliver_home != '/') + { + common_error(TRUE, addr, ERRNO_NOTABSOLUTE, US"home directory path \"%s\" " + "is not absolute for %s transport", deliver_home, tp->name); + return; + } + } + +working_directory = (tp->current_dir != NULL)? tp->current_dir : + (addr->current_dir != NULL)? addr->current_dir : NULL; + +if (working_directory != NULL) + { + uschar *raw = working_directory; + working_directory = expand_string(raw); + if (working_directory == NULL) + { + common_error(TRUE, addr, ERRNO_EXPANDFAIL, US"current directory \"%s\" " + "failed to expand for %s transport: %s", raw, tp->name, + expand_string_message); + return; + } + if (*working_directory != '/') + { + common_error(TRUE, addr, ERRNO_NOTABSOLUTE, US"current directory path " + "\"%s\" is not absolute for %s transport", working_directory, tp->name); + return; + } + } +else working_directory = (deliver_home == NULL)? US"/" : deliver_home; + +/* If one of the return_output flags is set on the transport, create and open a +file in the message log directory for the transport to write its output onto. +This is mainly used by pipe transports. The file needs to be unique to the +address. This feature is not available for shadow transports. */ + +if (!shadowing && (tp->return_output || tp->return_fail_output || + tp->log_output || tp->log_fail_output)) + { + uschar *error; + addr->return_filename = + string_sprintf("%s/msglog/%s/%s-%d-%d", spool_directory, message_subdir, + message_id, getpid(), return_count++); + addr->return_file = open_msglog_file(addr->return_filename, 0400, &error); + if (addr->return_file < 0) + { + common_error(TRUE, addr, errno, US"Unable to %s file for %s transport " + "to return message: %s", error, tp->name, strerror(errno)); + return; + } + } + +/* Create the pipe for inter-process communication. */ + +if (pipe(pfd) != 0) + { + common_error(TRUE, addr, ERRNO_PIPEFAIL, US"Creation of pipe failed: %s", + strerror(errno)); + return; + } + +/* Now fork the process to do the real work in the subprocess, but first +ensure that all cached resources are freed so that the subprocess starts with +a clean slate and doesn't interfere with the parent process. */ + +search_tidyup(); + +if ((pid = fork()) == 0) + { + BOOL replicate = TRUE; + + /* Prevent core dumps, as we don't want them in users' home directories. + HP-UX doesn't have RLIMIT_CORE; I don't know how to do this in that + system. Some experimental/developing systems (e.g. GNU/Hurd) may define + RLIMIT_CORE but not support it in setrlimit(). For such systems, do not + complain if the error is "not supported". */ + + #ifdef RLIMIT_CORE + struct rlimit rl; + rl.rlim_cur = 0; + rl.rlim_max = 0; + if (setrlimit(RLIMIT_CORE, &rl) < 0) + { + #ifdef SETRLIMIT_NOT_SUPPORTED + if (errno != ENOSYS && errno != ENOTSUP) + #endif + log_write(0, LOG_MAIN|LOG_PANIC, "setrlimit(RLIMIT_CORE) failed: %s", + strerror(errno)); + } + #endif + + /* Reset the random number generator, so different processes don't all + have the same sequence. */ + + random_seed = 0; + + /* If the transport has a setup entry, call this first, while still + privileged. (Appendfile uses this to expand quota, for example, while + able to read private files.) */ + + if (addr->transport->setup != NULL) + { + switch((addr->transport->setup)(addr->transport, addr, NULL, + &(addr->message))) + { + case DEFER: + addr->transport_return = DEFER; + goto PASS_BACK; + + case FAIL: + addr->transport_return = PANIC; + goto PASS_BACK; + } + } + + /* Ignore SIGINT and SIGTERM during delivery. Also ignore SIGUSR1, as + when the process becomes unprivileged, it won't be able to write to the + process log. SIGHUP is ignored throughout exim, except when it is being + run as a daemon. */ + + signal(SIGINT, SIG_IGN); + signal(SIGTERM, SIG_IGN); + signal(SIGUSR1, SIG_IGN); + + /* Close the unwanted half of the pipe, and set close-on-exec for the other + half - for transports that exec things (e.g. pipe). Then set the required + gid/uid. */ + + close(pfd[pipe_read]); + fcntl(pfd[pipe_write], F_SETFD, fcntl(pfd[pipe_write], F_GETFD) | + FD_CLOEXEC); + exim_setugid(uid, gid, use_initgroups, + string_sprintf("local delivery to %s <%s> transport=%s", addr->local_part, + addr->address, addr->transport->name)); + + DEBUG(D_deliver) + { + address_item *batched; + debug_printf(" home=%s current=%s\n", deliver_home, working_directory); + for (batched = addr->next; batched != NULL; batched = batched->next) + debug_printf("additional batched address: %s\n", batched->address); + } + + /* Set an appropriate working directory. */ + + if (Uchdir(working_directory) < 0) + { + addr->transport_return = DEFER; + addr->basic_errno = errno; + addr->message = string_sprintf("failed to chdir to %s", working_directory); + } + + /* If successful, call the transport */ + + else + { + BOOL ok = TRUE; + set_process_info("delivering %s to %s using %s", message_id, + addr->local_part, addr->transport->name); + + /* If a transport filter has been specified, set up its argument list. + Any errors will get put into the address, and FALSE yielded. */ + + if (addr->transport->filter_command != NULL) + { + ok = transport_set_up_command(&transport_filter_argv, + addr->transport->filter_command, + TRUE, PANIC, addr, US"transport filter", NULL); + transport_filter_timeout = addr->transport->filter_timeout; + } + else transport_filter_argv = NULL; + + if (ok) + { + debug_print_string(addr->transport->debug_string); + replicate = !(addr->transport->info->code)(addr->transport, addr); + } + } + + /* Pass the results back down the pipe. If necessary, first replicate the + status in the top address to the others in the batch. The label is the + subject of a goto when a call to the transport's setup function fails. We + pass the pointer to the transport back in case it got changed as a result of + file_format in appendfile. */ + + PASS_BACK: + + if (replicate) replicate_status(addr); + for (addr2 = addr; addr2 != NULL; addr2 = addr2->next) + { + int i; + int local_part_length = Ustrlen(addr2->local_part); + uschar *s; + + write(pfd[pipe_write], (void *)&(addr2->transport_return), sizeof(int)); + write(pfd[pipe_write], (void *)&transport_count, sizeof(transport_count)); + write(pfd[pipe_write], (void *)&(addr2->flags), sizeof(addr2->flags)); + write(pfd[pipe_write], (void *)&(addr2->basic_errno), sizeof(int)); + write(pfd[pipe_write], (void *)&(addr2->more_errno), sizeof(int)); + write(pfd[pipe_write], (void *)&(addr2->special_action), sizeof(int)); + write(pfd[pipe_write], (void *)&(addr2->transport), + sizeof(transport_instance *)); + + /* For a file delivery, pass back the local part, in case the original + was only part of the final delivery path. This gives more complete + logging. */ + + if (testflag(addr2, af_file)) + { + write(pfd[pipe_write], (void *)&local_part_length, sizeof(int)); + write(pfd[pipe_write], addr2->local_part, local_part_length); + } + + /* Now any messages */ + + for (i = 0, s = addr2->message; i < 2; i++, s = addr2->user_message) + { + int message_length = (s == NULL)? 0 : Ustrlen(s) + 1; + write(pfd[pipe_write], (void *)&message_length, sizeof(int)); + if (message_length > 0) write(pfd[pipe_write], s, message_length); + } + } + + /* OK, this process is now done. Free any cached resources that it opened, + and close the pipe we were writing down before exiting. */ + + close(pfd[pipe_write]); + search_tidyup(); + exit(EXIT_SUCCESS); + } + +/* Back in the main process: panic if the fork did not succeed. This seems +better than returning an error - if forking is failing it is probably best +not to try other deliveries for this message. */ + +if (pid < 0) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Fork failed for local delivery to %s", + addr->address); + +/* Read the pipe to get the delivery status codes and error messages. Our copy +of the writing end must be closed first, as otherwise read() won't return zero +on an empty pipe. We check that a status exists for each address before +overwriting the address structure. If data is missing, the default DEFER status +will remain. Afterwards, close the reading end. */ + +close(pfd[pipe_write]); + +for (addr2 = addr; addr2 != NULL; addr2 = addr2->next) + { + len = read(pfd[pipe_read], (void *)&status, sizeof(int)); + if (len > 0) + { + int i; + uschar **sptr; + + addr2->transport_return = status; + len = read(pfd[pipe_read], (void *)&transport_count, + sizeof(transport_count)); + len = read(pfd[pipe_read], (void *)&(addr2->flags), sizeof(addr2->flags)); + len = read(pfd[pipe_read], (void *)&(addr2->basic_errno), sizeof(int)); + len = read(pfd[pipe_read], (void *)&(addr2->more_errno), sizeof(int)); + len = read(pfd[pipe_read], (void *)&(addr2->special_action), sizeof(int)); + len = read(pfd[pipe_read], (void *)&(addr2->transport), + sizeof(transport_instance *)); + + if (testflag(addr2, af_file)) + { + int local_part_length; + len = read(pfd[pipe_read], (void *)&local_part_length, sizeof(int)); + len = read(pfd[pipe_read], (void *)big_buffer, local_part_length); + big_buffer[local_part_length] = 0; + addr2->local_part = string_copy(big_buffer); + } + + for (i = 0, sptr = &(addr2->message); i < 2; + i++, sptr = &(addr2->user_message)) + { + int message_length; + len = read(pfd[pipe_read], (void *)&message_length, sizeof(int)); + if (message_length > 0) + { + len = read(pfd[pipe_read], (void *)big_buffer, message_length); + if (len > 0) *sptr = string_copy(big_buffer); + } + } + } + + else + { + log_write(0, LOG_MAIN|LOG_PANIC, "failed to read delivery status for %s " + "from delivery subprocess", addr2->unique); + break; + } + } + +close(pfd[pipe_read]); + +/* Unless shadowing, write all successful addresses immediately to the journal +file, to ensure they are recorded asap. For homonymic addresses, use the base +address plus the transport name. Failure to write the journal is panic-worthy, +but don't stop, as it may prove possible subsequently to update the spool file +in order to record the delivery. */ + +if (!shadowing) + { + for (addr2 = addr; addr2 != NULL; addr2 = addr2->next) + { + if (addr2->transport_return != OK) continue; + + if (testflag(addr2, af_homonym)) + sprintf(CS big_buffer, "%.500s/%s\n", addr2->unique + 3, tp->name); + else + sprintf(CS big_buffer, "%.500s\n", addr2->unique); + + /* In the test harness, wait just a bit to let the subprocess finish off + any debug output etc first. */ + + if (running_in_test_harness) millisleep(300); + + DEBUG(D_deliver) debug_printf("journalling %s", big_buffer); + len = Ustrlen(big_buffer); + if (write(journal_fd, big_buffer, len) != len) + log_write(0, LOG_MAIN|LOG_PANIC, "failed to update journal for %s: %s", + big_buffer, strerror(errno)); + } + + /* Ensure the journal file is pushed out to disk. */ + + if (fsync(journal_fd) < 0) + log_write(0, LOG_MAIN|LOG_PANIC, "failed to fsync journal: %s", + strerror(errno)); + } + +/* Wait for the process to finish. If it terminates with a non-zero code, +freeze the message (except for SIGTERM, SIGKILL and SIGQUIT), but leave the +status values of all the addresses as they are. Take care to handle the case +when the subprocess doesn't seem to exist. This has been seen on one system +when Exim was called from an MUA that set SIGCHLD to SIG_IGN. When that +happens, wait() doesn't recognize the termination of child processes. Exim now +resets SIGCHLD to SIG_DFL, but this code should still be robust. */ + +while ((rc = wait(&status)) != pid) + { + if (rc < 0 && errno == ECHILD) /* Process has vanished */ + { + log_write(0, LOG_MAIN, "%s transport process vanished unexpectedly", + addr->transport->driver_name); + status = 0; + break; + } + } + +if ((status & 0xffff) != 0) + { + int msb = (status >> 8) & 255; + int lsb = status & 255; + int code = (msb == 0)? (lsb & 0x7f) : msb; + if (msb != 0 || (code != SIGTERM && code != SIGKILL && code != SIGQUIT)) + addr->special_action = SPECIAL_FREEZE; + log_write(0, LOG_MAIN|LOG_PANIC, "%s transport process returned non-zero " + "status 0x%04x: %s %d", + addr->transport->driver_name, + status, + (msb == 0)? "terminated by signal" : "exit code", + code); + } + +/* If SPECIAL_WARN is set in the top address, send a warning message. */ + +if (addr->special_action == SPECIAL_WARN && + addr->transport->warn_message != NULL) + { + int fd; + uschar *warn_message; + + DEBUG(D_deliver) debug_printf("Warning message requested by transport\n"); + + warn_message = expand_string(addr->transport->warn_message); + if (warn_message == NULL) + log_write(0, LOG_MAIN|LOG_PANIC, "Failed to expand \"%s\" (warning " + "message for %s transport): %s", addr->transport->warn_message, + addr->transport->name, expand_string_message); + else + { + pid_t pid = child_open_exim(&fd); + if (pid > 0) + { + FILE *f = fdopen(fd, "wb"); + + if (errors_reply_to != NULL) + fprintf(f, "Reply-To: %s\n", errors_reply_to); + fprintf(f, "Auto-Submitted: auto-generated\n"); + fprintf(f, "From: Mail Delivery System \n", + qualify_domain_sender); + fprintf(f, "%s", CS warn_message); + + /* Close and wait for child process to complete, without a timeout. */ + + fclose(f); + (void)child_close(pid, 0); + } + } + + addr->special_action = SPECIAL_NONE; + } +} + + + +/************************************************* +* Do local deliveries * +*************************************************/ + +/* This function processes the list of addresses in addr_local. True local +deliveries are always done one address at a time. However, local deliveries can +be batched up in some cases. Typically this is when writing batched SMTP output +files for use by some external transport mechanism, or when running local +deliveries over LMTP. + +Arguments: None +Returns: Nothing +*/ + +static void +do_local_deliveries(void) +{ +open_db dbblock; +open_db *dbm_file = NULL; +time_t now = time(NULL); + +/* Loop until we have exhausted the supply of local deliveries */ + +while (addr_local != NULL) + { + time_t delivery_start; + int deliver_time; + address_item *addr2, *addr3, *nextaddr; + int logflags = LOG_MAIN; + int logchar = dont_deliver? '*' : '='; + transport_instance *tp; + + /* Pick the first undelivered address off the chain */ + + address_item *addr = addr_local; + addr_local = addr->next; + addr->next = NULL; + + DEBUG(D_deliver|D_transport) + debug_printf("--------> %s <--------\n", addr->address); + + /* An internal disaster if there is no transport. Should not occur! */ + + if ((tp = addr->transport) == NULL) + { + logflags |= LOG_PANIC; + disable_logging = FALSE; /* Jic */ + addr->message = + (addr->router != NULL)? + string_sprintf("No transport set by %s router", addr->router->name) + : + string_sprintf("No transport set by system filter"); + post_process_one(addr, DEFER, logflags, DTYPE_TRANSPORT, 0); + continue; + } + + /* Check that this base address hasn't previously been delivered to this + transport. The check is necessary at this point to handle homonymic addresses + correctly in cases where the pattern of redirection changes between delivery + attempts. Non-homonymic previous delivery is detected earlier, at routing + time. */ + + if (previously_transported(addr)) continue; + + /* There are weird cases where logging is disabled */ + + disable_logging = tp->disable_logging; + + /* Check for batched addresses and possible amalgamation. File deliveries can + never be batched. Skip all the work if either batch_max <= 1 or there aren't + any other addresses for local delivery. */ + + if (!testflag(addr, af_file) && tp->batch_max > 1 && addr_local != NULL) + { + int batch_count = 1; + BOOL uses_dom = readconf_depends((driver_instance *)tp, US"domain"); + BOOL uses_lp = readconf_depends((driver_instance *)tp, US"local_part"); + uschar *batch_id = NULL; + address_item **anchor = &addr_local; + address_item *last = addr; + address_item *next; + + /* Expand the batch_id string for comparison with other addresses. + Expansion failure suppresses batching. */ + + if (tp->batch_id != NULL) + { + deliver_set_expansions(addr); + batch_id = expand_string(tp->batch_id); + deliver_set_expansions(NULL); + if (batch_id == NULL) + { + log_write(0, LOG_MAIN|LOG_PANIC, "Failed to expand batch_id option " + "in %s transport (%s): %s", tp->name, addr->address, + expand_string_message); + batch_count = tp->batch_max; + } + } + + /* Until we reach the batch_max limit, pick off addresses which have the + same characteristics. These are: + + same transport + same local part if the transport's configuration contains $local_part + same domain if the transport's configuration contains $domain + same errors address + same additional headers + same headers to be removed + same uid/gid for running the transport + same first host if a host list is set + */ + + while ((next = *anchor) != NULL && batch_count < tp->batch_max) + { + BOOL ok = + tp == next->transport && + (!uses_lp || Ustrcmp(next->local_part, addr->local_part) == 0) && + (!uses_dom || Ustrcmp(next->domain, addr->domain) == 0) && + same_strings(next->p.errors_address, addr->p.errors_address) && + same_headers(next->p.extra_headers, addr->p.extra_headers) && + same_strings(next->p.remove_headers, addr->p.remove_headers) && + same_ugid(tp, addr, next) && + ((addr->host_list == NULL && next->host_list == NULL) || + (addr->host_list != NULL && next->host_list != NULL && + Ustrcmp(addr->host_list->name, next->host_list->name) == 0)); + + /* If the transport has a batch_id setting, batch_id will be non-NULL + from the expansion outside the loop. Expand for this address and compare. + Expansion failure makes this address ineligible for batching. */ + + if (ok && batch_id != NULL) + { + uschar *bid; + address_item *save_nextnext = next->next; + next->next = NULL; /* Expansion for a single address */ + deliver_set_expansions(next); + next->next = save_nextnext; + bid = expand_string(tp->batch_id); + deliver_set_expansions(NULL); + if (bid == NULL) + { + log_write(0, LOG_MAIN|LOG_PANIC, "Failed to expand batch_id option " + "in %s transport (%s): %s", tp->name, next->address, + expand_string_message); + ok = FALSE; + } + else ok = (Ustrcmp(batch_id, bid) == 0); + } + + /* Take address into batch if OK. */ + + if (ok) + { + *anchor = next->next; /* Include the address */ + next->next = NULL; + last->next = next; + last = next; + batch_count++; + } + else anchor = &(next->next); /* Skip the address */ + } + } + + /* We now have one or more addresses that can be delivered in a batch. Check + whether the transport is prepared to accept a message of this size. If not, + fail them all forthwith. If the expansion fails, or does not yield an + integer, defer delivery. */ + + if (tp->message_size_limit != NULL) + { + int rc = check_message_size(tp, addr); + if (rc != OK) + { + replicate_status(addr); + while (addr != NULL) + { + addr2 = addr->next; + post_process_one(addr, rc, logflags, DTYPE_TRANSPORT, 0); + addr = addr2; + } + continue; /* With next batch of addresses */ + } + } + + /* If we are not running the queue, or if forcing, all deliveries will be + attempted. Otherwise, we must respect the retry times for each address. Even + when not doing this, we need to set up the retry key string, and determine + whether a retry record exists, because after a successful delivery, a delete + retry item must be set up. Keep the retry database open only for the duration + of these checks, rather than for all local deliveries, because some local + deliveries (e.g. to pipes) can take a substantial time. */ + + dbm_file = dbfn_open(US"retry", O_RDONLY, &dbblock, FALSE); + if (dbm_file == NULL) + { + DEBUG(D_deliver|D_retry|D_hints_lookup) + debug_printf("no retry data available\n"); + } + + addr2 = addr; + addr3 = NULL; + while (addr2 != NULL) + { + BOOL ok = TRUE; /* to deliver this address */ + uschar *retry_key; + + /* Set up the retry key to include the domain or not, and change its + leading character from "R" to "T". Must make a copy before doing this, + because the old key may be pointed to from a "delete" retry item after + a routing delay. */ + + retry_key = string_copy( + (tp->retry_use_local_part)? addr2->address_retry_key : + addr2->domain_retry_key); + *retry_key = 'T'; + + /* Inspect the retry data. If there is no hints file, delivery happens. */ + + if (dbm_file != NULL) + { + dbdata_retry *retry_record = dbfn_read(dbm_file, retry_key); + + /* If there is no retry record, delivery happens. If there is, + remember it exists so it can be deleted after a successful delivery. */ + + if (retry_record != NULL) + { + setflag(addr2, af_lt_retry_exists); + + /* A retry record exists for this address. If queue running and not + forcing, inspect its contents. If the record is too old, or if its + retry time has come, or if it has passed its cutoff time, delivery + will go ahead. */ + + DEBUG(D_retry) + { + debug_printf("retry record exists: age=%d (max=%d)\n", + (int)(now - retry_record->time_stamp), retry_data_expire); + debug_printf(" time to retry = %d expired = %d\n", + (int)(now - retry_record->next_try), retry_record->expired); + } + + if (queue_running && !deliver_force) + { + ok = (now - retry_record->time_stamp > retry_data_expire) || + (now >= retry_record->next_try) || + retry_record->expired; + + /* If we haven't reached the retry time, there is one more check + to do, which is for the ultimate address timeout. */ + + if (!ok) + { + retry_config *retry = + retry_find_config(retry_key+2, addr2->domain, + retry_record->basic_errno, + retry_record->more_errno); + + DEBUG(D_deliver|D_retry) + debug_printf("retry time not reached for %s: " + "checking ultimate address timeout\n", addr2->address); + + if (retry != NULL && retry->rules != NULL) + { + retry_rule *last_rule; + for (last_rule = retry->rules; + last_rule->next != NULL; + last_rule = last_rule->next); + if (now - received_time > last_rule->timeout) ok = TRUE; + } + else ok = TRUE; /* No rule => timed out */ + + DEBUG(D_deliver|D_retry) + { + if (ok) debug_printf("on queue longer than maximum retry for " + "address - allowing delivery\n"); + } + } + } + } + else DEBUG(D_retry) debug_printf("no retry record exists\n"); + } + + /* This address is to be delivered. Leave it on the chain. */ + + if (ok) + { + addr3 = addr2; + addr2 = addr2->next; + } + + /* This address is to be deferred. Take it out of the chain, and + post-process it as complete. Must take it out of the chain first, + because post processing puts it on another chain. */ + + else + { + address_item *this = addr2; + this->message = US"Retry time not yet reached"; + this->basic_errno = ERRNO_LRETRY; + if (addr3 == NULL) addr2 = addr = addr2->next; + else addr2 = addr3->next = addr2->next; + post_process_one(this, DEFER, logflags, DTYPE_TRANSPORT, 0); + } + } + + if (dbm_file != NULL) dbfn_close(dbm_file); + + /* If there are no addresses left on the chain, they all deferred. Loop + for the next set of addresses. */ + + if (addr == NULL) continue; + + /* So, finally, we do have some addresses that can be passed to the + transport. Before doing so, set up variables that are relevant to a + single delivery. */ + + deliver_set_expansions(addr); + delivery_start = time(NULL); + deliver_local(addr, FALSE); + deliver_time = (int)(time(NULL) - delivery_start); + + /* If a shadow transport (which must perforce be another local transport), is + defined, and its condition is met, we must pass the message to the shadow + too, but only those addresses that succeeded. We do this by making a new + chain of addresses - also to keep the original chain uncontaminated. We must + use a chain rather than doing it one by one, because the shadow transport may + batch. + + NOTE: if the condition fails because of a lookup defer, there is nothing we + can do! */ + + if (tp->shadow != NULL && + (tp->shadow_condition == NULL || + expand_check_condition(tp->shadow_condition, tp->name, US"transport"))) + { + transport_instance *stp; + address_item *shadow_addr = NULL; + address_item **last = &shadow_addr; + + for (stp = transports; stp != NULL; stp = stp->next) + if (Ustrcmp(stp->name, tp->shadow) == 0) break; + + if (stp == NULL) + log_write(0, LOG_MAIN|LOG_PANIC, "shadow transport \"%s\" not found ", + tp->shadow); + + /* Pick off the addresses that have succeeded, and make clones. Put into + the shadow_message field a pointer to the shadow_message field of the real + address. */ + + else for (addr2 = addr; addr2 != NULL; addr2 = addr2->next) + { + if (addr2->transport_return != OK) continue; + addr3 = store_get(sizeof(address_item)); + *addr3 = *addr2; + addr3->next = NULL; + addr3->shadow_message = (uschar *)(&(addr2->shadow_message)); + addr3->transport = stp; + addr3->transport_return = DEFER; + addr3->return_filename = NULL; + addr3->return_file = -1; + *last = addr3; + last = &(addr3->next); + } + + /* If we found any addresses to shadow, run the delivery, and stick any + message back into the shadow_message field in the original. */ + + if (shadow_addr != NULL) + { + int save_count = transport_count; + + DEBUG(D_deliver|D_transport) + debug_printf(">>>>>>>>>>>>>>>> Shadow delivery >>>>>>>>>>>>>>>>\n"); + deliver_local(shadow_addr, TRUE); + + for(; shadow_addr != NULL; shadow_addr = shadow_addr->next) + { + int sresult = shadow_addr->transport_return; + *((uschar **)(shadow_addr->shadow_message)) = (sresult == OK)? + string_sprintf(" ST=%s", stp->name) : + string_sprintf(" ST=%s (%s%s%s)", stp->name, + (shadow_addr->basic_errno <= 0)? + US"" : US strerror(shadow_addr->basic_errno), + (shadow_addr->basic_errno <= 0 || shadow_addr->message == NULL)? + US"" : US": ", + (shadow_addr->message != NULL)? shadow_addr->message : + (shadow_addr->basic_errno <= 0)? US"unknown error" : US""); + + DEBUG(D_deliver|D_transport) + debug_printf("%s shadow transport returned %s for %s\n", + stp->name, + (sresult == OK)? "OK" : + (sresult == DEFER)? "DEFER" : + (sresult == FAIL)? "FAIL" : + (sresult == PANIC)? "PANIC" : "?", + shadow_addr->address); + } + + DEBUG(D_deliver|D_transport) + debug_printf(">>>>>>>>>>>>>>>> End shadow delivery >>>>>>>>>>>>>>>>\n"); + + transport_count = save_count; /* Restore original transport count */ + } + } + + /* Cancel the expansions that were set up for the delivery. */ + + deliver_set_expansions(NULL); + + /* Now we can process the results of the real transport. We must take each + address off the chain first, because post_process_one() puts it on another + chain. */ + + for (addr2 = addr; addr2 != NULL; addr2 = nextaddr) + { + int result = addr2->transport_return; + nextaddr = addr2->next; + + DEBUG(D_deliver|D_transport) + debug_printf("%s transport returned %s for %s\n", + tp->name, + (result == OK)? "OK" : + (result == DEFER)? "DEFER" : + (result == FAIL)? "FAIL" : + (result == PANIC)? "PANIC" : "?", + addr2->address); + + /* If there is a retry_record, or if delivery is deferred, build a retry + item for setting a new retry time or deleting the old retry record from + the database. These items are handled all together after all addresses + have been handled (so the database is open just for a short time for + updating). */ + + if (result == DEFER || testflag(addr2, af_lt_retry_exists)) + { + int flags = (result == DEFER)? 0 : rf_delete; + uschar *retry_key = string_copy((tp->retry_use_local_part)? + addr2->address_retry_key : addr2->domain_retry_key); + *retry_key = 'T'; + retry_add_item(addr2, retry_key, flags); + } + + /* Done with this address */ + + if (result == OK) addr2->more_errno = deliver_time; + post_process_one(addr2, result, logflags, DTYPE_TRANSPORT, logchar); + + /* If a pipe delivery generated text to be sent back, the result may be + changed to FAIL, and we must copy this for subsequent addresses in the + batch. */ + + if (addr2->transport_return != result) + { + for (addr3 = nextaddr; addr3 != NULL; addr3 = addr3->next) + { + addr3->transport_return = addr2->transport_return; + addr3->basic_errno = addr2->basic_errno; + addr3->message = addr2->message; + } + result = addr2->transport_return; + } + + /* Whether or not the result was changed to FAIL, we need to copy the + return_file value from the first address into all the addresses of the + batch, so they are all listed in the error message. */ + + addr2->return_file = addr->return_file; + + /* Change log character for recording successful deliveries. */ + + if (result == OK) logchar = '-'; + } + } /* Loop back for next batch of addresses */ +} + + + + +/************************************************* +* Sort remote deliveries * +*************************************************/ + +/* This function is called if remote_sort_domains is set. It arranges that the +chain of addresses for remote deliveries is ordered according to the strings +specified. Try to make this shuffling reasonably efficient by handling +sequences of addresses rather than just single ones. + +Arguments: None +Returns: Nothing +*/ + +static void +sort_remote_deliveries(void) +{ +int sep = 0; +address_item **aptr = &addr_remote; +uschar *listptr = remote_sort_domains; +uschar *pattern; +uschar patbuf[256]; + +while (*aptr != NULL && + (pattern = string_nextinlist(&listptr, &sep, patbuf, sizeof(patbuf))) + != NULL) + { + address_item *moved = NULL; + address_item **bptr = &moved; + + while (*aptr != NULL) + { + address_item **next; + deliver_domain = (*aptr)->domain; /* set $domain */ + if (match_isinlist(deliver_domain, &pattern, UCHAR_MAX+1, + &domainlist_anchor, NULL, MCL_DOMAIN, TRUE, NULL) == OK) + { + aptr = &((*aptr)->next); + continue; + } + + next = &((*aptr)->next); + while (*next != NULL && + (deliver_domain = (*next)->domain, /* Set $domain */ + match_isinlist(deliver_domain, &pattern, UCHAR_MAX+1, + &domainlist_anchor, NULL, MCL_DOMAIN, TRUE, NULL)) != OK) + next = &((*next)->next); + + /* If the batch of non-matchers is at the end, add on any that were + extracted further up the chain, and end this iteration. Otherwise, + extract them from the chain and hang on the moved chain. */ + + if (*next == NULL) + { + *next = moved; + break; + } + + *bptr = *aptr; + *aptr = *next; + *next = NULL; + bptr = next; + aptr = &((*aptr)->next); + } + + /* If the loop ended because the final address matched, *aptr will + be NULL. Add on to the end any extracted non-matching addresses. If + *aptr is not NULL, the loop ended via "break" when *next is null, that + is, there was a string of non-matching addresses at the end. In this + case the extracted addresses have already been added on the end. */ + + if (*aptr == NULL) *aptr = moved; + } + +DEBUG(D_deliver) + { + address_item *addr; + debug_printf("remote addresses after sorting:\n"); + for (addr = addr_remote; addr != NULL; addr = addr->next) + debug_printf(" %s\n", addr->address); + } +} + + + +/************************************************* +* Read from pipe for remote delivery subprocess * +*************************************************/ + +/* This function is called when the subprocess is complete, but can also be +called before it is complete, in order to empty a pipe that is full (to prevent +deadlock). It must therefore keep track of its progress in the parlist data +block. + +We read the pipe to get the delivery status codes and a possible error message +for each address, optionally preceded by unusability data for the hosts and +also by optional retry data. + +Read in large chunks into the big buffer and then scan through, interpreting +the data therein. In most cases, only a single read will be necessary. No +individual item will ever be anywhere near 500 bytes in length, so by ensuring +that we read the next chunk when there is less than 500 bytes left in the +non-final chunk, we can assume each item is complete in store before handling +it. Actually, each item is written using a single write(), which is atomic for +small items (less than PIPE_BUF, which seems to be at least 512 in any Unix) so +even if we are reading while the subprocess is still going, we should never +have only a partial item in the buffer. + +Argument: + poffset the offset of the parlist item + eop TRUE if the process has completed + +Returns: TRUE if the terminating 'Z' item has been read, + or there has been a disaster (i.e. no more data needed); + FALSE otherwise +*/ + +static BOOL +par_read_pipe(int poffset, BOOL eop) +{ +host_item *h; +pardata *p = parlist + poffset; +address_item *addrlist = p->addrlist; +address_item *addr = p->addr; +pid_t pid = p->pid; +int fd = p->fd; +uschar *endptr = big_buffer; +uschar *ptr = endptr; +uschar *msg = p->msg; +BOOL done = p->done; +BOOL unfinished = TRUE; + +/* Loop through all items, reading from the pipe when necessary. The pipe +is set up to be non-blocking, but there are two different Unix mechanisms in +use. Exim uses O_NONBLOCK if it is defined. This returns 0 for end of file, +and EAGAIN for no more data. If O_NONBLOCK is not defined, Exim uses O_NDELAY, +which returns 0 for both end of file and no more data. We distinguish the +two cases by taking 0 as end of file only when we know the process has +completed. + +Each separate item is written to the pipe in a single write(), and as they are +all short items, the writes will all be atomic and we should never find +ourselves in the position of having read an incomplete item. */ + +DEBUG(D_deliver) debug_printf("reading pipe for subprocess %d (%s)\n", + (int)p->pid, eop? "ended" : "not ended"); + +while (!done) + { + retry_item *r, **rp; + int remaining = endptr - ptr; + + /* Read (first time) or top up the chars in the buffer if necessary. + There will be only one read if we get all the available data (i.e. don't + fill the buffer completely). */ + + if (remaining < 500 && unfinished) + { + int len; + int available = big_buffer_size - remaining; + + if (remaining > 0) memmove(big_buffer, ptr, remaining); + + ptr = big_buffer; + endptr = big_buffer + remaining; + len = read(fd, endptr, available); + + DEBUG(D_deliver) debug_printf("read() yielded %d\n", len); + + /* If the result is EAGAIN and the process is not complete, just + stop reading any more and process what we have already. */ + + if (len < 0) + { + if (!eop && errno == EAGAIN) len = 0; else + { + msg = string_sprintf("failed to read pipe from transport process " + "%d for transport %s: %s", pid, addr->transport->driver_name, + strerror(errno)); + break; + } + } + + /* If the length is zero (eof or no-more-data), just process what we + already have. Note that if the process is still running and we have + read all the data in the pipe (but less that "available") then we + won't read any more, as "unfinished" will get set FALSE. */ + + endptr += len; + unfinished = len == available; + } + + /* If we are at the end of the available data, exit the loop. */ + + if (ptr >= endptr) break; + + /* Handle each possible type of item, assuming the complete item is + available in store. */ + + switch (*ptr++) + { + /* Host items exist only if any hosts were marked unusable. Match + up by checking the IP address. */ + + case 'H': + for (h = addrlist->host_list; h != NULL; h = h->next) + { + if (h->address == NULL || Ustrcmp(h->address, ptr+2) != 0) continue; + h->status = ptr[0]; + h->why = ptr[1]; + } + ptr += 2; + while (*ptr++); + break; + + /* Retry items are sent in a preceding R item for each address. This is + kept separate to keep each message short enough to guarantee it won't + be split in the pipe. Hopefully, in the majority of cases, there won't in + fact be any retry items at all. + + The complete set of retry items might include an item to delete a + routing retry if there was a previous routing delay. However, routing + retries are also used when a remote transport identifies an address error. + In that case, there may also be an "add" item for the same key. Arrange + that a "delete" item is dropped in favour of an "add" item. */ + + case 'R': + if (addr == NULL) goto ADDR_MISMATCH; + + DEBUG(D_deliver|D_retry) + debug_printf("reading retry information for %s from subprocess\n", + ptr+1); + + /* Cut out any "delete" items on the list. */ + + for (rp = &(addr->retries); (r = *rp) != NULL; rp = &(r->next)) + { + if (Ustrcmp(r->key, ptr+1) == 0) /* Found item with same key */ + { + if ((r->flags & rf_delete) == 0) break; /* It was not "delete" */ + *rp = r->next; /* Excise a delete item */ + DEBUG(D_deliver|D_retry) + debug_printf(" existing delete item dropped\n"); + } + } + + /* We want to add a delete item only if there is no non-delete item; + however we still have to step ptr through the data. */ + + if (r == NULL || (*ptr & rf_delete) == 0) + { + r = store_get(sizeof(retry_item)); + r->next = addr->retries; + addr->retries = r; + r->flags = *ptr++; + r->key = string_copy(ptr); + while (*ptr++); + memcpy(&(r->basic_errno), ptr, sizeof(r->basic_errno)); + ptr += sizeof(r->basic_errno); + memcpy(&(r->more_errno), ptr, sizeof(r->more_errno)); + ptr += sizeof(r->more_errno); + r->message = (*ptr)? string_copy(ptr) : NULL; + DEBUG(D_deliver|D_retry) + debug_printf(" added %s item\n", + ((r->flags & rf_delete) == 0)? "retry" : "delete"); + } + + else + { + DEBUG(D_deliver|D_retry) + debug_printf(" delete item not added: non-delete item exists\n"); + ptr++; + while(*ptr++); + ptr += sizeof(r->basic_errno) + sizeof(r->more_errno); + } + + while(*ptr++); + break; + + /* Put the amount of data written into the parlist block */ + + case 'S': + memcpy(&(p->transport_count), ptr, sizeof(transport_count)); + ptr += sizeof(transport_count); + break; + + /* Address items are in the order of items on the address chain. We + remember the current address value in case this function is called + several times to empty the pipe in stages. Information about delivery + over TLS is sent in a preceding X item for each address. We don't put + it in with the other info, in order to keep each message short enough to + guarantee it won't be split in the pipe. */ + + #ifdef SUPPORT_TLS + case 'X': + if (addr == NULL) goto ADDR_MISMATCH; /* Below, in 'A' handler */ + addr->cipher = (*ptr)? string_copy(ptr) : NULL; + while (*ptr++); + addr->peerdn = (*ptr)? string_copy(ptr) : NULL; + while (*ptr++); + break; + #endif + + case 'A': + if (addr == NULL) + { + ADDR_MISMATCH: + msg = string_sprintf("address count mismatch for data read from pipe " + "for transport process %d for transport %s", pid, + addrlist->transport->driver_name); + done = TRUE; + break; + } + + addr->transport_return = *ptr++; + addr->special_action = *ptr++; + memcpy(&(addr->basic_errno), ptr, sizeof(addr->basic_errno)); + ptr += sizeof(addr->basic_errno); + memcpy(&(addr->more_errno), ptr, sizeof(addr->more_errno)); + ptr += sizeof(addr->more_errno); + memcpy(&(addr->flags), ptr, sizeof(addr->flags)); + ptr += sizeof(addr->flags); + addr->message = (*ptr)? string_copy(ptr) : NULL; + while(*ptr++); + addr->user_message = (*ptr)? string_copy(ptr) : NULL; + while(*ptr++); + + /* Always two strings for host information, followed by the port number */ + + if (*ptr != 0) + { + h = store_get(sizeof(host_item)); + h->name = string_copy(ptr); + while (*ptr++); + h->address = string_copy(ptr); + while(*ptr++); + memcpy(&(h->port), ptr, sizeof(h->port)); + ptr += sizeof(h->port); + addr->host_used = h; + } + else ptr++; + + /* Finished with this address */ + + addr = addr->next; + break; + + /* Z marks the logical end of the data. It is followed by '0' if + continue_transport was NULL at the end of transporting, otherwise '1'. + We need to know when it becomes NULL during a delivery down a passed SMTP + channel so that we don't try to pass anything more down it. Of course, for + most normal messages it will remain NULL all the time. */ + + case 'Z': + if (*ptr == '0') + { + continue_transport = NULL; + continue_hostname = NULL; + } + done = TRUE; + DEBUG(D_deliver) debug_printf("Z%c item read\n", *ptr); + break; + + /* Anything else is a disaster. */ + + default: + msg = string_sprintf("malformed data (%d) read from pipe for transport " + "process %d for transport %s", ptr[-1], pid, + addr->transport->driver_name); + done = TRUE; + break; + } + } + +/* The done flag is inspected externally, to determine whether or not to +call the function again when the process finishes. */ + +p->done = done; + +/* If the process hadn't finished, and we haven't seen the end of the data +or suffered a disaster, update the rest of the state, and return FALSE to +indicate "not finished". */ + +if (!eop && !done) + { + p->addr = addr; + p->msg = msg; + return FALSE; + } + +/* Close our end of the pipe, to prevent deadlock if the far end is still +pushing stuff into it. */ + +close(fd); +p->fd = -1; + +/* If we have finished without error, but haven't had data for every address, +something is wrong. */ + +if (msg == NULL && addr != NULL) + msg = string_sprintf("insufficient address data read from pipe " + "for transport process %d for transport %s", pid, + addr->transport->driver_name); + +/* If an error message is set, something has gone wrong in getting back +the delivery data. Put the message into each address and freeze it. */ + +if (msg != NULL) + { + for (addr = addrlist; addr != NULL; addr = addr->next) + { + addr->transport_return = DEFER; + addr->special_action = SPECIAL_FREEZE; + addr->message = msg; + } + } + +/* Return TRUE to indicate we have got all we need from this process, even +if it hasn't actually finished yet. */ + +return TRUE; +} + + + +/************************************************* +* Post-process a set of remote addresses * +*************************************************/ + +/* Do what has to be done immediately after a remote delivery for each set of +addresses, then re-write the spool if necessary. Note that post_process_one +puts the address on an appropriate queue; hence we must fish off the next +one first. This function is also called if there is a problem with setting +up a subprocess to do a remote delivery in parallel. In this case, the final +argument contains a message, and the action must be forced to DEFER. + +Argument: + addr pointer to chain of address items + logflags flags for logging + msg NULL for normal cases; -> error message for unexpected problems + fallback TRUE if processing fallback hosts + +Returns: nothing +*/ + +static void +remote_post_process(address_item *addr, int logflags, uschar *msg, + BOOL fallback) +{ +host_item *h; + +/* If any host addresses were found to be unusable, add them to the unusable +tree so that subsequent deliveries don't try them. */ + +for (h = addr->host_list; h != NULL; h = h->next) + { + if (h->address == NULL) continue; + if (h->status >= hstatus_unusable) tree_add_unusable(h); + } + +/* Now handle each address on the chain. The transport has placed '=' or '-' +into the special_action field for each successful delivery. */ + +while (addr != NULL) + { + address_item *next = addr->next; + + /* If msg == NULL (normal processing) and the result is DEFER and we are + processing the main hosts and there are fallback hosts available, put the + address on the list for fallback delivery. */ + + if (addr->transport_return == DEFER && + addr->fallback_hosts != NULL && + !fallback && + msg == NULL) + { + addr->host_list = addr->fallback_hosts; + addr->next = addr_fallback; + addr_fallback = addr; + DEBUG(D_deliver) debug_printf("%s queued for fallback host(s)\n", addr->address); + } + + /* If msg is set (=> unexpected problem), set it in the address before + doing the ordinary post processing. */ + + else + { + if (msg != NULL) + { + addr->message = msg; + addr->transport_return = DEFER; + } + (void)post_process_one(addr, addr->transport_return, logflags, + DTYPE_TRANSPORT, addr->special_action); + } + + /* Next address */ + + addr = next; + } + +/* If we have just delivered down a passed SMTP channel, and that was +the last address, the channel will have been closed down. Now that +we have logged that delivery, set continue_sequence to 1 so that +any subsequent deliveries don't get "*" incorrectly logged. */ + +if (continue_transport == NULL) continue_sequence = 1; +} + + + +/************************************************* +* Wait for one remote delivery subprocess * +*************************************************/ + +/* This function is called while doing remote deliveries when either the +maximum number of processes exist and we need one to complete so that another +can be created, or when waiting for the last ones to complete. It must wait for +the completion of one subprocess, empty the control block slot, and return a +pointer to the address chain. + +Arguments: none +Returns: pointer to the chain of addresses handled by the process; + NULL if no subprocess found - this is an unexpected error +*/ + +static address_item * +par_wait(void) +{ +int poffset, status; +address_item *addr, *addrlist; +pid_t pid; + +set_process_info("delivering %s: waiting for a remote delivery subprocess " + "to finish", message_id); + +/* Loop until either a subprocess completes, or there are no subprocesses in +existence - in which case give an error return. We cannot proceed just by +waiting for a completion, because a subprocess may have filled up its pipe, and +be waiting for it to be emptied. Therefore, if no processes have finished, we +wait for one of the pipes to acquire some data by calling select(), with a +timeout just in case. + +The simple approach is just to iterate after reading data from a ready pipe. +This leads to non-ideal behaviour when the subprocess has written its final Z +item, closed the pipe, and is in the process of exiting (the common case). A +call to waitpid() yields nothing completed, but select() shows the pipe ready - +reading it yields EOF, so you end up with busy-waiting until the subprocess has +actually finished. + +To avoid this, if all the data that is needed has been read from a subprocess +after select(), an explicit wait() for it is done. We know that all it is doing +is writing to the pipe and then exiting, so the wait should not be long. + +The non-blocking waitpid() is to some extent just insurance; if we could +reliably detect end-of-file on the pipe, we could always know when to do a +blocking wait() for a completed process. However, because some systems use +NDELAY, which doesn't distinguish between EOF and pipe empty, it is easier to +use code that functions without the need to recognize EOF. + +There's a double loop here just in case we end up with a process that is not in +the list of remote delivery processes. Something has obviously gone wrong if +this is the case. (For example, a process that is incorrectly left over from +routing or local deliveries might be found.) The damage can be minimized by +looping back and looking for another process. If there aren't any, the error +return will happen. */ + +for (;;) /* Normally we do not repeat this loop */ + { + while ((pid = waitpid(-1, &status, WNOHANG)) <= 0) + { + struct timeval tv; + fd_set select_pipes; + int maxpipe, readycount; + + /* A return value of -1 can mean several things. If errno != ECHILD, it + either means invalid options (which we discount), or that this process was + interrupted by a signal. Just loop to try the waitpid() again. + + If errno == ECHILD, waitpid() is telling us that there are no subprocesses + in existence. This should never happen, and is an unexpected error. + However, there is a nasty complication when running under Linux. If "strace + -f" is being used under Linux to trace this process and its children, + subprocesses are "stolen" from their parents and become the children of the + tracing process. A general wait such as the one we've just obeyed returns + as if there are no children while subprocesses are running. Once a + subprocess completes, it is restored to the parent, and waitpid(-1) finds + it. Thanks to Joachim Wieland for finding all this out and suggesting a + palliative. + + This does not happen using "truss" on Solaris, nor (I think) with other + tracing facilities on other OS. It seems to be specific to Linux. + + What we do to get round this is to use kill() to see if any of our + subprocesses are still in existence. If kill() gives an OK return, we know + it must be for one of our processes - it can't be for a re-use of the pid, + because if our process had finished, waitpid() would have found it. If any + of our subprocesses are in existence, we proceed to use select() as if + waitpid() had returned zero. I think this is safe. */ + + if (pid < 0) + { + if (errno != ECHILD) continue; /* Repeats the waitpid() */ + + DEBUG(D_deliver) + debug_printf("waitpid() returned -1/ECHILD: checking explicitly " + "for process existence\n"); + + for (poffset = 0; poffset < remote_max_parallel; poffset++) + { + if ((pid = parlist[poffset].pid) != 0 && kill(pid, 0) == 0) + { + DEBUG(D_deliver) debug_printf("process %d still exists: assume " + "stolen by strace\n", (int)pid); + break; /* With poffset set */ + } + } + + if (poffset >= remote_max_parallel) + { + DEBUG(D_deliver) debug_printf("*** no delivery children found\n"); + return NULL; /* This is the error return */ + } + } + + /* A pid value greater than 0 breaks the "while" loop. A negative value has + been handled above. A return value of zero means that there is at least one + subprocess, but there are no completed subprocesses. See if any pipes are + ready with any data for reading. */ + + DEBUG(D_deliver) debug_printf("selecting on subprocess pipes\n"); + + maxpipe = 0; + FD_ZERO(&select_pipes); + for (poffset = 0; poffset < remote_max_parallel; poffset++) + { + if (parlist[poffset].pid != 0) + { + int fd = parlist[poffset].fd; + FD_SET(fd, &select_pipes); + if (fd > maxpipe) maxpipe = fd; + } + } + + /* Stick in a 60-second timeout, just in case. */ + + tv.tv_sec = 60; + tv.tv_usec = 0; + + readycount = select(maxpipe + 1, (SELECT_ARG2_TYPE *)&select_pipes, + NULL, NULL, &tv); + + /* Scan through the pipes and read any that are ready; use the count + returned by select() to stop when there are no more. Select() can return + with no processes (e.g. if interrupted). This shouldn't matter. + + If par_read_pipe() returns TRUE, it means that either the terminating Z was + read, or there was a disaster. In either case, we are finished with this + process. Do an explicit wait() for the process and break the main loop if + it succeeds. + + It turns out that we have to deal with the case of an interrupted system + call, which can happen on some operating systems if the signal handling is + set up to do that by default. */ + + for (poffset = 0; + readycount > 0 && poffset < remote_max_parallel; + poffset++) + { + if ((pid = parlist[poffset].pid) != 0 && + FD_ISSET(parlist[poffset].fd, &select_pipes)) + { + readycount--; + if (par_read_pipe(poffset, FALSE)) /* Finished with this pipe */ + { + for (;;) /* Loop for signals */ + { + pid_t endedpid = waitpid(pid, &status, 0); + if (endedpid == pid) goto PROCESS_DONE; + if (endedpid != (pid_t)(-1) || errno != EINTR) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Unexpected error return " + "%d (errno = %d) from waitpid() for process %d", + (int)endedpid, errno, (int)pid); + } + } + } + } + + /* Now go back and look for a completed subprocess again. */ + } + + /* A completed process was detected by the non-blocking waitpid(). Find the + data block that corresponds to this subprocess. */ + + for (poffset = 0; poffset < remote_max_parallel; poffset++) + if (pid == parlist[poffset].pid) break; + + /* Found the data block; this is a known remote delivery process. We don't + need to repeat the outer loop. This should be what normally happens. */ + + if (poffset < remote_max_parallel) break; + + /* This situation is an error, but it's probably better to carry on looking + for another process than to give up (as we used to do). */ + + log_write(0, LOG_MAIN|LOG_PANIC, "Process %d finished: not found in remote " + "transport process list", pid); + } /* End of the "for" loop */ + +/* Come here when all the data was completely read after a select(), and +the process in pid has been wait()ed for. */ + +PROCESS_DONE: + +DEBUG(D_deliver) + { + if (status == 0) + debug_printf("remote delivery process %d ended\n", (int)pid); + else + debug_printf("remote delivery process %d ended: status=%04x\n", (int)pid, + status); + } + +set_process_info("delivering %s", message_id); + +/* Get the chain of processed addresses */ + +addrlist = parlist[poffset].addrlist; + +/* If the process did not finish cleanly, record an error and freeze (except +for SIGTERM, SIGKILL and SIGQUIT), and also ensure the journal is not removed, +in case the delivery did actually happen. */ + +if ((status & 0xffff) != 0) + { + uschar *msg; + int msb = (status >> 8) & 255; + int lsb = status & 255; + int code = (msb == 0)? (lsb & 0x7f) : msb; + + msg = string_sprintf("%s transport process returned non-zero status 0x%04x: " + "%s %d", + addrlist->transport->driver_name, + status, + (msb == 0)? "terminated by signal" : "exit code", + code); + + if (msb != 0 || (code != SIGTERM && code != SIGKILL && code != SIGQUIT)) + addrlist->special_action = SPECIAL_FREEZE; + + for (addr = addrlist; addr != NULL; addr = addr->next) + { + addr->transport_return = DEFER; + addr->message = msg; + } + + remove_journal = FALSE; + } + +/* Else complete reading the pipe to get the result of the delivery, if all +the data has not yet been obtained. */ + +else if (!parlist[poffset].done) (void)par_read_pipe(poffset, TRUE); + +/* Put the data count and return path into globals, mark the data slot unused, +decrement the count of subprocesses, and return the address chain. */ + +transport_count = parlist[poffset].transport_count; +used_return_path = parlist[poffset].return_path; +parlist[poffset].pid = 0; +parcount--; +return addrlist; +} + + + +/************************************************* +* Wait for subprocesses and post-process * +*************************************************/ + +/* This function waits for subprocesses until the number that are still running +is below a given threshold. For each complete subprocess, the addresses are +post-processed. If we can't find a running process, there is some shambles. +Better not bomb out, as that might lead to multiple copies of the message. Just +log and proceed as if all done. + +Arguments: + max maximum number of subprocesses to leave running + fallback TRUE if processing fallback hosts + +Returns: nothing +*/ + +static void +par_reduce(int max, BOOL fallback) +{ +while (parcount > max) + { + address_item *doneaddr = par_wait(); + if (doneaddr == NULL) + { + log_write(0, LOG_MAIN|LOG_PANIC, + "remote delivery process count got out of step"); + parcount = 0; + } + else remote_post_process(doneaddr, LOG_MAIN, NULL, fallback); + } +} + + + + +/************************************************* +* Do remote deliveries * +*************************************************/ + +/* This function is called to process the addresses in addr_remote. We must +pick off the queue all addresses that have the same transport, remote +destination, and errors address, and hand them to the transport in one go, +subject to some configured limitations. If this is a run to continue delivering +to an existing delivery channel, skip all but those addresses that can go to +that channel. The skipped addresses just get deferred. + +If mua_wrapper is set, all addresses must be able to be sent in a single +transaction. If not, this function yields FALSE. + +In Exim 4, remote deliveries are always done in separate processes, even +if remote_max_parallel = 1 or if there's only one delivery to do. The reason +is so that the base process can retain privilege. This makes the +implementation of fallback transports feasible (though not initially done.) + +We create up to the configured number of subprocesses, each of which passes +back the delivery state via a pipe. (However, when sending down an existing +connection, remote_max_parallel is forced to 1.) + +Arguments: + fallback TRUE if processing fallback hosts + +Returns: TRUE normally + FALSE if mua_wrapper is set and the addresses cannot all be sent + in one transaction +*/ + +static BOOL +do_remote_deliveries(BOOL fallback) +{ +int parmax; +int delivery_count; +int poffset; + +parcount = 0; /* Number of executing subprocesses */ + +/* When sending down an existing channel, only do one delivery at a time. +We use a local variable (parmax) to hold the maximum number of processes; +this gets reduced from remote_max_parallel if we can't create enough pipes. */ + +if (continue_transport != NULL) remote_max_parallel = 1; +parmax = remote_max_parallel; + +/* If the data for keeping a list of processes hasn't yet been +set up, do so. */ + +if (parlist == NULL) + { + parlist = store_get(remote_max_parallel * sizeof(pardata)); + for (poffset = 0; poffset < remote_max_parallel; poffset++) + parlist[poffset].pid = 0; + } + +/* Now loop for each remote delivery */ + +for (delivery_count = 0; addr_remote != NULL; delivery_count++) + { + pid_t pid; + uid_t uid; + gid_t gid; + int pfd[2]; + int address_count = 1; + int address_count_max; + BOOL multi_domain; + BOOL use_initgroups; + BOOL pipe_done = FALSE; + transport_instance *tp; + address_item **anchor = &addr_remote; + address_item *addr = addr_remote; + address_item *last = addr; + address_item *next; + + /* Pull the first address right off the list. */ + + addr_remote = addr->next; + addr->next = NULL; + + DEBUG(D_deliver|D_transport) + debug_printf("--------> %s <--------\n", addr->address); + + /* If no transport has been set, there has been a big screw-up somewhere. */ + + if ((tp = addr->transport) == NULL) + { + disable_logging = FALSE; /* Jic */ + remote_post_process(addr, LOG_MAIN|LOG_PANIC, + US"No transport set by router", fallback); + continue; + } + + /* Check that this base address hasn't previously been delivered to this + transport. The check is necessary at this point to handle homonymic addresses + correctly in cases where the pattern of redirection changes between delivery + attempts. Non-homonymic previous delivery is detected earlier, at routing + time. */ + + if (previously_transported(addr)) continue; + + /* Force failure if the message is too big. */ + + if (tp->message_size_limit != NULL) + { + int rc = check_message_size(tp, addr); + if (rc != OK) + { + addr->transport_return = rc; + remote_post_process(addr, LOG_MAIN, NULL, fallback); + continue; + } + } + + /* Get the flag which specifies whether the transport can handle different + domains that nevertheless resolve to the same set of hosts. */ + + multi_domain = tp->multi_domain; + + /* Get the maximum it can handle in one envelope, with zero meaning + unlimited, which is forced for the MUA wrapper case. */ + + address_count_max = tp->max_addresses; + if (address_count_max == 0 || mua_wrapper) address_count_max = 999999; + + + /************************************************************************/ + /***** This is slightly experimental code, but should be safe. *****/ + + /* The address_count_max value is the maximum number of addresses that the + transport can send in one envelope. However, the transport must be capable of + dealing with any number of addresses. If the number it gets exceeds its + envelope limitation, it must send multiple copies of the message. This can be + done over a single connection for SMTP, so uses less resources than making + multiple connections. On the other hand, if remote_max_parallel is greater + than one, it is perhaps a good idea to use parallel processing to move the + message faster, even if that results in multiple simultaneous connections to + the same host. + + How can we come to some compromise between these two ideals? What we do is to + limit the number of addresses passed to a single instance of a transport to + the greater of (a) its address limit (rcpt_max for SMTP) and (b) the total + number of addresses routed to remote transports divided by + remote_max_parallel. For example, if the message has 100 remote recipients, + remote max parallel is 2, and rcpt_max is 10, we'd never send more than 50 at + once. But if rcpt_max is 100, we could send up to 100. + + Of course, not all the remotely addresses in a message are going to go to the + same set of hosts (except in smarthost configurations), so this is just a + heuristic way of dividing up the work. + + Furthermore (1), because this may not be wanted in some cases, and also to + cope with really pathological cases, there is also a limit to the number of + messages that are sent over one connection. This is the same limit that is + used when sending several different messages over the same connection. + Continue_sequence is set when in this situation, to the number sent so + far, including this message. + + Furthermore (2), when somebody explicitly sets the maximum value to 1, it + is probably because they are using VERP, in which case they want to pass only + one address at a time to the transport, in order to be able to use + $local_part and $domain in constructing a new return path. We could test for + the use of these variables, but as it is so likely they will be used when the + maximum is 1, we don't bother. Just leave the value alone. */ + + if (address_count_max != 1 && + address_count_max < remote_delivery_count/remote_max_parallel) + { + int new_max = remote_delivery_count/remote_max_parallel; + int message_max = tp->connection_max_messages; + if (connection_max_messages >= 0) message_max = connection_max_messages; + message_max -= continue_sequence - 1; + if (message_max > 0 && new_max > address_count_max * message_max) + new_max = address_count_max * message_max; + address_count_max = new_max; + } + + /************************************************************************/ + + + /* Pick off all addresses which have the same transport, errors address, + destination, and extra headers. In some cases they point to the same host + list, but we also need to check for identical host lists generated from + entirely different domains. The host list pointers can be NULL in the case + where the hosts are defined in the transport. There is also a configured + maximum limit of addresses that can be handled at once (see comments above + for how it is computed). */ + + while ((next = *anchor) != NULL && address_count < address_count_max) + { + if ((multi_domain || Ustrcmp(next->domain, addr->domain) == 0) + && + tp == next->transport + && + same_hosts(next->host_list, addr->host_list) + && + same_strings(next->p.errors_address, addr->p.errors_address) + && + same_headers(next->p.extra_headers, addr->p.extra_headers) + && + same_ugid(tp, next, addr) + && + (next->p.remove_headers == addr->p.remove_headers || + (next->p.remove_headers != NULL && + addr->p.remove_headers != NULL && + Ustrcmp(next->p.remove_headers, addr->p.remove_headers) == 0))) + { + *anchor = next->next; + next->next = NULL; + next->first = addr; /* remember top one (for retry processing) */ + last->next = next; + last = next; + address_count++; + } + else anchor = &(next->next); + } + + /* If we are acting as an MUA wrapper, all addresses must go in a single + transaction. If not, put them back on the chain and yield FALSE. */ + + if (mua_wrapper && addr_remote != NULL) + { + last->next = addr_remote; + addr_remote = addr; + return FALSE; + } + + /* Set up the expansion variables for this set of addresses */ + + deliver_set_expansions(addr); + + /* Compute the return path, expanding a new one if required. The old one + must be set first, as it might be referred to in the expansion. */ + + return_path = (addr->p.errors_address != NULL)? + addr->p.errors_address : sender_address; + + if (tp->return_path != NULL) + { + uschar *new_return_path = expand_string(tp->return_path); + if (new_return_path == NULL) + { + if (!expand_string_forcedfail) + { + remote_post_process(addr, LOG_MAIN|LOG_PANIC, + string_sprintf("Failed to expand return path \"%s\": %s", + tp->return_path, expand_string_message), fallback); + continue; + } + } + else return_path = new_return_path; + } + + /* If this transport has a setup function, call it now so that it gets + run in this process and not in any subprocess. That way, the results of + any setup that are retained by the transport can be reusable. */ + + if (tp->setup != NULL) + (void)((tp->setup)(addr->transport, addr, NULL, NULL)); + + /* If this is a run to continue delivery down an already-established + channel, check that this set of addresses matches the transport and + the channel. If it does not, defer the addresses. If a host list exists, + we must check that the continue host is on the list. Otherwise, the + host is set in the transport. */ + + continue_more = FALSE; /* In case got set for the last lot */ + if (continue_transport != NULL) + { + BOOL ok = Ustrcmp(continue_transport, tp->name) == 0; + if (ok && addr->host_list != NULL) + { + host_item *h; + ok = FALSE; + for (h = addr->host_list; h != NULL; h = h->next) + { + if (Ustrcmp(h->name, continue_hostname) == 0) + { ok = TRUE; break; } + } + } + + /* Addresses not suitable; defer or queue for fallback hosts (which + might be the continue host) and skip to next address. */ + + if (!ok) + { + DEBUG(D_deliver) debug_printf("not suitable for continue_transport\n"); + next = addr; + + if (addr->fallback_hosts != NULL && !fallback) + { + for (;;) + { + next->host_list = next->fallback_hosts; + DEBUG(D_deliver) debug_printf("%s queued for fallback host(s)\n", next->address); + if (next->next == NULL) break; + next = next->next; + } + next->next = addr_fallback; + addr_fallback = addr; + } + + else + { + while (next->next != NULL) next = next->next; + next->next = addr_defer; + addr_defer = addr; + } + + continue; + } + + /* Set a flag indicating whether there are further addresses that list + the continued host. This tells the transport to leave the channel open, + but not to pass it to another delivery process. */ + + for (next = addr_remote; next != NULL; next = next->next) + { + host_item *h; + for (h = next->host_list; h != NULL; h = h->next) + { + if (Ustrcmp(h->name, continue_hostname) == 0) + { continue_more = TRUE; break; } + } + } + } + + /* The transports set up the process info themselves as they may connect + to more than one remote machine. They also have to set up the filter + arguments, if required, so that the host name and address are available + for expansion. */ + + transport_filter_argv = NULL; + + /* Find the uid, gid, and use_initgroups setting for this transport. Failure + logs and sets up error messages, so we just post-process and continue with + the next address. */ + + if (!findugid(addr, tp, &uid, &gid, &use_initgroups)) + { + remote_post_process(addr, LOG_MAIN|LOG_PANIC, NULL, fallback); + continue; + } + + /* Create the pipe for inter-process communication. If pipe creation + fails, it is probably because the value of remote_max_parallel is so + large that too many file descriptors for pipes have been created. Arrange + to wait for a process to finish, and then try again. If we still can't + create a pipe when all processes have finished, break the retry loop. */ + + while (!pipe_done) + { + if (pipe(pfd) == 0) pipe_done = TRUE; + else if (parcount > 0) parmax = parcount; + else break; + + /* We need to make the reading end of the pipe non-blocking. There are + two different options for this. Exim is cunningly (I hope!) coded so + that it can use either of them, though it prefers O_NONBLOCK, which + distinguishes between EOF and no-more-data. */ + + #ifdef O_NONBLOCK + fcntl(pfd[pipe_read], F_SETFL, O_NONBLOCK); + #else + fcntl(pfd[pipe_read], F_SETFL, O_NDELAY); + #endif + + /* If the maximum number of subprocesses already exist, wait for a process + to finish. If we ran out of file descriptors, parmax will have been reduced + from its initial value of remote_max_parallel. */ + + par_reduce(parmax - 1, fallback); + } + + /* If we failed to create a pipe and there were no processes to wait + for, we have to give up on this one. Do this outside the above loop + so that we can continue the main loop. */ + + if (!pipe_done) + { + remote_post_process(addr, LOG_MAIN|LOG_PANIC, + string_sprintf("unable to create pipe: %s", strerror(errno)), fallback); + continue; + } + + /* Find a free slot in the pardata list. Must do this after the possible + waiting for processes to finish, because a terminating process will free + up a slot. */ + + for (poffset = 0; poffset < remote_max_parallel; poffset++) + if (parlist[poffset].pid == 0) break; + + /* If there isn't one, there has been a horrible disaster. */ + + if (poffset >= remote_max_parallel) + { + close(pfd[pipe_write]); + close(pfd[pipe_read]); + remote_post_process(addr, LOG_MAIN|LOG_PANIC, + US"Unexpectedly no free subprocess slot", fallback); + continue; + } + + /* Now fork a subprocess to do the remote delivery, but before doing so, + ensure that any cached resourses are released so as not to interfere with + what happens in the subprocess. */ + + search_tidyup(); + + if ((pid = fork()) == 0) + { + int fd = pfd[pipe_write]; + host_item *h; + + /* There are weird circumstances in which logging is disabled */ + + disable_logging = tp->disable_logging; + + /* Show pids on debug output if parallelism possible */ + + if (parmax > 1 && (parcount > 0 || addr_remote != NULL)) + { + DEBUG(D_any|D_v) debug_selector |= D_pid; + DEBUG(D_deliver) debug_printf("Remote delivery process started\n"); + } + + /* Reset the random number generator, so different processes don't all + have the same sequence. In the test harness we want different, but + predictable settings for each delivery process, so do something explicit + here rather they rely on the fixed reset in the random number function. */ + + random_seed = running_in_test_harness? 42 + 2*delivery_count : 0; + + /* Set close-on-exec on the pipe so that it doesn't get passed on to + a new process that may be forked to do another delivery down the same + SMTP connection. */ + + fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); + + /* Close open file descriptors for the pipes of other processes + that are running in parallel. */ + + for (poffset = 0; poffset < remote_max_parallel; poffset++) + if (parlist[poffset].pid != 0) close(parlist[poffset].fd); + + /* This process has inherited a copy of the file descriptor + for the data file, but its file pointer is shared with all the + other processes running in parallel. Therefore, we have to re-open + the file in order to get a new file descriptor with its own + file pointer. We don't need to lock it, as the lock is held by + the parent process. There doesn't seem to be any way of doing + a dup-with-new-file-pointer. */ + + close(deliver_datafile); + sprintf(CS spoolname, "%s/input/%s/%s-D", spool_directory, message_subdir, + message_id); + deliver_datafile = Uopen(spoolname, O_RDWR | O_APPEND, 0); + + if (deliver_datafile < 0) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Failed to reopen %s for remote " + "parallel delivery: %s", spoolname, strerror(errno)); + + /* Set the close-on-exec flag */ + + fcntl(deliver_datafile, F_SETFD, fcntl(deliver_datafile, F_GETFD) | + FD_CLOEXEC); + + /* Set the uid/gid of this process; bombs out on failure. */ + + exim_setugid(uid, gid, use_initgroups, + string_sprintf("remote delivery to %s with transport=%s", + addr->address, tp->name)); + + /* Close the unwanted half of this process' pipe, set the process state, + and run the transport. Afterwards, transport_count will contain the number + of bytes written. */ + + close(pfd[pipe_read]); + set_process_info("delivering %s using %s", message_id, tp->name); + debug_print_string(tp->debug_string); + if (!(tp->info->code)(addr->transport, addr)) replicate_status(addr); + + set_process_info("delivering %s (just run %s for %s%s in subprocess)", + message_id, tp->name, addr->address, (addr->next == NULL)? "" : ", ..."); + + /* Ensure any cached resources that we used are now released */ + + search_tidyup(); + + /* Pass the result back down the pipe. This is a lot more information + than is needed for a local delivery. We have to send back the error + status for each address, the usability status for each host that is + flagged as unusable, and all the retry items. When TLS is in use, we + send also the cipher and peerdn information. Each type of information + is flagged by an identifying byte, and is then in a fixed format (with + strings terminated by zeros), and there is a final terminator at the + end. The host information and retry information is all attached to + the first address, so that gets sent at the start. */ + + /* Host unusability information: for most success cases this will + be null. */ + + for (h = addr->host_list; h != NULL; h = h->next) + { + if (h->address == NULL || h->status < hstatus_unusable) continue; + sprintf(CS big_buffer, "H%c%c%s", h->status, h->why, h->address); + write(fd, big_buffer, Ustrlen(big_buffer+3) + 4); + } + + /* The number of bytes written. This is the same for each address. Even + if we sent several copies of the message down the same connection, the + size of each one is the same, and it's that value we have got because + transport_count gets reset before calling transport_write_message(). */ + + big_buffer[0] = 'S'; + memcpy(big_buffer+1, &transport_count, sizeof(transport_count)); + write(fd, big_buffer, sizeof(transport_count) + 1); + + /* Information about what happened to each address. Three item types are + used: an optional 'X' item first, for TLS information, followed by 'R' + items for any retry settings, and finally an 'A' item for the remaining + data. */ + + for(; addr != NULL; addr = addr->next) + { + uschar *ptr; + retry_item *r; + + /* The certificate verification status goes into the flags */ + + if (tls_certificate_verified) setflag(addr, af_cert_verified); + + /* Use an X item only if there's something to send */ + + #ifdef SUPPORT_TLS + if (addr->cipher != NULL) + { + ptr = big_buffer; + *ptr++ = 'X'; + sprintf(CS ptr, "%.128s", addr->cipher); + while(*ptr++); + if (addr->peerdn == NULL) *ptr++ = 0; else + { + sprintf(CS ptr, "%.512s", addr->peerdn); + while(*ptr++); + } + write(fd, big_buffer, ptr - big_buffer); + } + #endif + + /* Retry information: for most success cases this will be null. */ + + for (r = addr->retries; r != NULL; r = r->next) + { + uschar *ptr; + sprintf(CS big_buffer, "R%c%.500s", r->flags, r->key); + ptr = big_buffer + Ustrlen(big_buffer+2) + 3; + memcpy(ptr, &(r->basic_errno), sizeof(r->basic_errno)); + ptr += sizeof(r->basic_errno); + memcpy(ptr, &(r->more_errno), sizeof(r->more_errno)); + ptr += sizeof(r->more_errno); + if (r->message == NULL) *ptr++ = 0; else + { + sprintf(CS ptr, "%.512s", r->message); + while(*ptr++); + } + write(fd, big_buffer, ptr - big_buffer); + } + + /* The rest of the information goes in an 'A' item. */ + + ptr = big_buffer + 3; + sprintf(CS big_buffer, "A%c%c", addr->transport_return, + addr->special_action); + memcpy(ptr, &(addr->basic_errno), sizeof(addr->basic_errno)); + ptr += sizeof(addr->basic_errno); + memcpy(ptr, &(addr->more_errno), sizeof(addr->more_errno)); + ptr += sizeof(addr->more_errno); + memcpy(ptr, &(addr->flags), sizeof(addr->flags)); + ptr += sizeof(addr->flags); + + if (addr->message == NULL) *ptr++ = 0; else + { + sprintf(CS ptr, "%.1024s", addr->message); + while(*ptr++); + } + + if (addr->user_message == NULL) *ptr++ = 0; else + { + sprintf(CS ptr, "%.1024s", addr->user_message); + while(*ptr++); + } + + if (addr->host_used == NULL) *ptr++ = 0; else + { + sprintf(CS ptr, "%.256s", addr->host_used->name); + while(*ptr++); + sprintf(CS ptr, "%.64s", addr->host_used->address); + while(*ptr++); + memcpy(ptr, &(addr->host_used->port), sizeof(addr->host_used->port)); + ptr += sizeof(addr->host_used->port); + } + write(fd, big_buffer, ptr - big_buffer); + } + + /* Add termination flag, close the pipe, and that's it. The character + after 'Z' indicates whether continue_transport is now NULL or not. + A change from non-NULL to NULL indicates a problem with a continuing + connection. */ + + big_buffer[0] = 'Z'; + big_buffer[1] = (continue_transport == NULL)? '0' : '1'; + write(fd, big_buffer, 2); + close(fd); + exit(EXIT_SUCCESS); + } + + /* Back in the mainline: close the unwanted half of the pipe. */ + + close(pfd[pipe_write]); + + /* Fork failed; defer with error message */ + + if (pid < 0) + { + close(pfd[pipe_read]); + remote_post_process(addr, LOG_MAIN|LOG_PANIC, + string_sprintf("fork failed for remote delivery to %s: %s", + addr->domain, strerror(errno)), fallback); + continue; + } + + /* Fork succeeded; increment the count, and remember relevant data for + when the process finishes. */ + + parcount++; + parlist[poffset].addrlist = parlist[poffset].addr = addr; + parlist[poffset].pid = pid; + parlist[poffset].fd = pfd[pipe_read]; + parlist[poffset].done = FALSE; + parlist[poffset].msg = NULL; + parlist[poffset].return_path = return_path; + + /* If the process we've just started is sending a message down an existing + channel, wait for it now. This ensures that only one such process runs at + once, whatever the value of remote_max parallel. Otherwise, we might try to + send two or more messages simultaneously down the same channel. This could + happen if there are different domains that include the same host in otherwise + different host lists. + + Also, if the transport closes down the channel, this information gets back + (continue_transport gets set to NULL) before we consider any other addresses + in this message. */ + + if (continue_transport != NULL) par_reduce(0, fallback); + + /* Otherwise, if we are running in the test harness, wait a bit, to let the + newly created process get going before we create another process. This should + ensure repeatability in the tests. We only need to wait a tad. */ + + else if (running_in_test_harness) millisleep(500); + } + +/* Reached the end of the list of addresses. Wait for all the subprocesses that +are still running and post-process their addresses. */ + +par_reduce(0, fallback); +return TRUE; +} + + + + +/************************************************* +* Split an address into local part and domain * +*************************************************/ + +/* This function initializes an address for routing by splitting it up into a +local part and a domain. The local part is set up twice - once in its original +casing, and once in lower case, and it is dequoted. We also do the "percent +hack" for configured domains. This may lead to a DEFER result if a lookup +defers. When a percent-hacking takes place, we insert a copy of the original +address as a new parent of this address, as if we have had a redirection. + +Argument: + addr points to an addr_item block containing the address + +Returns: OK + DEFER - could not determine if domain is %-hackable +*/ + +int +deliver_split_address(address_item *addr) +{ +uschar *address = addr->address; +uschar *domain = Ustrrchr(address, '@'); +uschar *t; +int len = domain - address; + +addr->domain = string_copylc(domain+1); /* Domains are always caseless */ + +/* The implication in the RFCs (though I can't say I've seen it spelled out +explicitly) is that quoting should be removed from local parts at the point +where they are locally interpreted. [The new draft "821" is more explicit on +this, Jan 1999.] We know the syntax is valid, so this can be done by simply +removing quoting backslashes and any unquoted doublequotes. */ + +t = addr->cc_local_part = store_get(len+1); +while(len-- > 0) + { + register int c = *address++; + if (c == '\"') continue; + if (c == '\\') + { + *t++ = *address++; + len--; + } + else *t++ = c; + } +*t = 0; + +/* We do the percent hack only for those domains that are listed in +percent_hack_domains. A loop is required, to copy with multiple %-hacks. */ + +if (percent_hack_domains != NULL) + { + int rc; + uschar *new_address = NULL; + uschar *local_part = addr->cc_local_part; + + deliver_domain = addr->domain; /* set $domain */ + + while ((rc = match_isinlist(deliver_domain, &percent_hack_domains, 0, + &domainlist_anchor, addr->domain_cache, MCL_DOMAIN, TRUE, NULL)) + == OK && + (t = Ustrrchr(local_part, '%')) != NULL) + { + new_address = string_copy(local_part); + new_address[t - local_part] = '@'; + deliver_domain = string_copylc(t+1); + local_part = string_copyn(local_part, t - local_part); + } + + if (rc == DEFER) return DEFER; /* lookup deferred */ + + /* If hackery happened, set up new parent and alter the current address. */ + + if (new_address != NULL) + { + address_item *new_parent = store_get(sizeof(address_item)); + *new_parent = *addr; + addr->parent = new_parent; + addr->address = new_address; + addr->unique = string_copy(new_address); + addr->domain = deliver_domain; + addr->cc_local_part = local_part; + DEBUG(D_deliver) debug_printf("%%-hack changed address to: %s\n", + addr->address); + } + } + +/* Create the lowercased version of the final local part, and make that the +default one to be used. */ + +addr->local_part = addr->lc_local_part = string_copylc(addr->cc_local_part); +return OK; +} + + + + +/************************************************* +* Get next error message text * +*************************************************/ + +/* If f is not NULL, read the next "paragraph", from a customized error message +text file, terminated by a line containing ****, and expand it. + +Arguments: + f NULL or a file to read from + which string indicating which string (for errors) + +Returns: NULL or an expanded string +*/ + +static uschar * +next_emf(FILE *f, uschar *which) +{ +int size = 256; +int ptr = 0; +uschar *para, *yield; +uschar buffer[256]; + +if (f == NULL) return NULL; + +if (Ufgets(buffer, sizeof(buffer), f) == NULL || + Ustrcmp(buffer, "****\n") == 0) return NULL; + +para = store_get(size); +for (;;) + { + para = string_cat(para, &size, &ptr, buffer, Ustrlen(buffer)); + if (Ufgets(buffer, sizeof(buffer), f) == NULL || + Ustrcmp(buffer, "****\n") == 0) break; + } +para[ptr] = 0; + +yield = expand_string(para); +if (yield != NULL) return yield; + +log_write(0, LOG_MAIN|LOG_PANIC, "Failed to expand string from " + "bounce_message_file or warn_message_file (%s): %s", which, + expand_string_message); +return NULL; +} + + + + +/************************************************* +* Close down a passed transport channel * +*************************************************/ + +/* This function is called when a passed transport channel cannot be used. +It attempts to close it down tidily. The yield is always DELIVER_NOT_ATTEMPTED +so that the function call can be the argument of a "return" statement. + +Arguments: None +Returns: DELIVER_NOT_ATTEMPTED +*/ + +static int +continue_closedown(void) +{ +if (continue_transport != NULL) + { + transport_instance *t; + for (t = transports; t != NULL; t = t->next) + { + if (Ustrcmp(t->name, continue_transport) == 0) + { + if (t->info->closedown != NULL) (t->info->closedown)(t); + break; + } + } + } +return DELIVER_NOT_ATTEMPTED; +} + + + + +/************************************************* +* Print address information * +*************************************************/ + +/* This function is called to output an address, or information about an +address, for bounce or defer messages. If the hide_child flag is set, all we +output is the original ancestor address. + +Arguments: + addr points to the address + f the FILE to print to + si an initial string + sc a continuation string for before "generated" + se an end string + +Returns: TRUE if the address is not hidden +*/ + +static BOOL +print_address_information(address_item *addr, FILE *f, uschar *si, uschar *sc, + uschar *se) +{ +BOOL yield = TRUE; +uschar *printed = US""; +address_item *ancestor = addr; +while (ancestor->parent != NULL) ancestor = ancestor->parent; + +fprintf(f, "%s", CS si); + +if (addr->parent != NULL && testflag(addr, af_hide_child)) + { + printed = US"an undisclosed address"; + yield = FALSE; + } + +else if (!testflag(addr, af_pfr) || addr->parent == NULL) + printed = addr->address; + +else + { + uschar *s = addr->address; + uschar *ss; + + if (addr->address[0] == '>') { ss = US"mail"; s++; } + else if (addr->address[0] == '|') ss = US"pipe"; + else ss = US"save"; + + fprintf(f, "%s to %s%sgenerated by ", ss, s, sc); + printed = addr->parent->address; + } + +fprintf(f, "%s", CS string_printing(printed)); + +if (ancestor != addr) + { + uschar *original = (ancestor->onetime_parent == NULL)? + ancestor->address : ancestor->onetime_parent; + if (strcmpic(original, printed) != 0) + fprintf(f, "%s(%sgenerated from %s)", sc, + (ancestor != addr->parent)? "ultimately " : "", + string_printing(original)); + } + +fprintf(f, "%s", CS se); +return yield; +} + + + + + + +/************************************************* +* Print error for an address * +*************************************************/ + +/* This function is called to print the error information out of an address for +a bounce or a warning message. It tries to format the message reasonably by +introducing newlines. All lines are indented by 4; the initial printing +position must be set before calling. + +Arguments: + addr points to the address + f the FILE to print on + +Returns: nothing +*/ + +static void +print_address_error(address_item *addr, FILE *f) +{ +uschar *s = (addr->user_message != NULL)? addr->user_message : addr->message; +if (addr->basic_errno > 0) + { + fprintf(f, "%s%s", strerror(addr->basic_errno), + (s == NULL)? "" : ":\n "); + } +if (s == NULL) + { + if (addr->basic_errno <= 0) fprintf(f, "unknown error"); + } +else + { + int count = 0; + while (*s != 0) + { + if (*s == '\\' && s[1] == 'n') + { + fprintf(f, "\n "); + s += 2; + count = 0; + } + else + { + fputc(*s, f); + count++; + if (*s++ == ':' && isspace(*s) && count > 45) + { + fprintf(f, "\n "); /* sic (because space follows) */ + count = 0; + } + } + } + } +} + + + + +/************************************************* +* Deliver one message * +*************************************************/ + +/* This is the function which is called when a message is to be delivered. It +is passed the id of the message. It is possible that the message no longer +exists, if some other process has delivered it, and it is also possible that +the message is being worked on by another process, in which case the data file +will be locked. + +If no delivery is attempted for any of the above reasons, the function returns +DELIVER_NOT_ATTEMPTED. + +If the give_up flag is set true, do not attempt any deliveries, but instead +fail all outstanding addresses and return the message to the sender (or +whoever). + +A delivery operation has a process all to itself; we never deliver more than +one message in the same process. Therefore we needn't worry too much about +store leakage. + +Arguments: + id the id of the message to be delivered + forced TRUE if delivery was forced by an administrator; this overrides + retry delays and causes a delivery to be tried regardless + give_up TRUE if an administrator has requested that delivery attempts + be abandoned + +Returns: When the global variable mua_wrapper is FALSE: + DELIVER_ATTEMPTED_NORMAL if a delivery attempt was made + DELIVER_NOT_ATTEMPTED otherwise (see comment above) + When the global variable mua_wrapper is TRUE: + DELIVER_MUA_SUCCEEDED if delivery succeeded + DELIVER_MUA_FAILED if delivery failed + DELIVER_NOT_ATTEMPTED if not attempted (should not occur) +*/ + +int +deliver_message(uschar *id, BOOL forced, BOOL give_up) +{ +int i, rc; +int final_yield = DELIVER_ATTEMPTED_NORMAL; +time_t now = time(NULL); +address_item *addr_last = NULL; +uschar *filter_message = NULL; +FILE *jread; +int process_recipients = RECIP_ACCEPT; +open_db dbblock; +open_db *dbm_file; + +uschar *info = (queue_run_pid == (pid_t)0)? + string_sprintf("delivering %s", id) : + string_sprintf("delivering %s (queue run pid %d)", id, queue_run_pid); + +/* If the D_process_info bit is on, set_process_info() will output debugging +information. If not, we want to show this initial information if D_deliver or +D_queue_run is set or in verbose mode. */ + +set_process_info("%s", info); + +if ((debug_selector & D_process_info) == 0 && + (debug_selector & (D_deliver|D_queue_run|D_v)) != 0) + debug_printf("%s\n", info); + +/* Ensure that we catch any subprocesses that are created. Although Exim +sets SIG_DFL as its initial default, some routes through the code end up +here with it set to SIG_IGN - cases where a non-synchronous delivery process +has been forked, but no re-exec has been done. We use sigaction rather than +plain signal() on those OS where SA_NOCLDWAIT exists, because we want to be +sure it is turned off. (There was a problem on AIX with this.) */ + +#ifdef SA_NOCLDWAIT + { + struct sigaction act; + act.sa_handler = SIG_DFL; + sigemptyset(&(act.sa_mask)); + act.sa_flags = 0; + sigaction(SIGCHLD, &act, NULL); + } +#else +signal(SIGCHLD, SIG_DFL); +#endif + +/* Make the forcing flag available for routers and transports, set up the +global message id field, and initialize the count for returned files and the +message size. This use of strcpy() is OK because the length id is checked when +it is obtained from a command line (the -M or -q options), and otherwise it is +known to be a valid message id. */ + +Ustrcpy(message_id, id); +deliver_force = forced; +return_count = 0; +message_size = 0; + +/* Initialize some flags */ + +update_spool = FALSE; +remove_journal = TRUE; + +/* Reset the random number generator, so that if several delivery processes are +started from a queue runner that has already used random numbers (for sorting), +they don't all get the same sequence. */ + +random_seed = 0; + +/* Open and lock the message's data file. Exim locks on this one because the +header file may get replaced as it is re-written during the delivery process. +Any failures cause messages to be written to the log, except for missing files +while queue running - another process probably completed delivery. As part of +opening the data file, message_subdir gets set. */ + +if (!spool_open_datafile(id)) + return continue_closedown(); /* yields DELIVER_NOT_ATTEMPTED */ + +/* The value of message_size at this point has been set to the data length, +plus one for the blank line that notionally precedes the data. */ + +/* Now read the contents of the header file, which will set up the headers in +store, and also the list of recipients and the tree of non-recipients and +assorted flags. It updates message_size. If there is a reading or format error, +give up; if the message has been around for sufficiently long, remove it. */ + +sprintf(CS spoolname, "%s-H", id); +if ((rc = spool_read_header(spoolname, TRUE, TRUE)) != spool_read_OK) + { + if (errno == ERRNO_SPOOLFORMAT) + { + struct stat statbuf; + sprintf(CS big_buffer, "%s/input/%s/%s", spool_directory, message_subdir, + spoolname); + if (Ustat(big_buffer, &statbuf) == 0) + { + int size = statbuf.st_size; /* Because might be a long */ + log_write(0, LOG_MAIN, "Format error in spool file %s: size=%d", + spoolname, size); + } + else log_write(0, LOG_MAIN, "Format error in spool file %s", spoolname); + } + else + log_write(0, LOG_MAIN, "Error reading spool file %s: %s", spoolname, + strerror(errno)); + + /* If we managed to read the envelope data, received_time contains the + time the message was received. Otherwise, we can calculate it from the + message id. */ + + if (rc != spool_read_hdrerror) + { + received_time = 0; + for (i = 0; i < 6; i++) + received_time = received_time * BASE_62 + tab62[id[i] - '0']; + } + + /* If we've had this malformed message too long, sling it. */ + + if (now - received_time > keep_malformed) + { + sprintf(CS spoolname, "%s/msglog/%s/%s", spool_directory, message_subdir, id); + Uunlink(spoolname); + sprintf(CS spoolname, "%s/input/%s/%s-D", spool_directory, message_subdir, id); + Uunlink(spoolname); + sprintf(CS spoolname, "%s/input/%s/%s-H", spool_directory, message_subdir, id); + Uunlink(spoolname); + sprintf(CS spoolname, "%s/input/%s/%s-J", spool_directory, message_subdir, id); + Uunlink(spoolname); + log_write(0, LOG_MAIN, "Message removed because older than %s", + readconf_printtime(keep_malformed)); + } + + close(deliver_datafile); + deliver_datafile = -1; + return continue_closedown(); /* yields DELIVER_NOT_ATTEMPTED */ + } + +/* The spool header file has been read. Look to see if there is an existing +journal file for this message. If there is, it means that a previous delivery +attempt crashed (program or host) before it could update the spool header file. +Read the list of delivered addresses from the journal and add them to the +nonrecipients tree. Then update the spool file. We can leave the journal in +existence, as it will get further successful deliveries added to it in this +run, and it will be deleted if this function gets to its end successfully. +Otherwise it might be needed again. */ + +sprintf(CS spoolname, "%s/input/%s/%s-J", spool_directory, message_subdir, id); +jread = Ufopen(spoolname, "rb"); +if (jread != NULL) + { + while (Ufgets(big_buffer, big_buffer_size, jread) != NULL) + { + int n = Ustrlen(big_buffer); + big_buffer[n-1] = 0; + tree_add_nonrecipient(big_buffer); + DEBUG(D_deliver) debug_printf("Previously delivered address %s taken from " + "journal file\n", big_buffer); + } + fclose(jread); + /* Panic-dies on error */ + (void)spool_write_header(message_id, SW_DELIVERING, NULL); + } +else if (errno != ENOENT) + { + log_write(0, LOG_MAIN|LOG_PANIC, "attempt to open journal for reading gave: " + "%s", strerror(errno)); + return continue_closedown(); /* yields DELIVER_NOT_ATTEMPTED */ + } + +/* A null recipients list indicates some kind of disaster. */ + +if (recipients_list == NULL) + { + close(deliver_datafile); + deliver_datafile = -1; + log_write(0, LOG_MAIN, "Spool error: no recipients for %s", spoolname); + return continue_closedown(); /* yields DELIVER_NOT_ATTEMPTED */ + } + + +/* Handle a message that is frozen. There are a number of different things that +can happen, but in the default situation, unless forced, no delivery is +attempted. */ + +if (deliver_freeze) + { + #ifdef SUPPORT_MOVE_FROZEN_MESSAGES + /* Moving to another directory removes the message from Exim's view. Other + tools must be used to deal with it. Logging of this action happens in + spool_move_message() and its subfunctions. */ + + if (move_frozen_messages && + spool_move_message(id, message_subdir, US"", US"F")) + return continue_closedown(); /* yields DELIVER_NOT_ATTEMPTED */ + #endif + + /* For all frozen messages (bounces or not), timeout_frozen_after sets the + maximum time to keep messages that are frozen. Thaw if we reach it, with a + flag causing all recipients to be failed. The time is the age of the + message, not the time since freezing. */ + + if (timeout_frozen_after > 0 && message_age >= timeout_frozen_after) + { + log_write(0, LOG_MAIN, "cancelled by timeout_frozen_after"); + process_recipients = RECIP_FAIL_TIMEOUT; + } + + /* For bounce messages (and others with no sender), thaw if the error message + ignore timer is exceeded. The message will be discarded if this delivery + fails. */ + + else if (sender_address[0] == 0 && message_age >= ignore_bounce_errors_after) + { + log_write(0, LOG_MAIN, "Unfrozen by errmsg timer"); + } + + /* If there's no auto thaw, or we haven't reached the auto thaw time yet, and + this delivery is not forced by an admin user, do not attempt delivery of this + message. Note that forced is set for continuing messages down the same + channel, in order to skip load checking and ignore hold domains, but we + don't want unfreezing in that case. */ + + else + { + if ((auto_thaw <= 0 || now <= deliver_frozen_at + auto_thaw) && + (!forced || !deliver_force_thaw || !admin_user || + continue_hostname != NULL)) + { + close(deliver_datafile); + deliver_datafile = -1; + log_write(L_skip_delivery, LOG_MAIN, "Message is frozen"); + return continue_closedown(); /* yields DELIVER_NOT_ATTEMPTED */ + } + + /* If delivery was forced (by an admin user), assume a manual thaw. + Otherwise it's an auto thaw. */ + + if (forced) + { + deliver_manual_thaw = TRUE; + log_write(0, LOG_MAIN, "Unfrozen by forced delivery"); + } + else log_write(0, LOG_MAIN, "Unfrozen by auto-thaw"); + } + + /* We get here if any of the rules for unfreezing have triggered. */ + + deliver_freeze = FALSE; + update_spool = TRUE; + } + + +/* Open the message log file if we are using them. This records details of +deliveries, deferments, and failures for the benefit of the mail administrator. +The log is not used by exim itself to track the progress of a message; that is +done by rewriting the header spool file. */ + +if (message_logs) + { + uschar *error; + int fd; + + sprintf(CS spoolname, "%s/msglog/%s/%s", spool_directory, message_subdir, id); + fd = open_msglog_file(spoolname, SPOOL_MODE, &error); + + if (fd < 0) + { + log_write(0, LOG_MAIN|LOG_PANIC, "Couldn't %s message log %s: %s", error, + spoolname, strerror(errno)); + return continue_closedown(); /* yields DELIVER_NOT_ATTEMPTED */ + } + + /* Make a C stream out of it. */ + + message_log = fdopen(fd, "a"); + if (message_log == NULL) + { + log_write(0, LOG_MAIN|LOG_PANIC, "Couldn't fdopen message log %s: %s", + spoolname, strerror(errno)); + return continue_closedown(); /* yields DELIVER_NOT_ATTEMPTED */ + } + } + + +/* If asked to give up on a message, log who did it, and set the action for all +the addresses. */ + +if (give_up) + { + struct passwd *pw = getpwuid(real_uid); + log_write(0, LOG_MAIN, "cancelled by %s", (pw != NULL)? + US pw->pw_name : string_sprintf("uid %ld", (long int)real_uid)); + process_recipients = RECIP_FAIL; + } + +/* Otherwise, if there are too many Received: headers, fail all recipients. */ + +else if (received_count > received_headers_max) + process_recipients = RECIP_FAIL_LOOP; + +/* Otherwise, if a system-wide, address-independent message filter is +specified, run it now, except in the case when we are failing all recipients as +a result of timeout_frozen_after. If the system filter yields "delivered", then +ignore the true recipients of the message. Failure of the filter file is +logged, and the delivery attempt fails. */ + +else if (system_filter != NULL && process_recipients != RECIP_FAIL_TIMEOUT) + { + int rc; + int filtertype; + ugid_block ugid; + redirect_block redirect; + + if (system_filter_uid_set) + { + ugid.uid = system_filter_uid; + ugid.gid = system_filter_gid; + ugid.uid_set = ugid.gid_set = TRUE; + } + else + { + ugid.uid_set = ugid.gid_set = FALSE; + } + + return_path = sender_address; + enable_dollar_recipients = TRUE; /* Permit $recipients in system filter */ + system_filtering = TRUE; + + /* Any error in the filter file causes a delivery to be abandoned. */ + + redirect.string = system_filter; + redirect.isfile = TRUE; + redirect.check_owner = redirect.check_group = FALSE; + redirect.owners = NULL; + redirect.owngroups = NULL; + redirect.pw = NULL; + redirect.modemask = 0; + + DEBUG(D_deliver|D_filter) debug_printf("running system filter\n"); + + rc = rda_interpret( + &redirect, /* Where the data is */ + RDO_DEFER | /* Turn on all the enabling options */ + RDO_FAIL | /* Leave off all the disabling options */ + RDO_FILTER | + RDO_FREEZE | + RDO_REALLOG | + RDO_REWRITE, + NULL, /* No :include: restriction (not used in filter) */ + NULL, /* No sieve vacation directory (not sieve!) */ + &ugid, /* uid/gid data */ + &addr_new, /* Where to hang generated addresses */ + &filter_message, /* Where to put error message */ + NULL, /* Don't skip syntax errors */ + &filtertype, /* Will always be set to FILTER_EXIM for this call */ + US"system filter"); /* For error messages */ + + DEBUG(D_deliver|D_filter) debug_printf("system filter returned %d\n", rc); + + if (rc == FF_ERROR || rc == FF_NONEXIST) + { + close(deliver_datafile); + deliver_datafile = -1; + log_write(0, LOG_MAIN|LOG_PANIC, "Error in system filter: %s", + string_printing(filter_message)); + return continue_closedown(); /* yields DELIVER_NOT_ATTEMPTED */ + } + + /* Reset things. If the filter message is an empty string, which can happen + for a filter "fail" or "freeze" command with no text, reset it to NULL. */ + + system_filtering = FALSE; + enable_dollar_recipients = FALSE; + if (filter_message != NULL && filter_message[0] == 0) filter_message = NULL; + + /* Save the values of the system filter variables so that user filters + can use them. */ + + memcpy(filter_sn, filter_n, sizeof(filter_sn)); + + /* The filter can request that delivery of the original addresses be + deferred. */ + + if (rc == FF_DEFER) + { + process_recipients = RECIP_DEFER; + deliver_msglog("Delivery deferred by system filter\n"); + log_write(0, LOG_MAIN, "Delivery deferred by system filter"); + } + + /* The filter can request that a message be frozen, but this does not + take place if the message has been manually thawed. In that case, we must + unset "delivered", which is forced by the "freeze" command to make -bF + work properly. */ + + else if (rc == FF_FREEZE && !deliver_manual_thaw) + { + deliver_freeze = TRUE; + deliver_frozen_at = time(NULL); + process_recipients = RECIP_DEFER; + frozen_info = string_sprintf(" by the system filter%s%s", + (filter_message == NULL)? US"" : US": ", + (filter_message == NULL)? US"" : filter_message); + } + + /* The filter can request that a message be failed. The error message may be + quite long - it is sent back to the sender in the bounce - but we don't want + to fill up the log with repetitions of it. If it starts with << then the text + between << and >> is written to the log, with the rest left for the bounce + message. */ + + else if (rc == FF_FAIL) + { + uschar *colon = US""; + uschar *logmsg = US""; + int loglen = 0; + + process_recipients = RECIP_FAIL_FILTER; + + if (filter_message != NULL) + { + uschar *logend; + colon = US": "; + if (filter_message[0] == '<' && filter_message[1] == '<' && + (logend = Ustrstr(filter_message, ">>")) != NULL) + { + logmsg = filter_message + 2; + loglen = logend - logmsg; + filter_message = logend + 2; + if (filter_message[0] == 0) filter_message = NULL; + } + else + { + logmsg = filter_message; + loglen = Ustrlen(filter_message); + } + } + + log_write(0, LOG_MAIN, "cancelled by system filter%s%.*s", colon, loglen, + logmsg); + } + + /* Delivery can be restricted only to those recipients (if any) that the + filter specified. */ + + else if (rc == FF_DELIVERED) + { + process_recipients = RECIP_IGNORE; + if (addr_new == NULL) + log_write(0, LOG_MAIN, "=> discarded (system filter)"); + else + log_write(0, LOG_MAIN, "original recipients ignored (system filter)"); + } + + /* If any new addresses were created by the filter, fake up a "parent" + for them. This is necessary for pipes, etc., which are expected to have + parents, and it also gives some sensible logging for others. Allow + pipes, files, and autoreplies, and run them as the filter uid if set, + otherwise as the current uid. */ + + if (addr_new != NULL) + { + int uid = (system_filter_uid_set)? system_filter_uid : geteuid(); + int gid = (system_filter_gid_set)? system_filter_gid : getegid(); + + /* The text "system-filter" is tested in transport_set_up_command() and in + set_up_shell_command() in the pipe transport, to enable them to permit + $recipients, so don't change it here without also changing it there. */ + + address_item *p = addr_new; + address_item *parent = deliver_make_addr(US"system-filter", FALSE); + + parent->domain = string_copylc(qualify_domain_recipient); + parent->local_part = US"system-filter"; + + /* As part of this loop, we arrange for addr_last to end up pointing + at the final address. This is used if we go on to add addresses for the + original recipients. */ + + while (p != NULL) + { + parent->child_count++; + p->parent = parent; + + if (testflag(p, af_pfr)) + { + uschar *tpname; + uschar *type; + p->uid = uid; + p->gid = gid; + setflag(p, af_uid_set | + af_gid_set | + af_allow_file | + af_allow_pipe | + af_allow_reply); + + /* Find the name of the system filter's appropriate pfr transport */ + + if (p->address[0] == '|') + { + type = US"pipe"; + tpname = system_filter_pipe_transport; + address_pipe = p->address; + } + else if (p->address[0] == '>') + { + type = US"reply"; + tpname = system_filter_reply_transport; + } + else + { + if (p->address[Ustrlen(p->address)-1] == '/') + { + type = US"directory"; + tpname = system_filter_directory_transport; + } + else + { + type = US"file"; + tpname = system_filter_file_transport; + } + address_file = p->address; + } + + /* Now find the actual transport, first expanding the name. We have + set address_file or address_pipe above. */ + + if (tpname != NULL) + { + uschar *tmp = expand_string(tpname); + address_file = address_pipe = NULL; + if (tmp == NULL) + p->message = string_sprintf("failed to expand \"%s\" as a " + "system filter transport name", tpname); + tpname = tmp; + } + else + { + p->message = string_sprintf("system_filter_%s_transport is unset", + type); + } + + if (tpname != NULL) + { + transport_instance *tp; + for (tp = transports; tp != NULL; tp = tp->next) + { + if (Ustrcmp(tp->name, tpname) == 0) + { + p->transport = tp; + break; + } + } + if (tp == NULL) + p->message = string_sprintf("failed to find \"%s\" transport " + "for system filter delivery", tpname); + } + + /* If we couldn't set up a transport, defer the delivery, putting the + error on the panic log as well as the main log. */ + + if (p->transport == NULL) + { + address_item *badp = p; + p = p->next; + if (addr_last == NULL) addr_new = p; else addr_last->next = p; + badp->local_part = badp->address; /* Needed for log line */ + post_process_one(badp, DEFER, LOG_MAIN|LOG_PANIC, DTYPE_ROUTER, 0); + continue; + } + } /* End of pfr handling */ + + /* Either a non-pfr delivery, or we found a transport */ + + DEBUG(D_deliver|D_filter) + debug_printf("system filter added %s\n", p->address); + + addr_last = p; + p = p->next; + } /* Loop through all addr_new addresses */ + } + } + + +/* Scan the recipients list, and for every one that is not in the non- +recipients tree, add an addr item to the chain of new addresses. If the pno +value is non-negative, we must set the onetime parent from it. This which +points to the relevant entry in the recipients list. + +This processing can be altered by the setting of the process_recipients +variable, which is changed if recipients are to be ignored, failed, or +deferred. This can happen as a result of system filter activity, or if the -Mg +option is used to fail all of them. + +Duplicate addresses are handled later by a different tree structure; we can't +just extend the non-recipients tree, because that will be re-written to the +spool if the message is deferred, and in any case there are casing +complications for local addresses. */ + +if (process_recipients != RECIP_IGNORE) + { + for (i = 0; i < recipients_count; i++) + { + if (tree_search(tree_nonrecipients, recipients_list[i].address) == NULL) + { + recipient_item *r = recipients_list + i; + address_item *new = deliver_make_addr(r->address, FALSE); + new->p.errors_address = r->errors_to; + + if (r->pno >= 0) + new->onetime_parent = recipients_list[r->pno].address; + + switch (process_recipients) + { + /* RECIP_DEFER is set when a system filter freezes a message. */ + + case RECIP_DEFER: + new->next = addr_defer; + addr_defer = new; + break; + + + /* RECIP_FAIL_FILTER is set when a system filter has obeyed a "fail" + command. */ + + case RECIP_FAIL_FILTER: + new->message = + (filter_message == NULL)? US"delivery cancelled" : filter_message; + goto RECIP_QUEUE_FAILED; /* below */ + + + /* RECIP_FAIL_TIMEOUT is set when a message is frozen, but is older + than the value in timeout_frozen_after. Treat non-bounce messages + similarly to -Mg; for bounce messages we just want to discard, so + don't put the address on the failed list. The timeout has already + been logged. */ + + case RECIP_FAIL_TIMEOUT: + new->message = US"delivery cancelled; message timed out"; + goto RECIP_QUEUE_FAILED; /* below */ + + + /* RECIP_FAIL is set when -Mg has been used. */ + + case RECIP_FAIL: + new->message = US"delivery cancelled by administrator"; + /* Fall through */ + + /* Common code for the failure cases above. If this is not a bounce + message, put the address on the failed list so that it is used to + create a bounce. Otherwise do nothing - this just discards the address. + The incident has already been logged. */ + + RECIP_QUEUE_FAILED: + if (sender_address[0] != 0) + { + new->next = addr_failed; + addr_failed = new; + } + break; + + + /* RECIP_FAIL_LOOP is set when there are too many Received: headers + in the message. Process each address as a routing failure; if this + is a bounce message, it will get frozen. */ + + case RECIP_FAIL_LOOP: + new->message = US"Too many \"Received\" headers - suspected mail loop"; + post_process_one(new, FAIL, LOG_MAIN, DTYPE_ROUTER, 0); + break; + + + /* Value should be RECIP_ACCEPT; take this as the safe default. */ + + default: + if (addr_new == NULL) addr_new = new; else addr_last->next = new; + addr_last = new; + break; + } + } + } + } + +DEBUG(D_deliver) + { + address_item *p = addr_new; + debug_printf("Delivery address list:\n"); + while (p != NULL) + { + debug_printf(" %s %s\n", p->address, (p->onetime_parent == NULL)? US"" : + p->onetime_parent); + p = p->next; + } + } + +/* Set up the buffers used for copying over the file when delivering. */ + +deliver_in_buffer = store_malloc(DELIVER_IN_BUFFER_SIZE); +deliver_out_buffer = store_malloc(DELIVER_OUT_BUFFER_SIZE); + + + +/* Until there are no more new addresses, handle each one as follows: + + . If this is a generated address (indicated by the presence of a parent + pointer) then check to see whether it is a pipe, file, or autoreply, and + if so, handle it directly here. The router that produced the address will + have set the allow flags into the address, and also set the uid/gid required. + Having the routers generate new addresses and then checking them here at + the outer level is tidier than making each router do the checking, and + means that routers don't need access to the failed address queue. + + . Break up the address into local part and domain, and make lowercased + versions of these strings. We also make unquoted versions of the local part. + + . Handle the percent hack for those domains for which it is valid. + + . For child addresses, determine if any of the parents have the same address. + If so, generate a different string for previous delivery checking. Without + this code, if the address spqr generates spqr via a forward or alias file, + delivery of the generated spqr stops further attempts at the top level spqr, + which is not what is wanted - it may have generated other addresses. + + . Check on the retry database to see if routing was previously deferred, but + only if in a queue run. Addresses that are to be routed are put on the + addr_route chain. Addresses that are to be deferred are put on the + addr_defer chain. We do all the checking first, so as not to keep the + retry database open any longer than necessary. + + . Now we run the addresses through the routers. A router may put the address + on either the addr_local or the addr_remote chain for local or remote + delivery, respectively, or put it on the addr_failed chain if it is + undeliveable, or it may generate child addresses and put them on the + addr_new chain, or it may defer an address. All the chain anchors are + passed as arguments so that the routers can be called for verification + purposes as well. + + . If new addresses have been generated by the routers, da capo. +*/ + +header_rewritten = FALSE; /* No headers rewritten yet */ +while (addr_new != NULL) /* Loop until all addresses dealt with */ + { + address_item *addr, *parent; + dbm_file = dbfn_open(US"retry", O_RDONLY, &dbblock, FALSE); + + /* Failure to open the retry database is treated the same as if it does + not exist. In both cases, dbm_file is NULL. */ + + if (dbm_file == NULL) + { + DEBUG(D_deliver|D_retry|D_route|D_hints_lookup) + debug_printf("no retry data available\n"); + } + + /* Scan the current batch of new addresses, to handle pipes, files and + autoreplies, and determine which others are ready for routing. */ + + while (addr_new != NULL) + { + int rc; + uschar *p; + tree_node *tnode; + dbdata_retry *domain_retry_record; + dbdata_retry *address_retry_record; + + addr = addr_new; + addr_new = addr->next; + + DEBUG(D_deliver|D_retry|D_route) + { + debug_printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"); + debug_printf("Considering: %s\n", addr->address); + } + + /* Handle generated address that is a pipe or a file or an autoreply. */ + + if (testflag(addr, af_pfr)) + { + int offset = testflag(addr->parent, af_homonym)? 3:0; + + /* If two different users specify delivery to the same pipe or file or + autoreply, there should be two different deliveries, so build a unique + string that incorporates the original address, and use this for + duplicate testing and recording delivery, and also for retrying. */ + + addr->unique = + string_sprintf("%s:%s", addr->address, addr->parent->unique + offset); + + addr->address_retry_key = addr->domain_retry_key = + string_sprintf("T:%s", addr->unique); + + /* If a filter file specifies two deliveries to the same pipe or file, + we want to de-duplicate, but this is probably not wanted for two mail + commands to the same address, where probably both should be delivered. + So, we have to invent a different unique string in that case. Just + keep piling '>' characters on the front. */ + + if (addr->address[0] == '>') + { + while (tree_search(tree_duplicates, addr->unique) != NULL) + addr->unique = string_sprintf(">%s", addr->unique); + } + + else if ((tnode = tree_search(tree_duplicates, addr->unique)) != NULL) + { + DEBUG(D_deliver|D_route) + debug_printf("%s is a duplicate address: discarded\n", addr->address); + addr->dupof = tnode->data.ptr; + addr->next = addr_duplicate; + addr_duplicate = addr; + continue; + } + + DEBUG(D_deliver|D_route) debug_printf("unique = %s\n", addr->unique); + + /* Check for previous delivery */ + + if (tree_search(tree_nonrecipients, addr->unique) != NULL) + { + DEBUG(D_deliver|D_route) + debug_printf("%s was previously delivered: discarded\n", addr->address); + child_done(addr, tod_stamp(tod_log)); + continue; + } + + /* Save for checking future duplicates */ + + tree_add_duplicate(addr->unique, addr); + + /* Set local part and domain */ + + addr->local_part = addr->address; + addr->domain = addr->parent->domain; + + /* Ensure that the delivery is permitted. */ + + if (testflag(addr, af_file)) + { + if (!testflag(addr, af_allow_file)) + { + addr->basic_errno = ERRNO_FORBIDFILE; + addr->message = US"delivery to file forbidden"; + (void)post_process_one(addr, FAIL, LOG_MAIN, DTYPE_ROUTER, 0); + continue; /* with the next new address */ + } + } + else if (addr->address[0] == '|') + { + if (!testflag(addr, af_allow_pipe)) + { + addr->basic_errno = ERRNO_FORBIDPIPE; + addr->message = US"delivery to pipe forbidden"; + (void)post_process_one(addr, FAIL, LOG_MAIN, DTYPE_ROUTER, 0); + continue; /* with the next new address */ + } + } + else if (!testflag(addr, af_allow_reply)) + { + addr->basic_errno = ERRNO_FORBIDREPLY; + addr->message = US"autoreply forbidden"; + (void)post_process_one(addr, FAIL, LOG_MAIN, DTYPE_ROUTER, 0); + continue; /* with the next new address */ + } + + /* If the errno field is already set to BADTRANSPORT, it indicates + failure to expand a transport string, or find the associated transport, + or an unset transport when one is required. Leave this test till now so + that the forbid errors are given in preference. */ + + if (addr->basic_errno == ERRNO_BADTRANSPORT) + { + (void)post_process_one(addr, DEFER, LOG_MAIN, DTYPE_ROUTER, 0); + continue; + } + + /* Treat /dev/null as a special case and abandon the delivery. This + avoids having to specify a uid on the transport just for this case. + Arrange for the transport name to be logged as "**bypassed**". */ + + if (Ustrcmp(addr->address, "/dev/null") == 0) + { + uschar *save = addr->transport->name; + addr->transport->name = US"**bypassed**"; + (void)post_process_one(addr, OK, LOG_MAIN, DTYPE_TRANSPORT, '='); + addr->transport->name = save; + continue; /* with the next new address */ + } + + /* Pipe, file, or autoreply delivery is to go ahead as a normal local + delivery. */ + + DEBUG(D_deliver|D_route) + debug_printf("queued for %s transport\n", addr->transport->name); + addr->next = addr_local; + addr_local = addr; + continue; /* with the next new address */ + } + + /* Handle normal addresses. First, split up into local part and domain, + handling the %-hack if necessary. There is the possibility of a defer from + a lookup in percent_hack_domains. */ + + if ((rc = deliver_split_address(addr)) == DEFER) + { + addr->message = US"cannot check percent_hack_domains"; + addr->basic_errno = ERRNO_LISTDEFER; + (void)post_process_one(addr, DEFER, LOG_MAIN, DTYPE_NONE, 0); + continue; + } + + /* Check to see if the domain is held. If so, proceed only if the + delivery was forced by hand. */ + + deliver_domain = addr->domain; /* set $domain */ + if (!forced && hold_domains != NULL && + (rc = match_isinlist(addr->domain, &hold_domains, 0, + &domainlist_anchor, addr->domain_cache, MCL_DOMAIN, TRUE, + NULL)) != FAIL) + { + if (rc == DEFER) + { + addr->message = US"hold_domains lookup deferred"; + addr->basic_errno = ERRNO_LISTDEFER; + } + else + { + addr->message = US"domain is held"; + addr->basic_errno = ERRNO_HELD; + } + (void)post_process_one(addr, DEFER, LOG_MAIN, DTYPE_NONE, 0); + continue; + } + + /* Now we can check for duplicates and previously delivered addresses. In + order to do this, we have to generate a "unique" value for each address, + because there may be identical actual addresses in a line of descendents. + The "unique" field is initialized to the same value as the "address" field, + but gets changed here to cope with identically-named descendents. */ + + for (parent = addr->parent; parent != NULL; parent = parent->parent) + if (strcmpic(addr->address, parent->address) == 0) break; + + /* If there's an ancestor with the same name, set the homonym flag. This + influences how deliveries are recorded. Then add a prefix on the front of + the unique address. We use \n\ where n starts at 0 and increases each time. + It is unlikely to pass 9, but if it does, it may look odd but will still + work. This means that siblings or cousins with the same names are treated + as duplicates, which is what we want. */ + + if (parent != NULL) + { + setflag(addr, af_homonym); + if (parent->unique[0] != '\\') + addr->unique = string_sprintf("\\0\\%s", addr->address); + else + addr->unique = string_sprintf("\\%c\\%s", parent->unique[1] + 1, + addr->address); + } + + /* Ensure that the domain in the unique field is lower cased, because + domains are always handled caselessly. */ + + p = Ustrrchr(addr->unique, '@'); + while (*p != 0) { *p = tolower(*p); p++; } + + DEBUG(D_deliver|D_route) debug_printf("unique = %s\n", addr->unique); + + if (tree_search(tree_nonrecipients, addr->unique) != NULL) + { + DEBUG(D_deliver|D_route) + debug_printf("%s was previously delivered: discarded\n", addr->unique); + child_done(addr, tod_stamp(tod_log)); + continue; + } + + /* If it's a duplicate, remember what it's a duplicate of */ + + if ((tnode = tree_search(tree_duplicates, addr->unique)) != NULL) + { + DEBUG(D_deliver|D_route) + debug_printf("%s is a duplicate address: discarded\n", addr->unique); + addr->dupof = tnode->data.ptr; + addr->next = addr_duplicate; + addr_duplicate = addr; + continue; + } + + /* Record this address, so subsequent duplicates get picked up. */ + + tree_add_duplicate(addr->unique, addr); + + /* Get the routing retry status, saving the two retry keys (with and + without the local part) for subsequent use. Ignore retry records that + are too old. */ + + addr->domain_retry_key = string_sprintf("R:%s", addr->domain); + addr->address_retry_key = string_sprintf("R:%s@%s", addr->local_part, + addr->domain); + + if (dbm_file == NULL) + domain_retry_record = address_retry_record = NULL; + else + { + domain_retry_record = dbfn_read(dbm_file, addr->domain_retry_key); + if (domain_retry_record != NULL && + now - domain_retry_record->time_stamp > retry_data_expire) + domain_retry_record = NULL; + + address_retry_record = dbfn_read(dbm_file, addr->address_retry_key); + if (address_retry_record != NULL && + now - address_retry_record->time_stamp > retry_data_expire) + address_retry_record = NULL; + } + + DEBUG(D_deliver|D_retry) + { + if (domain_retry_record == NULL) + debug_printf("no domain retry record\n"); + if (address_retry_record == NULL) + debug_printf("no address retry record\n"); + } + + /* If we are sending a message down an existing SMTP connection, we must + assume that the message which created the connection managed to route + an address to that connection. We do not want to run the risk of taking + a long time over routing here, because if we do, the server at the other + end of the connection may time it out. This is especially true for messages + with lots of addresses. For this kind of delivery, queue_running is not + set, so we would normally route all addresses. We take a pragmatic approach + and defer routing any addresses that have any kind of domain retry record. + That is, we don't even look at their retry times. It doesn't matter if this + doesn't work occasionally. This is all just an optimization, after all. + + The reason for not doing the same for address retries is that they normally + arise from 4xx responses, not DNS timeouts. */ + + if (continue_hostname != NULL && domain_retry_record != NULL) + { + addr->message = US"reusing SMTP connection skips previous routing defer"; + addr->basic_errno = ERRNO_RRETRY; + (void)post_process_one(addr, DEFER, LOG_MAIN, DTYPE_ROUTER, 0); + } + + /* If queue_running, defer routing unless no retry data or we've + passed the next retry time, or this message is forced. However, + if the retry time has expired, allow the routing attempt. + If it fails again, the address will be failed. This ensures that + each address is routed at least once, even after long-term routing + failures. + + If there is an address retry, check that too; just wait for the next + retry time. This helps with the case when the temporary error on the + address was really message-specific rather than address specific, since + it allows other messages through. */ + + else if (!deliver_force && queue_running && + ((domain_retry_record != NULL && + now < domain_retry_record->next_try && + !domain_retry_record->expired) + || + (address_retry_record != NULL && + now < address_retry_record->next_try)) + ) + { + addr->message = US"retry time not reached"; + addr->basic_errno = ERRNO_RRETRY; + (void)post_process_one(addr, DEFER, LOG_MAIN, DTYPE_ROUTER, 0); + } + + /* The domain is OK for routing. Remember if retry data exists so it + can be cleaned up after a successful delivery. */ + + else + { + if (domain_retry_record != NULL || address_retry_record != NULL) + setflag(addr, af_dr_retry_exists); + addr->next = addr_route; + addr_route = addr; + DEBUG(D_deliver|D_route) + debug_printf("%s: queued for routing\n", addr->address); + } + } + + /* The database is closed while routing is actually happening. Requests to + update it are put on a chain and all processed together at the end. */ + + if (dbm_file != NULL) dbfn_close(dbm_file); + + /* If queue_domains is set, we don't even want to try routing addresses in + those domains. During queue runs, queue_domains is forced to be unset. + Optimize by skipping this pass through the addresses if nothing is set. */ + + if (!deliver_force && queue_domains != NULL) + { + address_item *okaddr = NULL; + while (addr_route != NULL) + { + address_item *addr = addr_route; + addr_route = addr->next; + + deliver_domain = addr->domain; /* set $domain */ + if ((rc = match_isinlist(addr->domain, &queue_domains, 0, + &domainlist_anchor, addr->domain_cache, MCL_DOMAIN, TRUE, NULL)) + != OK) + { + if (rc == DEFER) + { + addr->basic_errno = ERRNO_LISTDEFER; + addr->message = US"queue_domains lookup deferred"; + (void)post_process_one(addr, DEFER, LOG_MAIN, DTYPE_ROUTER, 0); + } + else + { + addr->next = okaddr; + okaddr = addr; + } + } + else + { + addr->basic_errno = ERRNO_QUEUE_DOMAIN; + addr->message = US"domain is in queue_domains"; + (void)post_process_one(addr, DEFER, LOG_MAIN, DTYPE_ROUTER, 0); + } + } + + addr_route = okaddr; + } + + /* Now route those addresses that are not deferred. */ + + while (addr_route != NULL) + { + int rc; + address_item *addr = addr_route; + uschar *old_domain = addr->domain; + uschar *old_unique = addr->unique; + addr_route = addr->next; + addr->next = NULL; + + /* Just in case some router parameter refers to it. */ + + return_path = (addr->p.errors_address != NULL)? + addr->p.errors_address : sender_address; + + /* If a router defers an address, add a retry item. Whether or not to + use the local part in the key is a property of the router. */ + + if ((rc = route_address(addr, &addr_local, &addr_remote, &addr_new, + &addr_succeed, v_none)) == DEFER) + retry_add_item(addr, (addr->router->retry_use_local_part)? + string_sprintf("R:%s@%s", addr->local_part, addr->domain) : + string_sprintf("R:%s", addr->domain), 0); + + /* Otherwise, if there is an existing retry record in the database, add + retry items to delete both forms. Since the domain might have been + rewritten (expanded to fully qualified) as a result of routing, ensure + that the rewritten form is also deleted. */ + + else if (testflag(addr, af_dr_retry_exists)) + { + retry_add_item(addr, addr->address_retry_key, rf_delete); + retry_add_item(addr, addr->domain_retry_key, rf_delete); + if (Ustrcmp(addr->domain, old_domain) != 0) + retry_add_item(addr, string_sprintf("R:%s", old_domain), rf_delete); + } + + /* DISCARD is given for :blackhole: and "seen finish". The event has been + logged, but we need to ensure the address (and maybe parents) is marked + done. */ + + if (rc == DISCARD) + { + address_done(addr, tod_stamp(tod_log)); + continue; /* route next address */ + } + + /* The address is finished with (failed or deferred). */ + + if (rc != OK) + { + (void)post_process_one(addr, rc, LOG_MAIN, DTYPE_ROUTER, 0); + continue; /* route next address */ + } + + /* The address has been routed. If the router changed the domain, it will + also have changed the unique address. We have to test whether this address + has already been delivered, because it's the unique address that finally + gets recorded. */ + + if (addr->unique != old_unique && + tree_search(tree_nonrecipients, addr->unique) != 0) + { + DEBUG(D_deliver|D_route) debug_printf("%s was previously delivered: " + "discarded\n", addr->address); + if (addr_remote == addr) addr_remote = addr->next; + else if (addr_local == addr) addr_local = addr->next; + } + + /* If the router has same_domain_copy_routing set, we are permitted to copy + the routing for any other addresses with the same domain. This is an + optimisation to save repeated DNS lookups for "standard" remote domain + routing. The option is settable only on routers that generate host lists. + We play it very safe, and do the optimization only if the address is routed + to a remote transport, there are no header changes, and the domain was not + modified by the router. */ + + if (addr_remote == addr && + addr->router->same_domain_copy_routing && + addr->p.extra_headers == NULL && + addr->p.remove_headers == NULL && + old_domain == addr->domain) + { + address_item **chain = &addr_route; + while (*chain != NULL) + { + address_item *addr2 = *chain; + if (Ustrcmp(addr2->domain, addr->domain) != 0) + { + chain = &(addr2->next); + continue; + } + + /* Found a suitable address; take it off the routing list and add it to + the remote delivery list. */ + + *chain = addr2->next; + addr2->next = addr_remote; + addr_remote = addr2; + + /* Copy the routing data */ + + addr2->domain = addr->domain; + addr2->router = addr->router; + addr2->transport = addr->transport; + addr2->host_list = addr->host_list; + addr2->fallback_hosts = addr->fallback_hosts; + addr2->p.errors_address = addr->p.errors_address; + copyflag(addr2, addr, af_hide_child | af_local_host_removed); + + DEBUG(D_deliver|D_route) + { + debug_printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" + "routing %s\n" + "Routing for %s copied from %s\n", + addr2->address, addr2->address, addr->address); + } + } + } + } /* Continue with routing the next address. */ + } /* Loop to process any child addresses that the routers created, and + any rerouted addresses that got put back on the new chain. */ + + +/* Debugging: show the results of the routing */ + +DEBUG(D_deliver|D_retry|D_route) + { + address_item *p = addr_local; + debug_printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"); + debug_printf("After routing:\n Local deliveries:\n"); + while (p != NULL) + { + debug_printf(" %s\n", p->address); + p = p->next; + } + + p = addr_remote; + debug_printf(" Remote deliveries:\n"); + while (p != NULL) + { + debug_printf(" %s\n", p->address); + p = p->next; + } + + p = addr_failed; + debug_printf(" Failed addresses:\n"); + while (p != NULL) + { + debug_printf(" %s\n", p->address); + p = p->next; + } + + p = addr_defer; + debug_printf(" Deferred addresses:\n"); + while (p != NULL) + { + debug_printf(" %s\n", p->address); + p = p->next; + } + } + +/* Free any resources that were cached during routing. */ + +search_tidyup(); +route_tidyup(); + +/* These two variables are set only during routing, after check_local_user. +Ensure they are not set in transports. */ + +local_user_gid = (gid_t)(-1); +local_user_uid = (uid_t)(-1); + +/* When acting as an MUA wrapper, we proceed only if all addresses route to a +remote transport. The check that they all end up in one transaction happens in +the do_remote_deliveries() function. */ + +if (mua_wrapper && (addr_local != NULL || addr_failed != NULL || + addr_defer != NULL)) + { + address_item *addr; + uschar *which, *colon, *msg; + + if (addr_local != NULL) + { + addr = addr_local; + which = US"local"; + } + else if (addr_defer != NULL) + { + addr = addr_defer; + which = US"deferred"; + } + else + { + addr = addr_failed; + which = US"failed"; + } + + while (addr->parent != NULL) addr = addr->parent; + + if (addr->message != NULL) + { + colon = US": "; + msg = addr->message; + } + else colon = msg = US""; + + /* We don't need to log here for a forced failure as it will already + have been logged. Defer will also have been logged, but as a defer, so we do + need to do the failure logging. */ + + if (addr != addr_failed) + log_write(0, LOG_MAIN, "** %s routing yielded a %s delivery", + addr->address, which); + + /* Always write an error to the caller */ + + fprintf(stderr, "routing %s yielded a %s delivery%s%s\n", addr->address, + which, colon, msg); + + final_yield = DELIVER_MUA_FAILED; + addr_failed = addr_defer = NULL; /* So that we remove the message */ + goto DELIVERY_TIDYUP; + } + + +/* If this is a run to continue deliveries to an external channel that is +already set up, defer any local deliveries. */ + +if (continue_transport != NULL) + { + if (addr_defer == NULL) addr_defer = addr_local; else + { + address_item *addr = addr_defer; + while (addr->next != NULL) addr = addr->next; + addr->next = addr_local; + } + addr_local = NULL; + } + + +/* Because address rewriting can happen in the routers, we should not really do +ANY deliveries until all addresses have been routed, so that all recipients of +the message get the same headers. However, this is in practice not always +possible, since sometimes remote addresses give DNS timeouts for days on end. +The pragmatic approach is to deliver what we can now, saving any rewritten +headers so that at least the next lot of recipients benefit from the rewriting +that has already been done. + +If any headers have been rewritten during routing, update the spool file to +remember them for all subsequent deliveries. This can be delayed till later if +there is only address to be delivered - if it succeeds the spool write need not +happen. */ + +if (header_rewritten && + ((addr_local != NULL && + (addr_local->next != NULL || addr_remote != NULL)) || + (addr_remote != NULL && addr_remote->next != NULL))) + { + /* Panic-dies on error */ + (void)spool_write_header(message_id, SW_DELIVERING, NULL); + header_rewritten = FALSE; + } + + +/* If there are any deliveries to be done, open the journal file. This is used +to record successful deliveries as soon as possible after each delivery is +known to be complete. A file opened with O_APPEND is used so that several +processes can run simultaneously. + +The journal is just insurance against crashes. When the spool file is +ultimately updated at the end of processing, the journal is deleted. If a +journal is found to exist at the start of delivery, the addresses listed +therein are added to the non-recipients. */ + +if (addr_local != NULL || addr_remote != NULL) + { + sprintf(CS spoolname, "%s/input/%s/%s-J", spool_directory, message_subdir, id); + journal_fd = Uopen(spoolname, O_WRONLY|O_APPEND|O_CREAT, SPOOL_MODE); + + if (journal_fd < 0) + { + log_write(0, LOG_MAIN|LOG_PANIC, "Couldn't open journal file %s: %s", + spoolname, strerror(errno)); + return DELIVER_NOT_ATTEMPTED; + } + + /* Set the close-on-exec flag, make the file owned by Exim, and ensure + that the mode is correct - the group setting doesn't always seem to get + set automatically. */ + + fcntl(journal_fd, F_SETFD, fcntl(journal_fd, F_GETFD) | FD_CLOEXEC); + fchown(journal_fd, exim_uid, exim_gid); + fchmod(journal_fd, SPOOL_MODE); + } + + +/* Now we can get down to the business of actually doing deliveries. Local +deliveries are done first, then remote ones. If ever the problems of how to +handle fallback transports are figured out, this section can be put into a loop +for handling fallbacks, though the uid switching will have to be revised. */ + +if (addr_local != NULL) + { + DEBUG(D_deliver|D_transport) + debug_printf(">>>>>>>>>>>>>>>> Local deliveries >>>>>>>>>>>>>>>>\n"); + do_local_deliveries(); + disable_logging = FALSE; + } + +/* If queue_run_local is set, we do not want to attempt any remote deliveries, +so just queue them all. */ + +if (queue_run_local) + { + while (addr_remote != NULL) + { + address_item *addr = addr_remote; + addr_remote = addr->next; + addr->next = NULL; + addr->basic_errno = ERRNO_LOCAL_ONLY; + addr->message = US"remote deliveries suppressed"; + (void)post_process_one(addr, DEFER, LOG_MAIN, DTYPE_TRANSPORT, 0); + } + } + +/* Handle remote deliveries */ + +if (addr_remote != NULL) + { + DEBUG(D_deliver|D_transport) + debug_printf(">>>>>>>>>>>>>>>> Remote deliveries >>>>>>>>>>>>>>>>\n"); + + /* Precompile some regex that are used to recognize parameters in response + to an EHLO command, if they aren't already compiled. */ + + if (regex_PIPELINING == NULL) regex_PIPELINING = + regex_must_compile(US"\\n250[\\s\\-]PIPELINING(\\s|\\n|$)", FALSE, TRUE); + + if (regex_SIZE == NULL) regex_SIZE = + regex_must_compile(US"\\n250[\\s\\-]SIZE(\\s|\\n|$)", FALSE, TRUE); + + if (regex_AUTH == NULL) regex_AUTH = + regex_must_compile(US"\\n250[\\s\\-]AUTH\\s+([\\-\\w\\s]+)(?:\\n|$)", + FALSE, TRUE); + + #ifdef SUPPORT_TLS + if (regex_STARTTLS == NULL) regex_STARTTLS = + regex_must_compile(US"\\n250[\\s\\-]STARTTLS(\\s|\\n|$)", FALSE, TRUE); + #endif + + /* Now sort the addresses if required, and do the deliveries. The yield of + do_remote_deliveries is FALSE when mua_wrapper is set and all addresses + cannot be delivered in one transaction. */ + + if (remote_sort_domains != NULL) sort_remote_deliveries(); + if (!do_remote_deliveries(FALSE)) + { + log_write(0, LOG_MAIN, "** mua_wrapper is set but recipients cannot all " + "be delivered in one transaction"); + fprintf(stderr, "delivery to smarthost failed (configuration problem)\n"); + + final_yield = DELIVER_MUA_FAILED; + addr_failed = addr_defer = NULL; /* So that we remove the message */ + goto DELIVERY_TIDYUP; + } + + /* See if any of the addresses that failed got put on the queue for delivery + to their fallback hosts. We do it this way because often the same fallback + host is used for many domains, so all can be sent in a single transaction + (if appropriately configured). */ + + if (addr_fallback != NULL && !mua_wrapper) + { + DEBUG(D_deliver) debug_printf("Delivering to fallback hosts\n"); + addr_remote = addr_fallback; + addr_fallback = NULL; + if (remote_sort_domains != NULL) sort_remote_deliveries(); + do_remote_deliveries(TRUE); + } + disable_logging = FALSE; + } + + +/* All deliveries are now complete. Ignore SIGTERM during this tidying up +phase, to minimize cases of half-done things. */ + +DEBUG(D_deliver) + debug_printf(">>>>>>>>>>>>>>>> deliveries are done >>>>>>>>>>>>>>>>\n"); + +/* Root privilege is no longer needed */ + +exim_setugid(exim_uid, exim_gid, FALSE, US"post-delivery tidying"); + +set_process_info("tidying up after delivering %s", message_id); +signal(SIGTERM, SIG_IGN); + +/* When we are acting as an MUA wrapper, the smtp transport will either have +succeeded for all addresses, or failed them all. We do not ever want to retry, +nor do we want to send a bounce message. */ + +if (mua_wrapper) + { + if (addr_failed == NULL) final_yield = DELIVER_MUA_SUCCEEDED; else + { + uschar *s = (addr_failed->user_message != NULL)? + addr_failed->user_message : addr_failed->message; + + fprintf(stderr, "Delivery failed: "); + if (addr_failed->basic_errno > 0) + { + fprintf(stderr, "%s", strerror(addr_failed->basic_errno)); + if (s != NULL) fprintf(stderr, ": "); + } + if (s == NULL) + { + if (addr_failed->basic_errno <= 0) fprintf(stderr, "unknown error"); + } + else fprintf(stderr, "%s", CS s); + fprintf(stderr, "\n"); + + final_yield = DELIVER_MUA_FAILED; + addr_failed = NULL; + } + } + +/* In a normal configuration, we now update the retry database. This is done in +one fell swoop at the end in order not to keep opening and closing (and +locking) the database. The code for handling retries is hived off into a +separate module for convenience. We pass it the addresses of the various +chains, because deferred addresses can get moved onto the failed chain if the +retry cutoff time has expired for all alternative destinations. Bypass the +updating of the database if the -N flag is set, which is a debugging thing that +prevents actual delivery. */ + +else if (!dont_deliver) retry_update(&addr_defer, &addr_failed, &addr_succeed); + +/* If any addresses failed, we must send a message to somebody, unless +af_ignore_error is set, in which case no action is taken. It is possible for +several messages to get sent if there are addresses with different +requirements. */ + +while (addr_failed != NULL) + { + pid_t pid; + int fd; + uschar *logtod = tod_stamp(tod_log); + address_item *addr; + address_item *handled_addr = NULL; + address_item **paddr; + address_item *msgchain = NULL; + address_item **pmsgchain = &msgchain; + + /* There are weird cases when logging is disabled in the transport. However, + there may not be a transport (address failed by a router). */ + + disable_logging = FALSE; + if (addr_failed->transport != NULL) + disable_logging = addr_failed->transport->disable_logging; + + DEBUG(D_deliver) + debug_printf("processing failed address %s\n", addr_failed->address); + + /* There are only two ways an address in a bounce message can get here: + + (1) When delivery was initially deferred, but has now timed out (in the call + to retry_update() above). We can detect this by testing for + af_retry_timedout. If the address does not have its own errors address, + we arrange to ignore the error. + + (2) If delivery failures for bounce messages are being ignored. We can detect + this by testing for af_ignore_error. This will also be set if a bounce + message has been autothawed and the ignore_bounce_errors_after time has + passed. It might also be set if a router was explicitly configured to + ignore errors (errors_to = ""). + + If neither of these cases obtains, something has gone wrong. Log the + incident, but then ignore the error. */ + + if (sender_address[0] == 0 && addr_failed->p.errors_address == NULL) + { + if (!testflag(addr_failed, af_retry_timedout) && + !testflag(addr_failed, af_ignore_error)) + { + log_write(0, LOG_MAIN|LOG_PANIC, "internal error: bounce message " + "failure is neither frozen nor ignored (it's been ignored)"); + } + setflag(addr_failed, af_ignore_error); + } + + /* If the first address on the list has af_ignore_error set, just remove + it from the list, throw away any saved message file, log it, and + mark the recipient done. */ + + if (testflag(addr_failed, af_ignore_error)) + { + addr = addr_failed; + addr_failed = addr->next; + if (addr->return_filename != NULL) Uunlink(addr->return_filename); + + log_write(0, LOG_MAIN, "%s%s%s%s: error ignored", + addr->address, + (addr->parent == NULL)? US"" : US" <", + (addr->parent == NULL)? US"" : addr->parent->address, + (addr->parent == NULL)? US"" : US">"); + + address_done(addr, logtod); + child_done(addr, logtod); + /* Panic-dies on error */ + (void)spool_write_header(message_id, SW_DELIVERING, NULL); + } + + /* Otherwise, handle the sending of a message. Find the error address for + the first address, then send a message that includes all failed addresses + that have the same error address. Note the bounce_recipient is a global so + that it can be accesssed by $bounce_recipient while creating a customized + error message. */ + + else + { + bounce_recipient = (addr_failed->p.errors_address == NULL)? + sender_address : addr_failed->p.errors_address; + + /* Make a subprocess to send a message */ + + pid = child_open_exim(&fd); + + /* Creation of child failed */ + + if (pid < 0) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Process %d (parent %d) failed to " + "create child process to send failure message: %s", getpid(), + getppid(), strerror(errno)); + + /* Creation of child succeeded */ + + else + { + int ch, rc; + int filecount = 0; + int rcount = 0; + uschar *bcc, *emf_text; + FILE *f = fdopen(fd, "wb"); + FILE *emf = NULL; + BOOL to_sender = strcmpic(sender_address, bounce_recipient) == 0; + int max = (bounce_return_size_limit/DELIVER_IN_BUFFER_SIZE + 1) * + DELIVER_IN_BUFFER_SIZE; + + DEBUG(D_deliver) + debug_printf("sending error message to: %s\n", bounce_recipient); + + /* Scan the addresses for all that have the same errors address, removing + them from the addr_failed chain, and putting them on msgchain. */ + + paddr = &addr_failed; + for (addr = addr_failed; addr != NULL; addr = *paddr) + { + if (Ustrcmp(bounce_recipient, (addr->p.errors_address == NULL)? + sender_address : addr->p.errors_address) != 0) + { + paddr = &(addr->next); /* Not the same; skip */ + } + else /* The same - dechain */ + { + *paddr = addr->next; + *pmsgchain = addr; + addr->next = NULL; + pmsgchain = &(addr->next); + } + } + + /* Include X-Failed-Recipients: for automatic interpretation, but do + not let any one header line get too long. We do this by starting a + new header every 50 recipients. Omit any addresses for which the + "hide_child" flag is set. */ + + for (addr = msgchain; addr != NULL; addr = addr->next) + { + if (testflag(addr, af_hide_child)) continue; + if (rcount >= 50) + { + fprintf(f, "\n"); + rcount = 0; + } + fprintf(f, "%s%s", + (rcount++ == 0)? "X-Failed-Recipients: " : ",\n ", + (testflag(addr, af_pfr) && addr->parent != NULL)? + string_printing(addr->parent->address) : + string_printing(addr->address)); + } + if (rcount > 0) fprintf(f, "\n"); + + /* Output the standard headers */ + + if (errors_reply_to != NULL) + fprintf(f, "Reply-To: %s\n", errors_reply_to); + fprintf(f, "Auto-Submitted: auto-generated\n"); + fprintf(f, "From: Mail Delivery System \n", + qualify_domain_sender); + fprintf(f, "To: %s\n", bounce_recipient); + + /* Open a template file if one is provided. Log failure to open, but + carry on - default texts will be used. */ + + if (bounce_message_file != NULL) + { + emf = Ufopen(bounce_message_file, "rb"); + if (emf == NULL) + log_write(0, LOG_MAIN|LOG_PANIC, "Failed to open %s for error " + "message texts: %s", bounce_message_file, strerror(errno)); + } + + /* Quietly copy to configured additional addresses if required. */ + + bcc = moan_check_errorcopy(bounce_recipient); + if (bcc != NULL) fprintf(f, "Bcc: %s\n", bcc); + + /* The texts for the message can be read from a template file; if there + isn't one, or if it is too short, built-in texts are used. The first + emf text is a Subject: and any other headers. */ + + emf_text = next_emf(emf, US"header"); + if (emf_text != NULL) fprintf(f, "%s\n", emf_text); else + { + fprintf(f, "Subject: Mail delivery failed%s\n\n", + to_sender? ": returning message to sender" : ""); + } + + emf_text = next_emf(emf, US"intro"); + if (emf_text != NULL) fprintf(f, "%s", CS emf_text); else + { + fprintf(f, +/* This message has been reworded several times. It seems to be confusing to +somebody, however it is worded. I have retreated to the original, simple +wording. */ +"This message was created automatically by mail delivery software.\n"); + if (bounce_message_text != NULL) fprintf(f, "%s", CS bounce_message_text); + if (to_sender) + { + fprintf(f, +"\nA message that you sent could not be delivered to one or more of its\n" +"recipients. This is a permanent error. The following address(es) failed:\n"); + } + else + { + fprintf(f, +"\nA message sent by\n\n <%s>\n\n" +"could not be delivered to one or more of its recipients. The following\n" +"address(es) failed:\n", sender_address); + } + } + fprintf(f, "\n"); + + /* Process the addresses, leaving them on the msgchain if they have a + file name for a return message. (There has already been a check in + post_process_one() for the existence of data in the message file.) */ + + paddr = &msgchain; + for (addr = msgchain; addr != NULL; addr = *paddr) + { + if (print_address_information(addr, f, US" ", US"\n ", US"")) + { + /* A TRUE return from print_address_information() means that the + address is not hidden. If there is a return file, it has already + been checked to ensure it is not empty. Omit the bland "return + message generated" error, but otherwise include error information. */ + + if (addr->return_file < 0 || + addr->message == NULL || + Ustrcmp(addr->message, "return message generated") != 0) + { + fprintf(f, "\n "); + print_address_error(addr, f); + } + } + + /* End the final line for the address */ + + fputc('\n', f); + + /* Leave on msgchain if there's a return file. */ + + if (addr->return_file >= 0) + { + paddr = &(addr->next); + filecount++; + } + + /* Else save so that we can tick off the recipient when the + message is sent. */ + + else + { + *paddr = addr->next; + addr->next = handled_addr; + handled_addr = addr; + } + } + + fprintf(f, "\n"); + + /* Get the next text, whether we need it or not, so as to be + positioned for the one after. */ + + emf_text = next_emf(emf, US"generated text"); + + /* If there were any file messages passed by the local transports, + include them in the message. Then put the address on the handled chain. + In the case of a batch of addresses that were all sent to the same + transport, the return_file field in all of them will contain the same + fd, and the return_filename field in the *last* one will be set (to the + name of the file). */ + + if (msgchain != NULL) + { + address_item *nextaddr; + + if (emf_text != NULL) fprintf(f, "%s", CS emf_text); else + fprintf(f, + "The following text was generated during the delivery " + "attempt%s:\n", (filecount > 1)? "s" : ""); + + for (addr = msgchain; addr != NULL; addr = nextaddr) + { + FILE *fm; + address_item *topaddr = addr; + + /* List all the addresses that relate to this file */ + + fprintf(f, "\n"); + while(addr != NULL) /* Insurance */ + { + print_address_information(addr, f, US"------ ", US"\n ", + US" ------\n"); + if (addr->return_filename != NULL) break; + addr = addr->next; + } + fprintf(f, "\n"); + + /* Now copy the file */ + + fm = Ufopen(addr->return_filename, "rb"); + + if (fm == NULL) + fprintf(f, " +++ Exim error... failed to open text file: %s\n", + strerror(errno)); + else + { + while ((ch = fgetc(fm)) != EOF) fputc(ch, f); + fclose(fm); + } + Uunlink(addr->return_filename); + + /* Can now add to handled chain, first fishing off the next + address on the msgchain. */ + + nextaddr = addr->next; + addr->next = handled_addr; + handled_addr = topaddr; + } + fprintf(f, "\n"); + } + + /* Now copy the message, trying to give an intelligible comment if + it is too long for it all to be copied. The limit isn't strictly + applied because of the buffering. There is, however, an option + to suppress copying altogether. */ + + emf_text = next_emf(emf, US"copy"); + + if (bounce_return_message) + { + int topt = topt_add_return_path; + if (!bounce_return_body) topt |= topt_no_body; + + if (emf_text != NULL) fprintf(f, "%s", CS emf_text); else + { + if (bounce_return_body) fprintf(f, +"------ This is a copy of the message, including all the headers. ------\n"); + else fprintf(f, +"------ This is a copy of the message's headers. ------\n"); + } + + /* While reading the "truncated" message, set return_size_limit to + the actual max testing value, rounded. We need to read the message + whether we are going to use it or not. */ + + { + int temp = bounce_return_size_limit; + bounce_return_size_limit = (max/1000)*1000; + emf_text = next_emf(emf, US"truncated"); + bounce_return_size_limit = temp; + } + + if (bounce_return_body && bounce_return_size_limit > 0) + { + struct stat statbuf; + if (fstat(deliver_datafile, &statbuf) == 0 && statbuf.st_size > max) + { + if (emf_text != NULL) fprintf(f, "%s", CS emf_text); else + { + fprintf(f, +"------ The body of the message is %d characters long; only the first\n" +"------ %d or so are included here.\n", (int)statbuf.st_size, max); + } + } + } + + fprintf(f, "\n"); + fflush(f); + transport_filter_argv = NULL; /* Just in case */ + return_path = sender_address; /* In case not previously set */ + transport_write_message(NULL, fileno(f), topt, + bounce_return_size_limit, NULL, NULL, NULL, NULL, NULL, 0); + } + + /* Write final text and close the template file if one is open */ + + if (emf != NULL) + { + emf_text = next_emf(emf, US"final"); + if (emf_text != NULL) fprintf(f, "%s", CS emf_text); + fclose(emf); + } + + /* Close the file, which should send an EOF to the child process + that is receiving the message. Wait for it to finish. */ + + fclose(f); + rc = child_close(pid, 0); /* Waits for child to close, no timeout */ + + /* In the test harness, let the child do it's thing first. */ + + if (running_in_test_harness) millisleep(500); + + /* If the process failed, there was some disaster in setting up the + error message. Unless the message is very old, ensure that addr_defer + is non-null, which will have the effect of leaving the message on the + spool. The failed addresses will get tried again next time. However, we + don't really want this to happen too often, so freeze the message unless + there are some genuine deferred addresses to try. To do this we have + to call spool_write_header() here, because with no genuine deferred + addresses the normal code below doesn't get run. */ + + if (rc != 0) + { + uschar *s = US""; + if (now - received_time < retry_maximum_timeout && addr_defer == NULL) + { + addr_defer = (address_item *)(+1); + deliver_freeze = TRUE; + deliver_frozen_at = time(NULL); + /* Panic-dies on error */ + (void)spool_write_header(message_id, SW_DELIVERING, NULL); + s = US" (frozen)"; + } + deliver_msglog("Process failed (%d) when writing error message " + "to %s%s", rc, bounce_recipient, s); + log_write(0, LOG_MAIN, "Process failed (%d) when writing error message " + "to %s%s", rc, bounce_recipient, s); + } + + /* The message succeeded. Ensure that the recipients that failed are + now marked finished with on the spool and their parents updated. */ + + else + { + for (addr = handled_addr; addr != NULL; addr = addr->next) + { + address_done(addr, logtod); + child_done(addr, logtod); + } + /* Panic-dies on error */ + (void)spool_write_header(message_id, SW_DELIVERING, NULL); + } + } + } + } + +disable_logging = FALSE; /* In case left set */ + +/* Come here from the mua_wrapper case if routing goes wrong */ + +DELIVERY_TIDYUP: + +/* If there are now no deferred addresses, we are done. Preserve the +message log if so configured, and we are using them. Otherwise, sling it. +Then delete the message itself. */ + +if (addr_defer == NULL) + { + if (message_logs) + { + sprintf(CS spoolname, "%s/msglog/%s/%s", spool_directory, message_subdir, + id); + if (preserve_message_logs) + { + int rc; + sprintf(CS big_buffer, "%s/msglog.OLD/%s", spool_directory, id); + if ((rc = Urename(spoolname, big_buffer)) < 0) + { + (void)directory_make(spool_directory, US"msglog.OLD", + MSGLOG_DIRECTORY_MODE, TRUE); + rc = Urename(spoolname, big_buffer); + } + if (rc < 0) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to move %s to the " + "msglog.OLD directory", spoolname); + } + else + { + if (Uunlink(spoolname) < 0) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to unlink %s", spoolname); + } + } + + /* Remove the two message files. */ + + sprintf(CS spoolname, "%s/input/%s/%s-D", spool_directory, message_subdir, id); + if (Uunlink(spoolname) < 0) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to unlink %s", spoolname); + sprintf(CS spoolname, "%s/input/%s/%s-H", spool_directory, message_subdir, id); + if (Uunlink(spoolname) < 0) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to unlink %s", spoolname); + log_write(0, LOG_MAIN, "Completed"); + } + +/* If there are deferred addresses, we are keeping this message because it is +not yet completed. Lose any temporary files that were catching output from +pipes for any of the deferred addresses, handle one-time aliases, and see if +the message has been on the queue for so long that it is time to send a warning +message to the sender, unless it is a mailer-daemon. If all deferred addresses +have the same domain, we can set deliver_domain for the expansion of +delay_warning_ condition - if any of them are pipes, files, or autoreplies, use +the parent's domain. + +If all the deferred addresses have an error number that indicates "retry time +not reached", skip sending the warning message, because it won't contain the +reason for the delay. It will get sent at the next real delivery attempt. +However, if at least one address has tried, we'd better include all of them in +the message. + +If we can't make a process to send the message, don't worry. + +For mailing list expansions we want to send the warning message to the +mailing list manager. We can't do a perfect job here, as some addresses may +have different errors addresses, but if we take the errors address from +each deferred address it will probably be right in most cases. + +If addr_defer == +1, it means there was a problem sending an error message +for failed addresses, and there were no "real" deferred addresses. The value +was set just to keep the message on the spool, so there is nothing to do here. +*/ + +else if (addr_defer != (address_item *)(+1)) + { + address_item *addr; + uschar *recipients = US""; + BOOL delivery_attempted = FALSE; + + deliver_domain = testflag(addr_defer, af_pfr)? + addr_defer->parent->domain : addr_defer->domain; + + for (addr = addr_defer; addr != NULL; addr = addr->next) + { + address_item *otaddr; + + if (addr->basic_errno > ERRNO_RETRY_BASE) delivery_attempted = TRUE; + + if (deliver_domain != NULL) + { + uschar *d = (testflag(addr, af_pfr))? addr->parent->domain : addr->domain; + + /* The domain may be unset for an address that has never been routed + because the system filter froze the message. */ + + if (d == NULL || Ustrcmp(d, deliver_domain) != 0) deliver_domain = NULL; + } + + if (addr->return_filename != NULL) Uunlink(addr->return_filename); + + /* Handle the case of one-time aliases. If any address in the ancestry + of this one is flagged, ensure it is in the recipients list, suitably + flagged, and that its parent is marked delivered. */ + + for (otaddr = addr; otaddr != NULL; otaddr = otaddr->parent) + if (otaddr->onetime_parent != NULL) break; + + if (otaddr != NULL) + { + int i; + int t = recipients_count; + + for (i = 0; i < recipients_count; i++) + { + uschar *r = recipients_list[i].address; + if (Ustrcmp(otaddr->onetime_parent, r) == 0) t = i; + if (Ustrcmp(otaddr->address, r) == 0) break; + } + + /* Didn't find the address already in the list, and did find the + ultimate parent's address in the list. After adding the recipient, + update the errors address in the recipients list. */ + + if (i >= recipients_count && t < recipients_count) + { + DEBUG(D_deliver) debug_printf("one_time: adding %s in place of %s\n", + otaddr->address, otaddr->parent->address); + receive_add_recipient(otaddr->address, t); + recipients_list[recipients_count-1].errors_to = otaddr->p.errors_address; + tree_add_nonrecipient(otaddr->parent->address); + update_spool = TRUE; + } + } + + /* Except for error messages, ensure that either the errors address for + this deferred address or, if there is none, the sender address, is on the + list of recipients for a warning message. */ + + if (sender_address[0] != 0) + { + if (addr->p.errors_address == NULL) + { + if (Ustrstr(recipients, sender_address) == NULL) + recipients = string_sprintf("%s%s%s", recipients, + (recipients[0] == 0)? "" : ",", sender_address); + } + else + { + if (Ustrstr(recipients, addr->p.errors_address) == NULL) + recipients = string_sprintf("%s%s%s", recipients, + (recipients[0] == 0)? "" : ",", addr->p.errors_address); + } + } + } + + /* Send a warning message if the conditions are right. If the condition check + fails because of a lookup defer, there is nothing we can do. The warning + is not sent. Another attempt will be made at the next delivery attempt (if + it also defers). */ + + if (!queue_2stage && delivery_attempted && + delay_warning[1] > 0 && sender_address[0] != 0 && + (delay_warning_condition == NULL || + expand_check_condition(delay_warning_condition, + US"delay_warning", US"option"))) + { + int count; + int show_time; + int queue_time = time(NULL) - received_time; + + /* When running in the test harness, there's an option that allows us to + fudge this time so as to get repeatability of the tests. Take the first + time off the list. In queue runs, the list pointer gets updated in the + calling process. */ + + if (running_in_test_harness && fudged_queue_times[0] != 0) + { + int qt = readconf_readtime(fudged_queue_times, '/', FALSE); + if (qt >= 0) + { + DEBUG(D_deliver) debug_printf("fudged queue_times = %s\n", + fudged_queue_times); + queue_time = qt; + } + } + + /* See how many warnings we should have sent by now */ + + for (count = 0; count < delay_warning[1]; count++) + if (queue_time < delay_warning[count+2]) break; + + show_time = delay_warning[count+1]; + + if (count >= delay_warning[1]) + { + int extra; + int last_gap = show_time; + if (count > 1) last_gap -= delay_warning[count]; + extra = (queue_time - delay_warning[count+1])/last_gap; + show_time += last_gap * extra; + count += extra; + } + + DEBUG(D_deliver) + { + debug_printf("time on queue = %s\n", readconf_printtime(queue_time)); + debug_printf("warning counts: required %d done %d\n", count, + warning_count); + } + + /* We have computed the number of warnings there should have been by now. + If there haven't been enough, send one, and up the count to what it should + have been. */ + + if (warning_count < count) + { + header_line *h; + int fd; + pid_t pid = child_open_exim(&fd); + + if (pid > 0) + { + uschar *wmf_text; + FILE *wmf = NULL; + FILE *f = fdopen(fd, "wb"); + + if (warn_message_file != NULL) + { + wmf = Ufopen(warn_message_file, "rb"); + if (wmf == NULL) + log_write(0, LOG_MAIN|LOG_PANIC, "Failed to open %s for warning " + "message texts: %s", warn_message_file, strerror(errno)); + } + + warnmsg_recipients = recipients; + warnmsg_delay = (queue_time < 120*60)? + string_sprintf("%d minutes", show_time/60): + string_sprintf("%d hours", show_time/3600); + + if (errors_reply_to != NULL) + fprintf(f, "Reply-To: %s\n", errors_reply_to); + fprintf(f, "Auto-Submitted: auto-generated\n"); + fprintf(f, "From: Mail Delivery System \n", + qualify_domain_sender); + fprintf(f, "To: %s\n", recipients); + + wmf_text = next_emf(wmf, US"header"); + if (wmf_text != NULL) + fprintf(f, "%s\n", wmf_text); + else + fprintf(f, "Subject: Warning: message %s delayed %s\n\n", + message_id, warnmsg_delay); + + wmf_text = next_emf(wmf, US"intro"); + if (wmf_text != NULL) fprintf(f, "%s", CS wmf_text); else + { + fprintf(f, +"This message was created automatically by mail delivery software.\n"); + + if (Ustrcmp(recipients, sender_address) == 0) + fprintf(f, +"A message that you sent has not yet been delivered to one or more of its\n" +"recipients after more than "); + + else fprintf(f, +"A message sent by\n\n <%s>\n\n" +"has not yet been delivered to one or more of its recipients after more than \n", + sender_address); + + fprintf(f, "%s on the queue on %s.\n\n", warnmsg_delay, + primary_hostname); + fprintf(f, "The message identifier is: %s\n", message_id); + + for (h = header_list; h != NULL; h = h->next) + { + if (strncmpic(h->text, US"Subject:", 8) == 0) + fprintf(f, "The subject of the message is: %s", h->text + 9); + else if (strncmpic(h->text, US"Date:", 5) == 0) + fprintf(f, "The date of the message is: %s", h->text + 6); + } + fprintf(f, "\n"); + + fprintf(f, "The address%s to which the message has not yet been " + "delivered %s:\n", + (addr_defer->next == NULL)? "" : "es", + (addr_defer->next == NULL)? "is": "are"); + } + + /* List the addresses. For any that are hidden, don't give the delay + reason, because it might expose that which is hidden. Also, do not give + "retry time not reached" because that isn't helpful. */ + + fprintf(f, "\n"); + while (addr_defer != NULL) + { + address_item *addr = addr_defer; + addr_defer = addr->next; + if (print_address_information(addr, f, US" ", US"\n ", US"") && + addr->basic_errno > ERRNO_RETRY_BASE) + { + fprintf(f, "\n Delay reason: "); + print_address_error(addr, f); + } + fprintf(f, "\n"); + } + fprintf(f, "\n"); + + /* Final text */ + + if (wmf != NULL) + { + wmf_text = next_emf(wmf, US"final"); + if (wmf_text != NULL) fprintf(f, "%s", CS wmf_text); + fclose(wmf); + } + else + { + fprintf(f, +"No action is required on your part. Delivery attempts will continue for\n" +"some time, and this warning may be repeated at intervals if the message\n" +"remains undelivered. Eventually the mail delivery software will give up,\n" +"and when that happens, the message will be returned to you.\n"); + } + + /* Close and wait for child process to complete, without a timeout. + If there's an error, don't update the count. */ + + fclose(f); + if (child_close(pid, 0) == 0) + { + warning_count = count; + update_spool = TRUE; /* Ensure spool rewritten */ + } + } + } + } + + /* Clear deliver_domain */ + + deliver_domain = NULL; + + /* If this was a first delivery attempt, unset the first time flag, and + ensure that the spool gets updated. */ + + if (deliver_firsttime) + { + deliver_firsttime = FALSE; + update_spool = TRUE; + } + + /* If delivery was frozen and freeze_tell is set, generate an appropriate + message, unless the message is a local error message (to avoid loops). Then + log the freezing. If the text in "frozen_info" came from a system filter, + it has been escaped into printing characters so as not to mess up log lines. + For the "tell" message, we turn \n back into newline. Also, insert a newline + near the start instead of the ": " string. */ + + if (deliver_freeze) + { + if (freeze_tell != NULL && freeze_tell[0] != 0 && !local_error_message) + { + uschar *s = string_copy(frozen_info); + uschar *ss = Ustrstr(s, " by the system filter: "); + + if (ss != NULL) + { + ss[21] = '.'; + ss[22] = '\n'; + } + + ss = s; + while (*ss != 0) + { + if (*ss == '\\' && ss[1] == 'n') + { + *ss++ = ' '; + *ss++ = '\n'; + } + else ss++; + } + moan_tell_someone(freeze_tell, addr_defer, US"Message frozen", + "Message %s has been frozen%s.\nThe sender is <%s>.\n", message_id, + s, sender_address); + } + + /* Log freezing just before we update the -H file, to minimize the chance + of a race problem. */ + + deliver_msglog("*** Frozen%s\n", frozen_info); + log_write(0, LOG_MAIN, "Frozen%s", frozen_info); + } + + /* If there have been any updates to the non-recipients list, or other things + that get written to the spool, we must now update the spool header file so + that it has the right information for the next delivery attempt. If there + was more than one address being delivered, the header_change update is done + earlier, in case one succeeds and then something crashes. */ + + DEBUG(D_deliver) + debug_printf("delivery deferred: update_spool=%d header_rewritten=%d\n", + update_spool, header_rewritten); + + if (update_spool || header_rewritten) + /* Panic-dies on error */ + (void)spool_write_header(message_id, SW_DELIVERING, NULL); + } + +/* Finished with the message log. If the message is complete, it will have +been unlinked or renamed above. */ + +if (message_logs) fclose(message_log); + +/* Now we can close and remove the journal file. Its only purpose is to record +successfully completed deliveries asap so that this information doesn't get +lost if Exim (or the machine) crashes. Forgetting about a failed delivery is +not serious, as trying it again is not harmful. The journal might not be open +if all addresses were deferred at routing or directing. Nevertheless, we must +remove it if it exists (may have been lying around from a crash during the +previous delivery attempt). We don't remove the journal if a delivery +subprocess failed to pass back delivery information; this is controlled by +the remove_journal flag. When the journal is left, we also don't move the +message off the main spool if frozen and the option is set. It should get moved +at the next attempt, after the journal has been inspected. */ + +if (journal_fd >= 0) close(journal_fd); + +if (remove_journal) + { + sprintf(CS spoolname, "%s/input/%s/%s-J", spool_directory, message_subdir, id); + if (Uunlink(spoolname) < 0 && errno != ENOENT) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to unlink %s: %s", spoolname, + strerror(errno)); + + /* Move the message off the spool if reqested */ + + #ifdef SUPPORT_MOVE_FROZEN_MESSAGES + if (deliver_freeze && move_frozen_messages) + (void)spool_move_message(id, message_subdir, US"", US"F"); + #endif + } + +/* Closing the data file frees the lock; if the file has been unlinked it +will go away. Otherwise the message becomes available for another process +to try delivery. */ + +close(deliver_datafile); +deliver_datafile = -1; +DEBUG(D_deliver) debug_printf("end delivery of %s\n", id); + +/* It is unlikely that there will be any cached resources, since they are +released after routing, and in the delivery subprocesses. However, it's +possible for an expansion for something afterwards (for example, +expand_check_condition) to do a lookup. We must therefore be sure everything is +released. */ + +search_tidyup(); +return final_yield; +} + +/* End of deliver.c */ diff --git a/src/src/directory.c b/src/src/directory.c new file mode 100644 index 000000000..db7f68c36 --- /dev/null +++ b/src/src/directory.c @@ -0,0 +1,91 @@ +/* $Cambridge: exim/src/src/directory.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +#include "exim.h" + + +/************************************************* +* Attempt to create a directory * +*************************************************/ + +/* All the directories that Exim ever creates for itself are within the spool +directory as defined by spool_directory. We are prepared to create as many as +necessary from that directory downwards, inclusive. However, directory creation +can also be required in appendfile and sieve filters. The making function +therefore has a parent argument, below which the new directories are to go. It +can be NULL if the name is absolute. + +If a non-root uid has been specified for exim, and we are currently running as +root, ensure the directory is owned by the non-root id if the parent is the +spool directory. + +Arguments: + parent parent directory name; if NULL the name must be absolute + name directory name within the parent that we want + mode mode for the new directory + panic if TRUE, panic on failure + +Returns: panic on failure if panic is set; otherwise return FALSE; + TRUE on success. +*/ + +BOOL +directory_make(uschar *parent, uschar *name, int mode, BOOL panic) +{ +BOOL use_chown = parent == spool_directory && geteuid() == root_uid; +uschar *p, *slash; +int c = 1; +struct stat statbuf; +uschar buffer[256]; + +if (parent == NULL) + { + p = buffer + 1; + slash = parent = US""; + } +else + { + p = buffer + Ustrlen(parent); + slash = US"/"; + } + +if (!string_format(buffer, sizeof(buffer), "%s%s%s", parent, slash, name)) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "name too long in directory_make"); + +while (c != 0 && *p != 0) + { + while (*p != 0 && *p != '/') p++; + c = *p; + *p = 0; + if (Ustat(buffer, &statbuf) != 0) + { + if (mkdir(CS buffer, mode) < 0 && errno != EEXIST) + { + if (!panic) return FALSE; + log_write(0, LOG_MAIN|LOG_PANIC_DIE, + "Failed to create directory \"%s\": %s\n", buffer, strerror(errno)); + } + + /* Set the ownership if necessary. */ + + if (use_chown) Uchown(buffer, exim_uid, exim_gid); + + /* It appears that any mode bits greater than 0777 are ignored by + mkdir(), at least on some operating systems. Therefore, if the mode + contains any such bits, do an explicit mode setting. */ + + if ((mode & 0777000) != 0) Uchmod(buffer, mode); + } + *p++ = c; + } + +return TRUE; +} + +/* End of directory.c */ diff --git a/src/src/dns.c b/src/src/dns.c new file mode 100644 index 000000000..237b734a6 --- /dev/null +++ b/src/src/dns.c @@ -0,0 +1,838 @@ +/* $Cambridge: exim/src/src/dns.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Functions for interfacing with the DNS. */ + +#include "exim.h" + + +/* Function declaration needed for mutual recursion when A6 records +are supported. */ + +#if HAVE_IPV6 +#ifdef SUPPORT_A6 +static void dns_complete_a6(dns_address ***, dns_answer *, dns_record *, + int, uschar *); +#endif +#endif + + + +/************************************************* +* Initialize and configure resolver * +*************************************************/ + +/* Initialize the resolver and the storage for holding DNS answers if this is +the first time we have been here, and set the resolver options. + +Arguments: + qualify_single TRUE to set the RES_DEFNAMES option + search_parents TRUE to set the RES_DNSRCH option + +Returns: nothing +*/ + +void +dns_init(BOOL qualify_single, BOOL search_parents) +{ +if ((_res.options & RES_INIT) == 0) + { + DEBUG(D_resolver) _res.options |= RES_DEBUG; /* For Cygwin */ + res_init(); + DEBUG(D_resolver) _res.options |= RES_DEBUG; + } + +_res.options &= ~(RES_DNSRCH | RES_DEFNAMES); +_res.options |= (qualify_single? RES_DEFNAMES : 0) | + (search_parents? RES_DNSRCH : 0); +if (dns_retrans > 0) _res.retrans = dns_retrans; +if (dns_retry > 0) _res.retry = dns_retry; +} + + + +/************************************************* +* Build key name for PTR records * +*************************************************/ + +/* This function inverts an IP address and adds the relevant domain, to produce +a name that can be used to look up PTR records. + +Arguments: + string the IP address as a string + buffer a suitable buffer, long enough to hold the result + +Returns: nothing +*/ + +void +dns_build_reverse(uschar *string, uschar *buffer) +{ +uschar *p = string + Ustrlen(string); +uschar *pp = buffer; + +/* Handle IPv4 address */ + +#if HAVE_IPV6 +if (Ustrchr(string, ':') == NULL) +#endif + { + int i; + for (i = 0; i < 4; i++) + { + uschar *ppp = p; + while (ppp > string && ppp[-1] != '.') ppp--; + Ustrncpy(pp, ppp, p - ppp); + pp += p - ppp; + *pp++ = '.'; + p = ppp - 1; + } + Ustrcpy(pp, "in-addr.arpa"); + } + +/* Handle IPv6 address; convert to binary so as to fill out any +abbreviation in the textual form. */ + +#if HAVE_IPV6 +else + { + int i; + int v6[4]; + (void)host_aton(string, v6); + + /* The original specification for IPv6 reverse lookup was to invert each + nibble, and look in the ip6.int domain. The domain was subsequently + changed to ip6.arpa. */ + + for (i = 3; i >= 0; i--) + { + int j; + for (j = 0; j < 32; j += 4) + { + sprintf(CS pp, "%x.", (v6[i] >> j) & 15); + pp += 2; + } + } + Ustrcpy(pp, "ip6.arpa."); + + /* Another way of doing IPv6 reverse lookups was proposed in conjunction + with A6 records. However, it fell out of favour when they did. The + alternative was to construct a binary key, and look in ip6.arpa. I tried + to make this code do that, but I could not make it work on Solaris 8. The + resolver seems to lose the initial backslash somehow. However, now that + this style of reverse lookup has been dropped, it doesn't matter. These + lines are left here purely for historical interest. */ + + /************************************************** + Ustrcpy(pp, "\\[x"); + pp += 3; + + for (i = 0; i < 4; i++) + { + sprintf(pp, "%08X", v6[i]); + pp += 8; + } + Ustrcpy(pp, "].ip6.arpa."); + **************************************************/ + + } +#endif +} + + + + +/************************************************* +* Get next DNS record from answer block * +*************************************************/ + +/* Call this with reset == RESET_ANSWERS to scan the answer block, reset == +RESET_ADDITIONAL to scan the additional records, and reset == RESET_NEXT to +get the next record. The result is in static storage which must be copied if +it is to be preserved. + +Arguments: + dnsa pointer to dns answer block + dnss pointer to dns scan block + reset option specifing what portion to scan, as described above + +Returns: next dns record, or NULL when no more +*/ + +dns_record * +dns_next_rr(dns_answer *dnsa, dns_scan *dnss, int reset) +{ +HEADER *h = (HEADER *)dnsa->answer; +int namelen; + +/* Reset the saved data when requested to, and skip to the first required RR */ + +if (reset != RESET_NEXT) + { + dnss->rrcount = ntohs(h->qdcount); + dnss->aptr = dnsa->answer + sizeof(HEADER); + + /* Skip over questions; failure to expand the name just gives up */ + + while (dnss->rrcount-- > 0) + { + namelen = dn_expand(dnsa->answer, dnsa->answer + dnsa->answerlen, + dnss->aptr, (DN_EXPAND_ARG4_TYPE) &(dnss->srr.name), DNS_MAXNAME); + if (namelen < 0) { dnss->rrcount = 0; return NULL; } + dnss->aptr += namelen + 4; /* skip name & type & class */ + } + + /* Get the number of answer records. */ + + dnss->rrcount = ntohs(h->ancount); + + /* Skip over answers and NS records if wanting to look at the additional + records. */ + + if (reset == RESET_ADDITIONAL) + { + dnss->rrcount += ntohs(h->nscount); + while (dnss->rrcount-- > 0) + { + namelen = dn_expand(dnsa->answer, dnsa->answer + dnsa->answerlen, + dnss->aptr, (DN_EXPAND_ARG4_TYPE) &(dnss->srr.name), DNS_MAXNAME); + if (namelen < 0) { dnss->rrcount = 0; return NULL; } + dnss->aptr += namelen + 8; /* skip name, type, class & TTL */ + GETSHORT(dnss->srr.size, dnss->aptr); /* size of data portion */ + dnss->aptr += dnss->srr.size; /* skip over it */ + } + dnss->rrcount = ntohs(h->arcount); + } + } + + +/* The variable dnss->aptr is now pointing at the next RR, and dnss->rrcount +contains the number of RR records left. */ + +if (dnss->rrcount-- <= 0) return NULL; + +/* If expanding the RR domain name fails, behave as if no more records +(something safe). */ + +namelen = dn_expand(dnsa->answer, dnsa->answer + dnsa->answerlen, dnss->aptr, + (DN_EXPAND_ARG4_TYPE) &(dnss->srr.name), DNS_MAXNAME); +if (namelen < 0) { dnss->rrcount = 0; return NULL; } + +/* Move the pointer past the name and fill in the rest of the data structure +from the following bytes. */ + +dnss->aptr += namelen; +GETSHORT(dnss->srr.type, dnss->aptr); /* Record type */ +dnss->aptr += 6; /* Don't want class or TTL */ +GETSHORT(dnss->srr.size, dnss->aptr); /* Size of data portion */ +dnss->srr.data = dnss->aptr; /* The record's data follows */ +dnss->aptr += dnss->srr.size; /* Advance to next RR */ + +/* Return a pointer to the dns_record structure within the dns_answer. This is +for convenience so that the scans can use nice-looking for loops. */ + +return &(dnss->srr); +} + + + + +/************************************************* +* Turn DNS type into text * +*************************************************/ + +/* Turn the coded record type into a string for printing. + +Argument: record type +Returns: pointer to string +*/ + +uschar * +dns_text_type(int t) +{ +switch(t) + { + case T_A: return US"A"; + case T_MX: return US"MX"; + case T_AAAA: return US"AAAA"; + case T_A6: return US"A6"; + case T_TXT: return US"TXT"; + case T_PTR: return US"PTR"; + case T_SRV: return US"SRV"; + default: return US"?"; + } +} + + + +/************************************************* +* Cache a failed DNS lookup result * +*************************************************/ + +/* We cache failed lookup results so as not to experience timeouts many +times for the same domain. We need to retain the resolver options because they +may change. For successful lookups, we rely on resolver and/or name server +caching. + +Arguments: + name the domain name + type the lookup type + rc the return code + +Returns: the return code +*/ + +static int +dns_return(uschar *name, int type, int rc) +{ +tree_node *node = store_get_perm(sizeof(tree_node) + 290); +sprintf(CS node->name, "%.255s-%s-%lx", name, dns_text_type(type), + _res.options); +node->data.val = rc; +(void)tree_insertnode(&tree_dns_fails, node); +return rc; +} + + + +/************************************************* +* Do basic DNS lookup * +*************************************************/ + +/* Call the resolver to look up the given domain name, using the given type, +and check the result. The error code TRY_AGAIN is documented as meaning "non- +Authoritive Host not found, or SERVERFAIL". Sometimes there are badly set +up nameservers that produce this error continually, so there is the option of +providing a list of domains for which this is treated as a non-existent +host. + +Arguments: + dnsa pointer to dns_answer structure + name name to look up + type type of DNS record required (T_A, T_MX, etc) + +Returns: DNS_SUCCEED successful lookup + DNS_NOMATCH name not found (NXDOMAIN) + or name contains illegal characters (if checking) + DNS_NODATA domain exists, but no data for this type (NODATA) + DNS_AGAIN soft failure, try again later + DNS_FAIL DNS failure +*/ + +int +dns_basic_lookup(dns_answer *dnsa, uschar *name, int type) +{ +#ifndef STAND_ALONE +int rc; +uschar *save; +#endif + +tree_node *previous; +uschar node_name[290]; + +/* DNS lookup failures of any kind are cached in a tree. This is mainly so that +a timeout on one domain doesn't happen time and time again for messages that +have many addresses in the same domain. We rely on the resolver and name server +caching for successful lookups. */ + +sprintf(CS node_name, "%.255s-%s-%lx", name, dns_text_type(type), + _res.options); +previous = tree_search(tree_dns_fails, node_name); +if (previous != NULL) + { + DEBUG(D_dns) debug_printf("DNS lookup of %.255s-%s: using cached value %s\n", + name, dns_text_type(type), + (previous->data.val == DNS_NOMATCH)? "DNS_NOMATCH" : + (previous->data.val == DNS_NODATA)? "DNS_NODATA" : + (previous->data.val == DNS_AGAIN)? "DNS_AGAIN" : + (previous->data.val == DNS_FAIL)? "DNS_FAIL" : "??"); + return previous->data.val; + } + +/* If we are running in the test harness, recognize a couple of special +names that always give error returns. This makes it straightforward to +test the handling of DNS errors. */ + +if (running_in_test_harness) + { + uschar *endname = name + Ustrlen(name); + if (Ustrcmp(endname - 14, "test.again.dns") == 0) + { + int delay = Uatoi(name); /* digits at the start of the name */ + DEBUG(D_dns) debug_printf("Real DNS lookup of %s (%s) bypassed for testing\n", + name, dns_text_type(type)); + if (delay > 0) + { + DEBUG(D_dns) debug_printf("delaying %d seconds\n", delay); + sleep(delay); + } + DEBUG(D_dns) debug_printf("returning DNS_AGAIN\n"); + return dns_return(name, type, DNS_AGAIN); + } + if (Ustrcmp(endname - 13, "test.fail.dns") == 0) + { + DEBUG(D_dns) debug_printf("Real DNS lookup of %s (%s) bypassed for testing\n", + name, dns_text_type(type)); + DEBUG(D_dns) debug_printf("returning DNS_FAIL\n"); + return dns_return(name, type, DNS_FAIL); + } + } + +/* If configured, check the hygene of the name passed to lookup. Otherwise, +although DNS lookups may give REFUSED at the lower level, some resolvers +turn this into TRY_AGAIN, which is silly. Give a NOMATCH return, since such +domains cannot be in the DNS. The check is now done by a regular expression; +give it space for substring storage to save it having to get its own if the +regex has substrings that are used - the default uses a conditional. + +This test is omitted for PTR records. These occur only in calls from the dnsdb +lookup, which constructs the names itself, so they should be OK. Besides, +bitstring labels don't conform to normal name syntax. (But the aren't used any +more.) + +For SRV records, we omit the initial _smtp._tcp. components at the start. */ + +#ifndef STAND_ALONE /* Omit this for stand-alone tests */ + +if (check_dns_names_pattern[0] != 0 && type != T_PTR) + { + uschar *checkname = name; + int ovector[3*(EXPAND_MAXN+1)]; + + if (regex_check_dns_names == NULL) + regex_check_dns_names = + regex_must_compile(check_dns_names_pattern, FALSE, TRUE); + + /* For an SRV lookup, skip over the first two components (the service and + protocol names, which both start with an underscore). */ + + if (type == T_SRV) + { + while (*checkname++ != '.'); + while (*checkname++ != '.'); + } + + if (pcre_exec(regex_check_dns_names, NULL, CS checkname, Ustrlen(checkname), + 0, PCRE_EOPT, ovector, sizeof(ovector)/sizeof(int)) < 0) + { + DEBUG(D_dns) + debug_printf("DNS name syntax check failed: %s (%s)\n", name, + dns_text_type(type)); + host_find_failed_syntax = TRUE; + return DNS_NOMATCH; + } + } + +#endif /* STAND_ALONE */ + +/* Call the resolver; for an overlong response, res_search() will return the +number of bytes the message would need, so we need to check for this case. +The effect is to truncate overlong data. */ + +dnsa->answerlen = res_search(CS name, C_IN, type, dnsa->answer, MAXPACKET); +if (dnsa->answerlen > MAXPACKET) dnsa->answerlen = MAXPACKET; + +if (dnsa->answerlen < 0) switch (h_errno) + { + case HOST_NOT_FOUND: + DEBUG(D_dns) debug_printf("DNS lookup of %s (%s) gave HOST_NOT_FOUND\n" + "returning DNS_NOMATCH\n", name, dns_text_type(type)); + return dns_return(name, type, DNS_NOMATCH); + + case TRY_AGAIN: + DEBUG(D_dns) debug_printf("DNS lookup of %s (%s) gave TRY_AGAIN\n", + name, dns_text_type(type)); + + /* Cut this out for various test programs */ + #ifndef STAND_ALONE + save = deliver_domain; + deliver_domain = name; /* set $domain */ + rc = match_isinlist(name, &dns_again_means_nonexist, 0, NULL, NULL, + MCL_DOMAIN, TRUE, NULL); + deliver_domain = save; + if (rc != OK) + { + DEBUG(D_dns) debug_printf("returning DNS_AGAIN\n"); + return dns_return(name, type, DNS_AGAIN); + } + DEBUG(D_dns) debug_printf("%s is in dns_again_means_nonexist: returning " + "DNS_NOMATCH\n", name); + return dns_return(name, type, DNS_NOMATCH); + + #else /* For stand-alone tests */ + return dns_return(name, type, DNS_AGAIN); + #endif + + case NO_RECOVERY: + DEBUG(D_dns) debug_printf("DNS lookup of %s (%s) gave NO_RECOVERY\n" + "returning DNS_FAIL\n", name, dns_text_type(type)); + return dns_return(name, type, DNS_FAIL); + + case NO_DATA: + DEBUG(D_dns) debug_printf("DNS lookup of %s (%s) gave NO_DATA\n" + "returning DNS_NODATA\n", name, dns_text_type(type)); + return dns_return(name, type, DNS_NODATA); + + default: + DEBUG(D_dns) debug_printf("DNS lookup of %s (%s) gave unknown DNS error %d\n" + "returning DNS_FAIL\n", name, dns_text_type(type), h_errno); + return dns_return(name, type, DNS_FAIL); + } + +DEBUG(D_dns) debug_printf("DNS lookup of %s (%s) succeeded\n", + name, dns_text_type(type)); + +return DNS_SUCCEED; +} + + + + +/************************************************ +* Do a DNS lookup and handle CNAMES * +************************************************/ + +/* Look up the given domain name, using the given type. Follow CNAMEs if +necessary, but only so many times. There aren't supposed to be CNAME chains in +the DNS, but you are supposed to cope with them if you find them. + +The assumption is made that if the resolver gives back records of the +requested type *and* a CNAME, we don't need to make another call to look up +the CNAME. I can't see how it could return only some of the right records. If +it's done a CNAME lookup in the past, it will have all of them; if not, it +won't return any. + +If fully_qualified_name is not NULL, set it to point to the full name +returned by the resolver, if this is different to what it is given, unless +the returned name starts with "*" as some nameservers seem to be returning +wildcards in this form. + +Arguments: + dnsa pointer to dns_answer structure + name domain name to look up + type DNS record type (T_A, T_MX, etc) + fully_qualified_name if not NULL, return the returned name here if its + contents are different (i.e. it must be preset) + +Returns: DNS_SUCCEED successful lookup + DNS_NOMATCH name not found + DNS_NODATA no data found + DNS_AGAIN soft failure, try again later + DNS_FAIL DNS failure +*/ + +int +dns_lookup(dns_answer *dnsa, uschar *name, int type, uschar **fully_qualified_name) +{ +int i; +uschar *orig_name = name; + +/* Loop to follow CNAME chains so far, but no further... */ + +for (i = 0; i < 10; i++) + { + uschar data[256]; + dns_record *rr, cname_rr, type_rr; + dns_scan dnss; + int datalen, rc; + + /* DNS lookup failures get passed straight back. */ + + if ((rc = dns_basic_lookup(dnsa, name, type)) != DNS_SUCCEED) return rc; + + /* We should have either records of the required type, or a CNAME record, + or both. We need to know whether both exist for getting the fully qualified + name, but avoid scanning more than necessary. Note that we must copy the + contents of any rr blocks returned by dns_next_rr() as they use the same + area in the dnsa block. */ + + cname_rr.data = type_rr.data = NULL; + for (rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS); + rr != NULL; + rr = dns_next_rr(dnsa, &dnss, RESET_NEXT)) + { + if (rr->type == type) + { + if (type_rr.data == NULL) type_rr = *rr; + if (cname_rr.data != NULL) break; + } + else if (rr->type == T_CNAME) cname_rr = *rr; + } + + /* If a CNAME was found, take the fully qualified name from it; otherwise + from the first data record, if present. For testing, there is a magic name + that gets its casing adjusted, because my resolver doesn't seem to pass back + upper case letters in domain names. */ + + if (fully_qualified_name != NULL) + { + if (cname_rr.data != NULL) + { + if (Ustrcmp(cname_rr.name, *fully_qualified_name) != 0 && + cname_rr.name[0] != '*') + *fully_qualified_name = string_copy_dnsdomain(cname_rr.name); + } + else if (type_rr.data != NULL) + { + if (running_in_test_harness && + Ustrcmp(type_rr.name, "uppercase.test.ex") == 0) + *fully_qualified_name = US"UpperCase.test.ex"; + else + { + if (Ustrcmp(type_rr.name, *fully_qualified_name) != 0 && + type_rr.name[0] != '*') + *fully_qualified_name = string_copy_dnsdomain(type_rr.name); + } + } + } + + /* If any data records of the correct type were found, we are done. */ + + if (type_rr.data != NULL) return DNS_SUCCEED; + + /* If there are no data records, we need to re-scan the DNS using the + domain given in the CNAME record, which should exist (otherwise we should + have had a failure from dns_lookup). However code against the possibility of + its not existing. */ + + if (cname_rr.data == NULL) return DNS_FAIL; + datalen = dn_expand(dnsa->answer, dnsa->answer + dnsa->answerlen, + cname_rr.data, (DN_EXPAND_ARG4_TYPE)data, 256); + if (datalen < 0) return DNS_FAIL; + name = data; + } /* Loop back to do another lookup */ + +/*Control reaches here after 10 times round the CNAME loop. Something isn't +right... */ + +log_write(0, LOG_MAIN, "CNAME loop for %s encountered", orig_name); +return DNS_FAIL; +} + + + +/* Support for A6 records has been commented out since they were demoted to +experimental status at IETF 51. */ + +#if HAVE_IPV6 && defined(SUPPORT_A6) + +/************************************************* +* Search DNS block for prefix RRs * +*************************************************/ + +/* Called from dns_complete_a6() to search an additional section or a main +answer section for required prefix records to complete an IPv6 address obtained +from an A6 record. For each prefix record, a recursive call to dns_complete_a6 +is made, with a new copy of the address so far. + +Arguments: + dnsa the DNS answer block + which RESET_ADDITIONAL or RESET_ANSWERS + name name of prefix record + yptrptr pointer to the pointer that points to where to hang the next + dns_address structure + bits number of bits we have already got + bitvec the bits we have already got + +Returns: TRUE if any records were found +*/ + +static BOOL +dns_find_prefix(dns_answer *dnsa, int which, uschar *name, dns_address + ***yptrptr, int bits, uschar *bitvec) +{ +BOOL yield = FALSE; +dns_record *rr; +dns_scan dnss; + +for (rr = dns_next_rr(dnsa, &dnss, which); + rr != NULL; + rr = dns_next_rr(dnsa, &dnss, RESET_NEXT)) + { + uschar cbitvec[16]; + if (rr->type != T_A6 || strcmpic(rr->name, name) != 0) continue; + yield = TRUE; + memcpy(cbitvec, bitvec, sizeof(cbitvec)); + dns_complete_a6(yptrptr, dnsa, rr, bits, cbitvec); + } + +return yield; +} + + + +/************************************************* +* Follow chains of A6 records * +*************************************************/ + +/* A6 records may be incomplete, with pointers to other records containing more +bits of the address. There can be a tree structure, leading to a number of +addresses originating from a single initial A6 record. + +Arguments: + yptrptr pointer to the pointer that points to where to hang the next + dns_address structure + dnsa the current DNS answer block + rr the RR we have at present + bits number of bits we have already got + bitvec the bits we have already got + +Returns: nothing +*/ + +static void +dns_complete_a6(dns_address ***yptrptr, dns_answer *dnsa, dns_record *rr, + int bits, uschar *bitvec) +{ +static uschar bitmask[] = { 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80 }; +uschar *p = (uschar *)(rr->data); +int prefix_len, suffix_len; +int i, j, k; +uschar *chainptr; +uschar chain[264]; +dns_answer cdnsa; + +/* The prefix length is the first byte. It defines the prefix which is missing +from the data in this record as a number of bits. Zero means this is the end of +a chain. The suffix is the data in this record; only sufficient bytes to hold +it are supplied. There may be zero bytes. We have to ignore trailing bits that +we have already obtained from earlier RRs in the chain. */ + +prefix_len = *p++; /* bits */ +suffix_len = (128 - prefix_len + 7)/8; /* bytes */ + +/* If the prefix in this record is greater than the prefix in the previous +record in the chain, we have to ignore the record (RFC 2874). */ + +if (prefix_len > 128 - bits) return; + +/* In this little loop, the number of bits up to and including the current byte +is held in k. If we have none of the bits in this byte, we can just or it into +the current data. If we have all of the bits in this byte, we skip it. +Otherwise, some masking has to be done. */ + +for (i = suffix_len - 1, j = 15, k = 8; i >= 0; i--) + { + int required = k - bits; + if (required >= 8) bitvec[j] |= p[i]; + else if (required > 0) bitvec[j] |= p[i] & bitmask[required]; + j--; /* I tried putting these in the "for" statement, but gcc muttered */ + k += 8; /* about computed values not being used. */ + } + +/* If the prefix_length is zero, we are at the end of a chain. Build a +dns_address item with the current data, hang it onto the end of the chain, +adjust the hanging pointer, and we are done. */ + +if (prefix_len == 0) + { + dns_address *new = store_get(sizeof(dns_address) + 50); + inet_ntop(AF_INET6, bitvec, CS new->address, 50); + new->next = NULL; + **yptrptr = new; + *yptrptr = &(new->next); + return; + } + +/* Prefix length is not zero. Reset the number of bits that we have collected +so far, and extract the chain name. */ + +bits = 128 - prefix_len; +p += suffix_len; + +chainptr = chain; +while ((i = *p++) != 0) + { + if (chainptr != chain) *chainptr++ = '.'; + memcpy(chainptr, p, i); + chainptr += i; + p += i; + } +*chainptr = 0; +chainptr = chain; + +/* Now scan the current DNS response record to see if the additional section +contains the records we want. This processing can be cut out for testing +purposes. */ + +if (dns_find_prefix(dnsa, RESET_ADDITIONAL, chainptr, yptrptr, bits, bitvec)) + return; + +/* No chain records were found in the current DNS response block. Do a new DNS +lookup to try to find these records. This opens up the possibility of DNS +failures. We ignore them at this point; if all branches of the tree fail, there +will be no addresses at the end. */ + +if (dns_lookup(&cdnsa, chainptr, T_A6, NULL) == DNS_SUCCEED) + (void)dns_find_prefix(&cdnsa, RESET_ANSWERS, chainptr, yptrptr, bits, bitvec); +} +#endif /* HAVE_IPV6 && defined(SUPPORT_A6) */ + + + + +/************************************************* +* Get address(es) from DNS record * +*************************************************/ + +/* The record type is either T_A for an IPv4 address or T_AAAA (or T_A6 when +supported) for an IPv6 address. In the A6 case, there may be several addresses, +generated by following chains. A recursive function does all the hard work. A6 +records now look like passing into history, so the code is only included when +explicitly asked for. + +Argument: + dnsa the DNS answer block + rr the RR + +Returns: pointer a chain of dns_address items +*/ + +dns_address * +dns_address_from_rr(dns_answer *dnsa, dns_record *rr) +{ +dns_address *yield = NULL; + +#if HAVE_IPV6 && defined(SUPPORT_A6) +dns_address **yieldptr = &yield; +uschar bitvec[16]; +#else +dnsa = dnsa; /* Stop picky compilers warning */ +#endif + +if (rr->type == T_A) + { + uschar *p = (uschar *)(rr->data); + yield = store_get(sizeof(dns_address) + 20); + (void)sprintf(CS yield->address, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]); + yield->next = NULL; + } + +#if HAVE_IPV6 + +#ifdef SUPPORT_A6 +else if (rr->type == T_A6) + { + memset(bitvec, 0, sizeof(bitvec)); + dns_complete_a6(&yieldptr, dnsa, rr, 0, bitvec); + } +#endif /* SUPPORT_A6 */ + +else + { + yield = store_get(sizeof(dns_address) + 50); + inet_ntop(AF_INET6, (uschar *)(rr->data), CS yield->address, 50); + yield->next = NULL; + } +#endif /* HAVE_IPV6 */ + +return yield; +} + +/* End of dns.c */ diff --git a/src/src/drtables.c b/src/src/drtables.c new file mode 100644 index 000000000..cd3ea7181 --- /dev/null +++ b/src/src/drtables.c @@ -0,0 +1,809 @@ +/* $Cambridge: exim/src/src/drtables.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + + +#include "exim.h" + + +/* This module contains tables that define the lookup methods and drivers +that are actually included in the binary. Its contents are controlled by +various macros in config.h that ultimately come from Local/Makefile. They are +all described in src/EDITME. */ + + +/* The OSF1 (Digital Unix) linker puts out a worrying warning if any sections +contain no executable code. It says + +Warning: Linking some objects which contain exception information sections + and some which do not. This may cause fatal runtime exception handling + problems. + +As this may cause people to worry needlessly, include a dummy function here +to stop the message from appearing. Make it call itself to stop picky compilers +compilers complaining that it is unused, and put in a dummy argument to stop +even pickier compilers complaining about infinite loops. */ + +static void dummy(int x) { dummy(x-1); } + + +/* Table of information about all possible lookup methods. The entries are +always present, but the "open" and "find" functions are set to NULL for those +that are not compiled into the binary. The "check" and "close" functions can +be NULL for methods that don't need them. */ + +#ifdef LOOKUP_CDB +#include "lookups/cdb.h" +#endif + +#ifdef LOOKUP_DBM +#include "lookups/dbmdb.h" +#endif + +#ifdef LOOKUP_DNSDB +#include "lookups/dnsdb.h" +#endif + +#ifdef LOOKUP_DSEARCH +#include "lookups/dsearch.h" +#endif + +#ifdef LOOKUP_IBASE +#include "lookups/ibase.h" +#endif + +#ifdef LOOKUP_LDAP +#include "lookups/ldap.h" +#endif + +#ifdef LOOKUP_LSEARCH +#include "lookups/lsearch.h" +#endif + +#ifdef LOOKUP_MYSQL +#include "lookups/mysql.h" +#endif + +#ifdef LOOKUP_NIS +#include "lookups/nis.h" +#endif + +#ifdef LOOKUP_NISPLUS +#include "lookups/nisplus.h" +#endif + +#ifdef LOOKUP_ORACLE +#include "lookups/oracle.h" +#endif + +#ifdef LOOKUP_PASSWD +#include "lookups/passwd.h" +#endif + +#ifdef LOOKUP_PGSQL +#include "lookups/pgsql.h" +#endif + +#ifdef LOOKUP_TESTDB +#include "lookups/testdb.h" +#endif + +#ifdef LOOKUP_WHOSON +#include "lookups/whoson.h" +#endif + +/* The second field in each item below is a set of bit flags: + + lookup_querystyle => this is a query-style lookup, + else single-key (+ file) style + lookup_absfile => an absolute file name is required, + (for single-key style only) + +This list must be in alphabetical order of lookup name because it is +searched by binary chop, having got rather large for the original linear +searching. */ + +lookup_info lookup_list[] = { + +/* cdb lookup in single file */ + + { + US"cdb", /* lookup name */ + lookup_absfile, /* uses absolute file name */ +#ifdef LOOKUP_CDB + cdb_open, /* open function */ + cdb_check, /* check function */ + cdb_find, /* find function */ + cdb_close, /* close function */ + NULL, /* no tidy function */ + NULL /* no quoting function */ +#else + NULL, NULL, NULL, NULL, NULL, NULL /* lookup not present */ +#endif + }, + +/* DBM file lookup; called "dbm" because that is the name in Exim, +but the code is called dbmdb to avoid name clashes. */ + + { + US"dbm", /* lookup name */ + lookup_absfile, /* uses absolute file name */ +#ifdef LOOKUP_DBM + dbmdb_open, /* open function */ + dbmdb_check, /* check function */ + dbmdb_find, /* find function */ + dbmdb_close, /* close function */ + NULL, /* no tidy function */ + NULL /* no quoting function */ +#else + NULL, NULL, NULL, NULL, NULL, NULL /* lookup not present */ +#endif + }, + +/* This variant of DBM does not include the binary zero on the end +of the key strings. */ + + { + US"dbmnz", /* lookup name */ + lookup_absfile, /* uses absolute file name */ +#ifdef LOOKUP_DBM + dbmdb_open, /* sic */ /* open function */ + dbmdb_check, /* sic */ /* check function */ + dbmnz_find, /* find function */ + dbmdb_close, /* sic */ /* close function */ + NULL, /* no tidy function */ + NULL /* no quoting function */ +#else + NULL, NULL, NULL, NULL, NULL, NULL /* lookup not present */ +#endif + }, + +/* Using DNS TXT records as a database */ + + { + US"dnsdb", /* lookup name */ + lookup_querystyle, /* query style */ +#ifdef LOOKUP_DNSDB + dnsdb_open, /* open function */ + NULL, /* check function */ + dnsdb_find, /* find function */ + NULL, /* no close function */ + NULL, /* no tidy function */ + NULL /* no quoting function */ +#else + NULL, NULL, NULL, NULL, NULL, NULL /* lookup not present */ +#endif + }, + +/* Search of files in a directory */ + + { + US"dsearch", /* lookup name */ + lookup_absfile, /* uses absolute file name */ +#ifdef LOOKUP_DSEARCH + dsearch_open, /* open function */ + dsearch_check, /* check function */ + dsearch_find, /* find function */ + dsearch_close, /* close function */ + NULL, /* no tidy function */ + NULL /* no quoting function */ +#else + NULL, NULL, NULL, NULL, NULL, NULL /* lookup not present */ +#endif + }, + +/* Interbase lookup */ + + { + US"ibase", /* lookup name */ + lookup_querystyle, /* query-style lookup */ +#ifdef LOOKUP_IBASE + ibase_open, /* open function */ + NULL, /* no check function */ + ibase_find, /* find function */ + NULL, /* no close function */ + ibase_tidy, /* tidy function */ + ibase_quote /* quoting function */ +#else + NULL, NULL, NULL, NULL, NULL, NULL /* lookup not present */ +#endif + }, + +/* Linear search of single file with ip-addresses and networks; shares many +functions with lsearch. */ + + { + US"iplsearch", /* lookup name */ + lookup_absfile, /* uses absolute file name */ +#ifdef LOOKUP_LSEARCH + lsearch_open, /* open function */ + lsearch_check, /* check function */ + iplsearch_find, /* find function */ + lsearch_close, /* close function */ + NULL, /* no tidy function */ + NULL /* no quoting function */ +#else + NULL, NULL, NULL, NULL, NULL, NULL /* lookup not present */ +#endif + }, + +/* LDAP lookup, allowing data from only one entry to be returned */ + + { + US"ldap", /* lookup name */ + lookup_querystyle, /* query-style lookup */ +#ifdef LOOKUP_LDAP + eldap_open, /* open function */ + NULL, /* check function */ + eldap_find, /* find function */ + NULL, /* no close function */ + eldap_tidy, /* tidy function */ + eldap_quote /* quoting function */ +#else + NULL, NULL, NULL, NULL, NULL, NULL /* lookup not present */ +#endif + }, + +/* LDAP lookup, allowing the DN from more one entry to be returned */ + + { + US"ldapdn", /* lookup name */ + lookup_querystyle, /* query-style lookup */ +#ifdef LOOKUP_LDAP + eldap_open, /* sic */ /* open function */ + NULL, /* check function */ + eldapdn_find, /* find function */ + NULL, /* no close function */ + eldap_tidy, /* sic */ /* tidy function */ + eldap_quote /* sic */ /* quoting function */ +#else + NULL, NULL, NULL, NULL, NULL, NULL /* lookup not present */ +#endif + }, + +/* LDAP lookup, allowing data from more than one entry to be returned */ + + { + US"ldapm", /* lookup name */ + lookup_querystyle, /* query-style lookup */ +#ifdef LOOKUP_LDAP + eldap_open, /* sic */ /* open function */ + NULL, /* check function */ + eldapm_find, /* find function */ + NULL, /* no close function */ + eldap_tidy, /* sic */ /* tidy function */ + eldap_quote /* sic */ /* quoting function */ +#else + NULL, NULL, NULL, NULL, NULL, NULL /* lookup not present */ +#endif + }, + +/* Linear search of single file */ + + { + US"lsearch", /* lookup name */ + lookup_absfile, /* uses absolute file name */ +#ifdef LOOKUP_LSEARCH + lsearch_open, /* open function */ + lsearch_check, /* check function */ + lsearch_find, /* find function */ + lsearch_close, /* close function */ + NULL, /* no tidy function */ + NULL /* no quoting function */ +#else + NULL, NULL, NULL, NULL, NULL, NULL /* lookup not present */ +#endif + }, + +/* MYSQL lookup */ + + { + US"mysql", /* lookup name */ + lookup_querystyle, /* query-style lookup */ +#ifdef LOOKUP_MYSQL + mysql_open, /* open function */ + NULL, /* no check function */ + mysql_find, /* find function */ + NULL, /* no close function */ + mysql_tidy, /* tidy function */ + mysql_quote /* quoting function */ +#else + NULL, NULL, NULL, NULL, NULL, NULL /* lookup not present */ +#endif + }, + +/* NIS lookup, excluding trailing 0 from key */ + + { + US"nis", /* lookup name */ + 0, /* not abs file, not query style*/ +#ifdef LOOKUP_NIS + nis_open, /* open function */ + NULL, /* check function */ + nis_find, /* find function */ + NULL, /* no close function */ + NULL, /* no tidy function */ + NULL /* no quoting function */ +#else + NULL, NULL, NULL, NULL, NULL, NULL /* lookup not present */ +#endif + }, + +/* NIS lookup, including trailing 0 in key */ + + { + US"nis0", /* lookup name */ + 0, /* not absfile, not query style */ +#ifdef LOOKUP_NIS + nis_open, /* sic */ /* open function */ + NULL, /* check function */ + nis0_find, /* find function */ + NULL, /* no close function */ + NULL, /* no tidy function */ + NULL /* no quoting function */ +#else + NULL, NULL, NULL, NULL, NULL, NULL /* lookup not present */ +#endif + }, + +/* NIS+ lookup */ + + { + US"nisplus", /* lookup name */ + lookup_querystyle, /* query-style lookup */ +#ifdef LOOKUP_NISPLUS + nisplus_open, /* open function */ + NULL, /* check function */ + nisplus_find, /* find function */ + NULL, /* no close function */ + NULL, /* no tidy function */ + nisplus_quote /* quoting function */ +#else + NULL, NULL, NULL, NULL, NULL, NULL /* lookup not present */ +#endif + }, + +/* Linear search of single file, with wildcarding but no pattern expansion. +Shares many functions with lsearch. */ + + { + US"nwildlsearch", /* lookup name */ + lookup_absfile, /* uses absolute file name */ +#ifdef LOOKUP_LSEARCH + lsearch_open, /* open function */ + lsearch_check, /* check function */ + nwildlsearch_find, /* find function */ + lsearch_close, /* close function */ + NULL, /* no tidy function */ + NULL /* no quoting function */ +#else + NULL, NULL, NULL, NULL, NULL, NULL /* lookup not present */ +#endif + }, + +/* Oracle lookup */ + + { + US"oracle", /* lookup name */ + lookup_querystyle, /* query-style lookup */ +#ifdef LOOKUP_ORACLE + oracle_open, /* open function */ + NULL, /* check function */ + oracle_find, /* find function */ + NULL, /* no close function */ + oracle_tidy, /* tidy function */ + oracle_quote /* quoting function */ +#else + NULL, NULL, NULL, NULL, NULL, NULL /* lookup not present */ +#endif + }, + +/* passwd lookup */ + + { + US"passwd", /* lookup name */ + lookup_querystyle, /* query-style lookup */ +#ifdef LOOKUP_PASSWD + passwd_open, /* open function */ + NULL, /* no check function */ + passwd_find, /* find function */ + NULL, /* no close function */ + NULL, /* no tidy function */ + NULL /* no quoting function */ +#else + NULL, NULL, NULL, NULL, NULL, NULL /* lookup not present */ +#endif + }, + +/* PGSQL lookup */ + + { + US"pgsql", /* lookup name */ + lookup_querystyle, /* query-style lookup */ +#ifdef LOOKUP_PGSQL + pgsql_open, /* open function */ + NULL, /* no check function */ + pgsql_find, /* find function */ + NULL, /* no close function */ + pgsql_tidy, /* tidy function */ + pgsql_quote /* quoting function */ +#else + NULL, NULL, NULL, NULL, NULL, NULL /* lookup not present */ +#endif + }, + +/* Testdb lookup is for testing Exim, not useful for normal running. +For that reason, we omit the entry entirely when not building it into +the binary, so that attempts to use it give "unknown lookup type" instead +of "lookup type not available". */ + +#ifdef LOOKUP_TESTDB + { + US"testdb", /* lookup name */ + lookup_querystyle, /* query-style lookup */ + testdb_open, /* open function */ + NULL, /* check function */ + testdb_find, /* find function */ + NULL, /* no close function */ + NULL, /* no tidy function */ + NULL /* no quoting function */ + }, +#endif + +/* "Whoson" lookup */ + + { + US"whoson", /* lookup name */ + lookup_querystyle, /* query-style lookup */ +#ifdef LOOKUP_WHOSON + whoson_open, /* open function */ + NULL, /* check function */ + whoson_find, /* find function */ + NULL, /* no close function */ + NULL, /* no tidy function */ + NULL /* no quoting function */ +#else + NULL, NULL, NULL, NULL, NULL, NULL /* lookup not present */ +#endif + }, + +/* Linear search of single file, with wildcarding and pattern expansion. Shares +many functions with lsearch. */ + + { + US"wildlsearch", /* lookup name */ + lookup_absfile, /* uses absolute file name */ +#ifdef LOOKUP_LSEARCH + lsearch_open, /* open function */ + lsearch_check, /* check function */ + wildlsearch_find, /* find function */ + lsearch_close, /* close function */ + NULL, /* no tidy function */ + NULL /* no quoting function */ +#else + NULL, NULL, NULL, NULL, NULL, NULL /* lookup not present */ +#endif + } +}; + +/* Number of entries in the list */ + +int lookup_list_count = sizeof(lookup_list)/sizeof(lookup_info); + + + +/* Table of information about all possible authentication mechamisms. All +entries are always present if any mechanism is declared, but the functions are +set to NULL for those that are not compiled into the binary. */ + +#ifdef AUTH_CRAM_MD5 +#include "auths/cram_md5.h" +#endif + +#ifdef AUTH_CYRUS_SASL +#include "auths/cyrus_sasl.h" +#endif + +#ifdef AUTH_PLAINTEXT +#include "auths/plaintext.h" +#endif + +#ifdef AUTH_SPA +#include "auths/spa.h" +#endif + +auth_info auths_available[] = { + +/* Checking by an expansion condition on plain text */ + +#ifdef AUTH_CRAM_MD5 + { + US"cram_md5", /* lookup name */ + auth_cram_md5_options, + &auth_cram_md5_options_count, + &auth_cram_md5_option_defaults, + sizeof(auth_cram_md5_options_block), + auth_cram_md5_init, /* init function */ + auth_cram_md5_server, /* server function */ + auth_cram_md5_client /* client function */ + }, +#endif + +#ifdef AUTH_CYRUS_SASL + { + US"cyrus_sasl", /* lookup name */ + auth_cyrus_sasl_options, + &auth_cyrus_sasl_options_count, + &auth_cyrus_sasl_option_defaults, + sizeof(auth_cyrus_sasl_options_block), + auth_cyrus_sasl_init, /* init function */ + auth_cyrus_sasl_server, /* server function */ + NULL /* client function */ + }, +#endif + +#ifdef AUTH_PLAINTEXT + { + US"plaintext", /* lookup name */ + auth_plaintext_options, + &auth_plaintext_options_count, + &auth_plaintext_option_defaults, + sizeof(auth_plaintext_options_block), + auth_plaintext_init, /* init function */ + auth_plaintext_server, /* server function */ + auth_plaintext_client /* client function */ + }, +#endif + +#ifdef AUTH_SPA + { + US"spa", /* lookup name */ + auth_spa_options, + &auth_spa_options_count, + &auth_spa_option_defaults, + sizeof(auth_spa_options_block), + auth_spa_init, /* init function */ + auth_spa_server, /* server function */ + auth_spa_client /* client function */ + }, +#endif + +{ US"", NULL, NULL, NULL, 0, NULL, NULL, NULL } +}; + + +/* Tables of information about which routers and transports are included in the +exim binary. */ + +/* Pull in the necessary header files */ + +#include "routers/rf_functions.h" + +#ifdef ROUTER_ACCEPT +#include "routers/accept.h" +#endif + +#ifdef ROUTER_DNSLOOKUP +#include "routers/dnslookup.h" +#endif + +#ifdef ROUTER_MANUALROUTE +#include "routers/manualroute.h" +#endif + +#ifdef ROUTER_IPLITERAL +#include "routers/ipliteral.h" +#endif + +#ifdef ROUTER_IPLOOKUP +#include "routers/iplookup.h" +#endif + +#ifdef ROUTER_QUERYPROGRAM +#include "routers/queryprogram.h" +#endif + +#ifdef ROUTER_REDIRECT +#include "routers/redirect.h" +#endif + +#ifdef TRANSPORT_APPENDFILE +#include "transports/appendfile.h" +#endif + +#ifdef TRANSPORT_AUTOREPLY +#include "transports/autoreply.h" +#endif + +#ifdef TRANSPORT_LMTP +#include "transports/lmtp.h" +#endif + +#ifdef TRANSPORT_PIPE +#include "transports/pipe.h" +#endif + +#ifdef TRANSPORT_SMTP +#include "transports/smtp.h" +#endif + + +/* Now set up the structures, terminated by an entry with a null name. */ + +router_info routers_available[] = { +#ifdef ROUTER_ACCEPT + { + US"accept", + accept_router_options, + &accept_router_options_count, + &accept_router_option_defaults, + sizeof(accept_router_options_block), + accept_router_init, + accept_router_entry, + NULL, /* no tidyup entry */ + ri_yestransport + }, +#endif +#ifdef ROUTER_DNSLOOKUP + { + US"dnslookup", + dnslookup_router_options, + &dnslookup_router_options_count, + &dnslookup_router_option_defaults, + sizeof(dnslookup_router_options_block), + dnslookup_router_init, + dnslookup_router_entry, + NULL, /* no tidyup entry */ + ri_yestransport + }, +#endif +#ifdef ROUTER_IPLITERAL + { + US"ipliteral", + ipliteral_router_options, + &ipliteral_router_options_count, + &ipliteral_router_option_defaults, + sizeof(ipliteral_router_options_block), + ipliteral_router_init, + ipliteral_router_entry, + NULL, /* no tidyup entry */ + ri_yestransport + }, +#endif +#ifdef ROUTER_IPLOOKUP + { + US"iplookup", + iplookup_router_options, + &iplookup_router_options_count, + &iplookup_router_option_defaults, + sizeof(iplookup_router_options_block), + iplookup_router_init, + iplookup_router_entry, + NULL, /* no tidyup entry */ + ri_notransport + }, +#endif +#ifdef ROUTER_MANUALROUTE + { + US"manualroute", + manualroute_router_options, + &manualroute_router_options_count, + &manualroute_router_option_defaults, + sizeof(manualroute_router_options_block), + manualroute_router_init, + manualroute_router_entry, + NULL, /* no tidyup entry */ + 0 + }, +#endif +#ifdef ROUTER_QUERYPROGRAM + { + US"queryprogram", + queryprogram_router_options, + &queryprogram_router_options_count, + &queryprogram_router_option_defaults, + sizeof(queryprogram_router_options_block), + queryprogram_router_init, + queryprogram_router_entry, + NULL, /* no tidyup entry */ + 0 + }, +#endif +#ifdef ROUTER_REDIRECT + { + US"redirect", + redirect_router_options, + &redirect_router_options_count, + &redirect_router_option_defaults, + sizeof(redirect_router_options_block), + redirect_router_init, + redirect_router_entry, + NULL, /* no tidyup entry */ + ri_notransport + }, +#endif +{ US"", NULL, NULL, NULL, 0, NULL, NULL, NULL, 0 } +}; + + + +transport_info transports_available[] = { +#ifdef TRANSPORT_APPENDFILE + { + US"appendfile", /* driver name */ + appendfile_transport_options, /* local options table */ + &appendfile_transport_options_count, /* number of entries */ + &appendfile_transport_option_defaults, /* private options defaults */ + sizeof(appendfile_transport_options_block), /* size of private block */ + appendfile_transport_init, /* init entry point */ + appendfile_transport_entry, /* main entry point */ + NULL, /* no tidyup entry */ + NULL, /* no closedown entry */ + TRUE, /* local flag */ + }, +#endif +#ifdef TRANSPORT_AUTOREPLY + { + US"autoreply", /* driver name */ + autoreply_transport_options, /* local options table */ + &autoreply_transport_options_count, /* number of entries */ + &autoreply_transport_option_defaults, /* private options defaults */ + sizeof(autoreply_transport_options_block), /* size of private block */ + autoreply_transport_init, /* init entry point */ + autoreply_transport_entry, /* main entry point */ + NULL, /* no tidyup entry */ + NULL, /* no closedown entry */ + TRUE /* local flag */ + }, +#endif +#ifdef TRANSPORT_LMTP + { + US"lmtp", /* driver name */ + lmtp_transport_options, /* local options table */ + &lmtp_transport_options_count, /* number of entries */ + &lmtp_transport_option_defaults, /* private options defaults */ + sizeof(lmtp_transport_options_block), /* size of private block */ + lmtp_transport_init, /* init entry point */ + lmtp_transport_entry, /* main entry point */ + NULL, /* no tidyup entry */ + NULL, /* no closedown entry */ + TRUE /* local flag */ + }, +#endif +#ifdef TRANSPORT_PIPE + { + US"pipe", /* driver name */ + pipe_transport_options, /* local options table */ + &pipe_transport_options_count, /* number of entries */ + &pipe_transport_option_defaults, /* private options defaults */ + sizeof(pipe_transport_options_block), /* size of private block */ + pipe_transport_init, /* init entry point */ + pipe_transport_entry, /* main entry point */ + NULL, /* no tidyup entry */ + NULL, /* no closedown entry */ + TRUE /* local flag */ + }, +#endif +#ifdef TRANSPORT_SMTP + { + US"smtp", /* driver name */ + smtp_transport_options, /* local options table */ + &smtp_transport_options_count, /* number of entries */ + &smtp_transport_option_defaults, /* private options defaults */ + sizeof(smtp_transport_options_block), /* size of private block */ + smtp_transport_init, /* init entry point */ + smtp_transport_entry, /* main entry point */ + NULL, /* no tidyup entry */ + smtp_transport_closedown, /* close down passed channel */ + FALSE /* local flag */ + }, +#endif +{ US"", NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, FALSE } +}; + +/* End of drtables.c */ diff --git a/src/src/dummies.c b/src/src/dummies.c new file mode 100644 index 000000000..c06202004 --- /dev/null +++ b/src/src/dummies.c @@ -0,0 +1,150 @@ +/* $Cambridge: exim/src/src/dummies.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* This file is not part of the main Exim code. There are little bits of test +code for some of Exim's modules, and when they are used, the module they are +testing may call other main Exim functions that are not available and/or +should not be used in a test. The classic case is log_write(). This module +contains dummy versions of such functions - well not really dummies, more like +alternates. */ + +#include +#include +#include +#include + +/* We don't have the full Exim headers dragged in, but this function +is used for debugging output. */ + +extern int string_vformat(char *, int, char *, va_list); + + +/************************************************* +* Handle calls to write the log * +*************************************************/ + +/* The message gets written to stderr when log_write() is called from a +utility. The message always gets '\n' added on the end of it. + +Arguments: + selector not relevant when running a utility + flags not relevant when running a utility + format a printf() format + ... arguments for format + +Returns: nothing +*/ + +void +log_write(unsigned int selector, int flags, char *format, ...) +{ +va_list ap; +va_start(ap, format); +vfprintf(stderr, format, ap); +fprintf(stderr, "\n"); +va_end(ap); +selector = selector; /* Keep picky compilers happy */ +flags = flags; +} + + +/************************************************* +* Handle calls to print debug output * +*************************************************/ + +/* The message just gets written to stderr + +Arguments: + format a printf() format + ... arguments for format + +Returns: nothing +*/ + +void +debug_printf(char *format, ...) +{ +va_list ap; +char buffer[1024]; + +va_start(ap, format); + +if (!string_vformat(buffer, sizeof(buffer), format, ap)) + { + char *s = "**** debug string overflowed buffer ****\n"; + char *p = buffer + (int)strlen(buffer); + int maxlen = sizeof(buffer) - (int)strlen(s) - 3; + if (p > buffer + maxlen) p = buffer + maxlen; + if (p > buffer && p[-1] != '\n') *p++ = '\n'; + strcpy(p, s); + } + +fprintf(stderr, "%s", buffer); +fflush(stderr); +va_end(ap); +} + + + +/************************************************* +* SIGALRM handler * +*************************************************/ + +extern int sigalrm_seen; + +void +sigalrm_handler(int sig) +{ +sig = sig; /* Keep picky compilers happy */ +sigalrm_seen = 1; +} + + + +/************************************************* +* Complete Dummies * +*************************************************/ + +int +header_checkname(void *h, char *name, int len) +{ +h = h; /* Keep picky compilers happy */ +name = name; +len = len; +return 0; +} + +void +directory_make(char *parent, char *name, int mode, int panic) +{ +parent = parent; /* Keep picky compilers happy */ +name = name; +mode = mode; +panic = panic; +} + +void +host_build_sender_fullhost(void) { } + +/* This one isn't needed for test_host */ + +#ifndef TEST_HOST +char * +host_ntoa(int type, const void *arg, char *buffer, int *portptr) +{ +type = type; /* Keep picky compilers happy */ +arg = arg; +buffer = buffer; +portptr = portptr; +return NULL; +} +#endif + + +/* End of dummies.c */ diff --git a/src/src/enq.c b/src/src/enq.c new file mode 100644 index 000000000..dc9be759c --- /dev/null +++ b/src/src/enq.c @@ -0,0 +1,102 @@ +/* $Cambridge: exim/src/src/enq.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Functions concerned with serialization. */ + + +#include "exim.h" + + + + +/************************************************* +* Test for host or ETRN serialization * +*************************************************/ + +/* This function is called when a host is listed for serialization of +connections. It is also called when ETRN is listed for serialization. We open +the misc database and look for a record, which implies an existing connection +or ETRN run. If not found, create one and return TRUE. + +Arguments: + key string on which to serialize + +Returns: TRUE if OK to proceed; FALSE otherwise +*/ + + +BOOL +enq_start(uschar *key) +{ +dbdata_serialize *serial_record; +dbdata_serialize new_record; +open_db dbblock; +open_db *dbm_file; + +DEBUG(D_transport) debug_printf("check serialized: %s\n", key); + +/* Open and lock the waiting information database. The absence of O_CREAT is +deliberate; the dbfn_open() function - which is an Exim function - always tries +to create if it can't open a read/write file. It expects only O_RDWR or +O_RDONLY as its argument. */ + +dbm_file = dbfn_open(US"misc", O_RDWR, &dbblock, TRUE); +if (dbm_file == NULL) return FALSE; + +/* See if there is a record for this host or queue run; if there is, we cannot +proceed with the connection unless the record is very old. */ + +serial_record = dbfn_read(dbm_file, key); +if (serial_record != NULL && time(NULL) - serial_record->time_stamp < 6*60*60) + { + dbfn_close(dbm_file); + DEBUG(D_transport) debug_printf("outstanding serialization record for %s\n", + key); + return FALSE; + } + +/* We can proceed - insert a new record or update the old one. At present +the count field is not used; just set it to 1. */ + +new_record.count = 1; +dbfn_write(dbm_file, key, &new_record, (int)sizeof(dbdata_serialize)); +dbfn_close(dbm_file); +return TRUE; +} + + + +/************************************************* +* Release serialization * +*************************************************/ + +/* This function is called when a serialized host's connection or serialized +ETRN queue run ends. We open the relevant database and delete its record. + +Arguments: + key the serialization key + +Returns: nothing +*/ + +void +enq_end(uschar *key) +{ +open_db dbblock; +open_db *dbm_file; + +DEBUG(D_transport) debug_printf("end serialized: %s\n", key); + +dbm_file = dbfn_open(US"misc", O_RDWR, &dbblock, TRUE); +if (dbm_file == NULL) return; +dbfn_delete(dbm_file, key); +dbfn_close(dbm_file); +} + +/* End of enq.c */ diff --git a/src/src/exicyclog.src b/src/src/exicyclog.src new file mode 100644 index 000000000..4762f21f7 --- /dev/null +++ b/src/src/exicyclog.src @@ -0,0 +1,254 @@ +#! /bin/sh +# $Cambridge: exim/src/src/exicyclog.src,v 1.1 2004/10/07 10:39:01 ph10 Exp $ + +# Copyright (c) 2004 University of Cambridge. +# See the file NOTICE for conditions of use and distribution. + +# Except when they appear in comments, the following placeholders in this +# source are replaced when it is turned into a runnable script: +# +# CONFIGURE_FILE_USE_NODE +# CONFIGURE_FILE_USE_EUID +# CONFIGURE_FILE +# BIN_DIRECTORY +# EXICYCLOG_MAX +# COMPRESS_COMMAND +# COMPRESS_SUFFIX +# CHOWN_COMMAND +# CHGRP_COMMAND +# MV_COMMAND +# RM_COMMAND + +# PROCESSED_FLAG + +# This is a shell script for cycling exim main and reject log files. Each time +# it is run, the files get "shuffled down" by one, the current one (e.g. +# mainlog) becoming mainlog.01, the previous mainlog.01 becoming mainlog.02, +# and so on, up to the limit configured here. The same happens to the reject +# logs. All those with numbers greater than 1 are compressed. + +# This script should be called regularly (e.g. daily) by a root crontab +# entry of the form + +# 1 0 * * * /opt/exim/bin/exicyclog + +# The following lines are generated from Exim's configuration file when +# this source is built into a script, but you can subsequently edit them +# without rebuilding things, as long are you are careful not to overwrite +# the script in the next Exim rebuild/install. "Keep" is the number of old log +# files that are required to be kept. "Compress" and "suffix" define your +# chosen compression method. The others are provided because the location +# of certain commands varies from OS to OS. Sigh. + +keep=EXICYCLOG_MAX +compress=COMPRESS_COMMAND +suffix=COMPRESS_SUFFIX + +chown=CHOWN_COMMAND +chgrp=CHGRP_COMMAND +mv=MV_COMMAND +rm=RM_COMMAND + +# End of editable lines +######################################################################### + +# Some operating systems have different versions in which the commands live +# in different places. We have a fudge that will search the usual suspects if +# requested. + +for cmd in chown chgrp mv rm ; do + eval "oldcmd=\$$cmd" + if [ "$oldcmd" != "look_for_it" ] ; then continue ; fi + newcmd=$cmd + for dir in /bin /usr/bin /usr/sbin /usr/etc ; do + if [ -f $dir/$cmd ] ; then + newcmd=$dir/$cmd + break + fi + done + eval $cmd=$newcmd +done + +# See if this installation is using the esoteric "USE_EUID" feature of Exim, +# in which it uses the effective user id as a suffix for the configuration file +# name. In order for this to work, exicyclog must be run under the appropriate +# euid. + +if [ "CONFIGURE_FILE_USE_EUID" = "yes" ]; then + euid=.`id -u` +fi + +# See if this installation is using the esoteric "USE_NODE" feature of Exim, +# in which it uses the host's name as a suffix for the configuration file name. + +if [ "CONFIGURE_FILE_USE_NODE" = "yes" ]; then + hostsuffix=.`uname -n` +fi + +# Now find the configuration file name. This has got complicated because the +# CONFIGURE_FILE value may now be a list of files. The one that is used is the +# first one that exists. Mimic the code in readconf.c by testing first for the +# suffixed file in each case. + +set `awk -F: '{ for (i = 1; i <= NF; i++) print $i }' <\s([+-])(\d\d)(\d\d))?/; + +my $seconds = mktime $sec, $min, $hour, $day, $month - 1, $year - 1900; + +if (defined $tzs) + { + $seconds -= $tzh * 3600 + $tzm * 60 if $tzs eq "+"; + $seconds += $tzh * 3600 + $tzm * 60 if $tzs eq "-"; + } + +return $seconds; +} + + +# This subroutine processes a single line (in $_) from a log file. Program +# defensively against short lines finding their way into the log. + +my (%saved, %id_list, $pattern, $queue_time); + +sub do_line { +return unless + my($date,$entry) = /^(\d{4}-\d\d-\d\d \d\d:\d\d:\d\d (?:[+-]\d{4} )?)(.*)/; + +# Handle the case when the log line belongs to a specific message. We save +# lines for specific messages until the message is complete. Then either print +# discard. + +if (my($id) = $entry =~ /^(\w{6}\-\w{6}\-\w{2})/) + { + $saved{$id} = '' unless defined($saved{$id}); + + # Save up the data for this message in case it becomes interesting later. + + $saved{$id} .= $_; + + # Are we interested in this id ? + + $id_list{$id} = 1 if /$pattern/io; + + # See if this is a completion for some message. If it is interesting, + # print it, but in any event, throw away what was saved. + + if ($entry =~ + /(?:Completed$|rejected (?:by local_scan|by non-SMTP ACL|after DATA))/) + { + if ($saved{$id} =~ /^(\d{4}-\d\d-\d\d \d\d:\d\d:\d\d ([+-]\d{4} )?)(\w{6}\-\w{6}\-\w{2})/) + { + my $old_sec = &seconds($1); + my $sec = &seconds($date); + delete $id_list{$id} if $id_list{$id} && $sec - $old_sec <= $queue_time; + } + + if ($id_list{$id}) + { + delete $id_list{$id}; + print "$saved{$id}\n"; + } + + delete $saved{$id}; + } + } + +# Handle the case where the log line does not belong to a specific message. +# Print it if it is interesting. + +elsif ($entry =~ /$pattern/io) { print "$_\n"; } +} + + +# The main program. Extract the pattern and make sure any relevant characters +# are quoted if the -l flag is given. The -t flag gives a time-on-queue value +# which is an additional condition. + +getopts('lt:',\my %args); +$queue_time = $args{'t'}? $args{'t'} : -1; + +die "usage: exigrep [-l] [-t ] []...\n" + if ($#ARGV < 0); + +$pattern = shift @ARGV; +$pattern = quotemeta $pattern if $args{l}; + + +# If file arguments are given, open each one and process according as it is +# is compressed or not. + +if (@ARGV) + { + foreach (@ARGV) + { + my $filename = $_; + if ($filename =~ /\.(?:COMPRESS_SUFFIX)$/) + { + open(LOG, "ZCAT_COMMAND $filename |") || + die "Unable to zcat $filename: $!\n"; + } + else + { + open(LOG, "<$filename") || die "Unable to open $filename: $!\n"; + } + do_line() while (); + close(LOG); + } + } + +# If no files are named, process STDIN only + +else { do_line() while (); } + +# At the end of processing all the input, print any uncompleted data + +for (keys %id_list) { print "+++ $_ not completed +++\n$saved{$_}\n;" } + +# End of exigrep diff --git a/src/src/exim.c b/src/src/exim.c new file mode 100644 index 000000000..6d227c33e --- /dev/null +++ b/src/src/exim.c @@ -0,0 +1,4641 @@ +/* $Cambridge: exim/src/src/exim.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + + +/* The main function: entry point, initialization, and high-level control. +Also a few functions that don't naturally fit elsewhere. */ + + +#include "exim.h" + + + +/************************************************* +* Function interface to store functions * +*************************************************/ + +/* We need some real functions to pass to the PCRE regular expression library +for store allocation via Exim's store manager. The normal calls are actually +macros that pass over location information to make tracing easier. These +functions just interface to the standard macro calls. A good compiler will +optimize out the tail recursion and so not make them too expensive. There +are two sets of functions; one for use when we want to retain the compiled +regular expression for a long time; the other for short-term use. */ + +static void * +function_store_get(size_t size) +{ +return store_get((int)size); +} + +static void +function_dummy_free(void *block) { block = block; } + +static void * +function_store_malloc(size_t size) +{ +return store_malloc((int)size); +} + +static void +function_store_free(void *block) +{ +store_free(block); +} + + + + +/************************************************* +* Compile regular expression and panic on fail * +*************************************************/ + +/* This function is called when failure to compile a regular expression leads +to a panic exit. In other cases, pcre_compile() is called directly. In many +cases where this function is used, the results of the compilation are to be +placed in long-lived store, so we temporarily reset the store management +functions that PCRE uses if the use_malloc flag is set. + +Argument: + pattern the pattern to compile + caseless TRUE if caseless matching is required + use_malloc TRUE if compile into malloc store + +Returns: pointer to the compiled pattern +*/ + +const pcre * +regex_must_compile(uschar *pattern, BOOL caseless, BOOL use_malloc) +{ +int offset; +int options = PCRE_COPT; +const pcre *yield; +const uschar *error; +if (use_malloc) + { + pcre_malloc = function_store_malloc; + pcre_free = function_store_free; + } +if (caseless) options |= PCRE_CASELESS; +yield = pcre_compile(CS pattern, options, (const char **)&error, &offset, NULL); +pcre_malloc = function_store_get; +pcre_free = function_dummy_free; +if (yield == NULL) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "regular expression error: " + "%s at offset %d while compiling %s", error, offset, pattern); +return yield; +} + + + + +/************************************************* +* Execute regular expression and set strings * +*************************************************/ + +/* This function runs a regular expression match, and sets up the pointers to +the matched substrings. + +Arguments: + re the compiled expression + subject the subject string + options additional PCRE options + setup if < 0 do full setup + if >= 0 setup from setup+1 onwards, + excluding the full matched string + +Returns: TRUE or FALSE +*/ + +BOOL +regex_match_and_setup(const pcre *re, uschar *subject, int options, int setup) +{ +int ovector[3*(EXPAND_MAXN+1)]; +int n = pcre_exec(re, NULL, CS subject, Ustrlen(subject), 0, + PCRE_EOPT | options, ovector, sizeof(ovector)/sizeof(int)); +BOOL yield = n >= 0; +if (n == 0) n = EXPAND_MAXN + 1; +if (yield) + { + int nn; + expand_nmax = (setup < 0)? 0 : setup + 1; + for (nn = (setup < 0)? 0 : 2; nn < n*2; nn += 2) + { + expand_nstring[expand_nmax] = subject + ovector[nn]; + expand_nlength[expand_nmax++] = ovector[nn+1] - ovector[nn]; + } + expand_nmax--; + } +return yield; +} + + + + +/************************************************* +* Handler for SIGUSR1 * +*************************************************/ + +/* SIGUSR1 causes any exim process to write to the process log details of +what it is currently doing. It will only be used if the OS is capable of +setting up a handler that causes automatic restarting of any system call +that is in progress at the time. + +Argument: the signal number (SIGUSR1) +Returns: nothing +*/ + +static void +usr1_handler(int sig) +{ +sig = sig; /* Keep picky compilers happy */ +log_write(0, LOG_PROCESS, "%s", process_info); +log_close_all(); +os_restarting_signal(SIGUSR1, usr1_handler); +} + + + +/************************************************* +* Timeout handler * +*************************************************/ + +/* This handler is enabled most of the time that Exim is running. The handler +doesn't actually get used unless alarm() has been called to set a timer, to +place a time limit on a system call of some kind. When the handler is run, it +re-enables itself. + +There are some other SIGALRM handlers that are used in special cases when more +than just a flag setting is required; for example, when reading a message's +input. These are normally set up in the code module that uses them, and the +SIGALRM handler is reset to this one afterwards. + +Argument: the signal value (SIGALRM) +Returns: nothing +*/ + +void +sigalrm_handler(int sig) +{ +sig = sig; /* Keep picky compilers happy */ +sigalrm_seen = TRUE; +os_non_restarting_signal(SIGALRM, sigalrm_handler); +} + + + +/************************************************* +* Sleep for a fractional time interval * +*************************************************/ + +/* This function is called by millisleep() and exim_wait_tick() to wait for a +period of time that may include a fraction of a second. The coding is somewhat +tedious... + +Argument: an itimerval structure containing the interval +Returns: nothing +*/ + +static void +milliwait(struct itimerval *itval) +{ +sigset_t sigmask; +sigset_t old_sigmask; +(void)sigemptyset(&sigmask); /* Empty mask */ +(void)sigaddset(&sigmask, SIGALRM); /* Add SIGALRM */ +(void)sigprocmask(SIG_BLOCK, &sigmask, &old_sigmask); /* Block SIGALRM */ +(void)setitimer(ITIMER_REAL, itval, NULL); /* Start timer */ +(void)sigfillset(&sigmask); /* All signals */ +(void)sigdelset(&sigmask, SIGALRM); /* Remove SIGALRM */ +(void)sigsuspend(&sigmask); /* Until SIGALRM */ +(void)sigprocmask(SIG_SETMASK, &old_sigmask, NULL); /* Restore mask */ +} + + + + +/************************************************* +* Millisecond sleep function * +*************************************************/ + +/* The basic sleep() function has a granularity of 1 second, which is too rough +in some cases - for example, when using an increasing delay to slow down +spammers. + +Argument: number of millseconds +Returns: nothing +*/ + +void +millisleep(int msec) +{ +struct itimerval itval; +itval.it_interval.tv_sec = 0; +itval.it_interval.tv_usec = 0; +itval.it_value.tv_sec = msec/1000; +itval.it_value.tv_usec = (msec % 1000) * 1000; +milliwait(&itval); +} + + + +/************************************************* +* Compare microsecond times * +*************************************************/ + +/* +Arguments: + tv1 the first time + tv2 the second time + +Returns: -1, 0, or +1 +*/ + +int +exim_tvcmp(struct timeval *t1, struct timeval *t2) +{ +if (t1->tv_sec > t2->tv_sec) return +1; +if (t1->tv_sec < t2->tv_sec) return -1; +if (t1->tv_usec > t2->tv_usec) return +1; +if (t1->tv_usec < t2->tv_usec) return -1; +return 0; +} + + + + +/************************************************* +* Clock tick wait function * +*************************************************/ + +/* Exim uses a time + a pid to generate a unique identifier in two places: its +message IDs, and in file names for maildir deliveries. Because some OS now +re-use pids within the same second, sub-second times are now being used. +However, for absolute certaintly, we must ensure the clock has ticked before +allowing the relevant process to complete. At the time of implementation of +this code (February 2003), the speed of processors is such that the clock will +invariably have ticked already by the time a process has done its job. This +function prepares for the time when things are faster - and it also copes with +clocks that go backwards. + +Arguments: + then_tv A timeval which was used to create uniqueness; its usec field + has been rounded down to the value of the resolution. + We want to be sure the current time is greater than this. + resolution The resolution that was used to divide the microseconds + (1 for maildir, larger for message ids) + +Returns: nothing +*/ + +void +exim_wait_tick(struct timeval *then_tv, int resolution) +{ +struct timeval now_tv; +long int now_true_usec; + +(void)gettimeofday(&now_tv, NULL); +now_true_usec = now_tv.tv_usec; +now_tv.tv_usec = (now_true_usec/resolution) * resolution; + +if (exim_tvcmp(&now_tv, then_tv) <= 0) + { + struct itimerval itval; + itval.it_interval.tv_sec = 0; + itval.it_interval.tv_usec = 0; + itval.it_value.tv_sec = then_tv->tv_sec - now_tv.tv_sec; + itval.it_value.tv_usec = then_tv->tv_usec + resolution - now_true_usec; + + /* We know that, overall, "now" is less than or equal to "then". Therefore, a + negative value for the microseconds is possible only in the case when "now" + is more than a second less than "then". That means that itval.it_value.tv_sec + is greater than zero. The following correction is therefore safe. */ + + if (itval.it_value.tv_usec < 0) + { + itval.it_value.tv_usec += 1000000; + itval.it_value.tv_sec -= 1; + } + + DEBUG(D_transport|D_receive) + { + if (!running_in_test_harness) + { + debug_printf("tick check: %lu.%06lu %lu.%06lu\n", + then_tv->tv_sec, then_tv->tv_usec, now_tv.tv_sec, now_tv.tv_usec); + debug_printf("waiting %lu.%06lu\n", itval.it_value.tv_sec, + itval.it_value.tv_usec); + } + } + + milliwait(&itval); + } +} + + + + +/************************************************* +* Set up processing details * +*************************************************/ + +/* Save a text string for dumping when SIGUSR1 is received. +Do checks for overruns. + +Arguments: format and arguments, as for printf() +Returns: nothing +*/ + +void +set_process_info(char *format, ...) +{ +int len; +va_list ap; +sprintf(CS process_info, "%5d ", (int)getpid()); +len = Ustrlen(process_info); +va_start(ap, format); +if (!string_vformat(process_info + len, PROCESS_INFO_SIZE - len, format, ap)) + Ustrcpy(process_info + len, "**** string overflowed buffer ****"); +DEBUG(D_process_info) debug_printf("set_process_info: %s\n", process_info); +va_end(ap); +} + + + + + +/************************************************* +* Ensure stdin, stdout, and stderr exist * +*************************************************/ + +/* Some operating systems grumble if an exec() happens without a standard +input, output, and error (fds 0, 1, 2) being defined. The worry is that some +file will be opened and will use these fd values, and then some other bit of +code will assume, for example, that it can write error messages to stderr. +This function ensures that fds 0, 1, and 2 are open if they do not already +exist, by connecting them to /dev/null. + +This function is also used to ensure that std{in,out,err} exist at all times, +so that if any library that Exim calls tries to use them, it doesn't crash. + +Arguments: None +Returns: Nothing +*/ + +void +exim_nullstd(void) +{ +int i; +int devnull = -1; +struct stat statbuf; +for (i = 0; i <= 2; i++) + { + if (fstat(i, &statbuf) < 0 && errno == EBADF) + { + if (devnull < 0) devnull = open("/dev/null", O_RDWR); + if (devnull < 0) log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s", + string_open_failed(errno, "/dev/null")); + if (devnull != i) dup2(devnull, i); + } + } +if (devnull > 2) close(devnull); +} + + + + +/************************************************* +* Close unwanted file descriptors for delivery * +*************************************************/ + +/* This function is called from a new process that has been forked to deliver +an incoming message, either directly, or using exec. + +We want any smtp input streams to be closed in this new process. However, it +has been observed that using fclose() here causes trouble. When reading in -bS +input, duplicate copies of messages have been seen. The files will be sharing a +file pointer with the parent process, and it seems that fclose() (at least on +some systems - I saw this on Solaris 2.5.1) messes with that file pointer, at +least sometimes. Hence we go for closing the underlying file descriptors. + +If TLS is active, we want to shut down the TLS library, but without molesting +the parent's SSL connection. + +For delivery of a non-SMTP message, we want to close stdin and stdout (and +stderr unless debugging) because the calling process might have set them up as +pipes and be waiting for them to close before it waits for the submission +process to terminate. If they aren't closed, they hold up the calling process +until the initial delivery process finishes, which is not what we want. + +Exception: We do want it for synchronous delivery! + +And notwithstanding all the above, if D_resolver is set, implying resolver +debugging, leave stdout open, because that's where the resolver writes its +debugging output. + +When we close stderr (which implies we've also closed stdout), we also get rid +of any controlling terminal. + +Arguments: None +Returns: Nothing +*/ + +static void +close_unwanted(void) +{ +if (smtp_input) + { + #ifdef SUPPORT_TLS + tls_close(FALSE); /* Shut down the TLS library */ + #endif + close(fileno(smtp_in)); + close(fileno(smtp_out)); + smtp_in = NULL; + } +else + { + close(0); /* stdin */ + if ((debug_selector & D_resolver) == 0) close(1); /* stdout */ + if (debug_selector == 0) /* stderr */ + { + if (!synchronous_delivery) + { + close(2); + log_stderr = NULL; + } + (void)setsid(); + } + } +} + + + + +/************************************************* +* Set uid and gid * +*************************************************/ + +/* This function sets a new uid and gid permanently, optionally calling +initgroups() to set auxiliary groups. There are some special cases when running +Exim in unprivileged modes. In these situations the effective uid will not be +root; if we already have the right effective uid/gid, and don't need to +initialize any groups, leave things as they are. + +Arguments: + uid the uid + gid the gid + igflag TRUE if initgroups() wanted + msg text to use in debugging output and failure log + +Returns: nothing; bombs out on failure +*/ + +void +exim_setugid(uid_t uid, gid_t gid, BOOL igflag, uschar *msg) +{ +uid_t euid = geteuid(); +gid_t egid = getegid(); + +if (euid == root_uid || euid != uid || egid != gid || igflag) + { + /* At least one OS returns +1 for initgroups failure, so just check for + non-zero. */ + + if (igflag) + { + struct passwd *pw = getpwuid(uid); + if (pw != NULL) + { + if (initgroups(pw->pw_name, gid) != 0) + log_write(0,LOG_MAIN|LOG_PANIC_DIE,"initgroups failed for uid=%ld: %s", + (long int)uid, strerror(errno)); + } + else log_write(0, LOG_MAIN|LOG_PANIC_DIE, "cannot run initgroups(): " + "no passwd entry for uid=%ld", (long int)uid); + } + + if (setgid(gid) < 0 || setuid(uid) < 0) + { + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "unable to set gid=%ld or uid=%ld " + "(euid=%ld): %s", (long int)gid, (long int)uid, (long int)euid, msg); + } + } + +/* Debugging output included uid/gid and all groups */ + +DEBUG(D_uid) + { + int group_count; + gid_t group_list[NGROUPS_MAX]; + debug_printf("changed uid/gid: %s\n uid=%ld gid=%ld pid=%ld\n", msg, + (long int)geteuid(), (long int)getegid(), (long int)getpid()); + group_count = getgroups(NGROUPS_MAX, group_list); + debug_printf(" auxiliary group list:"); + if (group_count > 0) + { + int i; + for (i = 0; i < group_count; i++) debug_printf(" %d", (int)group_list[i]); + } + else debug_printf(" "); + debug_printf("\n"); + } +} + + + + +/************************************************* +* Exit point * +*************************************************/ + +/* Exim exits via this function so that it always clears up any open +databases. + +Arguments: + rc return code + +Returns: does not return +*/ + +void +exim_exit(int rc) +{ +search_tidyup(); +DEBUG(D_any) + debug_printf(">>>>>>>>>>>>>>>> Exim pid=%d terminating with rc=%d " + ">>>>>>>>>>>>>>>>\n", (int)getpid(), rc); +exit(rc); +} + + + + +/************************************************* +* Extract port from host address * +*************************************************/ + +/* Called to extract the port from the values given to -oMa and -oMi. +It also checks the syntax of the address. + +Argument: + address the address, with possible port on the end + +Returns: the port, or zero if there isn't one + bombs out on a syntax error +*/ + +static int +check_port(uschar *address) +{ +int port = host_extract_port(address); +if (!string_is_ip_address(address, NULL)) + { + fprintf(stderr, "exim abandoned: \"%s\" is not an IP address\n", address); + exit(EXIT_FAILURE); + } +return port; +} + + + +/************************************************* +* Test/verify an address * +*************************************************/ + +/* This function is called by the -bv and -bt code. It extracts a working +address from a full RFC 822 address. This isn't really necessary per se, but it +has the effect of collapsing source routes. + +Arguments: + s the address string + flags flag bits for verify_address() + exit_value to be set for failures + +Returns: nothint +*/ + +static void +test_address(uschar *s, int flags, int *exit_value) +{ +int start, end, domain; +uschar *parse_error = NULL; +uschar *address = parse_extract_address(s, &parse_error, &start, &end, &domain, + FALSE); +if (address == NULL) + { + fprintf(stdout, "syntax error: %s\n", parse_error); + *exit_value = 2; + } +else + { + int rc = verify_address(deliver_make_addr(address,TRUE), stdout, flags, -1, + -1, NULL, NULL, NULL); + if (rc == FAIL) *exit_value = 2; + else if (rc == DEFER && *exit_value == 0) *exit_value = 1; + } +} + + + +/************************************************* +* Decode bit settings for log/debug * +*************************************************/ + +/* This function decodes a string containing bit settings in the form of +name +and/or -name sequences, and sets/unsets bits in a bit string accordingly. It +also recognizes a numeric setting of the form =, but this is not +intended for user use. It's an easy way for Exim to pass the debug settings +when it is re-exec'ed. + +The log options are held in two unsigned ints (because there became too many +for one). The top bit in the table means "put in 2nd selector". This does not +yet apply to debug options, so the "=" facility sets only the first selector. + +A bad value for a debug setting is treated as an unknown option - error message +to stderr and die. For log settings, which come from the configuration file, +we write to the log on the way out... + +Arguments: + selector1 address of the first bit string + selector2 address of the second bit string, or NULL + string the configured string + options the table of option names + count size of table + which "log" or "debug" + +Returns: nothing on success - bomb out on failure +*/ + +static void +decode_bits(unsigned int *selector1, unsigned int *selector2, uschar *string, + bit_table *options, int count, uschar *which) +{ +uschar *errmsg; +if (string == NULL) return; + +if (*string == '=') + { + char *end; /* Not uschar */ + *selector1 = strtoul(CS string+1, &end, 0); + if (*end == 0) return; + errmsg = string_sprintf("malformed numeric %s_selector setting: %s", which, + string); + goto ERROR_RETURN; + } + +/* Handle symbolic setting */ + +else for(;;) + { + BOOL adding; + uschar *s; + int len; + bit_table *start, *end; + + while (isspace(*string)) string++; + if (*string == 0) return; + + if (*string != '+' && *string != '-') + { + errmsg = string_sprintf("malformed %s_selector setting: " + "+ or - expected but found \"%s\"", which, string); + goto ERROR_RETURN; + } + + adding = *string++ == '+'; + s = string; + while (isalnum(*string) || *string == '_') string++; + len = string - s; + + start = options; + end = options + count; + + while (start < end) + { + bit_table *middle = start + (end - start)/2; + int c = Ustrncmp(s, middle->name, len); + if (c == 0) + { + if (middle->name[len] != 0) c = -1; else + { + unsigned int bit = middle->bit; + unsigned int *selector; + + /* The value with all bits set means "set all bits in both selectors" + in the case where two are being handled. However, the top bit in the + second selector is never set. */ + + if (bit == 0xffffffff) + { + *selector1 = adding? bit : 0; + if (selector2 != NULL) *selector2 = adding? 0x7fffffff : 0; + } + + /* Otherwise, the 0x80000000 bit means "this value, without the top + bit, belongs in the second selector". */ + + else + { + if ((bit & 0x80000000) != 0) + { + selector = selector2; + bit &= 0x7fffffff; + } + else selector = selector1; + if (adding) *selector |= bit; else *selector &= ~bit; + } + break; /* Out of loop to match selector name */ + } + } + if (c < 0) end = middle; else start = middle + 1; + } /* Loop to match selector name */ + + if (start >= end) + { + errmsg = string_sprintf("unknown %s_selector setting: %c%.*s", which, + adding? '+' : '-', len, s); + goto ERROR_RETURN; + } + } /* Loop for selector names */ + +/* Handle disasters */ + +ERROR_RETURN: +if (Ustrcmp(which, "debug") == 0) + { + fprintf(stderr, "exim: %s\n", errmsg); + exit(EXIT_FAILURE); + } +else log_write(0, LOG_CONFIG|LOG_PANIC_DIE, "%s", errmsg); +} + + + +/************************************************* +* Show supported features * +*************************************************/ + +/* This function is called for -bV and for -d to output the optional features +of the current Exim binary. + +Arguments: a FILE for printing +Returns: nothing +*/ + +static void +show_whats_supported(FILE *f) +{ +#ifdef DB_VERSION_STRING +fprintf(f, "Berkeley DB: %s\n", DB_VERSION_STRING); +#elif defined(BTREEVERSION) && defined(HASHVERSION) + #ifdef USE_DB + fprintf(f, "Probably Berkeley DB version 1.8x (native mode)\n"); + #else + fprintf(f, "Probably Berkeley DB version 1.8x (compatibility mode)\n"); + #endif +#elif defined(_DBM_RDONLY) || defined(dbm_dirfno) +fprintf(f, "Probably ndbm\n"); +#elif defined(USE_TDB) +fprintf(f, "Using tdb\n"); +#else + #ifdef USE_GDBM + fprintf(f, "Probably GDBM (native mode)\n"); + #else + fprintf(f, "Probably GDBM (compatibility mode)\n"); + #endif +#endif + +fprintf(f, "Support for:"); +#if HAVE_ICONV + fprintf(f, " iconv()"); +#endif +#if HAVE_IPV6 + fprintf(f, " IPv6"); +#endif +#ifdef SUPPORT_PAM + fprintf(f, " PAM"); +#endif +#ifdef EXIM_PERL + fprintf(f, " Perl"); +#endif +#ifdef USE_TCP_WRAPPERS + fprintf(f, " TCPwrappers"); +#endif +#ifdef SUPPORT_TLS + #ifdef USE_GNUTLS + fprintf(f, " GnuTLS"); + #else + fprintf(f, " OpenSSL"); + #endif +#endif +fprintf(f, "\n"); + +fprintf(f, "Lookups:"); +#ifdef LOOKUP_LSEARCH + fprintf(f, " lsearch wildlsearch nwildlsearch iplsearch"); +#endif +#ifdef LOOKUP_CDB + fprintf(f, " cdb"); +#endif +#ifdef LOOKUP_DBM + fprintf(f, " dbm dbmnz"); +#endif +#ifdef LOOKUP_DNSDB + fprintf(f, " dnsdb"); +#endif +#ifdef LOOKUP_DSEARCH + fprintf(f, " dsearch"); +#endif +#ifdef LOOKUP_IBASE + fprintf(f, " ibase"); +#endif +#ifdef LOOKUP_LDAP + fprintf(f, " ldap ldapdn ldapm"); +#endif +#ifdef LOOKUP_MYSQL + fprintf(f, " mysql"); +#endif +#ifdef LOOKUP_NIS + fprintf(f, " nis nis0"); +#endif +#ifdef LOOKUP_NISPLUS + fprintf(f, " nisplus"); +#endif +#ifdef LOOKUP_ORACLE + fprintf(f, " oracle"); +#endif +#ifdef LOOKUP_PASSWD + fprintf(f, " passwd"); +#endif +#ifdef LOOKUP_PGSQL + fprintf(f, " pgsql"); +#endif +#ifdef LOOKUP_TESTDB + fprintf(f, " testdb"); +#endif +#ifdef LOOKUP_WHOSON + fprintf(f, " whoson"); +#endif +fprintf(f, "\n"); + +fprintf(f, "Authenticators:"); +#ifdef AUTH_CRAM_MD5 + fprintf(f, " cram_md5"); +#endif +#ifdef AUTH_CYRUS_SASL + fprintf(f, " cyrus_sasl"); +#endif +#ifdef AUTH_PLAINTEXT + fprintf(f, " plaintext"); +#endif +#ifdef AUTH_SPA + fprintf(f, " spa"); +#endif +fprintf(f, "\n"); + +fprintf(f, "Routers:"); +#ifdef ROUTER_ACCEPT + fprintf(f, " accept"); +#endif +#ifdef ROUTER_DNSLOOKUP + fprintf(f, " dnslookup"); +#endif +#ifdef ROUTER_IPLITERAL + fprintf(f, " ipliteral"); +#endif +#ifdef ROUTER_IPLOOKUP + fprintf(f, " iplookup"); +#endif +#ifdef ROUTER_MANUALROUTE + fprintf(f, " manualroute"); +#endif +#ifdef ROUTER_QUERYPROGRAM + fprintf(f, " queryprogram"); +#endif +#ifdef ROUTER_REDIRECT + fprintf(f, " redirect"); +#endif +fprintf(f, "\n"); + +fprintf(f, "Transports:"); +#ifdef TRANSPORT_APPENDFILE + fprintf(f, " appendfile"); + #ifdef SUPPORT_MAILDIR + fprintf(f, "/maildir"); + #endif + #ifdef SUPPORT_MAILSTORE + fprintf(f, "/mailstore"); + #endif + #ifdef SUPPORT_MBX + fprintf(f, "/mbx"); + #endif +#endif +#ifdef TRANSPORT_AUTOREPLY + fprintf(f, " autoreply"); +#endif +#ifdef TRANSPORT_LMTP + fprintf(f, " lmtp"); +#endif +#ifdef TRANSPORT_PIPE + fprintf(f, " pipe"); +#endif +#ifdef TRANSPORT_SMTP + fprintf(f, " smtp"); +#endif +fprintf(f, "\n"); + +if (fixed_never_users[0] > 0) + { + int i; + fprintf(f, "Fixed never_users: "); + for (i = 1; i <= (int)fixed_never_users[0] - 1; i++) + fprintf(f, "%d:", (unsigned int)fixed_never_users[i]); + fprintf(f, "%d\n", (unsigned int)fixed_never_users[i]); + } +} + + + + +/************************************************* +* Quote a local part * +*************************************************/ + +/* This function is used when a sender address or a From: or Sender: header +line is being created from the caller's login, or from an authenticated_id. It +applies appropriate quoting rules for a local part. + +Argument: the local part +Returns: the local part, quoted if necessary +*/ + +uschar * +local_part_quote(uschar *lpart) +{ +BOOL needs_quote = FALSE; +int size, ptr; +uschar *yield; +uschar *t; + +for (t = lpart; !needs_quote && *t != 0; t++) + { + needs_quote = !isalnum(*t) && strchr("!#$%&'*+-/=?^_`{|}~", *t) == NULL && + (*t != '.' || t == lpart || t[1] == 0); + } + +if (!needs_quote) return lpart; + +size = ptr = 0; +yield = string_cat(NULL, &size, &ptr, US"\"", 1); + +for (;;) + { + uschar *nq = US Ustrpbrk(lpart, "\\\""); + if (nq == NULL) + { + yield = string_cat(yield, &size, &ptr, lpart, Ustrlen(lpart)); + break; + } + yield = string_cat(yield, &size, &ptr, lpart, nq - lpart); + yield = string_cat(yield, &size, &ptr, US"\\", 1); + yield = string_cat(yield, &size, &ptr, nq, 1); + lpart = nq + 1; + } + +yield = string_cat(yield, &size, &ptr, US"\"", 1); +yield[ptr] = 0; +return yield; +} + + + +#ifdef USE_READLINE +/************************************************* +* Load readline() functions * +*************************************************/ + +/* This function is called from testing executions that read data from stdin, +but only when running as the calling user. Currently, only -be does this. The +function loads the readline() function library and passes back the functions. +On some systems, it needs the curses library, so load that too, but try without +it if loading fails. All this functionality has to be requested at build time. + +Arguments: + fn_readline_ptr pointer to where to put the readline pointer + fn_addhist_ptr pointer to where to put the addhistory function + +Returns: the dlopen handle or NULL on failure +*/ + +static void * +set_readline(char * (**fn_readline_ptr)(char *), + char * (**fn_addhist_ptr)(char *)) +{ +void *dlhandle; +void *dlhandle_curses = dlopen("libcurses.so", RTLD_GLOBAL|RTLD_LAZY); + +dlhandle = dlopen("libreadline.so", RTLD_GLOBAL|RTLD_NOW); +if (dlhandle_curses != NULL) dlclose(dlhandle_curses); + +if (dlhandle != NULL) + { + *fn_readline_ptr = (char *(*)(char*))dlsym(dlhandle, "readline"); + *fn_addhist_ptr = (char *(*)(char*))dlsym(dlhandle, "add_history"); + } +else + { + DEBUG(D_any) debug_printf("failed to load readline: %s\n", dlerror()); + } + +return dlhandle; +} +#endif + + + +/************************************************* +* Get a line from stdin for testing things * +*************************************************/ + +/* This function is called when running tests that can take a number of lines +of input (for example, -be and -bt). It handles continuations and trailing +spaces. And prompting and a blank line output on eof. If readline() is in use, +the arguments are non-NULL and provide the relevant functions. + +Arguments: + fn_readline readline function or NULL + fn_addhist addhist function or NULL + +Returns: pointer to dynamic memory, or NULL at end of file +*/ + +static uschar * +get_stdinput(char *(*fn_readline)(char *), char *(*fn_addhist)(char *)) +{ +int i; +int size = 0; +int ptr = 0; +uschar *yield = NULL; + +if (fn_readline == NULL) printf("> "); + +for (i = 0;; i++) + { + uschar buffer[1024]; + uschar *p, *ss; + + #ifdef USE_READLINE + char *readline_line = NULL; + if (fn_readline != NULL) + { + if ((readline_line = fn_readline((i > 0)? "":"> ")) == NULL) break; + if (*readline_line != 0 && fn_addhist != NULL) fn_addhist(readline_line); + p = US readline_line; + } + else + #endif + + /* readline() not in use */ + + { + if (Ufgets(buffer, sizeof(buffer), stdin) == NULL) break; + p = buffer; + } + + /* Handle the line */ + + ss = p + (int)Ustrlen(p); + while (ss > p && isspace(ss[-1])) ss--; + + if (i > 0) + { + while (p < ss && isspace(*p)) p++; /* leading space after cont */ + } + + yield = string_cat(yield, &size, &ptr, p, ss - p); + + #ifdef USE_READLINE + if (fn_readline != NULL) free(readline_line); + #endif + + if (ss == p || yield[ptr-1] != '\\') + { + yield[ptr] = 0; + break; + } + yield[--ptr] = 0; + } + +if (yield == NULL) printf("\n"); +return yield; +} + + + +/************************************************* +* Entry point and high-level code * +*************************************************/ + +/* Entry point for the Exim mailer. Analyse the arguments and arrange to take +the appropriate action. All the necessary functions are present in the one +binary. I originally thought one should split it up, but it turns out that so +much of the apparatus is needed in each chunk that one might as well just have +it all available all the time, which then makes the coding easier as well. + +Arguments: + argc count of entries in argv + argv argument strings, with argv[0] being the program name + +Returns: EXIT_SUCCESS if terminated successfully + EXIT_FAILURE otherwise, except when a message has been sent + to the sender, and -oee was given +*/ + +int +main(int argc, char **cargv) +{ +uschar **argv = USS cargv; +int arg_receive_timeout = -1; +int arg_smtp_receive_timeout = -1; +int arg_error_handling = error_handling; +int filter_fd = -1; +int group_count; +int i; +int list_queue_option = 0; +int msg_action = 0; +int msg_action_arg = -1; +int namelen = (argv[0] == NULL)? 0 : Ustrlen(argv[0]); +int queue_only_reason = 0; +#ifdef EXIM_PERL +int perl_start_option = 0; +#endif +int recipients_arg = argc; +int sender_address_domain = 0; +int test_retry_arg = -1; +int test_rewrite_arg = -1; +BOOL arg_queue_only = FALSE; +BOOL bi_option = FALSE; +BOOL checking = FALSE; +BOOL count_queue = FALSE; +BOOL expansion_test = FALSE; +BOOL extract_recipients = FALSE; +BOOL forced_delivery = FALSE; +BOOL f_end_dot = FALSE; +BOOL deliver_give_up = FALSE; +BOOL list_queue = FALSE; +BOOL list_options = FALSE; +BOOL local_queue_only; +BOOL more = TRUE; +BOOL one_msg_action = FALSE; +BOOL queue_only_set = FALSE; +BOOL receiving_message = TRUE; +BOOL unprivileged; +BOOL removed_privilege = FALSE; +BOOL verify_address_mode = FALSE; +BOOL verify_as_sender = FALSE; +BOOL version_printed = FALSE; +uschar *alias_arg = NULL; +uschar *called_as = US""; +uschar *start_queue_run_id = NULL; +uschar *stop_queue_run_id = NULL; +uschar *ftest_domain = NULL; +uschar *ftest_localpart = NULL; +uschar *ftest_prefix = NULL; +uschar *ftest_suffix = NULL; +uschar *real_sender_address; +uschar *originator_home = US"/"; +BOOL ftest_system = FALSE; +void *reset_point; + +struct passwd *pw; +struct stat statbuf; +pid_t passed_qr_pid = (pid_t)0; +int passed_qr_pipe = -1; +gid_t group_list[NGROUPS_MAX]; + +/* Possible options for -R and -S */ + +static uschar *rsopts[] = { US"f", US"ff", US"r", US"rf", US"rff" }; + +/* Need to define this in case we need to change the environment in order +to get rid of a bogus time zone. We have to make it char rather than uschar +because some OS define it in /usr/include/unistd.h. */ + +extern char **environ; + +/* If the Exim user and/or group and/or the configuration file owner were +defined by ref:name at build time, we must now find the actual uid/gid values. +This is a feature to make the lives of binary distributors easier. */ + +#ifdef EXIM_USERNAME +if (route_finduser(US EXIM_USERNAME, &pw, &exim_uid)) + { + exim_gid = pw->pw_gid; + } +else + { + fprintf(stderr, "exim: failed to find uid for user name \"%s\"\n", + EXIM_USERNAME); + exit(EXIT_FAILURE); + } +#endif + +#ifdef EXIM_GROUPNAME +if (!route_findgroup(US EXIM_GROUPNAME, &exim_gid)) + { + fprintf(stderr, "exim: failed to find gid for group name \"%s\"\n", + EXIM_GROUPNAME); + exit(EXIT_FAILURE); + } +#endif + +#ifdef CONFIGURE_OWNERNAME +if (!route_finduser(US CONFIGURE_OWNERNAME, NULL, &config_uid)) + { + fprintf(stderr, "exim: failed to find uid for user name \"%s\"\n", + CONFIGURE_OWNERNAME); + exit(EXIT_FAILURE); + } +#endif + +/* In the Cygwin environment, some initialization needs doing. It is fudged +in by means of this macro. */ + +#ifdef OS_INIT +OS_INIT +#endif + +/* Check a field which is patched when we are running Exim within its +testing harness; do a fast initial check, and then the whole thing. */ + +running_in_test_harness = + *running_status == '<' && Ustrcmp(running_status, "<<>>") == 0; + +/* The C standard says that the equivalent of setlocale(LC_ALL, "C") is obeyed +at the start of a program; however, it seems that some environments do not +follow this. A "strange" locale can affect the formatting of timestamps, so we +make quite sure. */ + +setlocale(LC_ALL, "C"); + +/* Set up the default handler for timing using alarm(). */ + +os_non_restarting_signal(SIGALRM, sigalrm_handler); + +/* Ensure we have a buffer for constructing log entries. Use malloc directly, +because store_malloc writes a log entry on failure. */ + +log_buffer = (uschar *)malloc(LOG_BUFFER_SIZE); +if (log_buffer == NULL) + { + fprintf(stderr, "exim: failed to get store for log buffer\n"); + exit(EXIT_FAILURE); + } + +/* Set log_stderr to stderr, provided that stderr exists. This gets reset to +NULL when the daemon is run and the file is closed. We have to use this +indirection, because some systems don't allow writing to the variable "stderr". +*/ + +if (fstat(fileno(stderr), &statbuf) >= 0) log_stderr = stderr; + +/* Arrange for the PCRE regex library to use our store functions. Note that +the normal calls are actually macros that add additional arguments for +debugging purposes so we have to assign specially constructed functions here. +The default is to use store in the stacking pool, but this is overridden in the +regex_must_compile() function. */ + +pcre_malloc = function_store_get; +pcre_free = function_dummy_free; + +/* Ensure there is a big buffer for temporary use in several places. It is put +in malloc store so that it can be freed for enlargement if necessary. */ + +big_buffer = store_malloc(big_buffer_size); + +/* Set up the handler for the data request signal, and set the initial +descriptive text. */ + +set_process_info("initializing"); +os_restarting_signal(SIGUSR1, usr1_handler); + +/* SIGHUP is used to get the daemon to reconfigure. It gets set as appropriate +in the daemon code. For the rest of Exim's uses, we ignore it. */ + +signal(SIGHUP, SIG_IGN); + +/* We don't want to die on pipe errors as the code is written to handle +the write error instead. */ + +signal(SIGPIPE, SIG_IGN); + +/* Under some circumstance on some OS, Exim can get called with SIGCHLD +set to SIG_IGN. This causes subprocesses that complete before the parent +process waits for them not to hang around, so when Exim calls wait(), nothing +is there. The wait() code has been made robust against this, but let's ensure +that SIGCHLD is set to SIG_DFL, because it's tidier to wait and get a process +ending status. We use sigaction rather than plain signal() on those OS where +SA_NOCLDWAIT exists, because we want to be sure it is turned off. (There was a +problem on AIX with this.) */ + +#ifdef SA_NOCLDWAIT + { + struct sigaction act; + act.sa_handler = SIG_DFL; + sigemptyset(&(act.sa_mask)); + act.sa_flags = 0; + sigaction(SIGCHLD, &act, NULL); + } +#else +signal(SIGCHLD, SIG_DFL); +#endif + +/* Save the arguments for use if we re-exec exim as a daemon after receiving +SIGHUP. */ + +sighup_argv = argv; + +/* Set up the version number. Set up the leading 'E' for the external form of +message ids, set the pointer to the internal form, and initialize it to +indicate no message being processed. */ + +version_init(); +message_id_option[0] = '-'; +message_id_external = message_id_option + 1; +message_id_external[0] = 'E'; +message_id = message_id_external + 1; +message_id[0] = 0; + +/* Set the umask to zero so that any files that Exim creates are created +with the modes that it specifies. */ + +umask(0); + +/* Precompile the regular expression for matching a message id. Keep this in +step with the code that generates ids in the accept.c module. We need to do +this here, because the -M options check their arguments for syntactic validity +using mac_ismsgid, which uses this. */ + +regex_ismsgid = + regex_must_compile(US"^(?:[^\\W_]{6}-){2}[^\\W_]{2}$", FALSE, TRUE); + +/* If the program is called as "mailq" treat it as equivalent to "exim -bp"; +this seems to be a generally accepted convention, since one finds symbolic +links called "mailq" in standard OS configurations. */ + +if ((namelen == 5 && Ustrcmp(argv[0], "mailq") == 0) || + (namelen > 5 && Ustrncmp(argv[0] + namelen - 6, "/mailq", 6) == 0)) + { + list_queue = TRUE; + receiving_message = FALSE; + called_as = US"-mailq"; + } + +/* If the program is called as "rmail" treat it as equivalent to +"exim -i -oee", thus allowing UUCP messages to be input using non-SMTP mode, +i.e. preventing a single dot on a line from terminating the message, and +returning with zero return code, even in cases of error (provided an error +message has been sent). */ + +if ((namelen == 5 && Ustrcmp(argv[0], "rmail") == 0) || + (namelen > 5 && Ustrncmp(argv[0] + namelen - 6, "/rmail", 6) == 0)) + { + dot_ends = FALSE; + called_as = US"-rmail"; + errors_sender_rc = EXIT_SUCCESS; + } + +/* If the program is called as "rsmtp" treat it as equivalent to "exim -bS"; +this is a smail convention. */ + +if ((namelen == 5 && Ustrcmp(argv[0], "rsmtp") == 0) || + (namelen > 5 && Ustrncmp(argv[0] + namelen - 6, "/rsmtp", 6) == 0)) + { + smtp_input = smtp_batched_input = TRUE; + called_as = US"-rsmtp"; + } + +/* If the program is called as "runq" treat it as equivalent to "exim -q"; +this is a smail convention. */ + +if ((namelen == 4 && Ustrcmp(argv[0], "runq") == 0) || + (namelen > 4 && Ustrncmp(argv[0] + namelen - 5, "/runq", 5) == 0)) + { + queue_interval = 0; + receiving_message = FALSE; + called_as = US"-runq"; + } + +/* If the program is called as "newaliases" treat it as equivalent to +"exim -bi"; this is a sendmail convention. */ + +if ((namelen == 10 && Ustrcmp(argv[0], "newaliases") == 0) || + (namelen > 10 && Ustrncmp(argv[0] + namelen - 11, "/newaliases", 11) == 0)) + { + bi_option = TRUE; + receiving_message = FALSE; + called_as = US"-newaliases"; + } + +/* Save the original effective uid for a couple of uses later. It should +normally be root, but in some esoteric environments it may not be. */ + +original_euid = geteuid(); + +/* Get the real uid and gid. If the caller is root, force the effective uid/gid +to be the same as the real ones. This makes a difference only if Exim is setuid +(or setgid) to something other than root, which could be the case in some +special configurations. */ + +real_uid = getuid(); +real_gid = getgid(); + +if (real_uid == root_uid) + { + setgid(real_gid); + setuid(real_uid); + } + +/* If neither the original real uid nor the original euid was root, Exim is +running in an unprivileged state. */ + +unprivileged = (real_uid != root_uid && original_euid != root_uid); + +/* If the first argument is --help, pretend there are no arguments. This will +cause a brief message to be given. */ + +if (argc > 1 && Ustrcmp(argv[1], "--help") == 0) argc = 1; + +/* Scan the program's arguments. Some can be dealt with right away; others are +simply recorded for checking and handling afterwards. Do a high-level switch +on the second character (the one after '-'), to save some effort. */ + +for (i = 1; i < argc; i++) + { + BOOL badarg = FALSE; + uschar *arg = argv[i]; + uschar *argrest; + int switchchar; + + /* An argument not starting with '-' is the start of a recipients list; + break out of the options-scanning loop. */ + + if (arg[0] != '-') + { + recipients_arg = i; + break; + } + + /* An option consistion of -- terminates the options */ + + if (Ustrcmp(arg, "--") == 0) + { + recipients_arg = i + 1; + break; + } + + /* Handle flagged options */ + + switchchar = arg[1]; + argrest = arg+2; + + /* Make all -ex options synonymous with -oex arguments, since that + is assumed by various callers. Also make -qR options synonymous with -R + options, as that seems to be required as well. Allow for -qqR too, and + the same for -S options. */ + + if (Ustrncmp(arg+1, "oe", 2) == 0 || + Ustrncmp(arg+1, "qR", 2) == 0 || + Ustrncmp(arg+1, "qS", 2) == 0) + { + switchchar = arg[2]; + argrest++; + } + else if (Ustrncmp(arg+1, "qqR", 3) == 0 || Ustrncmp(arg+1, "qqS", 3) == 0) + { + switchchar = arg[3]; + argrest += 2; + queue_2stage = TRUE; + } + + /* Make -r synonymous with -f, since it is a documented alias */ + + else if (arg[1] == 'r') switchchar = 'f'; + + /* Make -ov synonymous with -v */ + + else if (Ustrcmp(arg, "-ov") == 0) + { + switchchar = 'v'; + argrest++; + } + + /* High-level switch on active initial letter */ + + switch(switchchar) + { + /* -Btype is a sendmail option for 7bit/8bit setting. Exim is 8-bit clean + so has no need of it. */ + + case 'B': + if (*argrest == 0) i++; /* Skip over the type */ + break; + + + case 'b': + receiving_message = FALSE; /* Reset TRUE for -bm, -bS, -bs below */ + + /* -bd: Run in daemon mode, awaiting SMTP connections. + -bdf: Ditto, but in the foreground. + */ + + if (*argrest == 'd') + { + daemon_listen = TRUE; + if (*(++argrest) == 'f') background_daemon = FALSE; + else if (*argrest != 0) { badarg = TRUE; break; } + } + + /* -be: Run in expansion test mode */ + + else if (*argrest == 'e') + expansion_test = checking = TRUE; + + /* -bf: Run in mail filter testing mode + -bF: Ditto, but for system filters + -bfd: Set domain for filter testing + -bfl: Set local part for filter testing + -bfp: Set prefix for filter testing + -bfs: Set suffix for filter testing + */ + + else if (*argrest == 'f' || *argrest == 'F') + { + ftest_system = *argrest++ == 'F'; + if (*argrest == 0) + { + if(++i < argc) filter_test = argv[i]; else + { + fprintf(stderr, "exim: file name expected after %s\n", argv[i-1]); + exit(EXIT_FAILURE); + } + } + else + { + if (++i >= argc) + { + fprintf(stderr, "exim: string expected after %s\n", arg); + exit(EXIT_FAILURE); + } + if (Ustrcmp(argrest, "d") == 0) ftest_domain = argv[i]; + else if (Ustrcmp(argrest, "l") == 0) ftest_localpart = argv[i]; + else if (Ustrcmp(argrest, "p") == 0) ftest_prefix = argv[i]; + else if (Ustrcmp(argrest, "s") == 0) ftest_suffix = argv[i]; + else { badarg = TRUE; break; } + } + } + + /* -bh: Host checking - an IP address must follow. */ + + else if (Ustrcmp(argrest, "h") == 0 || Ustrcmp(argrest, "hc") == 0) + { + if (++i >= argc) { badarg = TRUE; break; } + sender_host_address = argv[i]; + host_checking = checking = log_testing_mode = TRUE; + host_checking_callout = argrest[1] == 'c'; + } + + /* -bi: This option is used by sendmail to initialize *the* alias file, + though it has the -oA option to specify a different file. Exim has no + concept of *the* alias file, but since Sun's YP make script calls + sendmail this way, some support must be provided. */ + + else if (Ustrcmp(argrest, "i") == 0) bi_option = TRUE; + + /* -bm: Accept and deliver message - the default option. Reinstate + receiving_message, which got turned off for all -b options. */ + + else if (Ustrcmp(argrest, "m") == 0) receiving_message = TRUE; + + /* -bnq: For locally originating messages, do not qualify unqualified + addresses. In the envelope, this causes errors; in header lines they + just get left. */ + + else if (Ustrcmp(argrest, "nq") == 0) + { + allow_unqualified_sender = FALSE; + allow_unqualified_recipient = FALSE; + } + + /* -bpxx: List the contents of the mail queue, in various forms. If + the option is -bpc, just a queue count is needed. Otherwise, if the + first letter after p is r, then order is random. */ + + else if (*argrest == 'p') + { + if (*(++argrest) == 'c') + { + count_queue = TRUE; + if (*(++argrest) != 0) badarg = TRUE; + break; + } + + if (*argrest == 'r') + { + list_queue_option = 8; + argrest++; + } + else list_queue_option = 0; + + list_queue = TRUE; + + /* -bp: List the contents of the mail queue, top-level only */ + + if (*argrest == 0) {} + + /* -bpu: List the contents of the mail queue, top-level undelivered */ + + else if (Ustrcmp(argrest, "u") == 0) list_queue_option += 1; + + /* -bpa: List the contents of the mail queue, including all delivered */ + + else if (Ustrcmp(argrest, "a") == 0) list_queue_option += 2; + + /* Unknown after -bp[r] */ + + else + { + badarg = TRUE; + break; + } + } + + + /* -bP: List the configuration variables given as the address list. + Force -v, so configuration errors get displayed. */ + + else if (Ustrcmp(argrest, "P") == 0) + { + list_options = TRUE; + debug_selector |= D_v; + debug_file = stderr; + } + + /* -brt: Test retry configuration lookup */ + + else if (Ustrcmp(argrest, "rt") == 0) + { + test_retry_arg = i + 1; + goto END_ARG; + } + + /* -brw: Test rewrite configuration */ + + else if (Ustrcmp(argrest, "rw") == 0) + { + test_rewrite_arg = i + 1; + goto END_ARG; + } + + /* -bS: Read SMTP commands on standard input, but produce no replies - + all errors are reported by sending messages. */ + + else if (Ustrcmp(argrest, "S") == 0) + smtp_input = smtp_batched_input = receiving_message = TRUE; + + /* -bs: Read SMTP commands on standard input and produce SMTP replies + on standard output. */ + + else if (Ustrcmp(argrest, "s") == 0) smtp_input = receiving_message = TRUE; + + /* -bt: address testing mode */ + + else if (Ustrcmp(argrest, "t") == 0) + address_test_mode = checking = log_testing_mode = TRUE; + + /* -bv: verify addresses */ + + else if (Ustrcmp(argrest, "v") == 0) + verify_address_mode = checking = log_testing_mode = TRUE; + + /* -bvs: verify sender addresses */ + + else if (Ustrcmp(argrest, "vs") == 0) + { + verify_address_mode = checking = log_testing_mode = TRUE; + verify_as_sender = TRUE; + } + + /* -bV: Print version string and support details */ + + else if (Ustrcmp(argrest, "V") == 0) + { + printf("Exim version %s #%s built %s\n", version_string, + version_cnumber, version_date); + printf("%s\n", CS version_copyright); + version_printed = TRUE; + show_whats_supported(stdout); + } + + else badarg = TRUE; + break; + + + /* -C: change configuration file list; ignore if it isn't really + a change! Enforce a prefix check if required. */ + + case 'C': + if (*argrest == 0) + { + if(++i < argc) argrest = argv[i]; else + { badarg = TRUE; break; } + } + if (Ustrcmp(config_main_filelist, argrest) != 0) + { + #ifdef ALT_CONFIG_PREFIX + int sep = 0; + int len = Ustrlen(ALT_CONFIG_PREFIX); + uschar *list = argrest; + uschar *filename; + while((filename = string_nextinlist(&list, &sep, big_buffer, + big_buffer_size)) != NULL) + { + if ((Ustrlen(filename) < len || + Ustrncmp(filename, ALT_CONFIG_PREFIX, len) != 0 || + Ustrstr(filename, "/../") != NULL) && + (Ustrcmp(filename, "/dev/null") != 0 || real_uid != root_uid)) + { + fprintf(stderr, "-C Permission denied\n"); + exit(EXIT_FAILURE); + } + } + #endif + + config_main_filelist = argrest; + config_changed = TRUE; + } + break; + + + /* -D: set up a macro definition */ + + case 'D': + #ifdef DISABLE_D_OPTION + fprintf(stderr, "exim: -D is not available in this Exim binary\n"); + exit(EXIT_FAILURE); + #else + { + int ptr = 0; + macro_item *mlast = NULL; + macro_item *m; + uschar name[24]; + uschar *s = argrest; + + while (isspace(*s)) s++; + + if (*s < 'A' || *s > 'Z') + { + fprintf(stderr, "exim: macro name set by -D must start with " + "an upper case letter\n"); + exit(EXIT_FAILURE); + } + + while (isalnum(*s) || *s == '_') + { + if (ptr < sizeof(name)-1) name[ptr++] = *s; + s++; + } + name[ptr] = 0; + if (ptr == 0) { badarg = TRUE; break; } + while (isspace(*s)) s++; + if (*s != 0) + { + if (*s++ != '=') { badarg = TRUE; break; } + while (isspace(*s)) s++; + } + + for (m = macros; m != NULL; m = m->next) + { + if (Ustrcmp(m->name, name) == 0) + { + fprintf(stderr, "exim: duplicated -D in command line\n"); + exit(EXIT_FAILURE); + } + mlast = m; + } + + m = store_get(sizeof(macro_item) + Ustrlen(name)); + m->next = NULL; + m->command_line = TRUE; + if (mlast == NULL) macros = m; else mlast->next = m; + Ustrcpy(m->name, name); + m->replacement = string_copy(s); + + if (clmacro_count >= MAX_CLMACROS) + { + fprintf(stderr, "exim: too many -D options on command line\n"); + exit(EXIT_FAILURE); + } + clmacros[clmacro_count++] = string_sprintf("-D%s=%s", m->name, + m->replacement); + } + #endif + break; + + /* -d: Set debug level (see also -v below) or set the drop_cr option. + The latter is now a no-opt, retained for compatibility only. */ + + case 'd': + if (Ustrcmp(argrest, "ropcr") == 0) + { + /* drop_cr = TRUE; */ + } + + /* Use an intermediate variable so that we don't set debugging while + decoding the debugging bits. */ + + else + { + unsigned int selector = D_default; + debug_selector = 0; + debug_file = NULL; + if (*argrest != 0) + decode_bits(&selector, NULL, argrest, debug_options, + debug_options_count, US"debug"); + debug_selector = selector; + } + break; + + + /* -E: This is a local error message. This option is not intended for + external use at all, but is not restricted to trusted callers because it + does no harm (just suppresses certain error messages) and if Exim is run + not setuid root it won't always be trusted when it generates error + messages using this option. If there is a message id following -E, point + message_reference at it, for logging. */ + + case 'E': + local_error_message = TRUE; + if (mac_ismsgid(argrest)) message_reference = argrest; + break; + + + /* -ex: The vacation program calls sendmail with the undocumented "-eq" + option, so it looks as if historically the -oex options are also callable + without the leading -o. So we have to accept them. Before the switch, + anything starting -oe has been converted to -e. Exim does not support all + of the sendmail error options. */ + + case 'e': + if (Ustrcmp(argrest, "e") == 0) + { + arg_error_handling = ERRORS_SENDER; + errors_sender_rc = EXIT_SUCCESS; + } + else if (Ustrcmp(argrest, "m") == 0) arg_error_handling = ERRORS_SENDER; + else if (Ustrcmp(argrest, "p") == 0) arg_error_handling = ERRORS_STDERR; + else if (Ustrcmp(argrest, "q") == 0) arg_error_handling = ERRORS_STDERR; + else if (Ustrcmp(argrest, "w") == 0) arg_error_handling = ERRORS_SENDER; + else badarg = TRUE; + break; + + + /* -F: Set sender's full name, used instead of the gecos entry from + the password file. Since users can usually alter their gecos entries, + there's no security involved in using this instead. The data can follow + the -F or be in the next argument. */ + + case 'F': + if (*argrest == 0) + { + if(++i < argc) argrest = argv[i]; else + { badarg = TRUE; break; } + } + originator_name = argrest; + break; + + + /* -f: Set sender's address - this value is only actually used if Exim is + run by a trusted user, or if untrusted_set_sender is set and matches the + address, except that the null address can always be set by any user. The + test for this happens later, when the value given here is ignored when not + permitted. For an untrusted user, the actual sender is still put in Sender: + if it doesn't match the From: header (unless no_local_from_check is set). + The data can follow the -f or be in the next argument. The -r switch is an + obsolete form of -f but since there appear to be programs out there that + use anything that sendmail has ever supported, better accept it - the + synonymizing is done before the switch above. + + At this stage, we must allow domain literal addresses, because we don't + know what the setting of allow_domain_literals is yet. Ditto for trailing + dots and strip_trailing_dot. */ + + case 'f': + { + int start, end; + uschar *errmess; + if (*argrest == 0) + { + if (i+1 < argc) argrest = argv[++i]; else + { badarg = TRUE; break; } + } + if (*argrest == 0) + { + sender_address = string_sprintf(""); /* Ensure writeable memory */ + } + else + { + uschar *temp = argrest + Ustrlen(argrest) - 1; + while (temp >= argrest && isspace(*temp)) temp--; + if (temp >= argrest && *temp == '.') f_end_dot = TRUE; + allow_domain_literals = TRUE; + strip_trailing_dot = TRUE; + sender_address = parse_extract_address(argrest, &errmess, &start, &end, + &sender_address_domain, TRUE); + allow_domain_literals = FALSE; + strip_trailing_dot = FALSE; + if (sender_address == NULL) + { + fprintf(stderr, "exim: bad -f address \"%s\": %s\n", argrest, errmess); + return EXIT_FAILURE; + } + } + sender_address_forced = TRUE; + } + break; + + /* This is some Sendmail thing which can be ignored */ + + case 'G': + break; + + /* -h: Set the hop count for an incoming message. Exim does not currently + support this; it always computes it by counting the Received: headers. + To put it in will require a change to the spool header file format. */ + + case 'h': + if (*argrest == 0) + { + if(++i < argc) argrest = argv[i]; else + { badarg = TRUE; break; } + } + if (!isdigit(*argrest)) badarg = TRUE; + break; + + + /* -i: Set flag so dot doesn't end non-SMTP input (same as -oi, seems + not to be documented for sendmail but mailx (at least) uses it) */ + + case 'i': + if (*argrest == 0) dot_ends = FALSE; else badarg = TRUE; + break; + + + case 'M': + receiving_message = FALSE; + + /* -MC: continue delivery of another message via an existing open + file descriptor. This option is used for an internal call by the + smtp transport when there is a pending message waiting to go to an + address to which it has got a connection. Five subsequent arguments are + required: transport name, host name, IP address, sequence number, and + message_id. Transports may decline to create new processes if the sequence + number gets too big. The channel is stdin. This (-MC) must be the last + argument. There's a subsequent check that the real-uid is privileged. + + If we are running in the test harness. delay for a bit, to let the process + that set this one up complete. This makes for repeatability of the logging, + etc. output. */ + + if (Ustrcmp(argrest, "C") == 0) + { + if (argc != i + 6) + { + fprintf(stderr, "exim: too many or too few arguments after -MC\n"); + return EXIT_FAILURE; + } + + if (msg_action_arg >= 0) + { + fprintf(stderr, "exim: incompatible arguments\n"); + return EXIT_FAILURE; + } + + continue_transport = argv[++i]; + continue_hostname = argv[++i]; + continue_host_address = argv[++i]; + continue_sequence = Uatoi(argv[++i]); + msg_action = MSG_DELIVER; + msg_action_arg = ++i; + forced_delivery = TRUE; + queue_run_pid = passed_qr_pid; + queue_run_pipe = passed_qr_pipe; + + if (!mac_ismsgid(argv[i])) + { + fprintf(stderr, "exim: malformed message id %s after -MC option\n", + argv[i]); + return EXIT_FAILURE; + } + + if (running_in_test_harness) millisleep(500); + break; + } + + /* -MCA: set the smtp_authenticated flag; this is useful only when it + precedes -MC (see above). The flag indicates that the host to which + Exim is connected has accepted an AUTH sequence. */ + + else if (Ustrcmp(argrest, "CA") == 0) + { + smtp_authenticated = TRUE; + break; + } + + /* -MCP: set the smtp_use_pipelining flag; this is useful only when + it preceded -MC (see above) */ + + else if (Ustrcmp(argrest, "CP") == 0) + { + smtp_use_pipelining = TRUE; + break; + } + + /* -MCQ: pass on the pid of the queue-running process that started + this chain of deliveries and the fd of its synchronizing pipe; this + is useful only when it precedes -MC (see above) */ + + else if (Ustrcmp(argrest, "CQ") == 0) + { + if(++i < argc) passed_qr_pid = (pid_t)(Uatol(argv[i])); + else badarg = TRUE; + if(++i < argc) passed_qr_pipe = (int)(Uatol(argv[i])); + else badarg = TRUE; + break; + } + + /* -MCS: set the smtp_use_size flag; this is useful only when it + precedes -MC (see above) */ + + else if (Ustrcmp(argrest, "CS") == 0) + { + smtp_use_size = TRUE; + break; + } + + /* -MCT: set the tls_offered flag; this is useful only when it + precedes -MC (see above). The flag indicates that the host to which + Exim is connected has offered TLS support. */ + + #ifdef SUPPORT_TLS + else if (Ustrcmp(argrest, "CT") == 0) + { + tls_offered = TRUE; + break; + } + #endif + + /* -M[x]: various operations on the following list of message ids: + -M deliver the messages, ignoring next retry times and thawing + -Mc deliver the messages, checking next retry times, no thawing + -Mf freeze the messages + -Mg give up on the messages + -Mt thaw the messages + -Mrm remove the messages + In the above cases, this must be the last option. There are also the + following options which are followed by a single message id, and which + act on that message. Some of them use the "recipient" addresses as well. + -Mar add recipient(s) + -Mmad mark all recipients delivered + -Mmd mark recipients(s) delivered + -Mes edit sender + -Mvb show body + -Mvh show header + -Mvl show log + */ + + else if (*argrest == 0) + { + msg_action = MSG_DELIVER; + forced_delivery = deliver_force_thaw = TRUE; + } + else if (Ustrcmp(argrest, "ar") == 0) + { + msg_action = MSG_ADD_RECIPIENT; + one_msg_action = TRUE; + } + else if (Ustrcmp(argrest, "c") == 0) msg_action = MSG_DELIVER; + else if (Ustrcmp(argrest, "es") == 0) + { + msg_action = MSG_EDIT_SENDER; + one_msg_action = TRUE; + } + else if (Ustrcmp(argrest, "f") == 0) msg_action = MSG_FREEZE; + else if (Ustrcmp(argrest, "g") == 0) + { + msg_action = MSG_DELIVER; + deliver_give_up = TRUE; + } + else if (Ustrcmp(argrest, "mad") == 0) + { + msg_action = MSG_MARK_ALL_DELIVERED; + } + else if (Ustrcmp(argrest, "md") == 0) + { + msg_action = MSG_MARK_DELIVERED; + one_msg_action = TRUE; + } + else if (Ustrcmp(argrest, "rm") == 0) msg_action = MSG_REMOVE; + else if (Ustrcmp(argrest, "t") == 0) msg_action = MSG_THAW; + else if (Ustrcmp(argrest, "vb") == 0) + { + msg_action = MSG_SHOW_BODY; + one_msg_action = TRUE; + } + else if (Ustrcmp(argrest, "vh") == 0) + { + msg_action = MSG_SHOW_HEADER; + one_msg_action = TRUE; + } + else if (Ustrcmp(argrest, "vl") == 0) + { + msg_action = MSG_SHOW_LOG; + one_msg_action = TRUE; + } + else { badarg = TRUE; break; } + + /* All the -Mxx options require at least one message id. */ + + msg_action_arg = i + 1; + if (msg_action_arg >= argc) + { + fprintf(stderr, "exim: no message ids given after %s option\n", arg); + return EXIT_FAILURE; + } + + /* Some require only message ids to follow */ + + if (!one_msg_action) + { + int j; + for (j = msg_action_arg; j < argc; j++) if (!mac_ismsgid(argv[j])) + { + fprintf(stderr, "exim: malformed message id %s after %s option\n", + argv[j], arg); + return EXIT_FAILURE; + } + goto END_ARG; /* Remaining args are ids */ + } + + /* Others require only one message id, possibly followed by addresses, + which will be handled as normal arguments. */ + + else + { + if (!mac_ismsgid(argv[msg_action_arg])) + { + fprintf(stderr, "exim: malformed message id %s after %s option\n", + argv[msg_action_arg], arg); + return EXIT_FAILURE; + } + i++; + } + break; + + + /* Some programs seem to call the -om option without the leading o; + for sendmail it askes for "me too". Exim always does this. */ + + case 'm': + if (*argrest != 0) badarg = TRUE; + break; + + + /* -N: don't do delivery - a debugging option that stops transports doing + their thing. It implies debugging at the D_v level. */ + + case 'N': + if (*argrest == 0) + { + dont_deliver = TRUE; + debug_selector |= D_v; + debug_file = stderr; + } + else badarg = TRUE; + break; + + + /* -n: This means "don't alias" in sendmail, apparently. Just ignore + it. */ + + case 'n': + break; + + /* -O: Just ignore it. In sendmail, apparently -O option=value means set + option to the specified value. This form uses long names. We need to handle + -O option=value and -Ooption=value. */ + + case 'O': + if (*argrest == 0) + { + if (++i >= argc) + { + fprintf(stderr, "exim: string expected after -O\n"); + exit(EXIT_FAILURE); + } + } + break; + + case 'o': + + /* -oA: Set an argument for the bi command (sendmail's "alternate alias + file" option). */ + + if (*argrest == 'A') + { + alias_arg = argrest + 1; + if (alias_arg[0] == 0) + { + if (i+1 < argc) alias_arg = argv[++i]; else + { + fprintf(stderr, "exim: string expected after -oA\n"); + exit(EXIT_FAILURE); + } + } + } + + /* -oB: Set a connection message max value for remote deliveries */ + + else if (*argrest == 'B') + { + uschar *p = argrest + 1; + if (p[0] == 0) + { + if (i+1 < argc && isdigit((argv[i+1][0]))) p = argv[++i]; else + { + connection_max_messages = 1; + p = NULL; + } + } + + if (p != NULL) + { + if (!isdigit(*p)) + { + fprintf(stderr, "exim: number expected after -oB\n"); + exit(EXIT_FAILURE); + } + connection_max_messages = Uatoi(p); + } + } + + /* -odb: background delivery */ + + else if (Ustrcmp(argrest, "db") == 0) + { + synchronous_delivery = FALSE; + arg_queue_only = FALSE; + queue_only_set = TRUE; + } + + /* -odf: foreground delivery (smail-compatible option); same effect as + -odi: interactive (synchronous) delivery (sendmail-compatible option) + */ + + else if (Ustrcmp(argrest, "df") == 0 || Ustrcmp(argrest, "di") == 0) + { + synchronous_delivery = TRUE; + arg_queue_only = FALSE; + queue_only_set = TRUE; + } + + /* -odq: queue only */ + + else if (Ustrcmp(argrest, "dq") == 0) + { + synchronous_delivery = FALSE; + arg_queue_only = TRUE; + queue_only_set = TRUE; + } + + /* -odqs: queue SMTP only - do local deliveries and remote routing, + but no remote delivery */ + + else if (Ustrcmp(argrest, "dqs") == 0) + { + queue_smtp = TRUE; + arg_queue_only = FALSE; + queue_only_set = TRUE; + } + + /* -oex: Sendmail error flags. As these are also accepted without the + leading -o prefix, for compatibility with vacation and other callers, + they are handled with -e above. */ + + /* -oi: Set flag so dot doesn't end non-SMTP input (same as -i) + -oitrue: Another sendmail syntax for the same */ + + else if (Ustrcmp(argrest, "i") == 0 || + Ustrcmp(argrest, "itrue") == 0) + dot_ends = FALSE; + + /* -oM*: Set various characteristics for an incoming message; actually + acted on for trusted callers only. */ + + else if (*argrest == 'M') + { + if (i+1 >= argc) + { + fprintf(stderr, "exim: data expected after -o%s\n", argrest); + exit(EXIT_FAILURE); + } + + /* -oMa: Set sender host address */ + + if (Ustrcmp(argrest, "Ma") == 0) sender_host_address = argv[++i]; + + /* -oMaa: Set authenticator name */ + + else if (Ustrcmp(argrest, "Maa") == 0) + sender_host_authenticated = argv[++i]; + + /* -oMas: setting authenticated sender */ + + else if (Ustrcmp(argrest, "Mas") == 0) authenticated_sender = argv[++i]; + + /* -oMai: setting authenticated id */ + + else if (Ustrcmp(argrest, "Mai") == 0) authenticated_id = argv[++i]; + + /* -oMi: Set incoming interface address */ + + else if (Ustrcmp(argrest, "Mi") == 0) interface_address = argv[++i]; + + /* -oMr: Received protocol */ + + else if (Ustrcmp(argrest, "Mr") == 0) received_protocol = argv[++i]; + + /* -oMs: Set sender host name */ + + else if (Ustrcmp(argrest, "Ms") == 0) sender_host_name = argv[++i]; + + /* -oMt: Set sender ident */ + + else if (Ustrcmp(argrest, "Mt") == 0) sender_ident = argv[++i]; + + /* Else a bad argument */ + + else + { + badarg = TRUE; + break; + } + } + + /* -om: Me-too flag for aliases. Exim always does this. Some programs + seem to call this as -m (undocumented), so that is also accepted (see + above). */ + + else if (Ustrcmp(argrest, "m") == 0) {} + + /* -oo: An ancient flag for old-style addresses which still seems to + crop up in some calls (see in SCO). */ + + else if (Ustrcmp(argrest, "o") == 0) {} + + /* -oP : set pid file path for daemon */ + + else if (Ustrcmp(argrest, "P") == 0) + override_pid_file_path = argv[++i]; + + /* -or : set timeout for non-SMTP acceptance + -os : set timeout for SMTP acceptance */ + + else if (*argrest == 'r' || *argrest == 's') + { + int *tp = (*argrest == 'r')? + &arg_receive_timeout : &arg_smtp_receive_timeout; + if (argrest[1] == 0) + { + if (i+1 < argc) *tp= readconf_readtime(argv[++i], 0, FALSE); + } + else *tp = readconf_readtime(argrest + 1, 0, FALSE); + if (*tp < 0) + { + fprintf(stderr, "exim: bad time value %s: abandoned\n", argv[i]); + exit(EXIT_FAILURE); + } + } + + /* -oX : Override local_interfaces and/or default daemon ports */ + + else if (Ustrcmp(argrest, "X") == 0) + override_local_interfaces = argv[++i]; + + /* Unknown -o argument */ + + else badarg = TRUE; + break; + + + /* -ps: force Perl startup; -pd force delayed Perl startup */ + + case 'p': + #ifdef EXIM_PERL + if (*argrest == 's' && argrest[1] == 0) + { + perl_start_option = 1; + break; + } + if (*argrest == 'd' && argrest[1] == 0) + { + perl_start_option = -1; + break; + } + #endif + + /* -panythingelse is taken as the Sendmail-compatible argument -prval:sval, + which sets the host protocol and host name */ + + if (*argrest == 0) + { + if (i+1 < argc) argrest = argv[++i]; else + { badarg = TRUE; break; } + } + + if (*argrest != 0) + { + uschar *hn = Ustrchr(argrest, ':'); + if (hn == NULL) + { + received_protocol = argrest; + } + else + { + received_protocol = string_copyn(argrest, hn - argrest); + sender_host_name = hn + 1; + } + } + break; + + + case 'q': + receiving_message = FALSE; + + /* -qq...: Do queue runs in a 2-stage manner */ + + if (*argrest == 'q') + { + queue_2stage = TRUE; + argrest++; + } + + /* -qi...: Do only first (initial) deliveries */ + + if (*argrest == 'i') + { + queue_run_first_delivery = TRUE; + argrest++; + } + + /* -qf...: Run the queue, forcing deliveries + -qff..: Ditto, forcing thawing as well */ + + if (*argrest == 'f') + { + queue_run_force = TRUE; + if (*(++argrest) == 'f') + { + deliver_force_thaw = TRUE; + argrest++; + } + } + + /* -q[f][f]l...: Run the queue only on local deliveries */ + + if (*argrest == 'l') + { + queue_run_local = TRUE; + argrest++; + } + + /* -q[f][f][l]: Run the queue, optionally forced, optionally local only, + optionally starting from a given message id. */ + + if (*argrest == 0 && + (i + 1 >= argc || argv[i+1][0] == '-' || mac_ismsgid(argv[i+1]))) + { + queue_interval = 0; + if (i+1 < argc && mac_ismsgid(argv[i+1])) + start_queue_run_id = argv[++i]; + if (i+1 < argc && mac_ismsgid(argv[i+1])) + stop_queue_run_id = argv[++i]; + } + + /* -q[f][f][l]: Run the queue at regular intervals, optionally forced, + optionally local only. */ + + else + { + if (*argrest != 0) + queue_interval = readconf_readtime(argrest, 0, FALSE); + else + queue_interval = readconf_readtime(argv[++i], 0, FALSE); + if (queue_interval <= 0) + { + fprintf(stderr, "exim: bad time value %s: abandoned\n", argv[i]); + exit(EXIT_FAILURE); + } + } + break; + + + case 'R': /* Synonymous with -qR... */ + receiving_message = FALSE; + + /* -Rf: As -R (below) but force all deliveries, + -Rff: Ditto, but also thaw all frozen messages, + -Rr: String is regex + -Rrf: Regex and force + -Rrff: Regex and force and thaw + + in all cases provided there are no further characters in this + argument. */ + + if (*argrest != 0) + { + int i; + for (i = 0; i < sizeof(rsopts)/sizeof(uschar *); i++) + { + if (Ustrcmp(argrest, rsopts[i]) == 0) + { + if (i != 2) queue_run_force = TRUE; + if (i >= 2) deliver_selectstring_regex = TRUE; + if (i == 1 || i == 4) deliver_force_thaw = TRUE; + argrest += Ustrlen(rsopts[i]); + } + } + } + + /* -R: Set string to match in addresses for forced queue run to + pick out particular messages. */ + + if (*argrest == 0) + { + if (i+1 < argc) deliver_selectstring = argv[++i]; else + { + fprintf(stderr, "exim: string expected after -R\n"); + exit(EXIT_FAILURE); + } + } + else deliver_selectstring = argrest; + if (queue_interval < 0) queue_interval = 0; + break; + + + /* -r: an obsolete synonym for -f (see above) */ + + + /* -S: Like -R but works on sender. */ + + case 'S': /* Synonymous with -qS... */ + receiving_message = FALSE; + + /* -Sf: As -S (below) but force all deliveries, + -Sff: Ditto, but also thaw all frozen messages, + -Sr: String is regex + -Srf: Regex and force + -Srff: Regex and force and thaw + + in all cases provided there are no further characters in this + argument. */ + + if (*argrest != 0) + { + int i; + for (i = 0; i < sizeof(rsopts)/sizeof(uschar *); i++) + { + if (Ustrcmp(argrest, rsopts[i]) == 0) + { + if (i != 2) queue_run_force = TRUE; + if (i >= 2) deliver_selectstring_sender_regex = TRUE; + if (i == 1 || i == 4) deliver_force_thaw = TRUE; + argrest += Ustrlen(rsopts[i]); + } + } + } + + /* -S: Set string to match in addresses for forced queue run to + pick out particular messages. */ + + if (*argrest == 0) + { + if (i+1 < argc) deliver_selectstring_sender = argv[++i]; else + { + fprintf(stderr, "exim: string expected after -S\n"); + exit(EXIT_FAILURE); + } + } + else deliver_selectstring_sender = argrest; + if (queue_interval < 0) queue_interval = 0; + break; + + /* -Tqt is an option that is exclusively for use by the testing suite. + It is not recognized in other circumstances. It allows for the setting up + of explicit "queue times" so that various warning/retry things can be + tested. Otherwise variability of clock ticks etc. cause problems. */ + + case 'T': + if (running_in_test_harness && Ustrcmp(argrest, "qt") == 0) + fudged_queue_times = argv[++i]; + else badarg = TRUE; + break; + + + /* -t: Set flag to extract recipients from body of message. */ + + case 't': + if (*argrest == 0) extract_recipients = TRUE; + + /* -ti: Set flag to extract recipients from body of message, and also + specify that dot does not end the message. */ + + else if (Ustrcmp(argrest, "i") == 0) + { + extract_recipients = TRUE; + dot_ends = FALSE; + } + + /* -tls-on-connect: don't wait for STARTTLS (for old clients) */ + + #ifdef SUPPORT_TLS + else if (Ustrcmp(argrest, "ls-on-connect") == 0) tls_on_connect = TRUE; + #endif + + else badarg = TRUE; + break; + + + /* -U: This means "initial user submission" in sendmail, apparently. The + doc claims that in future sendmail may refuse syntactically invalid + messages instead of fixing them. For the moment, we just ignore it. */ + + case 'U': + break; + + + /* -v: verify things - this is a very low-level debugging */ + + case 'v': + if (*argrest == 0) + { + debug_selector |= D_v; + debug_file = stderr; + } + else badarg = TRUE; + break; + + + /* -x: AIX uses this to indicate some fancy 8-bit character stuff: + + The -x flag tells the sendmail command that mail from a local + mail program has National Language Support (NLS) extended characters + in the body of the mail item. The sendmail command can send mail with + extended NLS characters across networks that normally corrupts these + 8-bit characters. + + As Exim is 8-bit clean, it just ignores this flag. */ + + case 'x': + if (*argrest != 0) badarg = TRUE; + break; + + /* All other initial characters are errors */ + + default: + badarg = TRUE; + break; + } /* End of high-level switch statement */ + + /* Failed to recognize the option, or syntax error */ + + if (badarg) + { + fprintf(stderr, "exim abandoned: unknown, malformed, or incomplete " + "option %s\n", arg); + exit(EXIT_FAILURE); + } + } + + +/* Arguments have been processed. Check for incompatibilities. */ + +END_ARG: +if (( + (smtp_input || extract_recipients || recipients_arg < argc) && + (daemon_listen || queue_interval >= 0 || bi_option || + test_retry_arg >= 0 || test_rewrite_arg >= 0 || + filter_test != NULL || (msg_action_arg > 0 && !one_msg_action)) + ) || + ( + msg_action_arg > 0 && + (daemon_listen || queue_interval >= 0 || list_options || checking || + bi_option || test_retry_arg >= 0 || test_rewrite_arg >= 0) + ) || + ( + (daemon_listen || queue_interval >= 0) && + (sender_address != NULL || list_options || list_queue || checking || + bi_option) + ) || + ( + daemon_listen && queue_interval == 0 + ) || + ( + list_options && + (checking || smtp_input || extract_recipients || + filter_test != NULL || bi_option) + ) || + ( + verify_address_mode && + (address_test_mode || smtp_input || extract_recipients || + filter_test != NULL || bi_option) + ) || + ( + address_test_mode && (smtp_input || extract_recipients || + filter_test != NULL || bi_option) + ) || + ( + smtp_input && (sender_address != NULL || filter_test != NULL || + extract_recipients) + ) || + ( + deliver_selectstring != NULL && queue_interval < 0 + ) + ) + { + fprintf(stderr, "exim: incompatible command-line options or arguments\n"); + exit(EXIT_FAILURE); + } + +/* If debugging is set up, set the file and the file descriptor to pass on to +child processes. It should, of course, be 2 for stderr. Also, force the daemon +to run in the foreground. */ + +if (debug_selector != 0) + { + debug_file = stderr; + debug_fd = fileno(debug_file); + background_daemon = FALSE; + if (running_in_test_harness) millisleep(100); /* lets caller finish */ + if (debug_selector != D_v) /* -v only doesn't show this */ + { + debug_printf("Exim version %s uid=%ld gid=%ld pid=%d D=%x\n", + version_string, (long int)real_uid, (long int)real_gid, (int)getpid(), + debug_selector); + show_whats_supported(stderr); + } + } + +/* When started with root privilege, ensure that the limits on the number of +open files and the number of processes (where that is accessible) are +sufficiently large, or are unset, in case Exim has been called from an +environment where the limits are screwed down. Not all OS have the ability to +change some of these limits. */ + +if (unprivileged) + { + DEBUG(D_any) debug_print_ids(US"Exim has no root privilege:"); + } +else + { + struct rlimit rlp; + + #ifdef RLIMIT_NOFILE + if (getrlimit(RLIMIT_NOFILE, &rlp) < 0) + { + log_write(0, LOG_MAIN|LOG_PANIC, "getrlimit(RLIMIT_NOFILE) failed: %s", + strerror(errno)); + rlp.rlim_cur = rlp.rlim_max = 0; + } + if (rlp.rlim_cur < 1000) + { + rlp.rlim_cur = rlp.rlim_max = 1000; + if (setrlimit(RLIMIT_NOFILE, &rlp) < 0) + log_write(0, LOG_MAIN|LOG_PANIC, "setrlimit(RLIMIT_NOFILE) failed: %s", + strerror(errno)); + } + #endif + + #ifdef RLIMIT_NPROC + if (getrlimit(RLIMIT_NPROC, &rlp) < 0) + { + log_write(0, LOG_MAIN|LOG_PANIC, "getrlimit(RLIMIT_NPROC) failed: %s", + strerror(errno)); + rlp.rlim_cur = rlp.rlim_max = 0; + } + + #ifdef RLIM_INFINITY + if (rlp.rlim_cur != RLIM_INFINITY && rlp.rlim_cur < 1000) + { + rlp.rlim_cur = rlp.rlim_max = RLIM_INFINITY; + #else + if (rlp.rlim_cur < 1000) + { + rlp.rlim_cur = rlp.rlim_max = 1000; + #endif + if (setrlimit(RLIMIT_NPROC, &rlp) < 0) + log_write(0, LOG_MAIN|LOG_PANIC, "setrlimit(RLIMIT_NPROC) failed: %s", + strerror(errno)); + } + #endif + } + +/* Exim is normally entered as root (but some special configurations are +possible that don't do this). However, it always spins off sub-processes that +set their uid and gid as required for local delivery. We don't want to pass on +any extra groups that root may belong to, so we want to get rid of them all at +this point. + +We need to obey setgroups() at this stage, before possibly giving up root +privilege for a changed configuration file, but later on we might need to +check on the additional groups for the admin user privilege - can't do that +till after reading the config, which might specify the exim gid. Therefore, +save the group list here first. */ + +group_count = getgroups(NGROUPS_MAX, group_list); + +/* There is a fundamental difference in some BSD systems in the matter of +groups. FreeBSD and BSDI are known to be different; NetBSD and OpenBSD are +known not to be different. On the "different" systems there is a single group +list, and the first entry in it is the current group. On all other versions of +Unix there is a supplementary group list, which is in *addition* to the current +group. Consequently, to get rid of all extraneous groups on a "standard" system +you pass over 0 groups to setgroups(), while on a "different" system you pass +over a single group - the current group, which is always the first group in the +list. Calling setgroups() with zero groups on a "different" system results in +an error return. The following code should cope with both types of system. + +However, if this process isn't running as root, setgroups() can't be used +since you have to be root to run it, even if throwing away groups. Not being +root here happens only in some unusual configurations. We just ignore the +error. */ + +if (setgroups(0, NULL) != 0) + { + if (setgroups(1, group_list) != 0 && !unprivileged) + { + fprintf(stderr, "exim: setgroups() failed: %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + } + +/* If the configuration file name has been altered by an argument on the +command line (either a new file name or a macro definition) and the caller is +not root or the exim user, or if this is a filter testing run, remove any +setuid privilege the program has, and run as the underlying user. + +If ALT_CONFIG_ROOT_ONLY is defined, the exim user is locked out of this, which +severely restricts the use of -C for some purposes. + +Otherwise, set the real ids to the effective values (should be root unless run +from inetd, which it can either be root or the exim uid, if one is configured). + +There is a private mechanism for bypassing some of this, in order to make it +possible to test lots of configurations automatically, without having either to +recompile each time, or to patch in an actual configuration file name and other +values (such as the path name). If running in the test harness, pretend that +configuration file changes and macro definitions haven't happened. */ + +if (( /* EITHER */ + (config_changed || macros != NULL) && /* Config changed, and */ + real_uid != root_uid && /* Not root, and */ + #ifndef ALT_CONFIG_ROOT_ONLY /* (when not locked out) */ + real_uid != exim_uid && /* Not exim, and */ + #endif + !running_in_test_harness /* Not fudged */ + ) || /* OR */ + expansion_test /* expansion testing */ + || /* OR */ + filter_test != NULL) /* Filter testing */ + { + setgroups(group_count, group_list); + exim_setugid(real_uid, real_gid, FALSE, + US"-C, -D, -be or -bf forces real uid"); + removed_privilege = TRUE; + + /* In the normal case when Exim is called like this, stderr is available + and should be used for any logging information because attempts to write + to the log will usually fail. To arrange this, we unset really_exim. However, + if no stderr is available there is no point - we might as well have a go + at the log (if it fails, syslog will be written). */ + + if (log_stderr != NULL) really_exim = FALSE; + } + +/* Privilege is to be retained for the moment. It may be dropped later, +depending on the job that this Exim process has been asked to do. For now, set +the real uid to the effective so that subsequent re-execs of Exim are done by a +privileged user. */ + +else exim_setugid(geteuid(), getegid(), FALSE, US"forcing real = effective"); + +/* If testing a filter, open the file now, before wasting time doing other +setups and reading the message. */ + +if (filter_test != NULL) + { + filter_fd = Uopen(filter_test, O_RDONLY,0); + if (filter_fd < 0) + { + fprintf(stderr, "exim: failed to open %s: %s\n", filter_test, + strerror(errno)); + return EXIT_FAILURE; + } + } + +/* Read the main runtime configuration data; this gives up if there +is a failure. It leaves the configuration file open so that the subsequent +configuration data for delivery can be read if needed. */ + +readconf_main(); + +/* Handle the decoding of logging options. */ + +decode_bits(&log_write_selector, &log_extra_selector, log_selector_string, + log_options, log_options_count, US"log"); + +DEBUG(D_any) + { + debug_printf("configuration file is %s\n", config_main_filename); + debug_printf("log selectors = %08x %08x\n", log_write_selector, + log_extra_selector); + } + +/* If domain literals are not allowed, check the sender address that was +supplied with -f. Ditto for a stripped trailing dot. */ + +if (sender_address != NULL) + { + if (sender_address[sender_address_domain] == '[' && !allow_domain_literals) + { + fprintf(stderr, "exim: bad -f address \"%s\": domain literals not " + "allowed\n", sender_address); + return EXIT_FAILURE; + } + if (f_end_dot && !strip_trailing_dot) + { + fprintf(stderr, "exim: bad -f address \"%s.\": domain is malformed " + "(trailing dot not allowed)\n", sender_address); + return EXIT_FAILURE; + } + } + +/* Paranoia check of maximum lengths of certain strings. There is a check +on the length of the log file path in log.c, which will come into effect +if there are any calls to write the log earlier than this. However, if we +get this far but the string is very long, it is better to stop now than to +carry on and (e.g.) receive a message and then have to collapse. The call to +log_write() from here will cause the ultimate panic collapse if the complete +file name exceeds the buffer length. */ + +if (Ustrlen(log_file_path) > 200) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, + "log_file_path is longer than 200 chars: aborting"); + +if (Ustrlen(pid_file_path) > 200) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, + "pid_file_path is longer than 200 chars: aborting"); + +if (Ustrlen(spool_directory) > 200) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, + "spool_directory is longer than 200 chars: aborting"); + +/* Length check on the process name given to syslog for its TAG field, +which is only permitted to be 32 characters or less. See RFC 3164. */ + +if (Ustrlen(syslog_processname) > 32) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, + "syslog_processname is longer than 32 chars: aborting"); + +/* In some operating systems, the environment variable TMPDIR controls where +temporary files are created; Exim doesn't use these (apart from when delivering +to MBX mailboxes), but called libraries such as DBM libraries may require them. +If TMPDIR is found in the environment, reset it to the value defined in the +TMPDIR macro, if this macro is defined. */ + +#ifdef TMPDIR + { + uschar **p; + for (p = USS environ; *p != NULL; p++) + { + if (Ustrncmp(*p, "TMPDIR=", 7) == 0 && + Ustrcmp(*p+7, TMPDIR) != 0) + { + uschar *newp = malloc(Ustrlen(TMPDIR) + 8); + sprintf(CS newp, "TMPDIR=%s", TMPDIR); + *p = newp; + DEBUG(D_any) debug_printf("reset TMPDIR=%s in environment\n", TMPDIR); + } + } + } +#endif + +/* Timezone handling. If timezone_string is "utc", set a flag to cause all +timestamps to be in UTC (gmtime() is used instead of localtime()). Otherwise, +we may need to get rid of a bogus timezone setting. This can arise when Exim is +called by a user who has set the TZ variable. This then affects the timestamps +in log files and in Received: headers, and any created Date: header lines. The +required timezone is settable in the configuration file, so nothing can be done +about this earlier - but hopefully nothing will normally be logged earlier than +this. We have to make a new environment if TZ is wrong, but don't bother if +timestamps_utc is set, because then all times are in UTC anyway. */ + +if (timezone_string != NULL && strcmpic(timezone_string, US"UTC") == 0) + { + timestamps_utc = TRUE; + } +else + { + uschar *envtz = US getenv("TZ"); + if ((envtz == NULL && timezone_string != NULL) || + (envtz != NULL && + (timezone_string == NULL || + Ustrcmp(timezone_string, envtz) != 0))) + { + uschar **p = USS environ; + uschar **new; + uschar **newp; + int count = 0; + while (*p++ != NULL) count++; + if (envtz == NULL) count++; + newp = new = malloc(sizeof(uschar *) * (count + 1)); + for (p = USS environ; *p != NULL; p++) + { + if (Ustrncmp(*p, "TZ=", 3) == 0) continue; + *newp++ = *p; + } + if (timezone_string != NULL) + { + *newp = malloc(Ustrlen(timezone_string) + 4); + sprintf(CS *newp++, "TZ=%s", timezone_string); + } + *newp = NULL; + environ = CSS new; + tzset(); + DEBUG(D_any) debug_printf("Reset TZ to %s: time is %s\n", timezone_string, + tod_stamp(tod_log)); + } + } + +/* Handle the case when we have removed the setuid privilege because of -C or +-D. This means that the caller of Exim was not root, and, provided that +ALT_CONFIG_ROOT_ONLY is not defined, was not the Exim user that is built into +the binary. + +If ALT_CONFIG_ROOT_ONLY is not defined, there is a problem if it turns out we +were running as the exim user defined in the configuration file (different to +the one in the binary). The sysadmin may expect this case to retain privilege +because "the binary was called by the Exim user", but it hasn't, because of the +order in which it handles this stuff. There are two possibilities: + + (1) If deliver_drop_privilege is set, Exim is not going to re-exec in order + to do message deliveries. Thus, the fact that it is running as a + non-privileged user is plausible, and might be wanted in some special + configurations. However, really_exim will have been set false when + privilege was dropped, to stop Exim trying to write to its normal log + files. Therefore, re-enable normal log processing, assuming the sysadmin + has set up the log directory correctly. + + (2) If deliver_drop_privilege is not set, the configuration won't work as + apparently intended, and so we log a panic message. In order to retain + root for -C or -D, the caller must either be root or the Exim user + defined in the binary (when deliver_drop_ privilege is false). + +If ALT_CONFIG_ROOT_ONLY is defined, we don't know whether we were called by the +built-in exim user or one defined in the configuration. In either event, +re-enable log processing, assuming the sysadmin knows what they are doing. */ + +if (removed_privilege && (config_changed || macros != NULL) && + real_uid == exim_uid) + { + #ifdef ALT_CONFIG_ROOT_ONLY + really_exim = TRUE; /* let logging work normally */ + #else + + if (deliver_drop_privilege) + really_exim = TRUE; /* let logging work normally */ + else + log_write(0, LOG_MAIN|LOG_PANIC, + "exim user (uid=%d) is defined only at runtime; privilege lost for %s", + (int)exim_uid, config_changed? "-C" : "-D"); + #endif + } + +/* Start up Perl interpreter if Perl support is configured and there is a +perl_startup option, and the configuration or the command line specifies +initializing starting. Note that the global variables are actually called +opt_perl_xxx to avoid clashing with perl's namespace (perl_*). */ + +#ifdef EXIM_PERL +if (perl_start_option != 0) + opt_perl_at_start = (perl_start_option > 0); +if (opt_perl_at_start && opt_perl_startup != NULL) + { + uschar *errstr; + DEBUG(D_any) debug_printf("Starting Perl interpreter\n"); + errstr = init_perl(opt_perl_startup); + if (errstr != NULL) + { + fprintf(stderr, "exim: error in perl_startup code: %s\n", errstr); + return EXIT_FAILURE; + } + opt_perl_started = TRUE; + } +#endif /* EXIM_PERL */ + +/* Log the arguments of the call if the configuration file said so. This is +a debugging feature for finding out what arguments certain MUAs actually use. +Don't attempt it if logging is disabled, or if listing variables or if +verifying/testing addresses or expansions. */ + +if ((log_extra_selector & LX_arguments) != 0 && really_exim + && !list_options && !checking) + { + int i; + uschar *p = big_buffer; + Ustrcpy(p, "cwd="); + (void)getcwd(CS p+4, big_buffer_size - 4); + while (*p) p++; + (void)string_format(p, big_buffer_size - (p - big_buffer), " %d args:", argc); + while (*p) p++; + for (i = 0; i < argc; i++) + { + int len = Ustrlen(argv[i]); + uschar *printing; + uschar *quote; + if (p + len + 8 >= big_buffer + big_buffer_size) + { + Ustrcpy(p, " ..."); + log_write(0, LOG_MAIN, "%s", big_buffer); + Ustrcpy(big_buffer, "..."); + p = big_buffer + 3; + } + printing = string_printing(argv[i]); + if (printing[0] == 0) quote = US"\""; else + { + uschar *pp = printing; + quote = US""; + while (*pp != 0) if (isspace(*pp++)) { quote = US"\""; break; } + } + sprintf(CS p, " %s%.*s%s", quote, (int)(big_buffer_size - + (p - big_buffer) - 4), printing, quote); + while (*p) p++; + } + log_write(0, LOG_MAIN, "%s", big_buffer); + } + +/* Set the working directory to be the top-level spool directory. We don't rely +on this in the code, which always uses fully qualified names, but it's useful +for core dumps etc. Don't complain if it fails - the spool directory might not +be generally accessible and calls with the -C option (and others) have lost +privilege by now. */ + +if (Uchdir(spool_directory) != 0) + { + (void)directory_make(spool_directory, US"", SPOOL_DIRECTORY_MODE, TRUE); + (void)Uchdir(spool_directory); + } + +/* Handle calls with the -bi option. This is a sendmail option to rebuild *the* +alias file. Exim doesn't have such a concept, but this call is screwed into +Sun's YP makefiles. Handle this by calling a configured script, as the real +user who called Exim. The -oA option can be used to pass an argument to the +script. */ + +if (bi_option) + { + fclose(config_file); + if (bi_command != NULL) + { + int i = 0; + uschar *argv[3]; + argv[i++] = bi_command; + if (alias_arg != NULL) argv[i++] = alias_arg; + argv[i++] = NULL; + + setgroups(group_count, group_list); + exim_setugid(real_uid, real_gid, FALSE, US"running bi_command"); + + DEBUG(D_exec) debug_printf("exec %.256s %.256s\n", argv[0], + (argv[1] == NULL)? US"" : argv[1]); + + execv(CS argv[0], (char *const *)argv); + fprintf(stderr, "exim: exec failed: %s\n", strerror(errno)); + exit(EXIT_FAILURE); + } + else + { + DEBUG(D_any) debug_printf("-bi used but bi_command not set; exiting\n"); + exit(EXIT_SUCCESS); + } + } + +/* If an action on specific messages is requested, or if a daemon or queue +runner is being started, we need to know if Exim was called by an admin user. +This is the case if the real user is root or exim, or if the real group is +exim, or if one of the supplementary groups is exim or a group listed in +admin_groups. We don't fail all message actions immediately if not admin_user, +since some actions can be performed by non-admin users. Instead, set admin_user +for later interrogation. */ + +if (real_uid == root_uid || real_uid == exim_uid || real_gid == exim_gid) + admin_user = TRUE; +else + { + int i, j; + + for (i = 0; i < group_count; i++) + { + if (group_list[i] == exim_gid) admin_user = TRUE; + else if (admin_groups != NULL) + { + for (j = 1; j <= (int)(admin_groups[0]); j++) + if (admin_groups[j] == group_list[i]) + { admin_user = TRUE; break; } + } + if (admin_user) break; + } + } + +/* Another group of privileged users are the trusted users. These are root, +exim, and any caller matching trusted_users or trusted_groups. Trusted callers +are permitted to specify sender_addresses with -f on the command line, and +other message parameters as well. */ + +if (real_uid == root_uid || real_uid == exim_uid) + trusted_caller = TRUE; +else + { + int i, j; + + if (trusted_users != NULL) + { + for (i = 1; i <= (int)(trusted_users[0]); i++) + if (trusted_users[i] == real_uid) + { trusted_caller = TRUE; break; } + } + + if (!trusted_caller && trusted_groups != NULL) + { + for (i = 1; i <= (int)(trusted_groups[0]); i++) + { + if (trusted_groups[i] == real_gid) + trusted_caller = TRUE; + else for (j = 0; j < group_count; j++) + { + if (trusted_groups[i] == group_list[j]) + { trusted_caller = TRUE; break; } + } + if (trusted_caller) break; + } + } + } + +if (trusted_caller) DEBUG(D_any) debug_printf("trusted user\n"); +if (admin_user) DEBUG(D_any) debug_printf("admin user\n"); + +/* Only an admin user may start the daemon or force a queue run in the default +configuration, but the queue run restriction can be relaxed. Only an admin +user may request that a message be returned to its sender forthwith. Only an +admin user may specify a debug level greater than D_v (because it might show +passwords, etc. in lookup queries). Only an admin user may request a queue +count. */ + +if (!admin_user) + { + BOOL debugset = (debug_selector & ~D_v) != 0; + if (deliver_give_up || daemon_listen || + (count_queue && queue_list_requires_admin) || + (list_queue && queue_list_requires_admin) || + (queue_interval >= 0 && prod_requires_admin) || + (debugset && !running_in_test_harness)) + { + fprintf(stderr, "exim:%s permission denied\n", debugset? " debugging" : ""); + exit(EXIT_FAILURE); + } + } + +/* If the real user is not root or the exim uid, the argument for passing +in an open TCP/IP connection for another message is not permitted, nor is +running with the -N option for any delivery action, unless this call to exim is +one that supplied an input message, or we are using a patched exim for +regression testing. */ + +if (real_uid != root_uid && real_uid != exim_uid && + (continue_hostname != NULL || + (dont_deliver && + (queue_interval >= 0 || daemon_listen || msg_action_arg > 0) + )) && !running_in_test_harness) + { + fprintf(stderr, "exim: Permission denied\n"); + return EXIT_FAILURE; + } + +/* If the caller is not trusted, certain arguments are ignored when running for +real, but are permitted when checking things (-be, -bv, -bt, -bh, -bf). Note +that authority for performing certain actions on messages is tested in the +queue_action() function. */ + +if (!trusted_caller && !checking && filter_test == NULL) + { + sender_host_name = sender_host_address = interface_address = + sender_ident = received_protocol = NULL; + sender_host_port = interface_port = 0; + sender_host_authenticated = authenticated_sender = authenticated_id = NULL; + } + +/* If a sender host address is set, extract the optional port number off the +end of it and check its syntax. Do the same thing for the interface address. +Exim exits if the syntax is bad. */ + +else + { + if (sender_host_address != NULL) + sender_host_port = check_port(sender_host_address); + if (interface_address != NULL) + interface_port = check_port(interface_address); + } + +/* If an SMTP message is being received check to see if the standard input is a +TCP/IP socket. If it is, we assume that Exim was called from inetd if the +caller is root or the Exim user, or if the port is a privileged one. Otherwise, +barf. */ + +if (smtp_input) + { + union sockaddr_46 inetd_sock; + SOCKLEN_T size = sizeof(inetd_sock); + if (getpeername(0, (struct sockaddr *)(&inetd_sock), &size) == 0) + { + int family = ((struct sockaddr *)(&inetd_sock))->sa_family; + if (family == AF_INET || family == AF_INET6) + { + union sockaddr_46 interface_sock; + size = sizeof(interface_sock); + + if (getsockname(0, (struct sockaddr *)(&interface_sock), &size) == 0) + interface_address = host_ntoa(-1, &interface_sock, NULL, + &interface_port); + + if (host_is_tls_on_connect_port(interface_port)) tls_on_connect = TRUE; + + if (real_uid == root_uid || real_uid == exim_uid || interface_port < 1024) + { + is_inetd = TRUE; + sender_host_address = host_ntoa(-1, (struct sockaddr *)(&inetd_sock), + NULL, &sender_host_port); + if (mua_wrapper) log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Input from " + "inetd is not supported when mua_wrapper is set"); + } + else + { + fprintf(stderr, + "exim: Permission denied (unprivileged user, unprivileged port)\n"); + return EXIT_FAILURE; + } + } + } + } + +/* If the load average is going to be needed while receiving a message, get it +now for those OS that require the first call to os_getloadavg() to be done as +root. There will be further calls later for each message received. */ + +#ifdef LOAD_AVG_NEEDS_ROOT +if (receiving_message && + (queue_only_load >= 0 || + (is_inetd && smtp_load_reserve >= 0) + )) + { + load_average = os_getloadavg(); + } +#endif + +/* The queue_only configuration option can be overridden by -odx on the command +line, except that if queue_only_override is false, queue_only cannot be unset +from the command line. */ + +if (queue_only_set && (queue_only_override || arg_queue_only)) + queue_only = arg_queue_only; + +/* The receive_timeout and smtp_receive_timeout options can be overridden by +-or and -os. */ + +if (arg_receive_timeout >= 0) receive_timeout = arg_receive_timeout; +if (arg_smtp_receive_timeout >= 0) + smtp_receive_timeout = arg_smtp_receive_timeout; + +/* If Exim was started with root privilege, unless we have already removed the +root privilege above as a result of -C, -D, -be, -bf or -bF, remove it now +except when starting the daemon or doing some kind of delivery or address +testing (-bt). These are the only cases when root need to be retained. We run +as exim for -bv and -bh. However, if deliver_drop_privilege is set, root is +retained only for starting the daemon. */ + +if (!unprivileged && /* originally had root AND */ + !removed_privilege && /* still got root AND */ + !daemon_listen && /* not starting the daemon */ + queue_interval <= 0 && /* (either kind of daemon) */ + ( /* AND EITHER */ + deliver_drop_privilege || /* requested unprivileged */ + ( /* OR */ + queue_interval < 0 && /* not running the queue */ + (msg_action_arg < 0 || /* and */ + msg_action != MSG_DELIVER) && /* not delivering and */ + (!checking || !address_test_mode) /* not address checking */ + ) + )) + { + exim_setugid(exim_uid, exim_gid, FALSE, US"privilege not needed"); + } + +/* When we are retaining a privileged uid, we still change to the exim gid. */ + +else setgid(exim_gid); + +/* Handle a request to list the delivery queue */ + +if (list_queue) + { + set_process_info("listing the queue"); + queue_list(list_queue_option, argv + recipients_arg, argc - recipients_arg); + exit(EXIT_SUCCESS); + } + +/* Handle a request to count the delivery queue */ + +if (count_queue) + { + set_process_info("counting the queue"); + queue_count(); + exit(EXIT_SUCCESS); + } + +/* Handle actions on specific messages, except for the force delivery action, +which is done below. Some actions take a whole list of message ids, which +are known to continue up to the end of the arguments. Others take a single +message id and then operate on the recipients list. */ + +if (msg_action_arg > 0 && msg_action != MSG_DELIVER) + { + int yield = EXIT_SUCCESS; + set_process_info("acting on specified messages"); + + if (!one_msg_action) + { + for (i = msg_action_arg; i < argc; i++) + if (!queue_action(argv[i], msg_action, NULL, 0, 0)) + yield = EXIT_FAILURE; + } + + else if (!queue_action(argv[msg_action_arg], msg_action, argv, argc, + recipients_arg)) yield = EXIT_FAILURE; + exit(yield); + } + +/* All the modes below here require the remaining configuration sections +to be read, except that we can skip over the ACL setting when delivering +specific messages, or doing a queue run. (For various testing cases we could +skip too, but as they are rare, it doesn't really matter.) The argument is TRUE +for skipping. */ + +readconf_rest(msg_action_arg > 0 || (queue_interval == 0 && !daemon_listen)); + +/* The configuration data will have been read into POOL_PERM because we won't +ever want to reset back past it. Change the current pool to POOL_MAIN. In fact, +this is just a bit of pedantic tidiness. It wouldn't really matter if the +configuration were read into POOL_MAIN, because we don't do any resets till +later on. However, it seems right, and it does ensure that both pools get used. +*/ + +store_pool = POOL_MAIN; + +/* Handle the -brt option. This is for checking out retry configurations. +The next three arguments are a domain name or a complete address, and +optionally two error numbers. All it does is to call the function that +scans the retry configuration data. */ + +if (test_retry_arg >= 0) + { + retry_config *yield; + int basic_errno = 0; + int more_errno = 0; + uschar *s1, *s2; + + if (test_retry_arg >= argc) + { + printf("-brt needs a domain or address argument\n"); + exim_exit(EXIT_FAILURE); + } + s1 = argv[test_retry_arg++]; + s2 = NULL; + + /* If the first argument contains no @ and no . it might be a local user + or it might be a single-component name. Treat as a domain. */ + + if (Ustrchr(s1, '@') == NULL && Ustrchr(s1, '.') == NULL) + { + printf("Warning: \"%s\" contains no '@' and no '.' characters. It is " + "being \ntreated as a one-component domain, not as a local part.\n\n", + s1); + } + + /* There may be an optional second domain arg. */ + + if (test_retry_arg < argc && Ustrchr(argv[test_retry_arg], '.') != NULL) + s2 = argv[test_retry_arg++]; + + /* The final arg is an error name */ + + if (test_retry_arg < argc) + { + uschar *ss = argv[test_retry_arg]; + uschar *error = + readconf_retry_error(ss, ss + Ustrlen(ss), &basic_errno, &more_errno); + if (error != NULL) + { + printf("%s\n", CS error); + return EXIT_FAILURE; + } + + /* For the rcpt_4xx errors, a value of 255 means "any", and a code > 100 as + an error is for matching codes to the decade. Turn them into a real error + code, off the decade. */ + + if (basic_errno == ERRNO_RCPT4XX) + { + int code = (more_errno >> 8) & 255; + if (code == 255) + more_errno = (more_errno & 0xffff00ff) | (21 << 8); + else if (code > 100) + more_errno = (more_errno & 0xffff00ff) | ((code - 96) << 8); + } + } + + yield = retry_find_config(s1, s2, basic_errno, more_errno); + if (yield == NULL) printf("No retry information found\n"); else + { + retry_rule *r; + more_errno = yield->more_errno; + printf("Retry rule: %s ", yield->pattern); + + if (yield->basic_errno == ERRNO_EXIMQUOTA) + { + printf("quota%s%s ", + (more_errno > 0)? "_" : "", + (more_errno > 0)? readconf_printtime(more_errno) : US""); + } + else if (yield->basic_errno == ECONNREFUSED) + { + printf("refused%s%s ", + (more_errno > 0)? "_" : "", + (more_errno == 'M')? "MX" : + (more_errno == 'A')? "A" : ""); + } + else if (yield->basic_errno == ETIMEDOUT) + { + printf("timeout"); + if ((more_errno & RTEF_CTOUT) != 0) printf("_connect"); + more_errno &= 255; + if (more_errno != 0) printf("_%s", + (more_errno == 'M')? "MX" : "A"); + printf(" "); + } + else if (yield->basic_errno == ERRNO_AUTHFAIL) + printf("auth_failed "); + else printf("* "); + + for (r = yield->rules; r != NULL; r = r->next) + { + printf("%c,%s", r->rule, readconf_printtime(r->timeout)); /* Do not */ + printf(",%s", readconf_printtime(r->p1)); /* amalgamate */ + if (r->rule == 'G') + { + int x = r->p2; + int f = x % 1000; + int d = 100; + printf(",%d.", x/1000); + do + { + printf("%d", f/d); + f %= d; + d /= 10; + } + while (f != 0); + } + printf("; "); + } + + printf("\n"); + } + exim_exit(EXIT_SUCCESS); + } + +/* Handle a request to list one or more configuration options */ + +if (list_options) + { + set_process_info("listing variables"); + if (recipients_arg >= argc) readconf_print(US"all", NULL); + else for (i = recipients_arg; i < argc; i++) + { + if (i < argc - 1 && + (Ustrcmp(argv[i], "router") == 0 || + Ustrcmp(argv[i], "transport") == 0 || + Ustrcmp(argv[i], "authenticator") == 0)) + { + readconf_print(argv[i+1], argv[i]); + i++; + } + else readconf_print(argv[i], NULL); + } + exim_exit(EXIT_SUCCESS); + } + + +/* Handle a request to deliver one or more messages that are already on the +queue. Values of msg_action other than MSG_DELIVER are dealt with above. This +is typically used for a small number when prodding by hand (when the option +forced_delivery will be set) or when re-execing to regain root privilege. +Each message delivery must happen in a separate process, so we fork a process +for each one, and run them sequentially so that debugging output doesn't get +intertwined, and to avoid spawning too many processes if a long list is given. +However, don't fork for the last one; this saves a process in the common case +when Exim is called to deliver just one message. */ + +if (msg_action_arg > 0) + { + if (prod_requires_admin && !admin_user) + { + fprintf(stderr, "exim: Permission denied\n"); + exim_exit(EXIT_FAILURE); + } + set_process_info("delivering specified messages"); + if (deliver_give_up) forced_delivery = deliver_force_thaw = TRUE; + for (i = msg_action_arg; i < argc; i++) + { + int status; + pid_t pid; + if (i == argc - 1) + (void)deliver_message(argv[i], forced_delivery, deliver_give_up); + else if ((pid = fork()) == 0) + { + (void)deliver_message(argv[i], forced_delivery, deliver_give_up); + _exit(EXIT_SUCCESS); + } + else if (pid < 0) + { + fprintf(stderr, "failed to fork delivery process for %s: %s\n", argv[i], + strerror(errno)); + exim_exit(EXIT_FAILURE); + } + else wait(&status); + } + exim_exit(EXIT_SUCCESS); + } + + +/* If only a single queue run is requested, without SMTP listening, we can just +turn into a queue runner, with an optional starting message id. */ + +if (queue_interval == 0 && !daemon_listen) + { + DEBUG(D_queue_run) debug_printf("Single queue run%s%s%s%s\n", + (start_queue_run_id == NULL)? US"" : US" starting at ", + (start_queue_run_id == NULL)? US"" : start_queue_run_id, + (stop_queue_run_id == NULL)? US"" : US" stopping at ", + (stop_queue_run_id == NULL)? US"" : stop_queue_run_id); + set_process_info("running the queue (single queue run)"); + queue_run(start_queue_run_id, stop_queue_run_id, FALSE); + exim_exit(EXIT_SUCCESS); + } + + +/* Find the login name of the real user running this process. This is always +needed when receiving a message, because it is written into the spool file. It +may also be used to construct a from: or a sender: header, and in this case we +need the user's full name as well, so save a copy of it, checked for RFC822 +syntax and munged if necessary, if it hasn't previously been set by the -F +argument. We may try to get the passwd entry more than once, in case NIS or +other delays are in evidence. Save the home directory for use in filter testing +(only). */ + +for (i = 0;;) + { + if ((pw = getpwuid(real_uid)) != NULL) + { + originator_login = string_copy(US pw->pw_name); + originator_home = string_copy(US pw->pw_dir); + + /* If user name has not been set by -F, set it from the passwd entry + unless -f has been used to set the sender address by a trusted user. */ + + if (originator_name == NULL) + { + if (sender_address == NULL || + (!trusted_caller && filter_test == NULL)) + { + uschar *name = US pw->pw_gecos; + uschar *amp = Ustrchr(name, '&'); + uschar buffer[256]; + + /* Most Unix specify that a '&' character in the gecos field is + replaced by a copy of the login name, and some even specify that + the first character should be upper cased, so that's what we do. */ + + if (amp != NULL) + { + int loffset; + string_format(buffer, sizeof(buffer), "%.*s%n%s%s", + amp - name, name, &loffset, originator_login, amp + 1); + buffer[loffset] = toupper(buffer[loffset]); + name = buffer; + } + + /* If a pattern for matching the gecos field was supplied, apply + it and then expand the name string. */ + + if (gecos_pattern != NULL && gecos_name != NULL) + { + const pcre *re; + re = regex_must_compile(gecos_pattern, FALSE, TRUE); /* Use malloc */ + + if (regex_match_and_setup(re, name, 0, -1)) + { + uschar *new_name = expand_string(gecos_name); + expand_nmax = -1; + if (new_name != NULL) + { + DEBUG(D_receive) debug_printf("user name \"%s\" extracted from " + "gecos field \"%s\"\n", new_name, name); + name = new_name; + } + else DEBUG(D_receive) debug_printf("failed to expand gecos_name string " + "\"%s\": %s\n", gecos_name, expand_string_message); + } + else DEBUG(D_receive) debug_printf("gecos_pattern \"%s\" did not match " + "gecos field \"%s\"\n", gecos_pattern, name); + store_free((void *)re); + } + originator_name = string_copy(name); + } + + /* A trusted caller has used -f but not -F */ + + else originator_name = US""; + } + + /* Break the retry loop */ + + break; + } + + if (++i > finduser_retries) break; + sleep(1); + } + +/* If we cannot get a user login, log the incident and give up, unless the +configuration specifies something to use. When running in the test harness, +any setting of unknown_login overrides the actual login name. */ + +if (originator_login == NULL || running_in_test_harness) + { + if (unknown_login != NULL) + { + originator_login = expand_string(unknown_login); + if (originator_name == NULL && unknown_username != NULL) + originator_name = expand_string(unknown_username); + if (originator_name == NULL) originator_name = US""; + } + if (originator_login == NULL) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Failed to get user name for uid %d", + (int)real_uid); + } + +/* Ensure that the user name is in a suitable form for use as a "phrase" in an +RFC822 address.*/ + +originator_name = string_copy(parse_fix_phrase(originator_name, + Ustrlen(originator_name), big_buffer, big_buffer_size)); + +/* If a message is created by this call of Exim, the uid/gid of its originator +are those of the caller. These values are overridden if an existing message is +read in from the spool. */ + +originator_uid = real_uid; +originator_gid = real_gid; + +DEBUG(D_receive) debug_printf("originator: uid=%d gid=%d login=%s name=%s\n", + (int)originator_uid, (int)originator_gid, originator_login, originator_name); + +/* Run in daemon and/or queue-running mode. The function daemon_go() never +returns. We leave this till here so that the originator_ fields are available +for incoming messages via the daemon. */ + +if (daemon_listen || queue_interval > 0) + { + if (mua_wrapper) log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Daemon cannot be " + "run when mua_wrapper is set"); + daemon_go(); + } + +/* If the sender ident has not been set (by a trusted caller) set it to +the caller. This will get overwritten below for an inetd call. If a trusted +caller has set it empty, unset it. */ + +if (sender_ident == NULL) sender_ident = originator_login; + else if (sender_ident[0] == 0) sender_ident = NULL; + +/* Handle the -brw option, which is for checking out rewriting rules. Cause log +writes (on errors) to go to stderr instead. Can't do this earlier, as want the +originator_* variables set. */ + +if (test_rewrite_arg >= 0) + { + really_exim = FALSE; + if (test_rewrite_arg >= argc) + { + printf("-brw needs an address argument\n"); + exim_exit(EXIT_FAILURE); + } + rewrite_test(argv[test_rewrite_arg]); + exim_exit(EXIT_SUCCESS); + } + +/* A locally-supplied message is considered to be coming from a local user +unless a trusted caller supplies a sender address with -f, or is passing in the +message via SMTP (inetd invocation or otherwise). */ + +if ((sender_address == NULL && !smtp_input) || + (!trusted_caller && filter_test == NULL)) + { + sender_local = TRUE; + + /* A trusted caller can supply authenticated_sender and authenticated_id + via -oMas and -oMai and if so, they will already be set. */ + + if (authenticated_sender == NULL) + authenticated_sender = string_sprintf("%s@%s", originator_login, + qualify_domain_sender); + if (authenticated_id == NULL) authenticated_id = originator_login; + } + +/* Trusted callers are always permitted to specify the sender address. +Untrusted callers may specify it if it matches untrusted_set_sender, or if what +is specified is the empty address. However, if a trusted caller does not +specify a sender address for SMTP input, we leave sender_address unset. This +causes the MAIL commands to be honoured. */ + +if ((!smtp_input && sender_address == NULL) || + !receive_check_set_sender(sender_address)) + { + /* Either the caller is not permitted to set a general sender, or this is + non-SMTP input and the trusted caller has not set a sender. If there is no + sender, or if a sender other than <> is set, override with the originator's + login (which will get qualified below), except when checking things. */ + + if (sender_address == NULL /* No sender_address set */ + || /* OR */ + (sender_address[0] != 0 && /* Non-empty sender address, AND */ + !checking && /* Not running tests, AND */ + filter_test == NULL)) /* Not testing a filter */ + { + sender_address = originator_login; + sender_address_forced = FALSE; + sender_address_domain = 0; + } + } + +/* Remember whether an untrusted caller set the sender address */ + +sender_set_untrusted = sender_address != originator_login && !trusted_caller; + +/* Ensure that the sender address is fully qualified unless it is the empty +address, which indicates an error message, or doesn't exist (root caller, smtp +interface, no -f argument). */ + +if (sender_address != NULL && sender_address[0] != 0 && + sender_address_domain == 0) + sender_address = string_sprintf("%s@%s", local_part_quote(sender_address), + qualify_domain_sender); + +DEBUG(D_receive) debug_printf("sender address = %s\n", sender_address); + +/* Handle a request to verify a list of addresses, or test them for delivery. +This must follow the setting of the sender address, since routers can be +predicated upon the sender. If no arguments are given, read addresses from +stdin. Set debug_level to at least D_v to get full output for address testing. +*/ + +if (verify_address_mode || address_test_mode) + { + int exit_value = 0; + int flags = vopt_qualify; + + if (verify_address_mode) + { + if (!verify_as_sender) flags |= vopt_is_recipient; + DEBUG(D_verify) debug_print_ids(US"Verifying:"); + } + + else + { + flags |= vopt_is_recipient; + debug_selector |= D_v; + debug_file = stderr; + debug_fd = fileno(debug_file); + DEBUG(D_verify) debug_print_ids(US"Address testing:"); + } + + if (recipients_arg < argc) + { + while (recipients_arg < argc) + { + uschar *s = argv[recipients_arg++]; + while (*s != 0) + { + BOOL finished = FALSE; + uschar *ss = parse_find_address_end(s, FALSE); + if (*ss == ',') *ss = 0; else finished = TRUE; + test_address(s, flags, &exit_value); + s = ss; + if (!finished) + while (*(++s) != 0 && (*s == ',' || isspace(*s))); + } + } + } + + else for (;;) + { + uschar *s = get_stdinput(NULL, NULL); + if (s == NULL) break; + test_address(s, flags, &exit_value); + } + + route_tidyup(); + exim_exit(exit_value); + } + +/* Handle expansion checking */ + +if (expansion_test) + { + if (recipients_arg < argc) + { + while (recipients_arg < argc) + { + uschar *s = argv[recipients_arg++]; + uschar *ss = expand_string(s); + if (ss == NULL) + printf ("Failed: %s\n", expand_string_message); + else printf("%s\n", CS ss); + } + } + + /* Read stdin */ + + else + { + char *(*fn_readline)(char *) = NULL; + char *(*fn_addhist)(char *) = NULL; + + #ifdef USE_READLINE + void *dlhandle = set_readline(&fn_readline, &fn_addhist); + #endif + + for (;;) + { + uschar *ss; + uschar *source = get_stdinput(fn_readline, fn_addhist); + if (source == NULL) break; + ss = expand_string(source); + if (ss == NULL) + printf ("Failed: %s\n", expand_string_message); + else printf("%s\n", CS ss); + } + + #ifdef USE_READLINE + if (dlhandle != NULL) dlclose(dlhandle); + #endif + } + + exim_exit(EXIT_SUCCESS); + } + + +/* The active host name is normally the primary host name, but it can be varied +for hosts that want to play several parts at once. We need to ensure that it is +set for host checking, and for receiving messages. */ + +smtp_active_hostname = primary_hostname; +if (raw_active_hostname != NULL) + { + uschar *nah = expand_string(raw_active_hostname); + if (nah == NULL) + { + if (!expand_string_forcedfail) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to expand \"%s\" " + "(smtp_active_hostname): %s", raw_active_hostname, + expand_string_message); + } + else if (nah[0] != 0) smtp_active_hostname = nah; + } + +/* Handle host checking: this facility mocks up an incoming SMTP call from a +given IP address so that the blocking and relay configuration can be tested. An +RFC 1413 call is made only if we are running in the test harness and an +incoming interface and both ports are specified, because there is no TCP/IP +call to find the ident for. */ + +if (host_checking) + { + sender_ident = NULL; + if (running_in_test_harness && sender_host_port != 0 && + interface_address != NULL && interface_port != 0) + verify_get_ident(1413); + + host_build_sender_fullhost(); + smtp_input = TRUE; + smtp_in = stdin; + smtp_out = stdout; + sender_local = FALSE; + sender_host_notsocket = TRUE; + debug_file = stderr; + debug_fd = fileno(debug_file); + fprintf(stdout, "\n**** SMTP testing session as if from host %s\n" + "**** but without any ident (RFC 1413) callback.\n" + "**** This is not for real!\n\n", + sender_host_address); + + if (verify_check_host(&hosts_connection_nolog) == OK) + log_write_selector &= ~L_smtp_connection; + log_write(L_smtp_connection, LOG_MAIN, "%s", smtp_get_connection_info()); + + if (smtp_start_session()) + { + reset_point = store_get(0); + for (;;) + { + store_reset(reset_point); + if (smtp_setup_msg() <= 0) break; + if (!receive_msg(FALSE)) break; + } + } + exim_exit(EXIT_SUCCESS); + } + + +/* Arrange for message reception if recipients or SMTP were specified; +otherwise complain unless a version print (-bV) happened or this is a filter +verification test. In the former case, show the configuration file name. */ + +if (recipients_arg >= argc && !extract_recipients && !smtp_input) + { + if (version_printed) + { + printf("Configuration file is %s\n", config_main_filename); + return EXIT_SUCCESS; + } + if (filter_test == NULL) + { + fprintf(stderr, +"Exim is a Mail Transfer Agent. It is normally called by Mail User Agents,\n" +"not directly from a shell command line. Options and/or arguments control\n" +"what it does when called. For a list of options, see the Exim documentation.\n"); + return EXIT_FAILURE; + } + } + + +/* If mua_wrapper is set, Exim is being used to turn an MUA that submits on the +standard input into an MUA that submits to a smarthost over TCP/IP. We know +that we are not called from inetd, because that is rejected above. The +following configuration settings are forced here: + + (1) Synchronous delivery (-odi) + (2) Errors to stderr (-oep == -oeq) + (3) No parallel remote delivery + (4) Unprivileged delivery + +We don't force overall queueing options because there are several of them; +instead, queueing is avoided below when mua_wrapper is set. However, we do need +to override any SMTP queueing. */ + +if (mua_wrapper) + { + synchronous_delivery = TRUE; + arg_error_handling = ERRORS_STDERR; + remote_max_parallel = 1; + deliver_drop_privilege = TRUE; + queue_smtp = FALSE; + queue_smtp_domains = NULL; + } + + +/* Prepare to accept one or more new messages on the standard input. When a +message has been read, its id is returned in message_id[]. If doing immediate +delivery, we fork a delivery process for each received message, except for the +last one, where we can save a process switch. + +It is only in non-smtp mode that error_handling is allowed to be changed from +its default of ERRORS_SENDER by argument. (Idle thought: are any of the +sendmail error modes other than -oem ever actually used? Later: yes.) */ + +if (!smtp_input) error_handling = arg_error_handling; + +/* If this is an inetd call, ensure that stderr is closed to prevent panic +logging being sent down the socket and make an identd call to get the +sender_ident. */ + +else if (is_inetd) + { + fclose(stderr); + exim_nullstd(); /* Re-open to /dev/null */ + verify_get_ident(IDENT_PORT); + host_build_sender_fullhost(); + set_process_info("handling incoming connection from %s via inetd", + sender_fullhost); + } + +/* If the sender host address has been set, build sender_fullhost if it hasn't +already been done (which it will have been for inetd). This caters for the +case when it is forced by -oMa. However, we must flag that it isn't a socket, +so that the test for IP options is skipped for -bs input. */ + +if (sender_host_address != NULL && sender_fullhost == NULL) + { + host_build_sender_fullhost(); + set_process_info("handling incoming connection from %s via -oMa", + sender_fullhost); + sender_host_notsocket = TRUE; + } + +/* Otherwise, set the sender host as unknown except for inetd calls. This +prevents host checking in the case of -bs not from inetd and also for -bS. */ + +else if (!is_inetd) sender_host_unknown = TRUE; + +/* If stdout does not exist, then dup stdin to stdout. This can happen +if exim is started from inetd. In this case fd 0 will be set to the socket, +but fd 1 will not be set. This also happens for passed SMTP channels. */ + +if (fstat(1, &statbuf) < 0) dup2(0, 1); + +/* Set up the incoming protocol name and the state of the program. Root +is allowed to force received protocol via the -oMr option above, and if we are +in a non-local SMTP state it means we have come via inetd and the process info +has already been set up. We don't set received_protocol here for smtp input, +as it varies according to batch/HELO/EHLO/AUTH/TLS. */ + +if (smtp_input) + { + if (sender_local) set_process_info("accepting a local SMTP message from <%s>", + sender_address); + } +else + { + if (received_protocol == NULL) + received_protocol = string_sprintf("local%s", called_as); + set_process_info("accepting a local non-SMTP message from <%s>", + sender_address); + } + +/* Initialize the local_queue-only flag (this will be ignored if mua_wrapper is +set) */ + +queue_check_only(); +local_queue_only = queue_only; + +/* For non-SMTP and for batched SMTP input, check that there is enough space on +the spool if so configured. On failure, we must not attempt to send an error +message! (For interactive SMTP, the check happens at MAIL FROM and an SMTP +error code is given.) */ + +if ((!smtp_input || smtp_batched_input) && !receive_check_fs(0)) + { + fprintf(stderr, "exim: insufficient disk space\n"); + return EXIT_FAILURE; + } + +/* If this is smtp input of any kind, handle the start of the SMTP +session. */ + +if (smtp_input) + { + smtp_in = stdin; + smtp_out = stdout; + if (verify_check_host(&hosts_connection_nolog) == OK) + log_write_selector &= ~L_smtp_connection; + log_write(L_smtp_connection, LOG_MAIN, "%s", smtp_get_connection_info()); + if (!smtp_start_session()) + { + mac_smtp_fflush(); + exim_exit(EXIT_SUCCESS); + } + } + +/* Otherwise, set up the input size limit here */ + +else + { + thismessage_size_limit = expand_string_integer(message_size_limit); + if (thismessage_size_limit < 0) + { + if (thismessage_size_limit == -1) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to expand " + "message_size_limit: %s", expand_string_message); + else + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "invalid value for " + "message_size_limit: %s", expand_string_message); + } + } + +/* Loop for several messages when reading SMTP input. If we fork any child +processes, we don't want to wait for them unless synchronous delivery is +requested, so set SIGCHLD to SIG_IGN in that case. This is not necessarily the +same as SIG_DFL, despite the fact that documentation often lists the default as +"ignore". This is a confusing area. This is what I know: + +At least on some systems (e.g. Solaris), just setting SIG_IGN causes child +processes that complete simply to go away without ever becoming defunct. You +can't then wait for them - but we don't want to wait for them in the +non-synchronous delivery case. However, this behaviour of SIG_IGN doesn't +happen for all OS (e.g. *BSD is different). + +But that's not the end of the story. Some (many? all?) systems have the +SA_NOCLDWAIT option for sigaction(). This requests the behaviour that Solaris +has by default, so it seems that the difference is merely one of default +(compare restarting vs non-restarting signals). + +To cover all cases, Exim sets SIG_IGN with SA_NOCLDWAIT here if it can. If not, +it just sets SIG_IGN. To be on the safe side it also calls waitpid() at the end +of the loop below. Paranoia rules. + +February 2003: That's *still* not the end of the story. There are now versions +of Linux (where SIG_IGN does work) that are picky. If, having set SIG_IGN, a +process then calls waitpid(), a grumble is written to the system log, because +this is logically inconsistent. In other words, it doesn't like the paranoia. +As a consequenc of this, the waitpid() below is now excluded if we are sure +that SIG_IGN works. */ + +if (!synchronous_delivery) + { + #ifdef SA_NOCLDWAIT + struct sigaction act; + act.sa_handler = SIG_IGN; + sigemptyset(&(act.sa_mask)); + act.sa_flags = SA_NOCLDWAIT; + sigaction(SIGCHLD, &act, NULL); + #else + signal(SIGCHLD, SIG_IGN); + #endif + } + +/* Save the current store pool point, for resetting at the start of +each message, and save the real sender address, if any. */ + +reset_point = store_get(0); +real_sender_address = sender_address; + +/* Loop to receive messages; receive_msg() returns TRUE if there are more +messages to be read (SMTP input), or FALSE otherwise (not SMTP, or SMTP channel +collapsed). */ + +while (more) + { + store_reset(reset_point); + message_id[0] = 0; + + /* In the SMTP case, we have to handle the initial SMTP input and build the + recipients list, before calling receive_msg() to read the message proper. + Whatever sender address is actually given in the SMTP transaction is + actually ignored for local senders - we use the actual sender, which is + normally either the underlying user running this process or a -f argument + provided by a trusted caller. It is saved in real_sender_address. + + However, if this value is NULL, we are dealing with a trusted caller when + -f was not used; in this case, the SMTP sender is allowed to stand. + + Also, if untrusted_set_sender is set, we permit sender addresses that match + anything in its list. + + The variable raw_sender_address holds the sender address before rewriting. */ + + if (smtp_input) + { + int rc; + if ((rc = smtp_setup_msg()) > 0) + { + if (real_sender_address != NULL && + !receive_check_set_sender(sender_address)) + { + sender_address = raw_sender = real_sender_address; + sender_address_unrewritten = NULL; + } + more = receive_msg(extract_recipients); + if (message_id[0] == 0) + { + if (more) continue; + exim_exit(EXIT_FAILURE); + } + } + else exim_exit((rc == 0)? EXIT_SUCCESS : EXIT_FAILURE); + } + + /* In the non-SMTP case, we have all the information from the command + line, but must process it in case it is in the more general RFC822 + format, and in any case, to detect syntax errors. Also, it appears that + the use of comma-separated lists as single arguments is common, so we + had better support them. */ + + else + { + int i; + int rcount = 0; + int count = argc - recipients_arg; + uschar **list = argv + recipients_arg; + + /* Save before any rewriting */ + + raw_sender = string_copy(sender_address); + + /* Loop for each argument */ + + for (i = 0; i < count; i++) + { + int start, end, domain; + uschar *errmess; + uschar *s = list[i]; + + /* Loop for each comma-separated address */ + + while (*s != 0) + { + BOOL finished = FALSE; + uschar *recipient; + uschar *ss = parse_find_address_end(s, FALSE); + + if (*ss == ',') *ss = 0; else finished = TRUE; + + /* Check max recipients - if -t was used, these aren't recipients */ + + if (recipients_max > 0 && ++rcount > recipients_max && + !extract_recipients) + { + if (error_handling == ERRORS_STDERR) + { + fprintf(stderr, "exim: too many recipients\n"); + exim_exit(EXIT_FAILURE); + } + else + { + return + moan_to_sender(ERRMESS_TOOMANYRECIP, NULL, NULL, stdin, TRUE)? + errors_sender_rc : EXIT_FAILURE; + } + } + + recipient = + parse_extract_address(s, &errmess, &start, &end, &domain, FALSE); + + if (domain == 0 && !allow_unqualified_recipient) + { + recipient = NULL; + errmess = US"unqualified recipient address not allowed"; + } + + if (recipient == NULL) + { + if (error_handling == ERRORS_STDERR) + { + fprintf(stderr, "exim: bad recipient address \"%s\": %s\n", + string_printing(list[i]), errmess); + exim_exit(EXIT_FAILURE); + } + else + { + error_block eblock; + eblock.next = NULL; + eblock.text1 = string_printing(list[i]); + eblock.text2 = errmess; + return + moan_to_sender(ERRMESS_BADARGADDRESS, &eblock, NULL, stdin, TRUE)? + errors_sender_rc : EXIT_FAILURE; + } + } + + receive_add_recipient(recipient, -1); + s = ss; + if (!finished) + while (*(++s) != 0 && (*s == ',' || isspace(*s))); + } + } + + /* Show the recipients when debugging */ + + DEBUG(D_receive) + { + int i; + if (sender_address != NULL) debug_printf("Sender: %s\n", sender_address); + if (recipients_list != NULL) + { + debug_printf("Recipients:\n"); + for (i = 0; i < recipients_count; i++) + debug_printf(" %s\n", recipients_list[i].address); + } + } + + /* Read the data for the message. If filter_test is true, this will + just read the headers for the message, and not write anything onto + the spool. */ + + message_ended = END_NOTENDED; + more = receive_msg(extract_recipients); + + /* more is always FALSE here (not SMTP message) when reading a message + for real; when reading the headers of a message for filter testing, + it is TRUE if the headers were terminated by '.' and FALSE otherwise. */ + + if (message_id[0] == 0) exim_exit(EXIT_FAILURE); + } /* Non-SMTP message reception */ + + /* If this is a filter testing run, there are headers in store, but + no message on the spool. Run the filtering code in testing mode, setting + the domain to the qualify domain and the local part to the current user, + unless they have been set by options. The prefix and suffix are left unset + unless specified. The the return path is set to to the sender unless it has + already been set from a return-path header in the message. */ + + if (filter_test != NULL) + { + deliver_domain = (ftest_domain != NULL)? + ftest_domain : qualify_domain_recipient; + deliver_domain_orig = deliver_domain; + deliver_localpart = (ftest_localpart != NULL)? + ftest_localpart : originator_login; + deliver_localpart_orig = deliver_localpart; + deliver_localpart_prefix = ftest_prefix; + deliver_localpart_suffix = ftest_suffix; + deliver_home = originator_home; + + if (return_path == NULL) + { + printf("Return-path copied from sender\n"); + return_path = string_copy(sender_address); + } + else + { + printf("Return-path = %s\n", (return_path[0] == 0)? US"<>" : return_path); + } + printf("Sender = %s\n", (sender_address[0] == 0)? US"<>" : sender_address); + + receive_add_recipient( + string_sprintf("%s%s%s@%s", + (ftest_prefix == NULL)? US"" : ftest_prefix, + deliver_localpart, + (ftest_suffix == NULL)? US"" : ftest_suffix, + deliver_domain), -1); + + printf("Recipient = %s\n", recipients_list[0].address); + if (ftest_prefix != NULL) printf("Prefix = %s\n", ftest_prefix); + if (ftest_suffix != NULL) printf("Suffix = %s\n", ftest_suffix); + + chdir("/"); /* Get away from wherever the user is running this from */ + exim_exit(filter_runtest(filter_fd, ftest_system, more)? + EXIT_SUCCESS : EXIT_FAILURE); + } + + /* Else act on the result of message reception. We should not get here unless + message_id[0] is non-zero. If queue_only is set, local_queue_only will be + TRUE. If it is not, check on the number of messages received in this + connection. If that's OK and queue_only_load is set, check that the load + average is below it. If it is not, set local_queue_only TRUE. Note that it + then remains this way for any subsequent messages on the same SMTP connection. + This is a deliberate choice; even though the load average may fall, it + doesn't seem right to deliver later messages on the same call when not + delivering earlier ones. */ + + if (!local_queue_only) + { + if (smtp_accept_queue_per_connection > 0 && + receive_messagecount > smtp_accept_queue_per_connection) + { + local_queue_only = TRUE; + queue_only_reason = 2; + } + else if (queue_only_load >= 0) + { + local_queue_only = (load_average = os_getloadavg()) > queue_only_load; + if (local_queue_only) queue_only_reason = 3; + } + } + + /* If running as an MUA wrapper, all queueing options and freezing options + are ignored. */ + + if (mua_wrapper) + local_queue_only = queue_only_policy = deliver_freeze = FALSE; + + /* Log the queueing here, when it will get a message id attached, but + not if queue_only is set (case 0). Case 1 doesn't happen here (too many + connections). */ + + if (local_queue_only) switch(queue_only_reason) + { + case 2: + log_write(L_delay_delivery, + LOG_MAIN, "no immediate delivery: more than %d messages " + "received in one connection", smtp_accept_queue_per_connection); + break; + + case 3: + log_write(L_delay_delivery, + LOG_MAIN, "no immediate delivery: load average %.2f", + (double)load_average/1000.0); + break; + } + + /* Else do the delivery unless the ACL or local_scan() called for queue only + or froze the message. Always deliver in a separate process. A fork failure is + not a disaster, as the delivery will eventually happen on a subsequent queue + run. */ + + else if (!queue_only_policy && !deliver_freeze) + { + pid_t pid; + if ((pid = fork()) == 0) + { + int rc; + close_unwanted(); /* Close unwanted file descriptors and TLS */ + exim_nullstd(); /* Ensure std{in,out,err} exist */ + + /* Re-exec Exim if we need to regain privilege (note: in mua_wrapper + mode, deliver_drop_privilege is forced TRUE). */ + + if (geteuid() != root_uid && !deliver_drop_privilege && !unprivileged) + { + (void)child_exec_exim(CEE_EXEC_EXIT, FALSE, NULL, FALSE, 2, US"-Mc", + message_id); + /* Control does not return here. */ + } + + /* No need to re-exec */ + + rc = deliver_message(message_id, FALSE, FALSE); + search_tidyup(); + _exit((!mua_wrapper || rc == DELIVER_MUA_SUCCEEDED)? + EXIT_SUCCESS : EXIT_FAILURE); + } + + if (pid < 0) + { + log_write(0, LOG_MAIN|LOG_PANIC, "failed to fork automatic delivery " + "process: %s", strerror(errno)); + } + + /* In the parent, wait if synchronous delivery is required. This will + always be the case in MUA wrapper mode. */ + + else if (synchronous_delivery) + { + int status; + while (wait(&status) != pid); + if ((status & 0x00ff) != 0) + log_write(0, LOG_MAIN|LOG_PANIC, + "process %d crashed with signal %d while delivering %s", + (int)pid, status & 0x00ff, message_id); + if (mua_wrapper && (status & 0xffff) != 0) exim_exit(EXIT_FAILURE); + } + } + + /* The loop will repeat if more is TRUE. If we do not know know that the OS + automatically reaps children (see comments above the loop), clear away any + finished subprocesses here, in case there are lots of messages coming in + from the same source. */ + + #ifndef SIG_IGN_WORKS + while (waitpid(-1, NULL, WNOHANG) > 0); + #endif + } + +exim_exit(EXIT_SUCCESS); /* Never returns */ +return 0; /* To stop compiler warning */ +} + +/* End of exim.c */ diff --git a/src/src/exim.h b/src/src/exim.h new file mode 100644 index 000000000..96d634efe --- /dev/null +++ b/src/src/exim.h @@ -0,0 +1,461 @@ +/* $Cambridge: exim/src/src/exim.h,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + + +/* Source files for exim all #include this header, which drags in everything +that is needed. They don't all need everything, of course, but it's far too +messy to have each one importing its own list, and anyway, most of them need +most of these includes. */ + +/* Assume most systems have statfs() unless os.h undefines this macro */ + +#define HAVE_STATFS + +/* First of all include the os-specific header, which might set things that +are needed by any of the other headers, including system headers. */ + +#include "os.h" + +/* If it didn't define os_find_running_interfaces, use the common function. */ + +#ifndef os_find_running_interfaces +#define os_find_running_interfaces os_common_find_running_interfaces +#endif + +/* If it didn't define the base for "base 62" numbers, we really do use 62. +This is the case for all real Unix and Unix-like OS. It's only Cygwin and +Darwin, with their case-insensitive file systems, that can't use base 62 for +making unique names. */ + +#ifndef BASE_62 +#define BASE_62 62 +#endif + +/* The maximum value of localhost_number depends on the base being used */ + +#if BASE_62 == 62 +#define LOCALHOST_MAX 16 +#else +#define LOCALHOST_MAX 10 +#endif + +/* ANSI C standard includes */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Unix includes */ + +#include +#if defined(__svr4__) && defined(__sparc) && ! defined(__EXTENSIONS__) +#define __EXTENSIONS__ /* so that SunOS 5 gets NGROUPS_MAX */ +#include +#undef __EXTENSIONS__ +#else +#include +#endif + +/* Just in case some aged system doesn't define them... */ + +#ifndef INT_MAX +#define INT_MAX 2147483647 +#endif + +#ifndef UCHAR_MAX +#define UCHAR_MAX 255 +#endif + +/* Some systems have PATH_MAX and some have MAX_PATH_LEN. */ + +#ifndef PATH_MAX +#ifdef MAX_PATH_LEN +#define PATH_MAX MAX_PATH_LEN +#else +#define PATH_MAX 1024 +#endif +#endif + +#include +#include +#include +#include +#include +#include +#include + +/* Not all systems have flock() available. Those that do must define LOCK_SH +in sys/file.h. */ + +#ifndef LOCK_SH +#define NO_FLOCK +#endif + +#ifndef NO_SYSEXITS /* some OS don't have this */ +#include +#endif + +/* A few OS don't have socklen_t; their os.h files define SOCKLEN_T to +be size_t or whatever. */ + +#ifndef SOCKLEN_T +#define SOCKLEN_T socklen_t +#endif + +/* Ensure that the sysexits we reference are defined */ + +#ifndef EX_UNAVAILABLE +#define EX_UNAVAILABLE 69 /* service unavailable; used for execv fail */ +#endif +#ifndef EX_CANTCREAT +#define EX_CANTCREAT 73 /* can't create file: treat as temporary */ +#endif +#ifndef EX_TEMPFAIL +#define EX_TEMPFAIL 75 /* temp failure; user is invited to retry */ +#endif +#ifndef EX_CONFIG +#define EX_CONFIG 78 /* configuration error */ +#endif + +/* This one is not in any sysexits file that I've come across */ + +#define EX_EXECFAILED 127 /* execve() failed */ + + +#include +#include + +#ifndef NO_SYS_RESOURCE_H /* QNX doesn't have this */ +#include +#endif + +#include + +/* If we are on an IPv6 system, the macro AF_INET6 will have been defined in +the sys/socket.h header. It is helpful to have this defined on an IPv4 system +so that it can appear in the code, even if it is never actually used when +the code is run. It saves some #ifdef occurrences. */ + +#ifndef AF_INET6 +#define AF_INET6 24 +#endif + +#include + +/* The new standard is statvfs; some OS have statfs. For statvfs the block +counts must be multiplied by the "fragment size" f_frsize to get the actual +size. In other cases the value seems to be f_bsize (which is sometimes the only +block size), so we use a macro to get that instead. + +Also arrange to be able to cut it out altogether for way-out OS that don't have +anything. I've indented a bit here to try to make the mess a bit more +intelligible. Note that simply defining one name to be another when +HAVE_SYS_STATVFS_H is not set will not work if the system has a statvfs macro +or a macro with entries f_frsize and f_bsize. */ + +#ifdef HAVE_STATFS + #ifdef HAVE_SYS_STATVFS_H + #include + #define STATVFS statvfs + #define F_FRSIZE f_frsize + #else + #define STATVFS statfs + #define F_FRSIZE f_bsize + #ifdef HAVE_SYS_VFS_H + #include + #ifdef HAVE_SYS_STATFS_H + #include + #endif + #endif + #ifdef HAVE_SYS_MOUNT_H + #include + #endif + #endif + + /* Macros for the fields for the available space for non-superusers; define + these only if the OS header has not. Not all OS have f_favail; those that + are known to have it define F_FAVAIL as f_favail. The default is to use + f_free. */ + + #ifndef F_BAVAIL + #define F_BAVAIL f_bavail + #endif + + #ifndef F_FAVAIL + #define F_FAVAIL f_ffree + #endif + + /* All the systems I've been able to look at seem to have F_FILES */ + + #ifndef F_FILES + #define F_FILES f_files + #endif + +#endif + + +#ifndef SIOCGIFCONF /* HACK for SunOS 5 */ +#include +#endif + +#include +#include +#include +#include + +/* There's a shambles in IRIX6 - it defines EX_OK in unistd.h which conflicts +with the definition in sysexits.h. Arrange to preserve it, even though at +present Exim doesn't actually use it. */ + +#ifdef EX_OK +#define SAVE_EX_OK EX_OK +#undef EX_OK +#endif + +#include + +#ifdef SAVE_EX_OK +#ifdef EX_OK +#undef EX_OK +#endif +#define EX_OK SAVE_EX_OK +#endif + +#include +#ifndef NO_NET_IF_H +#include +#endif +#include +#include +#include +#include +#include + + +/* If arpa/nameser.h defines a maximum name server packet size, use it, +provided it is greater than 2048. Otherwise go for a default. PACKETSZ was used +for this, but it seems that NS_PACKETSZ is coming into use. */ + +#if defined(NS_PACKETSZ) && NS_PACKETSZ >= 2048 + #define MAXPACKET NS_PACKETSZ +#elif defined(PACKETSZ) && PACKETSZ >= 2048 + #define MAXPACKET PACKETSZ +#else + #define MAXPACKET 2048 +#endif + +/* While IPv6 is still young the definitions of T_AAAA and T_A6 may not be +included in arpa/nameser.h. Fudge them here. */ + +#ifndef T_AAAA +#define T_AAAA 28 +#endif + +#ifndef T_A6 +#define T_A6 38 +#endif + +/* Ancient systems (e.g. SunOS4) don't appear to have T_TXT defined in their +header files. I don't suppose they have T_SRV either. */ + +#ifndef T_TXT +#define T_TXT 16 +#endif + +#ifndef T_SRV +#define T_SRV 33 +#endif + +/* The resolv.h header defines __P(x) on some Solaris 2.5.1 systems (without +checking that it is already defined, in fact). This conflicts with other +headers that behave likewise (see below), leading to compiler warnings. Arrange +to undefine it if resolv.h defines it. */ + +#if defined(__P) +#define __P_WAS_DEFINED_BEFORE_RESOLV +#endif + +#include + +#if defined(__P) && ! defined (__P_WAS_DEFINED_BEFORE_RESOLV) +#undef __P +#endif + +/* These three are to support the IP option logging code. Linux is +different to everyone else and there are also other systems which don't +have netinet/ip_var.h, so there's a general macro to control its inclusion. */ + +#include +#include + +#ifndef NO_IP_VAR_H +#include +#endif + +/* Linux (and some others) uses a different type for the 2nd argument of +iconv(). It's os.h file defines ICONV_ARG2_TYPE. For the rest, define a default +here. */ + +#ifndef ICONV_ARG2_TYPE +#define ICONV_ARG2_TYPE const char ** +#endif + +/* One OS uses a different type for the 5th argument of getsockopt */ + +#ifndef GETSOCKOPT_ARG5_TYPE +#define GETSOCKOPT_ARG5_TYPE socklen_t * +#endif + +/* One operating system uses a different type for the 2nd argument of select(). +Its os.h file defines SELECT_ARG2_TYPE. For the rest, define a default here. */ + +#ifndef SELECT_ARG2_TYPE +#define SELECT_ARG2_TYPE fd_set +#endif + +/* One operating system uses a different type for the 4th argument of +dn_expand(). Its os.h file defines DN_EXPAND_ARG4_TYPE. For the rest, define a +default here. */ + +#ifndef DN_EXPAND_ARG4_TYPE +#define DN_EXPAND_ARG4_TYPE char * +#endif + +/* One operating system defines a different type for the yield of inet_addr(). +In Exim code, its value is always assigned to the s_addr members of address +structures. Casting the yield to the type of s_addr should fix the problem, +since the size of the data is correct. Just in case this ever has to be +changed, use a macro for the type, and define it here so that it is possible to +use different values for specific OS if ever necessary. */ + +#ifndef S_ADDR_TYPE +#define S_ADDR_TYPE u_long +#endif + +/* (At least) one operating system (Solaris) defines a different type for the +second argument of pam_converse() - the difference is the absence of "const". +Its os.h file defines PAM_CONVERSE_ARG2_TYPE. For the rest, define a default +here. */ + +#ifndef PAM_CONVERSE_ARG2_TYPE +#define PAM_CONVERSE_ARG2_TYPE const struct pam_message +#endif + +/* One operating system (SunOS4) defines getc, ungetc, feof, and ferror as +macros and not as functions. Exim needs them to be assignable functions. This +flag gets set to cause this to be sorted out here. */ + +#ifdef FUDGE_GETC_AND_FRIENDS +#undef getc +extern int getc(FILE *); +#undef ungetc +extern int ungetc(int, FILE *); +#undef feof +extern int feof(FILE *); +#undef ferror +extern int ferror(FILE *); +#endif + +/* The header from the PCRE regex package */ + +#include "pcre/pcre.h" + +/* Exim includes are in several files. Note that local_scan.h #includes +mytypes.h and store.h, so we don't need to mention them explicitly. */ + +#include "local_scan.h" +#include "macros.h" +#include "config.h" +#include "dbstuff.h" +#include "structs.h" +#include "globals.h" +#include "functions.h" +#include "dbfunctions.h" +#include "osfunctions.h" + +/* The following stuff must follow the inclusion of config.h because it +requires various things that are set therein. */ + +#if HAVE_ICONV /* Not all OS have this */ +#include +#endif + +#ifdef USE_READLINE +#include +#endif + +/* Backward compatibility; LOOKUP_LSEARCH now includes all three */ + +#if (!defined LOOKUP_LSEARCH) && (defined LOOKUP_WILDLSEARCH || defined LOOKUP_NWILDLSEARCH) +#define LOOKUP_LSEARCH yes +#endif + +/* Define a union to hold either an IPv4 or an IPv6 sockaddr structure; this +simplifies some of the coding. */ + +union sockaddr_46 { + struct sockaddr_in v4; + #if HAVE_IPV6 + struct sockaddr_in6 v6; + #endif +}; + +/* If SUPPORT_TLS is not defined, ensure that USE_GNUTLS is also not defined +so that if USE_GNUTLS *is* set, we can assume SUPPORT_TLS is also set. */ + +#ifndef SUPPORT_TLS +#undef USE_GNUTLS +#endif + +/* If SPOOL_DIRECTORY, LOG_FILE_PATH or PID_FILE_PATH have not been defined, +set them to the null string. */ + +#ifndef SPOOL_DIRECTORY + #define SPOOL_DIRECTORY "" +#endif +#ifndef LOG_FILE_PATH + #define LOG_FILE_PATH "" +#endif +#ifndef PID_FILE_PATH + #define PID_FILE_PATH "" +#endif + +/* The EDQUOT error code isn't universally available, though it is widespread. +There is a particular shambles in SunOS5, where it did not exist originally, +but got installed with a particular patch for Solaris 2.4. There is a +configuration variable for specifying what the system's "over quota" error is, +which will end up in config.h if supplied in OS/Makefile-xxx. If it is not set, +default to EDQUOT if it exists, otherwise ENOSPC. */ + +#ifndef ERRNO_QUOTA +#ifdef EDQUOT +#define ERRNO_QUOTA EDQUOT +#else +#define ERRNO_QUOTA ENOSPC +#endif +#endif + +/* Ensure PATH_MAX is defined */ + +#ifndef PATH_MAX + #ifdef MAXPATHLEN + #define PATH_MAX MAXPATHLEN + #else + #define PATH_MAX 1024 + #endif +#endif + + +/* End of exim.h */ diff --git a/src/src/exim_checkaccess.src b/src/src/exim_checkaccess.src new file mode 100755 index 000000000..8169243bf --- /dev/null +++ b/src/src/exim_checkaccess.src @@ -0,0 +1,174 @@ +#! /bin/sh +# $Cambridge: exim/src/src/exim_checkaccess.src,v 1.1 2004/10/07 10:39:01 ph10 Exp $ + +# Copyright (c) 2002 University of Cambridge. +# See the file NOTICE for conditions of use and distribution. + +# Except when they appear in comments, the following placeholders in this +# source are replaced when it is turned into a runnable script: +# +# CONFIGURE_FILE_USE_NODE +# CONFIGURE_FILE +# BIN_DIRECTORY +# PERL_COMMAND + +# PROCESSED_FLAG + +# A shell+perl wrapper script to run an automated -bh test to check out +# ACLs for incoming addresses. + +# Save the shell arguments because we are going to need the shell variables +# while sorting out the configuration file. + +args="$@" + +# See if this installation is using the esoteric "USE_NODE" feature of Exim, +# in which it uses the host's name as a suffix for the configuration file name. + +if [ "CONFIGURE_FILE_USE_NODE" = "yes" ]; then + hostsuffix=.`uname -n` +fi + +# Now find the configuration file name. This has got complicated because +# CONFIGURE_FILE may now be a list of files. The one that is used is the first +# one that exists. Mimic the code in readconf.c by testing first for the +# suffixed file in each case. + +set `awk -F: '{ for (i = 1; i <= NF; i++) print $i }' < [exim options]\n"; + exit(1); + } + +$exim_path = $ARGV[0]; # Set up by the calling shell script +$host = $ARGV[1]; # Mandatory original first argument +$recipient = $ARGV[2]; # Mandatory original second argument + +$c4 = qr/2 (?:[0-4]\d | 5[0-5]) | 1\d\d | \d{1,2}/x; # IPv4 component +$a4 = qr/^$c4\.$c4\.$c4\.$c4$/; # IPv4 address + +$c6 = qr/[0-9a-f]{1,4}/i; # IPv6 component + +# Split the various formats of IPv6 addresses into several cases. I don't +# think I can graft regex that matches all of them without using alternatives. + +# 1. Starts with :: followed by up to 7 components + +$a6_0 = qr/^::(?:$c6:){0,6}$c6$/x; + +# 2. 8 non-empty components + +$a6_1 = qr/^(?:$c6:){7}$c6$/x; + +# 3. This is the cunning one. Up to 7 components, one (and only one) of which +# can be empty. We use 0 to cause a failure when we've already matched +# an empty component and may be hitting other. This has to fail, because we +# know we've just failed to match a component. We also do a final check to +# ensure that there has been an empty component. + +$a6_2 = qr/^(?: (?: $c6 | (?(1)0 | () ) ) : ){1,7}$c6 $ (?(1)|.)/x; + +if ($host !~ /$a4 | $a6_0 | $a6_1 | $a6_2/x) + { + print "** Invalid IP address \"$host\"\n"; + print "Usage: exim_checkaccess [exim options]\n"; + exit(1); + } + +# Build any remaining original arguments into a string for passing over +# as Exim options. + +$opt = ""; +for ($i = 3; $i < scalar(@ARGV); $i++) { $opt .= "$ARGV[$i] "; } + +# If the string contains "-f xxxx", extract that as the sender. Otherwise +# the sender is <>. + +$sender = ""; +if ($opt =~ /(?:^|\s)-f\s+(\S+|"[^"]*")/) + { + $sender = $1; + $opt = $` . $'; + } + +# Run a -bh test in Exim, passing the test data + +$pid = open2(*IN, *OUT, "$exim_path -bh $host $opt 2>/dev/null"); +print OUT "HELO [$host]\r\n"; +print OUT "MAIL FROM:<$sender>\r\n"; +print OUT "RCPT TO:<$recipient>\r\n"; +print OUT "QUIT\r\n"; +close OUT; + +# Read the output, ignoring anything but the SMTP response to the RCPT +# command. + +$count = 0; +$reply = ""; + +while () + { + next if !/^\d\d\d/; + $reply .= $_; + next if /^\d\d\d\-/; + + if (++$count != 4) + { + $reply = ""; + next; + } + + # We have the response we want. Interpret it. + + if ($reply =~ /^2\d\d/) + { + print "Accepted\n"; + } + else + { + print "Rejected:\n"; + $reply =~ s/\n(.)/\n $1/g; + print " $reply"; + } + last; + } + +# Reap the child process + +waitpid $pid, 0; + +End diff --git a/src/src/exim_dbmbuild.c b/src/src/exim_dbmbuild.c new file mode 100644 index 000000000..04ae3362c --- /dev/null +++ b/src/src/exim_dbmbuild.c @@ -0,0 +1,487 @@ +/* $Cambridge: exim/src/src/exim_dbmbuild.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + + +/* A small freestanding program to build dbm databases from serial input. For +alias files, this program fulfils the function of the newaliases program used +by other mailers, but it can be used for other dbm data files too. It operates +by writing a new file or files, and then renaming; otherwise old entries can +never get flushed out. + +This program is clever enough to cope with ndbm, which creates two files called +.dir and .pag, or with db, which creates a single file called +.db. If native db is in use (USE_DB defined) or tdb is in use (USE_TDB +defined) there is no extension to the output filename. This is also handled. If +there are any other variants, the program won't cope. + +The first argument to the program is the name of the serial file; the second +is the base name for the DBM file(s). When native db is in use, these must be +different. + +Input lines beginning with # are ignored, as are blank lines. Entries begin +with a key terminated by a colon or end of line or whitespace and continue with +indented lines. Keys may be quoted if they contain colons or whitespace or # +characters. */ + + +#include "exim.h" + + +#define max_insize 20000 +#define max_outsize 100000 + +/* This is global because it's defined in the headers and compilers grumble +if it is made static. */ + +uschar *hex_digits = US"0123456789abcdef"; + + +#ifdef STRERROR_FROM_ERRLIST +/* Some old-fashioned systems still around (e.g. SunOS4) don't have strerror() +in their libraries, but can provide the same facility by this simple +alternative function. */ + +char * +strerror(int n) +{ +if (n < 0 || n >= sys_nerr) return "unknown error number"; +return sys_errlist[n]; +} +#endif /* STRERROR_FROM_ERRLIST */ + + +/* For Berkeley DB >= 2, we can define a function to be called in case of DB +errors. This should help with debugging strange DB problems, e.g. getting "File +exists" when you try to open a db file. */ + +#if defined(USE_DB) && defined(DB_VERSION_STRING) +void +dbfn_bdb_error_callback(const char *pfx, char *msg) +{ +pfx = pfx; +printf("Berkeley DB error: %s\n", msg); +} +#endif + + + +/************************************************* +* Interpret escape sequence * +*************************************************/ + +/* This function is copied from the main Exim code. + +Arguments: + pp points a pointer to the initiating "\" in the string; + the pointer gets updated to point to the final character +Returns: the value of the character escape +*/ + +int +string_interpret_escape(uschar **pp) +{ +int ch; +uschar *p = *pp; +ch = *(++p); +if (isdigit(ch) && ch != '8' && ch != '9') + { + ch -= '0'; + if (isdigit(p[1]) && p[1] != '8' && p[1] != '9') + { + ch = ch * 8 + *(++p) - '0'; + if (isdigit(p[1]) && p[1] != '8' && p[1] != '9') + ch = ch * 8 + *(++p) - '0'; + } + } +else switch(ch) + { + case 'n': ch = '\n'; break; + case 'r': ch = '\r'; break; + case 't': ch = '\t'; break; + case 'x': + ch = 0; + if (isxdigit(p[1])) + { + ch = ch * 16 + + Ustrchr(hex_digits, tolower(*(++p))) - hex_digits; + if (isxdigit(p[1])) ch = ch * 16 + + Ustrchr(hex_digits, tolower(*(++p))) - hex_digits; + } + break; + } +*pp = p; +return ch; +} + + +/************************************************* +* Main Program * +*************************************************/ + +int main(int argc, char **argv) +{ +int started; +int count = 0; +int dupcount = 0; +int yield = 0; +int arg = 1; +int add_zero = 1; +BOOL lowercase = TRUE; +BOOL warn = TRUE; +BOOL duperr = TRUE; +BOOL lastdup = FALSE; +#if !defined (USE_DB) && !defined(USE_TDB) && !defined(USE_GDBM) +int is_db = 0; +struct stat statbuf; +#endif +FILE *f; +EXIM_DB *d; +EXIM_DATUM key, content; +uschar *bptr; +uschar keybuffer[256]; +uschar temp_dbmname[256]; +uschar real_dbmname[256]; +uschar *buffer = malloc(max_outsize); +uschar *line = malloc(max_insize); + +while (argc > 1) + { + if (Ustrcmp(argv[arg], "-nolc") == 0) lowercase = FALSE; + else if (Ustrcmp(argv[arg], "-nowarn") == 0) warn = FALSE; + else if (Ustrcmp(argv[arg], "-lastdup") == 0) lastdup = TRUE; + else if (Ustrcmp(argv[arg], "-noduperr") == 0) duperr = FALSE; + else if (Ustrcmp(argv[arg], "-nozero") == 0) add_zero = 0; + else break; + arg++; + argc--; + } + +if (argc != 3) + { + printf("usage: exim_dbmbuild [-nolc] \n"); + exit(1); + } + +if (Ustrcmp(argv[arg], "-") == 0) f = stdin; else + { + f = fopen(argv[arg], "rb"); + if (f == NULL) + { + printf("exim_dbmbuild: unable to open %s: %s\n", argv[arg], strerror(errno)); + exit(1); + } + } + +/* By default Berkeley db does not put extensions on... which +can be painful! */ + +#if defined(USE_DB) || defined(USE_TDB) || defined(USE_GDBM) +if (Ustrcmp(argv[arg], argv[arg+1]) == 0) + { + printf("exim_dbmbuild: input and output filenames are the same\n"); + exit(1); + } +#endif + +Ustrcpy(temp_dbmname, argv[arg+1]); +Ustrcat(temp_dbmname, ".dbmbuild_temp"); + +/* It is apparently necessary to open with O_RDWR for this to work +with gdbm-1.7.3, though no reading is actually going to be done. */ + +EXIM_DBOPEN(temp_dbmname, O_RDWR|O_CREAT|O_EXCL, 0644, &d); + +if (d == NULL) + { + printf("exim_dbmbuild: unable to create %s: %s\n", temp_dbmname, + strerror(errno)); + fclose(f); + exit(1); + } + +/* Unless using native db calls, see if we have created .db; if not, +assume .dir & .pag */ + +#if !defined(USE_DB) && !defined(USE_TDB) && !defined(USE_GDBM) +sprintf(CS real_dbmname, "%s.db", temp_dbmname); +is_db = Ustat(real_dbmname, &statbuf) == 0; +#endif + +/* Now do the business */ + +bptr = buffer; +started = 0; + +while (Ufgets(line, max_insize, f) != NULL) + { + uschar *p; + int len = Ustrlen(line); + + p = line + len; + + if (len >= max_insize - 1 && p[-1] != '\n') + { + printf("Overlong line read: max permitted length is %d\n", max_insize - 1); + return 1; + } + + if (line[0] == '#') continue; + while (p > line && isspace(p[-1])) p--; + *p = 0; + if (line[0] == 0) continue; + + /* A continuation line is valid only if there was a previous first + line. */ + + if (isspace(line[0])) + { + uschar *s = line; + if (!started) + { + printf("Unexpected continuation line ignored\n%s\n\n", line); + continue; + } + while (isspace(*s)) s++; + *(--s) = ' '; + + if (bptr - buffer + p - s >= max_outsize - 1) + { + printf("Continued set of lines is too long: max permitted length is %d\n", + max_outsize -1); + return 1; + } + + Ustrcpy(bptr, s); + bptr += p - s; + } + + /* A first line must have a name followed by a colon or whitespace or + end of line, but first finish with a previous line. The key is lower + cased by default - this is what the newaliases program for sendmail does. + However, there's an option not to do this. */ + + else + { + int i, rc; + uschar *s = line; + uschar *keystart; + + if (started) + { + EXIM_DATUM_INIT(content); + EXIM_DATUM_DATA(content) = CS buffer; + EXIM_DATUM_SIZE(content) = bptr - buffer + add_zero; + + switch(rc = EXIM_DBPUTB(d, key, content)) + { + case EXIM_DBPUTB_OK: + count++; + break; + + case EXIM_DBPUTB_DUP: + if (warn) fprintf(stderr, "** Duplicate key \"%s\"\n", + keybuffer); + dupcount++; + if(duperr) yield = 1; + if (lastdup) EXIM_DBPUT(d, key, content); + break; + + default: + fprintf(stderr, "Error %d while writing key %s: errno=%d\n", rc, + keybuffer, errno); + yield = 2; + goto TIDYUP; + } + + bptr = buffer; + } + + EXIM_DATUM_INIT(key); + EXIM_DATUM_DATA(key) = CS keybuffer; + + /* Deal with quoted keys. Escape sequences always make one character + out of several, so we can re-build in place. */ + + if (*s == '\"') + { + uschar *t = s++; + keystart = t; + while (*s != 0 && *s != '\"') + { + if (*s == '\\') *t++ = string_interpret_escape(&s); + else *t++ = *s; + s++; + } + if (*s != 0) s++; /* Past terminating " */ + EXIM_DATUM_SIZE(key) = t - keystart + add_zero; + } + else + { + keystart = s; + while (*s != 0 && *s != ':' && !isspace(*s)) s++; + EXIM_DATUM_SIZE(key) = s - keystart + add_zero; + } + + if (EXIM_DATUM_SIZE(key) > 256) + { + printf("Keys longer than 255 characters cannot be handled\n"); + started = 0; + yield = 2; + goto TIDYUP; + } + + if (lowercase) + { + for (i = 0; i < EXIM_DATUM_SIZE(key) - add_zero; i++) + keybuffer[i] = tolower(keystart[i]); + } + else + { + for (i = 0; i < EXIM_DATUM_SIZE(key) - add_zero; i++) + keybuffer[i] = keystart[i]; + } + + keybuffer[i] = 0; + started = 1; + + while (isspace(*s))s++; + if (*s == ':') + { + s++; + while (isspace(*s))s++; + } + if (*s != 0) + { + Ustrcpy(bptr, s); + bptr += p - s; + } + else buffer[0] = 0; + } + } + +if (started) + { + int rc; + EXIM_DATUM_INIT(content); + EXIM_DATUM_DATA(content) = CS buffer; + EXIM_DATUM_SIZE(content) = bptr - buffer + add_zero; + + switch(rc = EXIM_DBPUTB(d, key, content)) + { + case EXIM_DBPUTB_OK: + count++; + break; + + case EXIM_DBPUTB_DUP: + if (warn) fprintf(stderr, "** Duplicate key \"%s\"\n", keybuffer); + dupcount++; + if (duperr) yield = 1; + if (lastdup) EXIM_DBPUT(d, key, content); + break; + + default: + fprintf(stderr, "Error %d while writing key %s: errno=%d\n", rc, + keybuffer, errno); + yield = 2; + break; + } + } + +/* Close files, rename or abandon the temporary files, and exit */ + +TIDYUP: + +EXIM_DBCLOSE(d); +fclose(f); + +/* If successful, output the number of entries and rename the temporary +files. */ + +if (yield == 0 || yield == 1) + { + printf("%d entr%s written\n", count, (count == 1)? "y" : "ies"); + if (dupcount > 0) + { + printf("%d duplicate key%s \n", dupcount, (dupcount > 1)? "s" : ""); + } + + #if defined(USE_DB) || defined(USE_TDB) || defined(USE_GDBM) + Ustrcpy(real_dbmname, temp_dbmname); + Ustrcpy(buffer, argv[arg+1]); + if (Urename(real_dbmname, buffer) != 0) + { + printf("Unable to rename %s as %s\n", real_dbmname, buffer); + return 1; + } + #else + + /* Rename a single .db file */ + + if (is_db) + { + sprintf(CS real_dbmname, "%s.db", temp_dbmname); + sprintf(CS buffer, "%s.db", argv[arg+1]); + if (Urename(real_dbmname, buffer) != 0) + { + printf("Unable to rename %s as %s\n", real_dbmname, buffer); + return 1; + } + } + + /* Rename .dir and .pag files */ + + else + { + sprintf(CS real_dbmname, "%s.dir", temp_dbmname); + sprintf(CS buffer, "%s.dir", argv[arg+1]); + if (Urename(real_dbmname, buffer) != 0) + { + printf("Unable to rename %s as %s\n", real_dbmname, buffer); + return 1; + } + + sprintf(CS real_dbmname, "%s.pag", temp_dbmname); + sprintf(CS buffer, "%s.pag", argv[arg+1]); + if (Urename(real_dbmname, buffer) != 0) + { + printf("Unable to rename %s as %s\n", real_dbmname, buffer); + return 1; + } + } + + #endif /* USE_DB || USE_TDB || USE_GDBM */ + } + +/* Otherwise unlink the temporary files. */ + +else + { + printf("dbmbuild abandoned\n"); + #if defined(USE_DB) || defined(USE_TDB) || defined(USE_GDBM) + Uunlink(temp_dbmname); + #else + if (is_db) + { + sprintf(CS real_dbmname, "%s.db", temp_dbmname); + Uunlink(real_dbmname); + } + else + { + sprintf(CS real_dbmname, "%s.dir", temp_dbmname); + Uunlink(real_dbmname); + sprintf(CS real_dbmname, "%s.pag", temp_dbmname); + Uunlink(real_dbmname); + } + #endif /* USE_DB || USE_TDB */ + } + +return yield; +} + +/* End of exim_dbmbuild.c */ diff --git a/src/src/exim_dbutil.c b/src/src/exim_dbutil.c new file mode 100644 index 000000000..dd401741d --- /dev/null +++ b/src/src/exim_dbutil.c @@ -0,0 +1,1262 @@ +/* $Cambridge: exim/src/src/exim_dbutil.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + + +/* This single source file is used to compile three utility programs for +maintaining Exim hints databases. + + exim_dumpdb dumps out the contents + exim_fixdb patches the database (really for Exim maintenance/testing) + exim_tidydb removed obsolete data + +In all cases, the first argument is the name of the spool directory. The second +argument is the name of the database file. The available names are: + + retry: retry delivery information + misc: miscellaneous hints data + wait-: message waiting information; is a transport name + callout: callout verification cache + +There are a number of common subroutines, followed by three main programs, +whose inclusion is controlled by -D on the compilation command. */ + + +/* Standard C headers and Unix headers */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + + +/* These are two values from macros.h which should perhaps be accessible in +some better way than just repeating them here. */ + +#define WAIT_NAME_MAX 50 +#define MESSAGE_ID_LENGTH 16 + + +/* This selection of Exim headers contains exactly what we need, and hopefully +not too much extra baggage. */ + +#include "config.h" /* Needed to get the DB type */ +#include "mytypes.h" +#include "macros.h" +#include "dbstuff.h" +#include "osfunctions.h" +#include "store.h" + + +/* Identifiers for the different database types. */ + +#define type_retry 1 +#define type_wait 2 +#define type_misc 3 +#define type_callout 4 + + + + +/************************************************* +* Berkeley DB error callback * +*************************************************/ + +/* For Berkeley DB >= 2, we can define a function to be called in case of DB +errors. This should help with debugging strange DB problems, e.g. getting "File +exists" when you try to open a db file. */ + +#if defined(USE_DB) && defined(DB_VERSION_STRING) +void +dbfn_bdb_error_callback(const char *pfx, char *msg) +{ +pfx = pfx; +printf("Berkeley DB error: %s\n", msg); +} +#endif + + + +/************************************************* +* SIGALRM handler * +*************************************************/ + +static int sigalrm_seen; + +void +sigalrm_handler(int sig) +{ +sig = sig; /* Keep picky compilers happy */ +sigalrm_seen = 1; +} + + + +/************************************************* +* Output usage message and exit * +*************************************************/ + +static void +usage(uschar *name, uschar *options) +{ +printf("Usage: exim_%s%s \n", name, options); +printf(" = retry | misc | wait- | callout\n"); +exit(1); +} + + + +/************************************************* +* Sort out the command arguments * +*************************************************/ + +/* This function checks that there are exactly 2 arguments, and checks the +second of them to be sure it is a known database name. */ + +static int +check_args(int argc, uschar **argv, uschar *name, uschar *options) +{ +if (argc == 3) + { + if (Ustrcmp(argv[2], "retry") == 0) return type_retry; + if (Ustrcmp(argv[2], "misc") == 0) return type_misc; + if (Ustrncmp(argv[2], "wait-", 5) == 0) return type_wait; + if (Ustrcmp(argv[2], "callout") == 0) return type_callout; + } +usage(name, options); +return -1; /* Never obeyed */ +} + + + +/************************************************* +* Handle attempts to write the log * +*************************************************/ + +/* The message gets written to stderr when log_write() is called from a +utility. The message always gets '\n' added on the end of it. These calls come +from modules such as store.c when things go drastically wrong (e.g. malloc() +failing). In normal use they won't get obeyed. + +Arguments: + selector not relevant when running a utility + flags not relevant when running a utility + format a printf() format + ... arguments for format + +Returns: nothing +*/ + +void +log_write(unsigned int selector, int flags, char *format, ...) +{ +va_list ap; +va_start(ap, format); +vfprintf(stderr, format, ap); +fprintf(stderr, "\n"); +va_end(ap); +selector = selector; /* Keep picky compilers happy */ +flags = flags; +} + + + +/************************************************* +* Format a time value for printing * +*************************************************/ + +static uschar time_buffer[sizeof("09-xxx-1999 hh:mm:ss ")]; + +uschar * +print_time(time_t t) +{ +struct tm *tmstr = localtime(&t); +Ustrftime(time_buffer, sizeof(time_buffer), "%d-%b-%Y %H:%M:%S", tmstr); +return time_buffer; +} + + + +/************************************************* +* Format a cache value for printing * +*************************************************/ + +uschar * +print_cache(int value) +{ +return (value == ccache_accept)? US"accept" : + (value == ccache_reject)? US"reject" : + US"unknown"; +} + + +#ifdef EXIM_FIXDB +/************************************************* +* Read time value * +*************************************************/ + +static time_t +read_time(uschar *s) +{ +uschar *t = s; +int field = 0; +int value; +time_t now = time(NULL); +struct tm *tm = localtime(&now); + +tm->tm_sec = 0; +tm->tm_isdst = -1; + +for (t = s + Ustrlen(s) - 1; t >= s; t--) + { + if (*t == ':') continue; + if (!isdigit((uschar)*t)) return -1; + + value = *t - '0'; + if (--t >= s) + { + if (!isdigit((uschar)*t)) return -1; + value = value + (*t - '0')*10; + } + + switch (field++) + { + case 0: tm->tm_min = value; break; + case 1: tm->tm_hour = value; break; + case 2: tm->tm_mday = value; break; + case 3: tm->tm_mon = value - 1; break; + case 4: tm->tm_year = (value < 90)? value + 100 : value; break; + default: return -1; + } + } + +return mktime(tm); +} +#endif /* EXIM_FIXDB */ + + + +/************************************************* +* Open and lock a database file * +*************************************************/ + +/* This is a cut-down version from the function in dbfn.h that Exim itself +uses. We assume the database exists, and therefore give up if we cannot open +the lock file. + +Arguments: + spool The spool directory + name The single-component name of one of Exim's database files. + flags O_RDONLY or O_RDWR + dbblock Points to an open_db block to be filled in. + +Returns: NULL if the open failed, or the locking failed. + On success, dbblock is returned. This contains the dbm pointer and + the fd of the locked lock file. +*/ + +static open_db * +dbfn_open(uschar *spool, uschar *name, int flags, open_db *dbblock) +{ +int rc; +struct flock lock_data; +BOOL read_only = flags == O_RDONLY; +uschar buffer[256]; + +/* The first thing to do is to open a separate file on which to lock. This +ensures that Exim has exclusive use of the database before it even tries to +open it. If there is a database, there should be a lock file in existence. */ + +sprintf(CS buffer, "%s/db/%s.lockfile", spool, name); + +dbblock->lockfd = Uopen(buffer, flags, 0); +if (dbblock->lockfd < 0) + { + printf("** Failed to open database lock file %s: %s\n", buffer, + strerror(errno)); + return NULL; + } + +/* Now we must get a lock on the opened lock file; do this with a blocking +lock that times out. */ + +lock_data.l_type = read_only? F_RDLCK : F_WRLCK; +lock_data.l_whence = lock_data.l_start = lock_data.l_len = 0; + +sigalrm_seen = FALSE; +os_non_restarting_signal(SIGALRM, sigalrm_handler); +alarm(EXIMDB_LOCK_TIMEOUT); +rc = fcntl(dbblock->lockfd, F_SETLKW, &lock_data); +alarm(0); + +if (sigalrm_seen) errno = ETIMEDOUT; +if (rc < 0) + { + printf("** Failed to get %s lock for %s: %s", + ((flags & O_RDONLY) != 0)? "read" : "write", buffer, + (errno == ETIMEDOUT)? "timed out" : strerror(errno)); + close(dbblock->lockfd); + return NULL; + } + +/* At this point we have an opened and locked separate lock file, that is, +exclusive access to the database, so we can go ahead and open it. */ + +sprintf(CS buffer, "%s/db/%s", spool, name); +EXIM_DBOPEN(buffer, flags, 0, &(dbblock->dbptr)); + +if (dbblock->dbptr == NULL) + { + printf("** Failed to open DBM file %s for %s:\n %s%s\n", buffer, + read_only? "reading" : "writing", strerror(errno), + #ifdef USE_DB + " (or Berkeley DB error while opening)" + #else + "" + #endif + ); + close(dbblock->lockfd); + return NULL; + } + +return dbblock; +} + + + + +/************************************************* +* Unlock and close a database file * +*************************************************/ + +/* Closing a file automatically unlocks it, so after closing the database, just +close the lock file. + +Argument: a pointer to an open database block +Returns: nothing +*/ + +static void +dbfn_close(open_db *dbblock) +{ +EXIM_DBCLOSE(dbblock->dbptr); +close(dbblock->lockfd); +} + + + + +/************************************************* +* Read from database file * +*************************************************/ + +/* Passing back the pointer unchanged is useless, because there is no guarantee +of alignment. Since all the records used by Exim need to be properly aligned to +pick out the timestamps, etc., do the copying centrally here. + +Arguments: + dbblock a pointer to an open database block + key the key of the record to be read + length where to put the length (or NULL if length not wanted) + +Returns: a pointer to the retrieved record, or + NULL if the record is not found +*/ + +static void * +dbfn_read_with_length(open_db *dbblock, uschar *key, int *length) +{ +void *yield; +EXIM_DATUM key_datum, result_datum; + +EXIM_DATUM_INIT(key_datum); /* Some DBM libraries require the datum */ +EXIM_DATUM_INIT(result_datum); /* to be cleared before use. */ +EXIM_DATUM_DATA(key_datum) = CS key; +EXIM_DATUM_SIZE(key_datum) = Ustrlen(key) + 1; + +if (!EXIM_DBGET(dbblock->dbptr, key_datum, result_datum)) return NULL; + +yield = store_get(EXIM_DATUM_SIZE(result_datum)); +memcpy(yield, EXIM_DATUM_DATA(result_datum), EXIM_DATUM_SIZE(result_datum)); +if (length != NULL) *length = EXIM_DATUM_SIZE(result_datum); + +EXIM_DATUM_FREE(result_datum); /* Some DBM libs require freeing */ +return yield; +} + + + +#if defined(EXIM_TIDYDB) || defined(EXIM_FIXDB) + +/************************************************* +* Write to database file * +*************************************************/ + +/* +Arguments: + dbblock a pointer to an open database block + key the key of the record to be written + ptr a pointer to the record to be written + length the length of the record to be written + +Returns: the yield of the underlying dbm or db "write" function. If this + is dbm, the value is zero for OK. +*/ + +static int +dbfn_write(open_db *dbblock, uschar *key, void *ptr, int length) +{ +EXIM_DATUM key_datum, value_datum; +dbdata_generic *gptr = (dbdata_generic *)ptr; +gptr->time_stamp = time(NULL); + +EXIM_DATUM_INIT(key_datum); /* Some DBM libraries require the datum */ +EXIM_DATUM_INIT(value_datum); /* to be cleared before use. */ +EXIM_DATUM_DATA(key_datum) = CS key; +EXIM_DATUM_SIZE(key_datum) = Ustrlen(key) + 1; +EXIM_DATUM_DATA(value_datum) = CS ptr; +EXIM_DATUM_SIZE(value_datum) = length; +return EXIM_DBPUT(dbblock->dbptr, key_datum, value_datum); +} + + + +/************************************************* +* Delete record from database file * +*************************************************/ + +/* +Arguments: + dbblock a pointer to an open database block + key the key of the record to be deleted + +Returns: the yield of the underlying dbm or db "delete" function. +*/ + +static int +dbfn_delete(open_db *dbblock, uschar *key) +{ +EXIM_DATUM key_datum; +EXIM_DATUM_INIT(key_datum); /* Some DBM libraries require clearing */ +EXIM_DATUM_DATA(key_datum) = CS key; +EXIM_DATUM_SIZE(key_datum) = Ustrlen(key) + 1; +return EXIM_DBDEL(dbblock->dbptr, key_datum); +} + +#endif /* EXIM_TIDYDB || EXIM_FIXDB */ + + + +#if defined(EXIM_DUMPDB) || defined(EXIM_TIDYDB) +/************************************************* +* Scan the keys of a database file * +*************************************************/ + +/* +Arguments: + dbblock a pointer to an open database block + start TRUE if starting a new scan + FALSE if continuing with the current scan + cursor a pointer to a pointer to a cursor anchor, for those dbm libraries + that use the notion of a cursor + +Returns: the next record from the file, or + NULL if there are no more +*/ + +static uschar * +dbfn_scan(open_db *dbblock, BOOL start, EXIM_CURSOR **cursor) +{ +EXIM_DATUM key_datum, value_datum; +uschar *yield; +value_datum = value_datum; /* dummy; not all db libraries use this */ + +/* Some dbm require an initialization */ + +if (start) EXIM_DBCREATE_CURSOR(dbblock->dbptr, cursor); + +EXIM_DATUM_INIT(key_datum); /* Some DBM libraries require the datum */ +EXIM_DATUM_INIT(value_datum); /* to be cleared before use. */ + +yield = (EXIM_DBSCAN(dbblock->dbptr, key_datum, value_datum, start, *cursor))? + US EXIM_DATUM_DATA(key_datum) : NULL; + +/* Some dbm require a termination */ + +if (!yield) EXIM_DBDELETE_CURSOR(*cursor); +return yield; +} +#endif /* EXIM_DUMPDB || EXIM_TIDYDB */ + + + +#ifdef EXIM_DUMPDB +/************************************************* +* The exim_dumpdb main program * +*************************************************/ + +int +main(int argc, char **cargv) +{ +int dbdata_type = 0; +int yield = 0; +open_db dbblock; +open_db *dbm; +EXIM_CURSOR *cursor; +uschar **argv = USS cargv; +uschar *key; +uschar keybuffer[1024]; + +/* Check the arguments, and open the database */ + +dbdata_type = check_args(argc, argv, US"dumpdb", US""); +dbm = dbfn_open(argv[1], argv[2], O_RDONLY, &dbblock); +if (dbm == NULL) exit(1); + +/* Scan the file, formatting the information for each entry. Note +that data is returned in a malloc'ed block, in order that it be +correctly aligned. */ + +key = dbfn_scan(dbm, TRUE, &cursor); +while (key != NULL) + { + dbdata_retry *retry; + dbdata_wait *wait; + dbdata_callout_cache *callout; + int count_bad = 0; + int i, length; + uschar *t; + uschar name[MESSAGE_ID_LENGTH + 1]; + void *value; + + /* Keep a copy of the key separate, as in some DBM's the pointer is into data + which might change. */ + + if (Ustrlen(key) > sizeof(keybuffer) - 1) + { + printf("**** Overlong key encountered: %s\n", key); + return 1; + } + Ustrcpy(keybuffer, key); + value = dbfn_read_with_length(dbm, keybuffer, &length); + + if (value == NULL) + fprintf(stderr, "**** Entry \"%s\" was in the key scan, but the record " + "was not found in the file - something is wrong!\n", + CS keybuffer); + else + { + /* Note: don't use print_time more than once in one statement, since + it uses a single buffer. */ + + switch(dbdata_type) + { + case type_retry: + retry = (dbdata_retry *)value; + printf(" %s %d %d %s\n%s ", keybuffer, retry->basic_errno, + retry->more_errno, retry->text, + print_time(retry->first_failed)); + printf("%s ", print_time(retry->last_try)); + printf("%s %s\n", print_time(retry->next_try), + (retry->expired)? "*" : ""); + break; + + case type_wait: + wait = (dbdata_wait *)value; + printf("%s ", keybuffer); + t = wait->text; + name[MESSAGE_ID_LENGTH] = 0; + + if (wait->count > WAIT_NAME_MAX) + { + fprintf(stderr, + "**** Data for %s corrupted\n count=%d=0x%x max=%d\n", + CS keybuffer, wait->count, wait->count, WAIT_NAME_MAX); + wait->count = WAIT_NAME_MAX; + yield = count_bad = 1; + } + for (i = 1; i <= wait->count; i++) + { + Ustrncpy(name, t, MESSAGE_ID_LENGTH); + if (count_bad && name[0] == 0) break; + if (Ustrlen(name) != MESSAGE_ID_LENGTH || + Ustrspn(name, "0123456789" + "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ-") != MESSAGE_ID_LENGTH) + { + int j; + fprintf(stderr, + "**** Data for %s corrupted: bad character in message id\n", + CS keybuffer); + for (j = 0; j < MESSAGE_ID_LENGTH; j++) + fprintf(stderr, "%02x ", name[j]); + fprintf(stderr, "\n"); + yield = 1; + break; + } + printf("%s ", name); + t += MESSAGE_ID_LENGTH; + } + printf("\n"); + break; + + case type_misc: + printf("%s %s\n", print_time(((dbdata_generic *)value)->time_stamp), + keybuffer); + break; + + case type_callout: + callout = (dbdata_callout_cache *)value; + + /* New-style address record */ + + if (length == sizeof(dbdata_callout_cache_address)) + { + printf("%s %s callout=%s\n", + print_time(((dbdata_generic *)value)->time_stamp), + keybuffer, + print_cache(callout->result)); + } + + /* New-style domain record */ + + else if (length == sizeof(dbdata_callout_cache)) + { + printf("%s %s callout=%s postmaster=%s", + print_time(((dbdata_generic *)value)->time_stamp), + keybuffer, + print_cache(callout->result), + print_cache(callout->postmaster_result)); + if (callout->postmaster_result != ccache_unknown) + printf(" (%s)", print_time(callout->postmaster_stamp)); + printf(" random=%s", print_cache(callout->random_result)); + if (callout->random_result != ccache_unknown) + printf(" (%s)", print_time(callout->random_stamp)); + printf("\n"); + } + + /* Old-style domain record, without separate timestamps. This code can + eventually be thrown away, say in 5 years' time (it's now Feb 2003). */ + + else + { + printf("%s %s callout=%s postmaster=%s random=%s\n", + print_time(((dbdata_generic *)value)->time_stamp), + keybuffer, + print_cache(callout->result), + print_cache(callout->postmaster_result), + print_cache(callout->random_result)); + } + + break; + } + store_reset(value); + } + key = dbfn_scan(dbm, FALSE, &cursor); + } + +dbfn_close(dbm); +return yield; +} + +#endif /* EXIM_DUMPDB */ + + + + +#ifdef EXIM_FIXDB +/************************************************* +* The exim_fixdb main program * +*************************************************/ + +/* In order not to hold the database lock any longer than is necessary, each +operation on the database uses a separate open/close call. This is expensive, +but then using this utility is not expected to be very common. Its main use is +to provide a way of patching up hints databases in order to run tests. + +Syntax of commands: + +(1) + This causes the data from the given record to be displayed, or "not found" + to be output. Note that in the retry database, destination names are + preceded by R: or T: for router or transport retry info. + +(2) d + This causes the given record to be deleted or "not found" to be output. + +(3) + This sets the given value into the given field, identified by a number + which is output by the display command. Not all types of record can + be changed. + +(4) q + This exits from exim_fixdb. + +If the record name is omitted from (2) or (3), the previously used record name +is re-used. */ + + +int main(int argc, char **cargv) +{ +int dbdata_type; +uschar **argv = USS cargv; +uschar buffer[256]; +uschar name[256]; +void *reset_point = store_get(0); + +name[0] = 0; /* No name set */ + +/* Sort out the database type, verify what we are working on and then process +user requests */ + +dbdata_type = check_args(argc, argv, US"fixdb", US""); +printf("Modifying Exim hints database %s/db/%s\n", argv[1], argv[2]); + +for(;;) + { + open_db dbblock; + open_db *dbm; + void *record; + dbdata_retry *retry; + dbdata_wait *wait; + dbdata_callout_cache *callout; + int i, oldlength; + uschar *t; + uschar field[256], value[256]; + + store_reset(reset_point); + + printf("> "); + if (Ufgets(buffer, 256, stdin) == NULL) break; + + buffer[Ustrlen(buffer)-1] = 0; + field[0] = value[0] = 0; + + /* If the buffer contains just one digit, or just consists of "d", use the + previous name for an update. */ + + if ((isdigit((uschar)buffer[0]) && !isdigit((uschar)buffer[1])) || + Ustrcmp(buffer, "d") == 0) + { + if (name[0] == 0) + { + printf("No previous record name is set\n"); + continue; + } + sscanf(CS buffer, "%s %s", field, value); + } + else + { + name[0] = 0; + sscanf(CS buffer, "%s %s %s", name, field, value); + } + + /* Handle an update request */ + + if (field[0] != 0) + { + int verify = 1; + dbm = dbfn_open(argv[1], argv[2], O_RDWR, &dbblock); + if (dbm == NULL) continue; + + if (Ustrcmp(field, "d") == 0) + { + if (value[0] != 0) printf("unexpected value after \"d\"\n"); + else printf("%s\n", (dbfn_delete(dbm, name) < 0)? + "not found" : "deleted"); + dbfn_close(dbm); + continue; + } + + else if (isdigit((uschar)field[0])) + { + int fieldno = Uatoi(field); + if (value[0] == 0) + { + printf("value missing\n"); + dbfn_close(dbm); + continue; + } + else + { + record = dbfn_read_with_length(dbm, name, &oldlength); + if (record == NULL) printf("not found\n"); else + { + time_t tt; + int length = 0; /* Stops compiler warning */ + + switch(dbdata_type) + { + case type_retry: + retry = (dbdata_retry *)record; + length = sizeof(dbdata_retry) + Ustrlen(retry->text); + + switch(fieldno) + { + case 0: + retry->basic_errno = Uatoi(value); + break; + + case 1: + retry->more_errno = Uatoi(value); + break; + + case 2: + if ((tt = read_time(value)) > 0) retry->first_failed = tt; + else printf("bad time value\n"); + break; + + case 3: + if ((tt = read_time(value)) > 0) retry->last_try = tt; + else printf("bad time value\n"); + break; + + case 4: + if ((tt = read_time(value)) > 0) retry->next_try = tt; + else printf("bad time value\n"); + break; + + case 5: + if (Ustrcmp(value, "yes") == 0) retry->expired = TRUE; + else if (Ustrcmp(value, "no") == 0) retry->expired = FALSE; + else printf("\"yes\" or \"no\" expected=n"); + break; + + default: + printf("unknown field number\n"); + verify = 0; + break; + } + break; + + case type_wait: + printf("Can't change contents of wait database record\n"); + break; + + case type_misc: + printf("Can't change contents of misc database record\n"); + break; + + case type_callout: + callout = (dbdata_callout_cache *)record; + length = sizeof(dbdata_callout_cache); + switch(fieldno) + { + case 0: + callout->result = Uatoi(value); + break; + + case 1: + callout->postmaster_result = Uatoi(value); + break; + + case 2: + callout->random_result = Uatoi(value); + break; + + default: + printf("unknown field number\n"); + verify = 0; + break; + } + break; + } + + dbfn_write(dbm, name, record, length); + } + } + } + + else + { + printf("field number or d expected\n"); + verify = 0; + } + + dbfn_close(dbm); + if (!verify) continue; + } + + /* The "name" q causes an exit */ + + else if (Ustrcmp(name, "q") == 0) return 0; + + /* Handle a read request, or verify after an update. */ + + dbm = dbfn_open(argv[1], argv[2], O_RDONLY, &dbblock); + if (dbm == NULL) continue; + + record = dbfn_read_with_length(dbm, name, &oldlength); + if (record == NULL) + { + printf("record %s not found\n", name); + name[0] = 0; + } + else + { + int count_bad = 0; + printf("%s\n", CS print_time(((dbdata_generic *)record)->time_stamp)); + switch(dbdata_type) + { + case type_retry: + retry = (dbdata_retry *)record; + printf("0 error number: %d %s\n", retry->basic_errno, retry->text); + printf("1 extra data: %d\n", retry->more_errno); + printf("2 first failed: %s\n", print_time(retry->first_failed)); + printf("3 last try: %s\n", print_time(retry->last_try)); + printf("4 next try: %s\n", print_time(retry->next_try)); + printf("5 expired: %s\n", (retry->expired)? "yes" : "no"); + break; + + case type_wait: + wait = (dbdata_wait *)record; + t = wait->text; + printf("Sequence: %d\n", wait->sequence); + if (wait->count > WAIT_NAME_MAX) + { + printf("**** Data corrupted: count=%d=0x%x max=%d ****\n", wait->count, + wait->count, WAIT_NAME_MAX); + wait->count = WAIT_NAME_MAX; + count_bad = 1; + } + for (i = 1; i <= wait->count; i++) + { + Ustrncpy(value, t, MESSAGE_ID_LENGTH); + value[MESSAGE_ID_LENGTH] = 0; + if (count_bad && value[0] == 0) break; + if (Ustrlen(value) != MESSAGE_ID_LENGTH || + Ustrspn(value, "0123456789" + "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ-") != MESSAGE_ID_LENGTH) + { + int j; + printf("\n**** Data corrupted: bad character in message id ****\n"); + for (j = 0; j < MESSAGE_ID_LENGTH; j++) + printf("%02x ", value[j]); + printf("\n"); + break; + } + printf("%s ", value); + t += MESSAGE_ID_LENGTH; + } + printf("\n"); + break; + + case type_misc: + break; + + case type_callout: + callout = (dbdata_callout_cache *)record; + printf("0 callout: %s (%d)\n", print_cache(callout->result), + callout->result); + if (oldlength > sizeof(dbdata_callout_cache_address)) + { + printf("1 postmaster: %s (%d)\n", print_cache(callout->postmaster_result), + callout->postmaster_result); + printf("2 random: %s (%d)\n", print_cache(callout->random_result), + callout->random_result); + } + break; + } + } + + /* The database is closed after each request */ + + dbfn_close(dbm); + } + +printf("\n"); +return 0; +} + +#endif /* EXIM_FIXDB */ + + + +#ifdef EXIM_TIDYDB +/************************************************* +* The exim_tidydb main program * +*************************************************/ + + +/* Utility program to tidy the contents of an exim database file. There is one +option: + + -t + +Don't display relaying information that matches. + +=item B<-nt> + +Don't display transport information. + +=item B<-nt>I + +Don't display transport information that matches + +=item B<-q>I + +List of times for queuing information single 0 item suppresses. + +=item B<-t>I + +Display top sources/destinations +default is 50, 0 suppresses top listing. + +=item B<-tnl> + +Omit local sources/destinations in top listing. + +=item B<-t_remote_users> + +Include remote users in the top source/destination listings. + +=item B<-byhost> + +Show results by sending host. This may be combined with +B<-bydomain> and/or B<-byemail> and/or B<-byedomain>. If none of these options +are specified, then B<-byhost> is assumed as a default. + +=item B<-bydomain> + +Show results by sending domain. +May be combined with B<-byhost> and/or B<-byemail> and/or B<-byedomain>. + +=item B<-byemail> + +Show results by sender's email address. +May be combined with B<-byhost> and/or B<-bydomain> and/or B<-byedomain>. + +=item B<-byemaildomain> or B<-byedomain> + +Show results by sender's email domain. +May be combined with B<-byhost> and/or B<-bydomain> and/or B<-byemail>. + +=item B<-pattern> I I + +Look for the specified pattern and count the number of lines in which it appears. +This option can be specified multiple times. Eg: + + -pattern 'Refused connections' '/refused connection/' + + +=item B<-merge> + +This option allows eximstats to merge old eximstat reports together. Eg: + + eximstats mainlog.sun > report.sun.txt + eximstats mainlog.mon > report.mon.txt + eximstats mainlog.tue > report.tue.txt + eximstats mainlog.wed > report.web.txt + eximstats mainlog.thu > report.thu.txt + eximstats mainlog.fri > report.fri.txt + eximstats mainlog.sat > report.sat.txt + eximstats -merge report.*.txt > weekly_report.txt + eximstats -merge -html report.*.txt > weekly_report.html + +=over 4 + +=item * + +You can merge text or html reports and output the results as text or html. + +=item * + +You can use all the normal eximstat output options, but only data +included in the original reports can be shown! + +=item * + +When merging reports, some loss of accuracy may occur in the top I lists. +This will be towards the ends of the lists. + +=item * + +The order of items in the top I lists may vary when the data volumes +round to the same value. + +=back + +=item B<-html> + +Output the results in HTML. + +=item B<-charts> + +Create graphical charts to be displayed in HTML output. + +This requires the following modules which can be obtained +from http://www.cpan.org/modules/01modules.index.html + +=over 4 + +=item GD + +=item GDTextUtil + +=item GDGraph + +=back + +To install these, download and unpack them, then use the normal perl installation procedure: + + perl Makefile.PL + make + make test + make install + +=item B<-chartdir>I + +Create the charts in the directory + +=item B<-chartrel>I + +Specify the relative directory for the "img src=" tags from where to include +the charts + +=item B<-d> + +Debug flag. This outputs the eval()'d parser onto STDOUT which makes it +easier to trap errors in the eval section. Remember to add 1 to the line numbers to allow for the +title! + +=back + +=head1 DESCRIPTION + +Eximstats parses exim mainlog files and outputs a statistical +analysis of the messages processed. By default, a text +analysis is generated, but you can request an html analysis +by using the B<-html> flag. See the help (B<-help>) to learn +about how to create charts from the tables. + +=head1 AUTHOR + +There is a web site at http://www.exim.org - this contains details of the +mailing list exim-users@exim.org. + +=head1 TO DO + +This program does not perfectly handle messages whose received +and delivered log lines are in different files, which can happen +when you have multiple mail servers and a message cannot be +immeadiately delivered. Fixing this could be tricky... + +=head1 SUBROUTINES + +The following section will only be of interest to the +program maintainers: + +=cut + +use integer; +use strict; + +# use Time::Local; # PH/FANF +use POSIX; + +use vars qw($HAVE_GD_Graph_pie $HAVE_GD_Graph_linespoints); +eval { require GD::Graph::pie; }; +$HAVE_GD_Graph_pie = $@ ? 0 : 1; +eval { require GD::Graph::linespoints; }; +$HAVE_GD_Graph_linespoints = $@ ? 0 : 1; + + +################################################## +# Static data # +################################################## +# 'use vars' instead of 'our' as perl5.005 is still in use out there! +use vars qw(@tab62 @days_per_month $gig); +use vars qw($VERSION); +use vars qw($COLUMN_WIDTHS); + + +@tab62 = + (0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, # 0-9 + 0,10,11,12,13,14,15,16,17,18,19,20, # A-K + 21,22,23,24,25,26,27,28,29,30,31,32, # L-W + 33,34,35, 0, 0, 0, 0, 0, # X-Z + 0,36,37,38,39,40,41,42,43,44,45,46, # a-k + 47,48,49,50,51,52,53,54,55,56,57,58, # l-w + 59,60,61); # x-z + +@days_per_month = (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334); +$gig = 1024 * 1024 * 1024; +$VERSION = '1.31'; + +# How much space do we allow for the Hosts/Domains/Emails/Edomains column headers? +$COLUMN_WIDTHS = 8; + +# Declare global variables. +use vars qw($total_received_data $total_received_data_gigs $total_received_count); +use vars qw($total_delivered_data $total_delivered_data_gigs $total_delivered_count); +use vars qw(%arrival_time %size %from_host %from_address); +use vars qw(%timestamp2time); #Hash of timestamp => time. +use vars qw($last_timestamp $last_time); #The last time convertion done. +use vars qw($last_date $date_seconds); #The last date convertion done. +use vars qw($last_offset $offset_seconds); #The last time offset convertion done. +use vars qw($localtime_offset); +use vars qw($i); #General loop counter. +use vars qw($debug); #Debug mode? +use vars qw($ntopchart); #How many entries should make it into the chart? +use vars qw($gddirectory); #Where to put files from GD::Graph + +$ntopchart = 5; + +# The following are parameters whose values are +# set by command line switches: +use vars qw($show_errors $show_relay $show_transport $transport_pattern); +use vars qw($topcount $local_league_table $include_remote_users); +use vars qw($hist_opt $hist_interval $hist_number $volume_rounding); +use vars qw($relay_pattern @queue_times $html @user_patterns @user_descriptions); + +use vars qw(%do_sender); #Do sender by Host, Domain, Email, and/or Edomain tables. +use vars qw($charts $chartrel $chartdir $charts_option_specified); +use vars qw($merge_reports); #Merge old reports ? + +# The following are modified in the parse() routine, and +# referred to in the print_*() routines. +use vars qw($queue_more_than $delayed_count $relayed_unshown $begin $end); +use vars qw(%received_count %received_data %received_data_gigs); +use vars qw(%delivered_count %delivered_data %delivered_data_gigs); +use vars qw(%received_count_user %received_data_user %received_data_gigs_user); +use vars qw(%delivered_count_user %delivered_data_user %delivered_data_gigs_user); +use vars qw(%transported_count %transported_data %transported_data_gigs); +use vars qw(%remote_delivered %relayed %delayed %had_error %errors_count); +use vars qw(@queue_bin @remote_queue_bin @received_interval_count @delivered_interval_count); +use vars qw(@user_pattern_totals); + +use vars qw(%report_totals); + + + + +################################################## +# Subroutines # +################################################## + + +=head2 volume_rounded(); + + $rounded_volume = volume_rounded($bytes,$gigabytes); + +Given a data size in bytes, round it to KB, MB, or GB +as appropriate. + +Eg 12000 => 12KB, 15000000 => 14GB, etc. + +Note: I've experimented with Math::BigInt and it results in a 33% +performance degredation as opposed to storing numbers split into +bytes and gigabytes. + +=cut + +sub volume_rounded { + my($x,$g) = @_; + $x = 0 unless $x; + $g = 0 unless $g; + my($rounded); + + while ($x > $gig) { + $g++; + $x -= $gig; + } + + if ($volume_rounding) { + # Values < 1 GB + if ($g <= 0) { + if ($x < 10000) { + $rounded = sprintf("%6d", $x); + } + elsif ($x < 10000000) { + $rounded = sprintf("%4dKB", ($x + 512)/1024); + } + else { + $rounded = sprintf("%4dMB", ($x + 512*1024)/(1024*1024)); + } + } + # Values between 1GB and 10GB are printed in MB + elsif ($g < 10) { + $rounded = sprintf("%4dMB", ($g * 1024) + ($x + 512*1024)/(1024*1024)); + } + else { + # Handle values over 10GB + $rounded = sprintf("%4dGB", $g + ($x + $gig/2)/$gig); + } + } + else { + # We don't want any rounding to be done. + $rounded = sprintf("%4d", ($g * $gig) + $x); + } + + return $rounded; +} + + +=head2 un_round(); + + un_round($rounded_volume,\$bytes,\$gigabytes); + +Given a volume in KB, MB or GB, as generated by volume_rounded(), +do the reverse transformation and convert it back into Bytes and Gigabytes. +These are added to the $bytes and $gigabytes parameters. + +Given a data size in bytes, round it to KB, MB, or GB +as appropriate. + +EG: 500 => (500,0), 14GB => (0,14), etc. + +=cut + +sub un_round { + my($rounded,$bytes_sref,$gigabytes_sref) = @_; + + if ($rounded =~ /(\d+)GB/) { + $$gigabytes_sref += $1; + } + elsif ($rounded =~ /(\d+)MB/) { + $$gigabytes_sref += $1 / 1024; + $$bytes_sref += (($1 % 1024 ) * 1024 * 1024); + } + elsif ($rounded =~ /(\d+)KB/) { + $$gigabytes_sref += $1 / (1024 * 1024); + $$bytes_sref += ($1 % (1024 * 1024) * 1024); + } + elsif ($rounded =~ /(\d+)/) { + $$gigabytes_sref += $1 / $gig; + $$bytes_sref += $1 % $gig; + } + + #Now reduce the bytes down to less than 1GB. + add_volume($bytes_sref,$gigabytes_sref,0) if ($$bytes_sref > $gig); +} + + +=head2 add_volume(); + + add_volume(\$bytes,\$gigs,$size); + +Add $size to $bytes/$gigs where this is a number split into +bytes ($bytes) and gigabytes ($gigs). This is significantly +faster than using Math::BigInt. + +=cut + +sub add_volume { +my($bytes_ref,$gigs_ref,$size) = @_; +$$bytes_ref = 0 if ! defined $$bytes_ref; +$$gigs_ref = 0 if ! defined $$gigs_ref; +$$bytes_ref += $size; +while ($$bytes_ref > $gig) + { + $$gigs_ref++; + $$bytes_ref -= $gig; + } +} + + +=head2 format_time(); + + $formatted_time = format_time($seconds); + +Given a time in seconds, break it down into +weeks, days, hours, minutes, and seconds. + +Eg 12005 => 3h20m5s + +=cut + +sub format_time { +my($t) = pop @_; +my($s) = $t % 60; +$t /= 60; +my($m) = $t % 60; +$t /= 60; +my($h) = $t % 24; +$t /= 24; +my($d) = $t % 7; +my($w) = $t/7; +my($p) = ""; +$p .= "$w"."w" if $w > 0; +$p .= "$d"."d" if $d > 0; +$p .= "$h"."h" if $h > 0; +$p .= "$m"."m" if $m > 0; +$p .= "$s"."s" if $s > 0 || $p eq ""; +$p; +} + + +=head2 unformat_time(); + + $seconds = unformat_time($formatted_time); + +Given a time in weeks, days, hours, minutes, or seconds, convert it to seconds. + +Eg 3h20m5s => 12005 + +=cut + +sub unformat_time { + my($formated_time) = pop @_; + my $time = 0; + + while ($formated_time =~ s/^(\d+)([wdhms]?)//) { + $time += $1 if ($2 eq '' || $2 eq 's'); + $time += $1 * 60 if ($2 eq 'm'); + $time += $1 * 60 * 60 if ($2 eq 'h'); + $time += $1 * 60 * 60 * 24 if ($2 eq 'd'); + $time += $1 * 60 * 60 * 24 * 7 if ($2 eq 'w'); + } + $time; +} + + +=head2 seconds(); + + $time = seconds($timestamp); + +Given a time-of-day timestamp, convert it into a time() value using +POSIX::mktime. We expect the timestamp to be of the form +"$year-$mon-$day $hour:$min:$sec", with month going from 1 to 12, +and the year to be absolute (we do the necessary conversions). The +timestamp may be followed with an offset from UTC like "+$hh$mm"; if the +offset is not present, and we have not been told that the log is in UTC +(with the -utc option), then we adjust the time by the current local +time offset so that it can be compared with the time recorded in message +IDs, which is UTC. + +To improve performance, we only use mktime on the date ($year-$mon-$day), +and only calculate it if the date is different to the previous time we +came here. We then add on seconds for the '$hour:$min:$sec'. + +We also store the results of the last conversion done, and only +recalculate if the date is different. + +We used to have the '-cache' flag which would store the results of the +mktime() call. However, the current way of just using mktime() on the +date obsoletes this. + +=cut + +sub seconds { + my($timestamp) = @_; + + # Is the timestamp the same as the last one? + return $last_time if ($last_timestamp eq $timestamp); + + return 0 unless ($timestamp =~ /^((\d{4})\-(\d\d)-(\d\d))\s(\d\d):(\d\d):(\d\d)( ([+-])(\d\d)(\d\d))?/o); + + unless ($last_date eq $1) { + $last_date = $1; + my(@timestamp) = (0,0,0,$4,$3,$2); + $timestamp[5] -= 1900; + $timestamp[4]--; + $date_seconds = mktime(@timestamp); + } + my $time = $date_seconds + ($5 * 3600) + ($6 * 60) + $7; + + # SC. Use cacheing. Also note we want seconds not minutes. + #my($this_offset) = ($10 * 60 + $11) * ($9 . "1") if defined $8; + if (defined $8 && ($8 ne $last_offset)) { + $last_offset = $8; + $offset_seconds = ($10 * 60 + $11) * 60; + $offset_seconds = -$offset_seconds if ($9 eq '-'); + } + + + if (defined $7) { + #$time -= $this_offset; + $time -= $offset_seconds; + } elsif (defined $localtime_offset) { + $time -= $localtime_offset; + } + + # Store the last timestamp received. + $last_timestamp = $timestamp; + $last_time = $time; + + $time; +} + + +=head2 id_seconds(); + + $time = id_seconds($message_id); + +Given a message ID, convert it into a time() value. + +=cut + +sub id_seconds { +my($sub_id) = substr((pop @_), 0, 6); +my($s) = 0; +my(@c) = split(//, $sub_id); +while($#c >= 0) { $s = $s * 62 + $tab62[ord(shift @c) - ord('0')] } +$s; +} + + + +=head2 calculate_localtime_offset(); + + $localtime_offset = calculate_localtime_offset(); + +Calculate the the localtime offset from gmtime in seconds. + + $localtime = time() + $localtime_offset. + +These are the same semantics as ISO 8601 and RFC 2822 timezone offsets. +(West is negative, East is positive.) + +=cut + +# $localtime = gmtime() + $localtime_offset. OLD COMMENT +# This subroutine commented out as it's not currently in use. + +#sub calculate_localtime_offset { +# # Pick an arbitrary date, convert it to localtime & gmtime, and return the difference. +# my (@sample_date) = (0,0,0,5,5,100); +# my $localtime = timelocal(@sample_date); +# my $gmtime = timegm(@sample_date); +# my $offset = $localtime - $gmtime; +# return $offset; +#} + +sub calculate_localtime_offset { + # Assume that the offset at the moment is valid across the whole + # period covered by the logs that we're analysing. This may not + # be true around the time the clocks change in spring or autumn. + my $utc = time; + # mktime works on local time and gmtime works in UTC + my $local = mktime(gmtime($utc)); + return $local - $utc; +} + + +=head2 print_queue_times(); + + $time = print_queue_times($message_type,\@queue_times,$queue_more_than); + +Given the type of messages being output, the array of message queue times, +and the number of messages which exceeded the queue times, print out +a table. + +=cut + +sub print_queue_times { +no integer; +my($string,$array,$queue_more_than) = @_; +my(@chartdatanames); +my(@chartdatavals); + +my $printed_one = 0; +my $cumulative_percent = 0; +#$queue_unknown += keys %arrival_time; + +my $queue_total = $queue_more_than; +for ($i = 0; $i <= $#queue_times; $i++) { $queue_total += $$array[$i] } + +my $temp = "Time spent on the queue: $string"; + +my($format); +if ($html) { + print "

$temp

\n"; + print "\n"; + print "
\n"; + print "\n"; + print "\n"; + $format = "\n"; +} +else +{ + printf("%s\n%s\n\n", $temp, "-" x length($temp)); + $format = "%5s %4s %6d %5.1f%% %5.1f%%\n"; +} + +for ($i = 0; $i <= $#queue_times; $i++) { + if ($$array[$i] > 0) + { + my $percent = ($$array[$i] * 100)/$queue_total; + $cumulative_percent += $percent; + printf($format, + $printed_one? " " : "Under", + format_time($queue_times[$i]), + $$array[$i], $percent, $cumulative_percent); + if (!defined($queue_times[$i])) { + print "Not defined"; + } + push(@chartdatanames, + ($printed_one? "" : "Under") . format_time($queue_times[$i])); + push(@chartdatavals, $$array[$i]); + $printed_one = 1; + } +} + +if ($queue_more_than > 0) { + my $percent = ($queue_more_than * 100)/$queue_total; + $cumulative_percent += $percent; + printf($format, + "Over ", + format_time($queue_times[$#queue_times]), + $queue_more_than, $percent, $cumulative_percent); +} +push(@chartdatanames, "Over " . format_time($queue_times[$#queue_times])); +push(@chartdatavals, $queue_more_than); + +#printf("Unknown %6d\n", $queue_unknown) if $queue_unknown > 0; +if ($html) { + print "
TimeMessagesPercentageCumulative Percentage
%s %s%d%5.1f%%%5.1f%%
\n"; + print "
\n"; + + if ($HAVE_GD_Graph_pie && $charts) { + my @data = ( + \@chartdatanames, + \@chartdatavals + ); + my $graph = GD::Graph::pie->new(200, 200); + my $pngname; + my $title; + if ($string =~ /all/) { $pngname = "queue_all.png"; $title = "Queue (all)"; } + if ($string =~ /remote/) { $pngname = "queue_rem.png"; $title = "Queue (remote)"; } + $graph->set( + title => $title, + ); + my $gd = $graph->plot(\@data) or warn($graph->error); + if ($gd) { + open(IMG, ">$chartdir/$pngname") or die $!; + binmode IMG; + print IMG $gd->png; + close IMG; + print ""; + } + } + print "
\n"; +} +print "\n"; +} + + + +=head2 print_histogram(); + + print_histogram('Deliverieds|Messages received',@interval_count); + +Print a histogram of the messages delivered/received per time slot +(hour by default). + +=cut + +sub print_histogram { +my($text) = shift; +my(@interval_count) = @_; +my(@chartdatanames); +my(@chartdatavals); +my($maxd) = 0; +for ($i = 0; $i < $hist_number; $i++) + { $maxd = $interval_count[$i] if $interval_count[$i] > $maxd; } + +my $scale = int(($maxd + 25)/50); +$scale = 1 if $scale == 0; + +my($type); +if ($text eq "Deliveries") + { + $type = ($scale == 1)? "delivery" : "deliveries"; + } +else + { + $type = ($scale == 1)? "message" : "messages"; + } + +my($title) = sprintf("$text per %s (each dot is $scale $type)", + ($hist_interval == 60)? "hour" : + ($hist_interval == 1)? "minute" : "$hist_interval minutes"); + +if ($html) { + print "

$title

\n"; + print "\n"; + print "
\n";
+}
+else {
+  printf("%s\n%s\n\n", $title, "-" x length($title));
+}
+
+my $hour = 0;
+my $minutes = 0;
+for ($i = 0; $i < $hist_number; $i++)
+  {
+  my $c = $interval_count[$i];
+
+  # If the interval is an hour (the maximum) print the starting and
+  # ending hours as a label. Otherwise print the starting hour and
+  # minutes, which take up the same space.
+
+  my $temp;
+  if ($hist_opt == 1)
+    {
+    $temp = sprintf("%02d-%02d", $hour, $hour + 1);
+    print $temp;
+    push(@chartdatanames, $temp);
+    $hour++;
+    }
+  else
+    {
+    if ($minutes == 0)
+      { $temp = sprintf("%02d:%02d", $hour, $minutes) }
+    else
+      { $temp = sprintf("  :%02d", $minutes) }
+    print $temp;
+    push(@chartdatanames, $temp);
+    $minutes += $hist_interval;
+    if ($minutes >= 60)
+      {
+      $minutes = 0;
+      $hour++;
+      }
+    }
+  push(@chartdatavals, $c);
+  printf(" %6d %s\n", $c, "." x ($c/$scale));
+  }
+print "\n";
+if ($html)
+  {
+  print "
\n"; + print "
\n"; + if ($HAVE_GD_Graph_linespoints && $charts) { + # calculate the graph + my @data = ( + \@chartdatanames, + \@chartdatavals + ); + my $graph = GD::Graph::linespoints->new(300, 300); + $graph->set( + x_label => 'Time', + y_label => 'Amount', + title => $text, + x_labels_vertical => 1 + ); + my($pngname); + if ($text =~ /Deliveries/) { $pngname = "histogram_del.png"; } + if ($text =~ /Messages/) { $pngname = "histogram_mes.png"; } + my $gd = $graph->plot(\@data) or warn($graph->error); + if ($gd) { + open(IMG, ">$chartdir/$pngname") or die $!; + binmode IMG; + print IMG $gd->png; + close IMG; + print ""; + } + } + print "
\n"; +} +} + + + +=head2 print_league_table(); + + print_league_table($league_table_type,\%message_count,\%message_data,\%message_data_gigs); + +Given hashes of message count and message data, which are keyed by +the table type (eg by the sending host), print a league table +showing the top $topcount (defaults to 50). + +=cut + +sub print_league_table { +my($text,$m_count,$m_data,$m_data_gigs) = @_; +my($name) = ($topcount == 1)? "$text" : "$topcount ${text}s"; +my($temp) = "Top $name by message count"; +my(@chartdatanames) = (); +my(@chartdatavals) = (); +my $chartotherval = 0; + +my($format); +if ($html) { + print "

$temp

\n"; + print "\n"; + print "
\n"; + print "\n"; + print "\n"; + + # Align non-local addresses to the right (so all the .com's line up). + # Local addresses are aligned on the left as they are userids. + my $align = ($text !~ /local/i) ? 'right' : 'left'; + $format = "\n"; +} +else { + printf("%s\n%s\n\n", $temp, "-" x length($temp)); + $format = "%7d %10s %s\n"; +} + +my($key,$htmlkey); +foreach $key (top_n_sort($topcount,$m_count,$m_data_gigs,$m_data)) { + if ($html) { + $htmlkey = $key; + $htmlkey =~ s/>/\>\;/g; + $htmlkey =~ s/\n"; + print "
MessagesBytes\u$text
%d%s%s\n"; + if ($HAVE_GD_Graph_pie && $charts) + { + # calculate the graph + my @data = ( + \@chartdatanames, + \@chartdatavals + ); + my $graph = GD::Graph::pie->new(300, 300); + $graph->set( + x_label => 'Name', + y_label => 'Amount', + title => 'By count', + ); + my $gd = $graph->plot(\@data) or warn($graph->error); + if ($gd) { + my $temp = $text; + $temp =~ s/ /_/g; + open(IMG, ">$chartdir/${temp}_count.png") or die $!; + binmode IMG; + print IMG $gd->png; + close IMG; + print ""; + } + } + print "\n"; + print "
\n"; +} +print "\n"; + +$temp = "Top $name by volume"; +if ($html) { + print "

$temp

\n"; + print "\n"; + print ""; + $sender_txt_header .= " " x ($COLUMN_WIDTHS - length($_)) . $_ . 's'; + $sender_html_format .= ""; + $sender_txt_format .= " " x ($COLUMN_WIDTHS - 5) . "%6d"; + } + + my($format1,$format2); + if ($html) { + print << "EoText"; + +

Grand total summary

+
\n"; + print "\n"; + print "\n"; +} +else { + printf("%s\n%s\n\n", $temp, "-" x length($temp)); +} + +@chartdatanames = (); +@chartdatavals = (); +$chartotherval = 0; +foreach $key (top_n_sort($topcount,$m_data_gigs,$m_data,$m_count)) { + if ($html) { + $htmlkey = $key; + $htmlkey =~ s/>/\>\;/g; + $htmlkey =~ s/\n"; + print "
MessagesBytes\u$text\n"; + if ($HAVE_GD_Graph_pie && $charts) { + # calculate the graph + my @data = ( + \@chartdatanames, + \@chartdatavals + ); + my $graph = GD::Graph::pie->new(300, 300); + $graph->set( + x_label => 'Name', + y_label => 'Volume', + title => 'By Volume', + ); + my $gd = $graph->plot(\@data) or warn($graph->error); + if ($gd) { + my $temp = $text; + $temp =~ s/ /_/g; + open(IMG, ">$chartdir/${temp}_volume.png") or die $!; + binmode IMG; + print IMG $gd->png; + close IMG; + print ""; + } + } + print "\n"; + print "
\n"; +} + +print "\n"; +} + + +=head2 top_n_sort(); + + @sorted_keys = top_n_sort($n,$href1,$href2,$href3); + +Given a hash which has numerical values, return the sorted $n keys which +point to the top values. The second and third hashes are used as +tiebreakers. They all must have the same keys. + +The idea behind this routine is that when you only want to see the +top n members of a set, rather than sorting the entire set and then +plucking off the top n, sort through the stack as you go, discarding +any member which is lower than your current n'th highest member. + +This proves to be an order of magnitude faster for large hashes. +On 200,000 lines of mainlog it benchmarked 9 times faster. +On 700,000 lines of mainlog it benchmarked 13.8 times faster. + +We assume the values are > 0. + +=cut + +sub top_n_sort { + my($n,$href1,$href2,$href3) = @_; + + # PH's original sort was: + # + # foreach $key (sort + # { + # $$m_count{$b} <=> $$m_count{$a} || + # $$m_data_gigs{$b} <=> $$m_data_gigs{$a} || + # $$m_data{$b} <=> $$m_data{$a} || + # $a cmp $b + # } + # keys %{$m_count}) + # + + #We use a key of '_' to represent non-existant values, as null keys are valid. + #'_' is not a valid domain, edomain, host, or email. + my(@top_n_keys) = ('_') x $n; + my($minimum_value1,$minimum_value2,$minimum_value3) = (0,0,0); + my $top_n_key = ''; + my $n_minus_1 = $n - 1; + my $n_minus_2 = $n - 2; + + # Pick out the top $n keys. + my($key,$value1,$value2,$value3,$i,$comparison,$insert_position); + while (($key,$value1) = each %$href1) { + + #print STDERR "key $key ($value1,",$href2->{$key},",",$href3->{$key},") <=> ($minimum_value1,$minimum_value2,$minimum_value3)\n"; + + # Check to see that the new value is bigger than the lowest of the + # top n keys that we're keeping. + $comparison = $value1 <=> $minimum_value1 || + $href2->{$key} <=> $minimum_value2 || + $href3->{$key} <=> $minimum_value3 || + $top_n_key cmp $key; + next unless ($comparison == 1); + + # As we will be using these values a few times, extract them into scalars. + $value2 = $href2->{$key}; + $value3 = $href3->{$key}; + + # This key is bigger than the bottom n key, so the lowest position we + # will insert it into is $n minus 1 (the bottom of the list). + $insert_position = $n_minus_1; + + # Now go through the list, stopping when we find a key that we're + # bigger than, or we come to the penultimate position - we've + # already tested bigger than the last. + # + # Note: we go top down as the list starts off empty. + # Note: stepping through the list in this way benchmarks nearly + # three times faster than doing a sort() on the reduced list. + # I assume this is because the list is already in order, and + # we get a performance boost from not having to do hash lookups + # on the new key. + for ($i = 0; $i < $n_minus_1; $i++) { + $top_n_key = $top_n_keys[$i]; + if ( ($top_n_key eq '_') || + ( ($value1 <=> $href1->{$top_n_key} || + $value2 <=> $href2->{$top_n_key} || + $value3 <=> $href3->{$top_n_key} || + $top_n_key cmp $key) == 1 + ) + ) { + $insert_position = $i; + last; + } + } + + # Remove the last element, then insert the new one. + $#top_n_keys = $n_minus_2; + splice(@top_n_keys,$insert_position,0,$key); + + # Extract our new minimum values. + $top_n_key = $top_n_keys[$n_minus_1]; + if ($top_n_key ne '_') { + $minimum_value1 = $href1->{$top_n_key}; + $minimum_value2 = $href2->{$top_n_key}; + $minimum_value3 = $href3->{$top_n_key}; + } + } + + # Return the top n list, grepping out non-existant values, just in case + # we didn't have that many values. + return(grep(!/^_$/,@top_n_keys)); +} + + +=head2 html_header(); + + $header = html_header($title); + +Print our HTML header and start the block. + +=cut + +sub html_header { + my($title) = @_; + my $text = << "EoText"; + + + + +$title + + +

$title

+EoText + return $text; +} + + + +=head2 help(); + + help(); + +Display usage instructions and exit. + +=cut + +sub help { + print << "EoText"; + +eximstats Version $VERSION + +Usage: eximstats [Options] mainlog1 mainlog2 ... > report.txt + eximstats -html [Options] mainlog1 mainlog2 ... > report.html + eximstats -merge [Options] report.1.txt report.2.txt ... > weekly_rep.txt + eximstats -merge -html [Options] report.1.html ... > weekly_rep.html + +Parses exim mainlog files and generates a statistical analysis of +the messages processed. Valid options are: + +-h histogram divisions per hour. The default is 1, and + 0 suppresses histograms. Other valid values are: + 2, 3, 5, 10, 15, 20, 30 or 60. +-ne don't display error information +-nr don't display relaying information +-nr/pattern/ don't display relaying information that matches +-nt don't display transport information +-nt/pattern/ don't display transport information that matches +-nvr don't do volume rounding. Display in bytes, not KB/MB/GB. +-q list of times for queuing information + single 0 item suppresses +-t display top sources/destinations + default is 50, 0 suppresses top listing +-tnl omit local sources/destinations in top listing +-t_remote_users show top user sources/destinations from non-local domains + +-byhost show results by sending host (default unless bydomain or + byemail is specified) +-bydomain show results by sending domain. +-byemail show results by sender's email address +-byedomain show results by sender's email domain + +-pattern "Description" /pattern/ + Count lines matching specified patterns and show them in + the results. It can be specified multiple times. Eg: + -pattern 'Refused connections' '/refused connection/' + +-merge merge previously generated reports into a new report + +-html output the results in HTML +-charts Create charts (this requires the GD::Graph modules) +-chartdir Create the charts' png files in the directory +-chartrel Specify the relative directory for the "img src=" tags + from where to include the charts in the html file + -chartdir and -chartrel default to '.' + +-d Debug mode - dump the eval'ed parser onto STDERR. + +EoText + + exit 1; +} + + + +=head2 generate_parser(); + + $parser = generate_parser(); + +This subroutine generates the parsing routine which will be +used to parse the mainlog. We take the base operation, and remove bits not in use. +This improves performance depending on what bits you take out or add. + +I've tested using study(), but this does not improve performance. + +We store our parsing routing in a variable, and process it looking for #IFDEF (Expression) +or #IFNDEF (Expression) statements and corresponding #ENDIF (Expression) statements. If +the expression evaluates to true, then it is included/excluded accordingly. + +=cut + +sub generate_parser { + my $parser = ' + my($ip,$host,$email,$edomain,$domain,$thissize,$size,$old,$new); + my($tod,$m_hour,$m_min,$id,$flag); + while (<$fh>) { + next if length($_) < 38; + + # PH/FANF + # next unless /^(\\d{4}\\-\\d\\d-\\d\\d\\s(\\d\\d):(\\d\\d):\\d\\d)/; + next unless /^(\\d{4}\\-\\d\\d-\\d\\d\\s(\\d\\d):(\\d\\d):\\d\\d( [-+]\\d\\d\\d\\d)?)/o; + + ($tod,$m_hour,$m_min) = ($1,$2,$3); + + # PH + my($extra) = defined($4)? 6 : 0; + $id = substr($_, 20 + $extra, 16); + $flag = substr($_, 37 + $extra, 2); +'; + + # Watch for user specified patterns. + my $user_pattern_index = 0; + foreach (@user_patterns) { + $user_pattern_totals[$user_pattern_index] = 0; + $parser .= " \$user_pattern_totals[$user_pattern_index]++ if $_;\n"; + $user_pattern_index++; + } + + $parser .= ' + next unless ($flag =~ /<=|=>|->|==|\\*\\*|Co/); + + #Strip away the timestamp, ID and flag (which could be "Com" for completed) + #This speeds up the later pattern matches. + # $_ = substr($_, 40); + + $_ = substr($_, 40 + $extra); # PH + + # JN - Skip over certain transports as specified via the "-nt/.../" command + # line switch (where ... is a perl style regular expression). This is + # required so that transports that skew stats such as SpamAssassin can be + # ignored. + #IFDEF ($transport_pattern) + if (/\\sT=(\\S+)/) { + next if ($1 =~ /$transport_pattern/o) ; + } + #ENDIF ($transport_pattern) + + + $host = "local"; #Host is local unless otherwise specified. + $domain = "localdomain"; #Domain is localdomain unless otherwise specified. + + + # Do some pattern matches to get the host and IP address. + # We expect lines to be of the form "H=[IpAddr]" or "H=Host [IpAddr]" or + # "H=Host (UnverifiedHost) [IpAddr]" or "H=(UnverifiedHost) [IpAddr]". + # We do 2 separate matches to keep the matches simple and fast. + if (/\\sH=(\\S+)/) { + $host = $1; + + ($ip) = /\\sH=.*?(\\s\\[[^]]+\\])/; + # If there is only an IP address, it will be in $host and $ip will be + # unset. That is OK, because we only use $ip in conjunction with $host + # below. But make it empty to avoid warning messages. + $ip = "" if !defined $ip; + + #IFDEF ($do_sender{Domain}) + if ($host !~ /^\\[/ && $host =~ /^(\\(?)[^\\.]+\\.([^\\.]+\\..*)/) { + # Remove the host portion from the DNS name. We ensure that we end up with + # at least xxx.yyy. $host can be "(x.y.z)" or "x.y.z". + $domain = lc("$1.$2"); + $domain =~ s/^\\.//; #Remove preceding dot. + } + #ENDIF ($do_sender{Domain}) + + } + + #IFDEF ($do_sender{Email}) + $email = (/^(\S+)/) ? $1 : ""; + #ENDIF ($do_sender{Email}) + + #IFDEF ($do_sender{Edomain}) + $edomain = (/^\S*?\\@(\S+)/) ? lc($1) : ""; + #ENDIF ($do_sender{Edomain}) + + if ($tod lt $begin) { + $begin = $tod; + } + elsif ($tod gt $end) { + $end = $tod; + } + + + if ($flag eq "<=") { + $thissize = (/\\sS=(\\d+)( |$)/) ? $1 : 0; + $size{$id} = $thissize; + + #IFDEF ($show_relay) + if ($host ne "local") { + # Save incoming information in case it becomes interesting + # later, when delivery lines are read. + my($from) = /^(\\S+)/; + $from_host{$id} = "$host$ip"; + $from_address{$id} = $from; + } + #ENDIF ($show_relay) + + #IFDEF ($local_league_table || $include_remote_users) + if (/\sU=(\\S+)/) { + my $user = $1; + + #IFDEF ($local_league_table && $include_remote_users) + { #Store both local and remote users. + #ENDIF ($local_league_table && $include_remote_users) + + #IFDEF ($local_league_table && ! $include_remote_users) + if ($host eq "local") { #Store local users only. + #ENDIF ($local_league_table && ! $include_remote_users) + + #IFDEF ($include_remote_users && ! $local_league_table) + if ($host ne "local") { #Store remote users only. + #ENDIF ($include_remote_users && ! $local_league_table) + + $received_count_user{$user}++; + add_volume(\\$received_data_user{$user},\\$received_data_gigs_user{$user},$thissize); + } + } + #ENDIF ($local_league_table || $include_remote_users) + + #IFDEF ($do_sender{Host}) + $received_count{Host}{$host}++; + add_volume(\\$received_data{Host}{$host},\\$received_data_gigs{Host}{$host},$thissize); + #ENDIF ($do_sender{Host}) + + #IFDEF ($do_sender{Domain}) + if ($domain) { + $received_count{Domain}{$domain}++; + add_volume(\\$received_data{Domain}{$domain},\\$received_data_gigs{Domain}{$domain},$thissize); + } + #ENDIF ($do_sender{Domain}) + + #IFDEF ($do_sender{Email}) + $received_count{Email}{$email}++; + add_volume(\\$received_data{Email}{$email},\\$received_data_gigs{Email}{$email},$thissize); + #ENDIF ($do_sender{Email}) + + #IFDEF ($do_sender{Edomain}) + $received_count{Edomain}{$edomain}++; + add_volume(\\$received_data{Edomain}{$edomain},\\$received_data_gigs{Edomain}{$edomain},$thissize); + #ENDIF ($do_sender{Edomain}) + + $total_received_count++; + add_volume(\\$total_received_data,\\$total_received_data_gigs,$thissize); + + #IFDEF ($#queue_times >= 0) + $arrival_time{$id} = $tod; + #ENDIF ($#queue_times >= 0) + + #IFDEF ($hist_opt > 0) + $received_interval_count[($m_hour*60 + $m_min)/$hist_interval]++; + #ENDIF ($hist_opt > 0) + } + + elsif ($flag eq "=>") { + $size = $size{$id} || 0; + if ($host ne "local") { + $remote_delivered{$id} = 1; + + + #IFDEF ($show_relay) + # Determine relaying address if either only one address listed, + # or two the same. If they are different, it implies a forwarding + # or aliasing, which is not relaying. Note that for multi-aliased + # addresses, there may be a further address between the first + # and last. + + if (defined $from_host{$id}) { + if (/^(\\S+)(?:\\s+\\([^)]\\))?\\s+<([^>]+)>/) { + ($old,$new) = ($1,$2); + } + else { + $old = $new = ""; + } + + if ("\\L$new" eq "\\L$old") { + ($old) = /^(\\S+)/ if $old eq ""; + my $key = "H=\\L$from_host{$id}\\E A=\\L$from_address{$id}\\E => " . + "H=\\L$host\\E$ip A=\\L$old\\E"; + if (!defined $relay_pattern || $key !~ /$relay_pattern/o) { + $relayed{$key} = 0 if !defined $relayed{$key}; + $relayed{$key}++; + } + else { + $relayed_unshown++ + } + } + } + #ENDIF ($show_relay) + + } + + #IFDEF ($local_league_table || $include_remote_users) + #IFDEF ($local_league_table && $include_remote_users) + { #Store both local and remote users. + #ENDIF ($local_league_table && $include_remote_users) + + #IFDEF ($local_league_table && ! $include_remote_users) + if ($host eq "local") { #Store local users only. + #ENDIF ($local_league_table && ! $include_remote_users) + + #IFDEF ($include_remote_users && ! $local_league_table) + if ($host ne "local") { #Store remote users only. + #ENDIF ($include_remote_users && ! $local_league_table) + + if (my($user) = split((/\\s]*>)/; + $user = "$user $parent" if defined $parent; + } + $delivered_count_user{$user}++; + add_volume(\\$delivered_data_user{$user},\\$delivered_data_gigs_user{$user},$size); + } + } + #ENDIF ($local_league_table || $include_remote_users) + + #IFDEF ($do_sender{Host}) + $delivered_count{Host}{$host}++; + add_volume(\\$delivered_data{Host}{$host},\\$delivered_data_gigs{Host}{$host},$size); + #ENDIF ($do_sender{Host}) + #IFDEF ($do_sender{Domain}) + if ($domain) { + $delivered_count{Domain}{$domain}++; + add_volume(\\$delivered_data{Domain}{$domain},\\$delivered_data_gigs{Domain}{$domain},$size); + } + #ENDIF ($do_sender{Domain}) + #IFDEF ($do_sender{Email}) + $delivered_count{Email}{$email}++; + add_volume(\\$delivered_data{Email}{$email},\\$delivered_data_gigs{Email}{$email},$size); + #ENDIF ($do_sender{Email}) + #IFDEF ($do_sender{Edomain}) + $delivered_count{Edomain}{$edomain}++; + add_volume(\\$delivered_data{Edomain}{$edomain},\\$delivered_data_gigs{Edomain}{$edomain},$size); + #ENDIF ($do_sender{Edomain}) + + $total_delivered_count++; + add_volume(\\$total_delivered_data,\\$total_delivered_data_gigs,$size); + + #IFDEF ($show_transport) + my $transport = (/\\sT=(\\S+)/) ? $1 : ":blackhole:"; + $transported_count{$transport}++; + add_volume(\\$transported_data{$transport},\\$transported_data_gigs{$transport},$size); + #ENDIF ($show_transport) + + #IFDEF ($hist_opt > 0) + $delivered_interval_count[($m_hour*60 + $m_min)/$hist_interval]++; + #ENDIF ($hist_opt > 0) + + } + + elsif ($flag eq "==" && defined($size{$id}) && !defined($delayed{$id})) { + $delayed_count++; + $delayed{$id} = 1; + } + + elsif ($flag eq "**") { + $had_error{$id} = 1 if defined ($size{$id}); + + #IFDEF ($show_errors) + $errors_count{$_}++; + #ENDIF ($show_errors) + + } + + elsif ($flag eq "Co") { + #Completed? + #IFDEF ($#queue_times >= 0) + #Note: id_seconds() benchmarks as 42% slower than seconds() and computing + #the time accounts for a significant portion of the run time. + my($queued); + if (defined $arrival_time{$id}) { + $queued = seconds($tod) - seconds($arrival_time{$id}); + delete($arrival_time{$id}); + } + else { + $queued = seconds($tod) - id_seconds($id); + } + + for ($i = 0; $i <= $#queue_times; $i++) { + if ($queued < $queue_times[$i]) { + $queue_bin[$i]++; + $remote_queue_bin[$i]++ if $remote_delivered{$id}; + last; + } + } + $queue_more_than++ if $i > $#queue_times; + #ENDIF ($#queue_times >= 0) + + #IFDEF ($show_relay) + delete($from_host{$id}); + delete($from_address{$id}); + #ENDIF ($show_relay) + + } + }'; + + # We now do a 'C preprocessor style operation on our parser + # to remove bits not in use. + my(%defines_in_operation,$removing_lines,$processed_parser); + foreach (split (/\n/,$parser)) { + if ((/^\s*#\s*IFDEF\s*\((.*?)\)/i && ! eval $1) || + (/^\s*#\s*IFNDEF\s*\((.*?)\)/i && eval $1) ) { + $defines_in_operation{$1} = 1; + $removing_lines = 1; + } + + $processed_parser .= $_."\n" unless $removing_lines; + + if (/^\s*#\s*ENDIF\s*\((.*?)\)/i) { + delete $defines_in_operation{$1}; + unless (keys %defines_in_operation) { + $removing_lines = 0; + } + } + } + print STDERR "# START OF PARSER:\n$processed_parser\n# END OF PARSER\n\n" if $debug; + + return $processed_parser; +} + + + +=head2 parse(); + + parse($parser,\*FILEHANDLE); + +This subroutine accepts a parser and a filehandle from main and parses each +line. We store the results into global variables. + +=cut + +sub parse { + my($parser,$fh) = @_; + + if ($merge_reports) { + parse_old_eximstat_reports($fh); + } + else { + eval $parser; + die ($@) if $@; + } + +} + + + +=head2 print_header(); + + print_header(); + +Print our headers and contents. + +=cut + +sub print_header { + + my $title = "Exim statistics from $begin to $end"; + + if ($html) { + print html_header($title); + print "\n
\n"; + + } + else { + print "\n$title\n"; + } +} + + +=head2 print_grandtotals(); + + print_grandtotals(); + +Print the grand totals. + +=cut + +sub print_grandtotals { + + # Get the sender by headings and results. This is complicated as we can have + # different numbers of columns. + my($sender_txt_header,$sender_html_header,$sender_txt_format,$sender_html_format); + my(@received_totals,@delivered_totals); + foreach ('Host','Domain','Email','Edomain') { + next unless $do_sender{$_}; + if ($merge_reports) { + push(@received_totals, get_report_total($report_totals{Received},"${_}s")); + push(@delivered_totals,get_report_total($report_totals{Delivered},"${_}s")); + } + else { + push(@received_totals,scalar(keys %{$received_data{$_}})); + push(@delivered_totals,scalar(keys %{$delivered_data{$_}})); + } + $sender_html_header .= "
${_}s%d
+$sender_html_header +EoText + + $format1 = "$sender_html_format"; + $format2 = ""; + } + else { + my $sender_spaces = " " x length($sender_txt_header); + print << "EoText"; + +Grand total summary +------------------- + $sender_spaces At least one address + TOTAL Volume Messages $sender_txt_header Delayed Failed +EoText + $format1 = " %-16s %9s %6d $sender_txt_format"; + $format2 = " %6d %4.1f%% %6d %4.1f%%", + } + + my($volume,$failed_count); + if ($merge_reports) { + $volume = volume_rounded($report_totals{Received}{Volume}, $report_totals{Received}{'Volume-gigs'}); + $total_received_count = get_report_total($report_totals{Received},'Messages'); + $failed_count = get_report_total($report_totals{Received},'Failed'); + $delayed_count = get_report_total($report_totals{Received},'Delayed'); + } + else { + $volume = volume_rounded($total_received_data, $total_received_data_gigs); + $failed_count = keys %had_error; + } + + { + no integer; + printf("$format1$format2\n",'Received',$volume,$total_received_count, + @received_totals,$delayed_count, + ($total_received_count) ? ($delayed_count*100/$total_received_count) : 0, + $failed_count, + ($total_received_count) ? ($failed_count*100/$total_received_count) : 0); + } + + if ($merge_reports) { + $volume = volume_rounded($report_totals{Delivered}{Volume}, $report_totals{Delivered}{'Volume-gigs'}); + $total_delivered_count = get_report_total($report_totals{Delivered},'Messages'); + } + else { + $volume = volume_rounded($total_delivered_data, $total_delivered_data_gigs); + } + printf("$format1\n\n",'Delivered',$volume,$total_delivered_count,@delivered_totals); + print "
TOTALVolumeMessagesAt least one addr
Delayed
At least one addr
Failed
%s%s%d%d%4.1f%%%d%4.1f%%
\n" if $html; +} + + +=head2 print_user_patterns() + + print_user_patterns(); + +Print the counts of user specified patterns. + +=cut + +sub print_user_patterns { + my($format1); + + if ($html) { + print "

User Specified Patterns

\n"; + print "\n"; + print " + + my $reached_table = 0; + my($count,$sender); + while (<$fh>) { + unless ($reached_table) { + last if (/No relayed messages/); + $reached_table = 1 if (/^\s*\d/ || />\d+(\d+)<.td>
\n"; + print "\n"; + print "\n"; + $format1 = ""; + } + else { + print "User Specified Patterns\n"; + print "-----------------------"; + print "\n Total\n"; + $format1 = " %-18s %6d"; + } + + my($key); + if ($merge_reports) { + # We are getting our data from previous reports. + foreach $key (@user_descriptions) { + my $count = get_report_total($report_totals{patterns}{$key},'Total'); + printf("$format1\n",$key,$count); + } + } + else { + # We are getting our data from mainlog files. + my $user_pattern_index = 0; + foreach $key (@user_descriptions) { + printf("$format1\n",$key,$user_pattern_totals[$user_pattern_index]); + $user_pattern_index++; + } + } + if ($html) { + print "
 Total
%s%d
\n"; + } + print "\n"; +} + + +=head2 print_transport(); + + print_transport(); + +Print totals by transport. + +=cut + +sub print_transport { + my($format1); + my(@chartdatanames); + my(@chartdatavals_count); + my(@chartdatavals_vol); + no integer; #Lose this for charting the data. + + if ($html) { + print "

Deliveries by Transport

\n"; + print "\n"; + print "
\n"; + print "\n"; + print "\n"; + $format1 = ""; + } + else { + print "Deliveries by transport\n"; + print "-----------------------"; + print "\n Volume Messages\n"; + $format1 = " %-18s %6s %6d"; + } + + my($key); + if ($merge_reports) { + # We are getting our data from previous reports. + foreach $key (sort keys %{$report_totals{transport}}) { + my $count = get_report_total($report_totals{transport}{$key},'Messages'); + printf("$format1\n",$key, + volume_rounded($report_totals{transport}{$key}{Volume},$report_totals{transport}{$key}{'Volume-gigs'}), + $count); + push(@chartdatanames, $key); + push(@chartdatavals_count, $count); + push(@chartdatavals_vol, $report_totals{transport}{$key}{'Volume-gigs'}*$gig + $report_totals{transport}{$key}{Volume} ); + } + } + else { + # We are getting our data from mainlog files. + foreach $key (sort keys %transported_data) { + printf("$format1\n",$key, + volume_rounded($transported_data{$key},$transported_data_gigs{$key}), + $transported_count{$key}); + push(@chartdatanames, $key); + push(@chartdatavals_count, $transported_count{$key}); + push(@chartdatavals_vol, $transported_data_gigs{$key}*$gig + $transported_data{$key}); + } + } + if ($html) { + print "
 VolumeMessages
%s%s%d
\n"; + print "
\n"; + if ($HAVE_GD_Graph_pie && $charts) + { + # calculate the graph + my @data = ( + \@chartdatanames, + \@chartdatavals_count + ); + my $graph = GD::Graph::pie->new(200, 200); + $graph->set( + x_label => 'Transport', + y_label => 'Messages', + title => 'By count', + ); + my $gd = $graph->plot(\@data) or warn($graph->error); + if ($gd) { + open(IMG, ">$chartdir/transports_count.png") or die $!; + binmode IMG; + print IMG $gd->png; + close IMG; + print ""; + } + } + print "\n"; + + if ($HAVE_GD_Graph_pie && $charts) { + my @data = ( + \@chartdatanames, + \@chartdatavals_vol + ); + my $graph = GD::Graph::pie->new(200, 200); + $graph->set( + title => 'By volume', + ); + my $gd = $graph->plot(\@data) or warn($graph->error); + if ($gd) { + open(IMG, ">$chartdir/transports_vol.png") or die $!; + binmode IMG; + print IMG $gd->png; + close IMG; + print ""; + } + } + print "
\n"; + } + print "\n"; +} + + + +=head2 print_relay(); + + print_relay(); + +Print our totals by relay. + +=cut + +sub print_relay { + my $temp = "Relayed messages"; + print "

$temp

\n" if $html; + if (scalar(keys %relayed) > 0 || $relayed_unshown > 0) { + my $shown = 0; + my $spacing = ""; + my($format); + + if ($html) { + print "\n"; + print "\n"; + $format = "\n"; + } + else { + printf("%s\n%s\n\n", $temp, "-" x length($temp)); + $format = "%7d %s\n => %s\n"; + } + + my($key); + foreach $key (sort keys %relayed) { + my $count = $relayed{$key}; + $shown += $count; + $key =~ s/[HA]=//g; + my($one,$two) = split(/=> /, $key); + printf($format, $count, $one, $two); + $spacing = "\n"; + } + print "
CountFromTo
%d%s%s
\n

\n" if $html; + print "${spacing}Total: $shown (plus $relayed_unshown unshown)\n"; + } + else { + print "No relayed messages\n"; + print "-------------------\n" unless $html; + } + print "\n"; +} + + + +=head2 print_errors(); + + print_errors(); + +Print our errors. In HTML, we display them as a list rather than a table - +Netscape doesn't like large tables! + +=cut + +sub print_errors { + my $total_errors = 0; + + if (scalar(keys %errors_count) != 0) { + my $temp = "List of errors"; + my($format); + if ($html) { + print "


$temp

\n"; + print "
  • Count - Error\n"; + $format = "
  • %d - %s\n"; + } + else { + printf("%s\n%s\n\n", $temp, "-" x length($temp)); + } + + my($key); + foreach $key (sort keys %errors_count) { + my $text = $key; + chomp($text); + $text =~ s/\s\s+/ /g; #Convert multiple spaces to a single space. + $total_errors += $errors_count{$key}; + if ($html) { + + #Translate HTML tag characters. Sergey Sholokh. + $text =~ s/\/\>\;/g; + + printf($format,$errors_count{$key},$text); + } + else { + printf("%5d ", $errors_count{$key}); + while (length($text) > 65) { + my($first,$rest) = $text =~ /(.{50}\S*)\s+(.+)/; + last if !$first; + printf("%s\n ", $first); + $text = $rest; + } + printf("%s\n\n", $text); + } + } + print "
\n

\n" if $html; + + $temp = "Errors encountered: $total_errors"; + print $temp,"\n"; + print "-" x length($temp),"\n" unless $html; + } + +} + + +=head2 parse_old_eximstat_reports(); + + parse_old_eximstat_reports($fh); + +Parse old eximstat output so we can merge daily stats to weekly stats and weekly to monthly etc. + +To test that the merging still works after changes, do something like the following. +All the diffs should produce no output. + + options='-bydomain -byemail -byhost -byedomain' + options="$options -pattern 'Completed Messages' /Completed/" + options="$options -pattern 'Received Messages' /<=/" + + ./eximstats $options mainlog > mainlog.txt + ./eximstats $options -merge mainlog.txt > mainlog.2.txt + diff mainlog.txt mainlog.2.txt + + ./eximstats $options -html mainlog > mainlog.html + ./eximstats $options -merge -html mainlog.txt > mainlog.2.html + diff mainlog.html mainlog.2.html + + ./eximstats $options -merge mainlog.html > mainlog.3.txt + diff mainlog.txt mainlog.3.txt + + ./eximstats $options -merge -html mainlog.html > mainlog.3.html + diff mainlog.html mainlog.3.html + + ./eximstats $options -nvr mainlog > mainlog.nvr.txt + ./eximstats $options -merge mainlog.nvr.txt > mainlog.4.txt + diff mainlog.txt mainlog.4.txt + + # double_mainlog.txt should have twice the values that mainlog.txt has. + ./eximstats $options mainlog mainlog > double_mainlog.txt + +=cut + +sub parse_old_eximstat_reports { + my($fh) = @_; + + my(%league_table_value_entered, %league_table_value_was_zero, %table_order); + + while (<$fh>) { + if (/Exim statistics from ([\d\-]+ [\d:]+(\s+[\+\-]\d+)?) to ([\d\-]+ [\d:]+(\s+[\+\-]\d+)?)/) { + $begin = $1 if ($1 lt $begin); + $end = $3 if ($3 gt $end); + } + elsif (/Grand total summary/) { + # Fill in $report_totals{Received|Delivered}{Volume|Messages|Hosts|Domains|...|Delayed|DelayedPercent|Failed|FailedPercent} + my(@fields); + while (<$fh>) { + $_ = html2txt($_); #Convert general HTML markup to text. + s/At least one addr//g; #Another part of the HTML output we don't want. + +# TOTAL Volume Messages Hosts Domains Delayed Failed +# Received 26MB 237 177 23 8 3.4% 28 11.8% +# Delivered 13MB 233 99 88 + if (/TOTAL\s+(.*?)\s*$/) { + @fields = split(/\s+/,$1); + #Delayed and Failed have two columns each, so add the extra field names in. + splice(@fields,-1,1,'DelayedPercent','Failed','FailedPercent'); + } + elsif (/(Received|Delivered)\s+(.*?)\s*$/) { + print STDERR "Parsing $_" if $debug; + add_to_totals($report_totals{$1},\@fields,$2); + } + last if (/Delivered/); #Last line of this section. + } + } + + elsif (/User Specified Patterns/i) { +#User Specified Patterns +#----------------------- +# Total +# Description 85 + + while (<$fh>) { last if (/Total/); } #Wait until we get the table headers. + while (<$fh>) { + print STDERR "Parsing $_" if $debug; + $_ = html2txt($_); #Convert general HTML markup to text. + if (/^\s*(.*?)\s+(\d+)\s*$/) { + $report_totals{patterns}{$1} = {} unless (defined $report_totals{patterns}{$1}); + add_to_totals($report_totals{patterns}{$1},['Total'],$2); + } + last if (/^\s*$/); #Finished if we have a blank line. + } + } + + elsif (/Deliveries by transport/i) { +#Deliveries by transport +#----------------------- +# Volume Messages +# :blackhole: 70KB 51 +# address_pipe 655KB 1 +# smtp 11MB 151 + + while (<$fh>) { last if (/Volume/); } #Wait until we get the table headers. + while (<$fh>) { + print STDERR "Parsing $_" if $debug; + $_ = html2txt($_); #Convert general HTML markup to text. + if (/(\S+)\s+(\d+\S*\s+\d+)/) { + $report_totals{transport}{$1} = {} unless (defined $report_totals{transport}{$1}); + add_to_totals($report_totals{transport}{$1},['Volume','Messages'],$2); + } + last if (/^\s*$/); #Finished if we have a blank line. + } + } + elsif (/(Messages received|Deliveries) per/) { +# Messages received per hour (each dot is 2 messages) +#--------------------------------------------------- +# +#00-01 106 ..................................................... +#01-02 103 ................................................... + + # Set a pointer to the interval array so we can use the same code + # block for both messages received and delivered. + my $interval_aref = ($1 eq 'Deliveries') ? \@delivered_interval_count : \@received_interval_count; + my $reached_table = 0; + while (<$fh>) { + $reached_table = 1 if (/^00/); + next unless $reached_table; + print STDERR "Parsing $_" if $debug; + if (/^(\d+):(\d+)\s+(\d+)/) { #hh:mm start time format ? + $$interval_aref[($1*60 + $2)/$hist_interval] += $3; + } + elsif (/^(\d+)-(\d+)\s+(\d+)/) { #hh-hh start-end time format ? + $$interval_aref[($1*60)/$hist_interval] += $3; + } + else { #Finished the table ? + last; + } + } + } + + elsif (/Time spent on the queue: (all messages|messages with at least one remote delivery)/) { +#Time spent on the queue: all messages +#------------------------------------- +# +#Under 1m 217 91.9% 91.9% +# 5m 2 0.8% 92.8% +# 3h 8 3.4% 96.2% +# 6h 7 3.0% 99.2% +# 12h 2 0.8% 100.0% + + # Set a pointer to the queue bin so we can use the same code + # block for both all messages and remote deliveries. + my $bin_aref = ($1 eq 'all messages') ? \@queue_bin : \@remote_queue_bin; + my $reached_table = 0; + while (<$fh>) { + $_ = html2txt($_); #Convert general HTML markup to text. + $reached_table = 1 if (/^\s*Under/); + next unless $reached_table; + my $previous_seconds_on_queue = 0; + if (/^\s*(Under|Over|)\s+(\d+[smhdw])\s+(\d+)/) { + print STDERR "Parsing $_" if $debug; + my($modifier,$formated_time,$count) = ($1,$2,$3); + my $seconds = unformat_time($formated_time); + my $time_on_queue = ($seconds + $previous_seconds_on_queue) / 2; + $previous_seconds_on_queue = $seconds; + $time_on_queue = $seconds * 2 if ($modifier eq 'Over'); + my($i); + for ($i = 0; $i <= $#queue_times; $i++) { + if ($time_on_queue < $queue_times[$i]) { + $$bin_aref[$i] += $count; + last; + } + } + # There's only one counter for messages going over the queue + # times so make sure we only count it once. + $queue_more_than += $count if (($bin_aref == \@queue_bin) && ($i > $#queue_times)); + } + else { + last; #Finished the table ? + } + } + } + + elsif (/Relayed messages/) { +#Relayed messages +#---------------- +# +# 1 addr.domain.com [1.2.3.4] a.user@domain.com +# => addr2.domain2.com [5.6.7.8] a2.user2@domain2.com +# +#

1addr.domain.com [1.2.3.4] a.user@domain.com addr2.domain2.com [5.6.7.8] a2.user2@domain2.com(.*?) ?<.td>(.*?)\s+(.*?)\s*$/) { + update_relayed($count,$sender,$1); + } + else { + last; #Finished the table ? + } + } + } + + elsif (/Top (.*?) by (message count|volume)/) { +#Top 50 sending hosts by message count +#------------------------------------- +# +# 48 1468KB local + my($category,$by_count_or_volume) = ($1,$2); + + #As we show 2 views of each table (by count and by volume), + #most (but not all) entries will appear in both tables. + #Set up a hash to record which entries we have already seen + #and one to record which ones we are seeing for the first time. + if ($by_count_or_volume =~ /count/) { + undef %league_table_value_entered; + undef %league_table_value_was_zero; + undef %table_order; + } + + #As this section processes multiple different table categories, + #set up pointers to the hashes to be updated. + my($count_href,$data_href,$data_gigs_href); + if ($category =~ /local sender/) { + $count_href = \%received_count_user; + $data_href = \%received_data_user; + $data_gigs_href = \%received_data_gigs_user; + } + elsif ($category =~ /sending (\S+?)s?\b/) { + #Top 50 sending (host|domain|email|edomain)s + #Top sending (host|domain|email|edomain) + $count_href = \%{$received_count{"\u$1"}}; + $data_href = \%{$received_data{"\u$1"}}; + $data_gigs_href = \%{$received_data_gigs{"\u$1"}}; + } + elsif ($category =~ /local destination/) { + $count_href = \%delivered_count_user; + $data_href = \%delivered_data_user; + $data_gigs_href = \%delivered_data_gigs_user; + } + elsif ($category =~ /(\S+) destination/) { + #Top 50 (host|domain|email|edomain) destinations + #Top (host|domain|email|edomain) destination + $count_href = \%{$delivered_count{"\u$1"}}; + $data_href = \%{$delivered_data{"\u$1"}}; + $data_gigs_href = \%{$delivered_data_gigs{"\u$1"}}; + } + + my $reached_table = 0; + while (<$fh>) { + $_ = html2txt($_); #Convert general HTML markup to text. + $reached_table = 1 if (/^\s*\d/); + next unless $reached_table; + if (/^\s*(\d+)\s+(\S+)\s*(.*?)\s*$/) { + my($count,$rounded_volume,$entry) = ($1,$2,$3); + #Note: $entry fields can be both null and can contain spaces. + + #Add the entry into the %table_order hash if it has a rounded volume (KB/MB/GB). + push(@{$table_order{$rounded_volume}{$by_count_or_volume}},$entry) if ($rounded_volume =~ /\D/); + + unless ($league_table_value_entered{$entry}) { + $league_table_value_entered{$entry} = 1; + unless ($$count_href{$entry}) { + $$count_href{$entry} = 0; + $$data_href{$entry} = 0; + $$data_gigs_href{$entry} = 0; + $league_table_value_was_zero{$entry} = 1; + } + + $$count_href{$entry} += $count; + #Add the rounded value to the data and data_gigs hashes. + un_round($rounded_volume,\$$data_href{$entry},\$$data_gigs_href{$entry}); + print STDERR "$category by $by_count_or_volume: added $count,$rounded_volume to $entry\n" if $debug; + } + } + else { #Finished the table ? + if ($by_count_or_volume =~ /volume/) { + #Add a few bytes to appropriate entries to preserve the order. + + my($rounded_volume); + foreach $rounded_volume (keys %table_order) { + #For each rounded volume, we want to create a list which has things + #ordered from the volume table at the front, and additional things + #from the count table ordered at the back. + @{$table_order{$rounded_volume}{volume}} = () unless defined $table_order{$rounded_volume}{volume}; + @{$table_order{$rounded_volume}{'message count'}} = () unless defined $table_order{$rounded_volume}{'message count'}; + my(@order,%mark); + map {$mark{$_} = 1} @{$table_order{$rounded_volume}{volume}}; + @order = @{$table_order{$rounded_volume}{volume}}; + map {push(@order,$_)} grep(!$mark{$_},@{$table_order{$rounded_volume}{'message count'}}); + + my $bonus_bytes = $#order; + $bonus_bytes = 511 if ($bonus_bytes > 511); #Don't go over the half-K boundary! + while (@order and ($bonus_bytes > 0)) { + my $entry = shift(@order); + if ($league_table_value_was_zero{$entry}) { + $$data_href{$entry} += $bonus_bytes; + print STDERR "$category by $by_count_or_volume: added $bonus_bytes bonus bytes to $entry\n" if $debug; + } + $bonus_bytes--; + } + } + } + + last; + } + } + } + elsif (/List of errors/) { +#List of errors +#-------------- +# +# 1 07904931641@one2one.net R=external T=smtp: SMTP error +# from remote mailer after RCPT TO:<07904931641@one2one.net>: +# host mail.one2one.net [193.133.192.24]: 550 User unknown +# +#
  • 1 - ally.dufc@dunbar.org.uk R=external T=smtp: SMTP error from remote mailer after RCPT TO:: host mail.dunbar.org.uk [216.167.89.88]: 550 Unknown local part ally.dufc in + + + my $reached_table = 0; + my($count,$error,$blanks); + while (<$fh>) { + $reached_table = 1 if (/^( *|
  • )(\d+)/); + next unless $reached_table; + + s/^
  • (\d+) -/$1/; #Convert an HTML line to a text line. + $_ = html2txt($_); #Convert general HTML markup to text. + + if (/\t\s*(.*)/) { + $error .= ' ' . $1; #Join a multiline error. + } + elsif (/^\s*(\d+)\s+(.*)/) { + if ($error) { + #Finished with a previous multiline error so save it. + $errors_count{$error} = 0 unless $errors_count{$error}; + $errors_count{$error} += $count; + } + ($count,$error) = ($1,$2); + } + elsif (/Errors encountered/) { + if ($error) { + #Finished the section, so save our stored last error. + $errors_count{$error} = 0 unless $errors_count{$error}; + $errors_count{$error} += $count; + } + last; + } + } + } + + } +} + + + +=head2 update_relayed(); + + update_relayed($count,$sender,$recipient); + +Adds an entry into the %relayed hash. Currently only used when +merging reports. + +=cut + +sub update_relayed { + my($count,$sender,$recipient) = @_; + + #When generating the key, put in the 'H=' and 'A=' which can be used + #in searches. + my $key = "H=$sender => H=$recipient"; + $key =~ s/ ([^=\s]+\@\S+|<>)/ A=$1/g; + if (!defined $relay_pattern || $key !~ /$relay_pattern/o) { + $relayed{$key} = 0 if !defined $relayed{$key}; + $relayed{$key} += $count; + } + else { + $relayed_unshown += $count; + } +} + + +=head2 add_to_totals(); + + add_to_totals(\%totals,\@keys,$values); + +Given a line of space seperated values, add them into the provided hash using @keys +as the hash keys. + +If the value contains a '%', then the value is set rather than added. Otherwise, we +convert the value to bytes and gigs. The gigs get added to I-gigs. + +=cut + +sub add_to_totals { + my($totals_href,$keys_aref,$values) = @_; + my(@values) = split(/\s+/,$values); + my(@keys) = @$keys_aref; #Make a copy as we destroy the one we use. + my($value); + foreach $value (@values) { + my $key = shift(@keys) or next; + if ($value =~ /%/) { + $$totals_href{$key} = $value; + } + else { + $$totals_href{$key} = 0 unless ($$totals_href{$key}); + $$totals_href{"$key-gigs"} = 0 unless ($$totals_href{"$key-gigs"}); + un_round($value, \$$totals_href{$key}, \$$totals_href{"$key-gigs"}); + print STDERR "Added $value to $key - $$totals_href{$key} , " . $$totals_href{"$key-gigs"} . "GB.\n" if $debug; + } + } +} + +=head2 get_report_total(); + + $total = get_report_total(\%hash,$key); + +If %hash contains values split into Units and Gigs, we calculate and return + + $hash{$key} + 1024*1024*1024 * $hash{"${key}-gigs"} + +=cut + +sub get_report_total { + no integer; + my($hash_ref,$key) = @_; + if ($$hash_ref{"${key}-gigs"}) { + return $$hash_ref{$key} + $gig * $$hash_ref{"${key}-gigs"}; + } + return $$hash_ref{$key} || 0; +} + +=head2 html2txt(); + + $text_line = html2txt($html_line); + +Convert a line from html to text. Currently we just convert HTML tags to spaces +and convert >, <, and   tags back. + +=cut + +sub html2txt { + ($_) = @_; + + # Convert HTML tags to spacing. Note that the reports may contain and + # words, so explicitly specify the HTML tags we will remove + # (the ones used by this program). If someone is careless enough to have their + # Userid the same as an HTML tag, there's not much we can do about it. + s/<\/?(html|head|title|body|h\d|ul|li|a\s+|table|tr|td|th|pre|hr|p|br)\b.*?>/ /og; + + s/\<\;/\/og; #Convert '>' to '>'. + s/\ \;/ /og; #Convert ' ' to ' '. + return($_); +} + +=head2 get_next_arg(); + + $arg = get_next_arg(); + +Because eximstats arguments are often passed as variables, +we can't rely on shell parsing to deal with quotes. This +subroutine returns $ARGV[1] and does a shift. If $ARGV[1] +starts with a quote (' or "), and doesn't end in one, then +we append the next argument to it and shift again. We repeat +until we've got all of the argument. + +This isn't perfect as all white space gets reduced to one space, +but it's as good as we can get! If it's esential that spacing +be preserved precisely, then you get that by not using shell +variables. + +=cut + +sub get_next_arg { + my $arg = ''; + my $matched_pattern = 0; + while ($ARGV[1]) { + $arg .= ' ' if $arg; + $arg .= $ARGV[1]; shift(@ARGV); + if ($arg !~ /^['"]/) { + $matched_pattern = 1; + last; + } + if ($arg =~ s/^(['"])(.*)\1$/$2/) { + $matched_pattern = 1; + last; + } + } + die "Mismatched argument quotes - <$arg>.\n" unless $matched_pattern; + return $arg; +} + + + +################################################## +# Main Program # +################################################## + + +$last_timestamp = ''; +$last_date = ''; +$show_errors = 1; +$show_relay = 1; +$show_transport = 1; +$topcount = 50; +$local_league_table = 1; +$include_remote_users = 0; +$hist_opt = 1; +$volume_rounding = 1; +$localtime_offset = calculate_localtime_offset(); # PH/FANF + +$charts = 0; +$charts_option_specified = 0; +$chartrel = "."; +$chartdir = "."; + +@queue_times = (60, 5*60, 15*60, 30*60, 60*60, 3*60*60, 6*60*60, + 12*60*60, 24*60*60); + +$last_offset = ''; +$offset_seconds = 0; + +# Decode options + +while (@ARGV > 0 && substr($ARGV[0], 0, 1) eq '-') + { + if ($ARGV[0] =~ /^\-h(\d+)$/) { $hist_opt = $1 } + elsif ($ARGV[0] =~ /^\-ne$/) { $show_errors = 0 } + elsif ($ARGV[0] =~ /^\-nr(.?)(.*)\1$/) + { + if ($1 eq "") { $show_relay = 0 } else { $relay_pattern = $2 } + } + elsif ($ARGV[0] =~ /^\-q([,\d\+\-\*\/]+)$/) + { + @queue_times = split(/,/, $1); + my($q); + foreach $q (@queue_times) { $q = eval($q) + 0 } + @queue_times = sort { $a <=> $b } @queue_times; + @queue_times = () if ($#queue_times == 0 && $queue_times[0] == 0); + } + elsif ($ARGV[0] =~ /^-nt$/) { $show_transport = 0 } + elsif ($ARGV[0] =~ /^\-nt(.?)(.*)\1$/) + { + if ($1 eq "") { $show_transport = 0 } else { $transport_pattern = $2 } + } + elsif ($ARGV[0] =~ /^-t(\d+)$/) { $topcount = $1 } + elsif ($ARGV[0] =~ /^-tnl$/) { $local_league_table = 0 } + elsif ($ARGV[0] =~ /^-html$/) { $html = 1 } + elsif ($ARGV[0] =~ /^-merge$/) { $merge_reports = 1 } + elsif ($ARGV[0] =~ /^-charts$/) { + $charts = 1; + warn "WARNING: CPAN Module GD::Graph::pie not installed. Obtain from www.cpan.org\n" unless $HAVE_GD_Graph_pie; + warn "WARNING: CPAN Module GD::Graph::linespoints not installed. Obtain from www.cpan.org\n" unless $HAVE_GD_Graph_linespoints; + } + elsif ($ARGV[0] =~ /^-chartdir$/) { $chartdir = $ARGV[1]; shift; $charts_option_specified = 1; } + elsif ($ARGV[0] =~ /^-chartrel$/) { $chartrel = $ARGV[1]; shift; $charts_option_specified = 1; } + elsif ($ARGV[0] =~ /^-cache$/) { } #Not currently used. + elsif ($ARGV[0] =~ /^-byhost$/) { $do_sender{Host} = 1 } + elsif ($ARGV[0] =~ /^-bydomain$/) { $do_sender{Domain} = 1 } + elsif ($ARGV[0] =~ /^-byemail$/) { $do_sender{Email} = 1 } + elsif ($ARGV[0] =~ /^-byemaildomain$/) { $do_sender{Edomain} = 1 } + elsif ($ARGV[0] =~ /^-byedomain$/) { $do_sender{Edomain} = 1 } + elsif ($ARGV[0] =~ /^-nvr$/) { $volume_rounding = 0 } + elsif ($ARGV[0] =~ /^-d$/) { $debug = 1 } + elsif ($ARGV[0] =~ /^--?h(elp)?$/){ help() } + elsif ($ARGV[0] =~ /^-t_remote_users$/) { $include_remote_users = 1 } + elsif ($ARGV[0] =~ /^-pattern$/) + { + push(@user_descriptions,get_next_arg()); + push(@user_patterns,get_next_arg()); + } + elsif ($ARGV[0] =~ /^-utc$/) + { + # We don't need this value if the log is in UTC. + $localtime_offset = undef; + } + else + { + print STDERR "Eximstats: Unknown or malformed option $ARGV[0]\n"; + help(); + } + shift; + } + + # Check that all the charts options are specified. + warn "-charts option not specified. Use -help for help.\n" if ($charts_option_specified && ! $charts); + + # Default to display tables by sending Host. + $do_sender{Host} = 1 unless ($do_sender{Domain} || $do_sender{Email} || $do_sender{Edomain}); + + +for (my $i = 0; $i <= $#queue_times; $i++) { + $queue_bin[$i] = 0; + $remote_queue_bin[$i] = 0; +} + +# Compute the number of slots for the histogram + +if ($hist_opt > 0) + { + if ($hist_opt > 60 || 60 % $hist_opt != 0) + { + print "Eximstats: -h must specify a factor of 60\n"; + exit 1; + } + $hist_interval = 60/$hist_opt; #Interval in minutes. + $hist_number = (24*60)/$hist_interval; #Number of intervals per day. + @received_interval_count = (0) x $hist_number; + @delivered_interval_count = (0) x $hist_number; + } + +#$queue_unknown = 0; + +$total_received_data = 0; +$total_received_data_gigs = 0; +$total_received_count = 0; + +$total_delivered_data = 0; +$total_delivered_data_gigs = 0; +$total_delivered_count = 0; + +$queue_more_than = 0; +$delayed_count = 0; +$relayed_unshown = 0; +$begin = "9999-99-99 99:99:99"; +$end = "0000-00-00 00:00:00"; +my($section,$type); +foreach $section ('Received','Delivered') { + foreach $type ('Volume','Messages','Delayed','Failed','Hosts','Domains','Emails','Edomains') { + $report_totals{$section}{$type} = 0; + } +} + +# Generate our parser. +my $parser = generate_parser(); + + + +if (@ARGV) { + # Scan the input files and collect the data + foreach my $file (@ARGV) { + if ($file =~ /\.gz/) { + unless (open(FILE,"gunzip -c $file |")) { + print STDERR "Failed to gunzip -c $file: $!"; + next; + } + } + elsif ($file =~ /\.Z/) { + unless (open(FILE,"uncompress -c $file |")) { + print STDERR "Failed to uncompress -c $file: $!"; + next; + } + } + else { + unless (open(FILE,$file)) { + print STDERR "Failed to read $file: $!"; + next; + } + } + #Now parse the filehandle, updating the global variables. + parse($parser,\*FILE); + close FILE; + } +} +else { + #No files provided. Parse STDIN, updating the global variables. + parse($parser,\*STDIN); +} + + +if ($begin eq "9999-99-99 99:99:99") { + print "**** No valid log lines read\n"; + exit 1; +} + +# Output our results. +print_header(); +print_grandtotals(); + +# Print counts of user specified patterns if required. +print_user_patterns() if @user_patterns; + +# Print totals by transport if required. +print_transport() if $show_transport; + +# Print the deliveries per interval as a histogram, unless configured not to. +# First find the maximum in one interval and scale accordingly. +if ($hist_opt > 0) { + print_histogram("Messages received", @received_interval_count); + print_histogram("Deliveries", @delivered_interval_count); +} + +# Print times on queue if required. +if ($#queue_times >= 0) { + print_queue_times("all messages", \@queue_bin,$queue_more_than); + print_queue_times("messages with at least one remote delivery",\@remote_queue_bin,$queue_more_than); +} + +# Print relay information if required. +print_relay() if $show_relay; + +# Print the league tables, if topcount isn't zero. +if ($topcount > 0) { + foreach ('Host','Domain','Email','Edomain') { + next unless $do_sender{$_}; + print_league_table("sending \l$_", $received_count{$_}, $received_data{$_},$received_data_gigs{$_}); + } + + print_league_table("local sender", \%received_count_user, + \%received_data_user,\%received_data_gigs_user) if ($local_league_table || $include_remote_users); + foreach ('Host','Domain','Email','Edomain') { + next unless $do_sender{$_}; + print_league_table("\l$_ destination", $delivered_count{$_}, $delivered_data{$_},$delivered_data_gigs{$_}); + } + print_league_table("local destination", \%delivered_count_user, + \%delivered_data_user,\%delivered_data_gigs_user) if ($local_league_table || $include_remote_users); +} + +# Print the error statistics if required. +print_errors() if $show_errors; + +if ($html) { + print "\n\n" +} + +# End of eximstats diff --git a/src/src/exinext.src b/src/src/exinext.src new file mode 100644 index 000000000..c942120b0 --- /dev/null +++ b/src/src/exinext.src @@ -0,0 +1,251 @@ +#! /bin/sh +# $Cambridge: exim/src/src/exinext.src,v 1.1 2004/10/07 10:39:01 ph10 Exp $ + +# Copyright (c) 1996-2004 University of Cambridge. +# See the file NOTICE for conditions of use and distribution. + +# Except when they appear in comments, the following placeholders in this +# source are replaced when it is turned into a runnable script: +# +# CONFIGURE_FILE_USE_NODE +# CONFIGURE_FILE +# BIN_DIRECTORY + +# PROCESSED_FLAG + +# A shell+perl script to fish out the next retry time for a given domain; +# it first calls exim to find out which hosts are set up for that domain and +# then fishes out the retry data for each one. + +# For testing the selection and formatting logic, and perhaps for use in +# special cases, the script can have an argument -C to specify +# the use of an alternate Exim configuration file. It may also have any number +# of -D options to set macros that are passed to exim. + +config= +eximmacdef= +exim_path= + +if expr $1 : '\-' >/dev/null ; then + while expr $1 : '\-' >/dev/null ; do + if [ "$1" = "-C" ]; then + config=$2 + shift + shift + elif expr $1 : '\-D' >/dev/null ; then + eximmacdef="$eximmacdef $1" + if expr $1 : '\-DEXIM_PATH=' >/dev/null ; then + exim_path=`expr $1 : '\-DEXIM_PATH=\(.*\)'` + fi + shift + else + break + fi + done +fi + +# We need to save the script's argument because in the absence of -C we need to +# use shell arguments for sorting out the configuration file name. + +argone=$1 + +# This is the normal case when no config file or macros are specified + +if [ "$config" = "" ]; then + # See if this installation is using the esoteric "USE_NODE" feature of Exim, + # in which it uses the host's name as a suffix for the configuration file name. + + if [ "CONFIGURE_FILE_USE_NODE" = "yes" ]; then + hostsuffix=.`uname -n` + fi + + # Now find the configuration file name. This has got complicated because + # CONFIGURE_FILE may now be a list of files. The one that is used is the first + # one that exists. Mimic the code in readconf.c by testing first for the + # suffixed file in each case. + + set `awk -F: '{ for (i = 1; i <= NF; i++) print $i }' <||" + exit 1 +fi + +perl - $exim_path "$eximmacdef" $argone $spool_directory $qualify_domain <<'End' + + # Name the arguments + + $exim = $ARGV[0]; + $eximmacdef = $ARGV[1]; + $subject = $ARGV[2]; + $spool = $ARGV[3]; + $qualify = $ARGV[4]; + + # If the subject doesn't contain an @ then construct an address + # for the domain, and ensure that in both cases the domain is + # lower cased. + + $address = ($subject =~ /^([^\@]*)\@([^\@]*)$/)? + "$1\@\L$2\E" : "User\@\L$subject\E"; + + # Run Exim to get a list of hosts for the given domain; for + # each one construct the appropriate retry key. + + open(LIST, "$exim -v -bt $address |") || + die "can't run exim to route $address"; + + while () + { + chop; + push(@list, $_) if s/\s*host (\S+)\s+\[(.+)\].*/$1:$2/; + print "$_\n" if /cannot be resolved/; + } + close(LIST); + + # If there were no hosts, assume that what was given was a local + # username, unless it contains an @, and construct a suitable retry + # key for that. Also, if it looks like a message id, search for that + # as well, so as to pick up message-specific retry data. + + if (scalar(@list) == 0) + { + push(@list, $subject) if $subject =~ /^\w{6}-\w{6}-\w{2}$/; + + if ($subject !~ /\@/ && $subject !~ /\./) + { + push(@list, "$subject\@$qualify"); + } + else + { + print "No remote hosts found for $subject\n"; + } + } + + # Always search for the full address, even if hosts are found, in case + # there is a routing delay caused by a temporary recipient error. + + push(@list, $subject); + + # Run exim_dumpdb to get out the retry data and pick off what we want + + open(DATA, "${exim}_dumpdb $spool retry |") || + die "can't run exim_dumpdb"; + + while () + { + for ($i = 0; $i <= $#list; $i++) + { + if (/$list[$i]/) + { + $printed = 1; + if (/^\s*T:[^:\s]*:/) + { + ($key,$error,$error2,$text) = /^\s*T:(\S+)\s+(\S+)\s+(\S+)\s*(.*)$/; + + # Parsing the keys is a nightmare because of IPv6. The design of the + # format for the keys is a complete shambles. All my fault (PH). But + # I don't want to change it just for this purpose. If they key + # contains more than 3 colons, we have an IPv6 address, because + # an IPv6 address must contain at least two colons. + + # Deal with IPv4 addresses (3 colons or fewer) + + if ($key !~ /:([^:]*?:){3}/) + { + ($host,$ip,$port,$msgid) = $key =~ + /^([^:]*):([^:]*)(?::([^:]*)(?::(\S*)|)|)/; + } + + # Deal with IPv6 addresses; sorting out the colons is a complete + # mess. We should be able to find the host name and IP address from + # further in the message. That seems the easiest escape plan here. We + # can use those to match the rest of the key. + + else + { + ($host,$ip) = $text =~ /host\s(\S+)\s\[([^]]+)\]/; + if (defined $host) + { + ($port,$msgid) = $key =~ + /^$host:$ip(?::([^:]*)(?::(\S*)|)|)/; + } + + # This will probably be wrong... + + else + { + ($host,$ip) = $key =~ /([^:]*):(.*)/; + } + } + + printf("Transport: %s [%s]", $host, $ip); + print ":$port" if defined $port; + print " $msgid" if defined $msgid; + print " error $error: $text\n"; + } + + else + { + ($type,$domain,$error,$error2,$text) = + /^\s*(\S):(\S+)\s+(\S+)\s+(\S+)\s*(.*)$/; + $type = ($type eq 'R')? "Route: " : + ($type eq 'T')? "Transport: " : ""; + print "$type$domain error $error: $text\n"; + } + $_ = ; + ($first,$last,$next,$expired) = + /^(\S+\s+\S+)\s+(\S+\s+\S+)\s+(\S+\s+\S+)\s*(\*?)/; + print " first failed: $first\n"; + print " last tried: $last\n"; + print " next try at: $next\n"; + print " past final cutoff time\n" if $expired eq "*"; + } + } + } + + close(DATA); + print "No retry data found for $subject\n" if !$printed; +End + diff --git a/src/src/exipick.src b/src/src/exipick.src new file mode 100644 index 000000000..ac3f06ad3 --- /dev/null +++ b/src/src/exipick.src @@ -0,0 +1,1096 @@ +#!PERL_COMMAND +# $Cambridge: exim/src/src/exipick.src,v 1.1 2004/10/07 10:39:01 ph10 Exp $ + +# This variable should be set by the building process to Exim's spool directory. +my $spool = 'SPOOL_DIRECTORY'; + +use strict; +use Getopt::Long; + +my($p_name) = $0 =~ m|/?([^/]+)$|; +my $p_version = "20040725.0"; +my $p_usage = "Usage: $p_name [--help|--version] (see --help for details)"; +my $p_cp = < + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +EOM +ext_usage(); # before we do anything else, check for --help + +Getopt::Long::Configure("bundling_override"); +GetOptions( + 'spool:s' => \$G::spool, # exim spool dir + 'bp' => \$G::mailq_bp, # List the queue (noop - default) + 'bpa' => \$G::mailq_bpa, # ... with generated address as well + 'bpc' => \$G::mailq_bpc, # ... but just show a count of messages + 'bpr' => \$G::mailq_bpr, # ... do not sort + 'bpra' => \$G::mailq_bpra, # ... with generated addresses, unsorted + 'bpru' => \$G::mailq_bpru, # ... only undelivered addresses, unsorted + 'bpu' => \$G::mailq_bpu, # ... only undelivered addresses + 'and' => \$G::and, # 'and' the criteria (default) + 'or' => \$G::or, # 'or' the criteria + 'f:s' => \$G::qgrep_f, # from regexp + 'r:s' => \$G::qgrep_r, # recipient regexp + #'s:s' => \$G::qgrep_s, # match against size field + 'y:s' => \$G::qgrep_y, # message younger than (secs) + 'o:s' => \$G::qgrep_o, # message older than (secs) + 'z' => \$G::qgrep_z, # frozen only + 'x' => \$G::qgrep_x, # non-frozen only + 'c' => \$G::qgrep_c, # display match count + 'l' => \$G::qgrep_l, # long format (default) + 'i' => \$G::qgrep_i, # message ids only + 'b' => \$G::qgrep_b, # brief format + 'flatq' => \$G::flatq, # brief format + 'show-vars:s' => \$G::show_vars, # display the contents of these vars + 'show-rules' => \$G::show_rules # display compiled match rules +) || exit(1); + +push(@ARGV, "\$sender_address =~ /$G::qgrep_f/") if ($G::qgrep_f); +push(@ARGV, "\$recipients =~ /$G::qgrep_r/") if ($G::qgrep_r); +push(@ARGV, "\$message_age < $G::qgrep_y") if ($G::qgrep_y); +push(@ARGV, "\$message_age > $G::qgrep_o") if ($G::qgrep_o); +push(@ARGV, "\$deliver_freeze") if ($G::qgrep_z); +push(@ARGV, "!\$deliver_freeze") if ($G::qgrep_x); +$G::mailq_bp = $G::mailq_bp; # shut up -w +$G::and = $G::and; # shut up -w +$spool = $G::spool if ($G::spool); +my $count_only = 1 if ($G::mailq_bpc || $G::qgrep_c); +my $unsorted = 1 if ($G::mailq_bpr || $G::mailq_bpra || $G::mailq_bpru); +my $msg = get_all_msgs($spool, $unsorted); +my $crit = process_criteria(\@ARGV); +my $e = Exim::SpoolFile->new(); +my $tcount = 0 if ($count_only); +my $mcount = 0 if ($count_only); +$e->set_spool($spool); +$e->set_undelivered_only(1) if ($G::mailq_bpru || $G::mailq_bpu); +$e->set_show_generated(1) if ($G::mailq_bpa || $G::mailq_bpra); +$e->output_long() if ($G::qgrep_l); +$e->output_idonly() if ($G::qgrep_i); +$e->output_brief() if ($G::qgrep_b); +$e->output_flatq() if ($G::flatq); +$e->set_show_vars($G::show_vars) if ($G::show_vars); + +MSG: +foreach my $m (@$msg) { + if (!$e->parse_message($m->{message})) { + warn "Couldn't parse $m->{message}: ".$e->error()."\n"; + next(MSG); + } + $tcount++; + my $match = 0; + CRITERIA: + foreach my $c (@$crit) { + my $var = $e->get_var($c->{var}); + my $ret = eval($c->{cmp}); + if ($@) { + print STDERR "Error in eval '$c->{cmp}': $@\n"; + next(MSG); + } elsif ($ret) { + $match = 1; + if ($G::or) { last(CRITERIA); } + else { next(CRITERIA); } + } else { # no match + if ($G::or) { next(CRITERIA); } + else { next(MSG); } + } + } + next(MSG) if (scalar(@$crit) > 0 && !$match); + + if ($count_only) { + $mcount++; + } else { + $e->print_message(\*STDOUT); + } +} + +if ($G::mailq_bpc) { + print "$tcount\n"; +} elsif ($G::qgrep_c) { + print "$mcount matches out of $tcount messages\n"; +} + +exit; + +sub process_criteria { + my $a = shift; + my @c = (); + my $e = 0; + + foreach (@$a) { + foreach my $t ('@') { s/$t/\\$t/g; } # '$' + if (/^(.*?)\s+(<=|>=|==|!=|<|>)\s+(.*)$/) { + #print STDERR "found as integer\n"; + my $v = $1; my $o = $2; my $n = $3; + if ($n =~ /^([\d\.]+)M$/) { $n = $1 * 1024 * 1024; } + elsif ($n =~ /^([\d\.]+)K$/) { $n = $1 * 1024; } + elsif ($n =~ /^([\d\.]+)B?$/) { $n = $1; } + elsif ($n =~ /^([\d\.]+)d$/) { $n = $1 * 60 * 60 * 24; } + elsif ($n =~ /^([\d\.]+)h$/) { $n = $1 * 60 * 60; } + elsif ($n =~ /^([\d\.]+)m$/) { $n = $1 * 60; } + elsif ($n =~ /^([\d\.]+)s?$/) { $n = $1; } + else { + print STDERR "Expression $_ did not parse: numeric comparison with ", + "non-number\n"; + $e = 1; + next; + } + push(@c, { var => lc($v), cmp => "(\$var $o $n) ? 1 : 0" }); + } elsif (/^(.*?)\s+(=~|!~)\s+(.*)$/) { + #print STDERR "found as string regexp\n"; + push(@c, { var => lc($1), cmp => "(\"\$var\" $2 $3) ? 1 : 0" }); + } elsif (/^(.*?)\s+=\s+(.*)$/) { + #print STDERR "found as bare string regexp\n"; + push(@c, { var => lc($1), cmp => "(\"\$var\" =~ /$2/) ? 1 : 0" }); + } elsif (/^(.*?)\s+(eq|ne)\s+(.*)$/) { + #print STDERR "found as string cmp\n"; + push(@c, { var => lc($1), cmp => "(\"\$var\" $2 \"$3\") ? 1 : 0" }); + } elsif (/^(!)?(\S+)$/) { + #print STDERR "found as boolean\n"; + push(@c, { var => lc($2), cmp => "($1\$var) ? 1 : 0" }); + } else { + print STDERR "Expression $_ did not parse\n"; + $e = 1; + } + } + + exit(1) if ($e); + + if ($G::show_rules) { foreach (@c) { print "$_->{var}\t$_->{cmp}\n"; } } + + return(\@c); +} + +sub get_all_msgs { + my $d = shift() . '/input'; + my $u = shift; + my @m = (); + + opendir(D, "$d") || die "Couldn't opendir $d: $!\n"; + foreach my $e (grep !/^\./, readdir(D)) { + if ($e =~ /^[a-zA-Z0-9]$/) { + opendir(DD, "$d/$e") || next; + foreach my $f (grep !/^\./, readdir(DD)) { + push(@m, { message => $1, path => "$e/$1" }) if ($f =~ /^(.{16})-H$/); + } + closedir(DD); + } elsif ($e =~ /^(.{16})-H$/) { + push(@m, { message => $1, path => $1 }); + } + } + closedir(D); + + return($u ? \@m : [ sort { $a->{message} cmp $b->{message} } @m ]); +} + +BEGIN { + +package Exim::SpoolFile; + +$Exim::SpoolFile::ACL_C_MAX = 10; +#$Exim::SpoolFile::ACL_M_MAX = 10; + +sub new { + my $class = shift; + my $self = {}; + bless($self, $class); + + $self->{_spool_dir} = ''; + $self->{_undelivered_only} = 0; + $self->{_show_generated} = 0; + $self->{_output_long} = 1; + $self->{_output_idonly} = 0; + $self->{_output_brief} = 0; + $self->{_output_flatq} = 0; + $self->{_show_vars} = {}; + + $self->_reset(); + return($self); +} + +sub output_long { + my $self = shift; + + $self->{_output_long} = 1; + $self->{_output_idonly} = 0; + $self->{_output_brief} = 0; + $self->{_output_flatq} = 0; +} + +sub output_idonly { + my $self = shift; + + $self->{_output_long} = 0; + $self->{_output_idonly} = 1; + $self->{_output_brief} = 0; + $self->{_output_flatq} = 0; +} + +sub output_brief { + my $self = shift; + + $self->{_output_long} = 0; + $self->{_output_idonly} = 0; + $self->{_output_brief} = 1; + $self->{_output_flatq} = 0; +} + +sub output_flatq { + my $self = shift; + + $self->{_output_long} = 0; + $self->{_output_idonly} = 0; + $self->{_output_brief} = 0; + $self->{_output_flatq} = 1; +} + +sub set_show_vars { + my $self = shift; + my $s = shift; + + foreach my $v (split(/\s*,\s*/, $s)) { + $self->{_show_vars}{$v}++; + } +} + +sub set_show_generated { + my $self = shift; + $self->{_show_generated} = shift; +} + +sub set_undelivered_only { + my $self = shift; + $self->{_undelivered_only} = shift; +} + +sub error { + my $self = shift; + return $self->{_error}; +} + +sub _error { + my $self = shift; + $self->{_error} = shift; + return(undef); +} + +sub _reset { + my $self = shift; + + $self->{_error} = ''; + $self->{_delivered} = 0; + $self->{_message} = ''; + $self->{_path} = ''; + $self->{_vars} = {}; + + $self->{_numrecips} = 0; + $self->{_udel_tree} = {}; + $self->{_del_tree} = {}; + $self->{_recips} = {}; + + return($self); +} + +sub parse_message { + my $self = shift; + + $self->_reset(); + $self->{_message} = shift || return(0); + return(0) if (!$self->{_spool_dir}); + if (!$self->_find_path()) { + # assume the message was delivered from under us and ignore + $self->{_delivered} = 1; + return(1); + } + $self->_parse_header() || return(0); + + return(1); +} + +sub _find_path { + my $self = shift; + + return(0) if (!$self->{_message}); + return(0) if (!$self->{_spool_dir}); + + foreach my $f ('', substr($self->{_message}, 5, 1).'/') { + if (-f $self->{_spool_dir} . "/input/$f" . $self->{_message} . '-H') { + $self->{_path} = $self->{_spool_dir} . "/input/$f"; + return(1); + } + } + return(0); +} + +sub set_spool { + my $self = shift; + $self->{_spool_dir} = shift; +} + +# accepts a variable with or without leading '$' or trailing ':' +sub get_var { + my $self = shift; + my $var = shift; + + $var =~ s/^\$//; + $var =~ s/:$//; + + $self->_parse_body() + if ($var eq 'message_body' && !$self->{_vars}{message_body}); + + return $self->{_vars}{$var}; +} + +sub _parse_body { + my $self = shift; + my $f = $self->{_path} . '/' . $self->{_message} . '-D'; + + open(I, "<$f") || return($self->_error("Couldn't open $f: $!")); + chomp($_ = ); + return(0) if ($self->{_message}.'-D' ne $_); + + $self->{_vars}{message_body} = join('', ); + close(I); + $self->{_vars}{message_body} =~ s/\n/ /g; + $self->{_vars}{message_body} =~ s/\000/ /g; + return(1); +} + +sub _parse_header { + my $self = shift; + my $f = $self->{_path} . '/' . $self->{_message} . '-H'; + + open(I, "<$f") || return($self->_error("Couldn't open $f: $!")); + chomp($_ = ); + return(0) if ($self->{_message}.'-H' ne $_); + $self->{_vars}{message_id} = $self->{_message}; + + # line 2 + chomp($_ = ); + return(0) if (!/^(\S+)\s(\d+)\s(\d+)$/); + $self->{_vars}{originator_login} = $1; + $self->{_vars}{originator_uid} = $2; + $self->{_vars}{originator_gid} = $3; + + # line 3 + chomp($_ = ); + return(0) if (!/^<(.*)>$/); + $self->{_vars}{sender_address} = $1; + $self->{_vars}{sender_address_domain} = $1; + $self->{_vars}{sender_address_local_part} = $1; + $self->{_vars}{sender_address_domain} =~ s/^.*\@//; + $self->{_vars}{sender_address_local_part} =~ s/^(.*)\@.*$/$1/; + + # line 4 + chomp($_ = ); + return(0) if (!/^(\d+)\s(\d+)$/); + $self->{_vars}{received_time} = $1; + $self->{_vars}{warning_count} = $2; + $self->{_vars}{message_age} = time() - $self->{_vars}{received_time}; + + while () { + chomp(); + if (/^(-\S+)\s*(.*$)/) { + my $tag = $1; + my $arg = $2; + if ($tag eq '-acl') { + my $t; + return(0) if ($arg !~ /^(\d+)\s(\d+)$/); + if ($1 < $Exim::SpoolFile::ACL_C_MAX) { + $t = "acl_c$1"; + } else { + $t = "acl_m" . ($1 - $Exim::SpoolFile::ACL_C_MAX); + } + read(I, $self->{_vars}{$t}, $2+1) || return(0); + chomp($self->{_vars}{$t}); + } elsif ($tag eq '-local') { + $self->{_vars}{sender_local} = 1; + } elsif ($tag eq '-localerror') { + $self->{_vars}{local_error_message} = 1; + } elsif ($tag eq '-local_scan') { + $self->{_vars}{local_scan_data} = $arg; + } elsif ($tag eq '-host_lookup_failed') { + $self->{_vars}{host_lookup_failed} = 1; + } elsif ($tag eq '-body_linecount') { + $self->{_vars}{body_linecount} = $arg; + } elsif ($tag eq '-frozen') { + $self->{_vars}{deliver_freeze} = 1; + $self->{_vars}{deliver_frozen_at} = $arg; + } elsif ($tag eq '-deliver_firsttime') { + $self->{_vars}{deliver_firsttime} = 1; + $self->{_vars}{first_delivery} = 1; + } elsif ($tag eq '-manual_thaw') { + $self->{_vars}{deliver_manual_thaw} = 1; + $self->{_vars}{manually_thawed} = 1; + } elsif ($tag eq '-auth_id') { + $self->{_vars}{authenticated_id} = $arg; + } elsif ($tag eq '-auth_sender') { + $self->{_vars}{authenticated_sender} = $arg; + } elsif ($tag eq '-sender_set_untrusted') { + $self->{_vars}{sender_set_untrusted} = 1; + } elsif ($tag eq '-tls_certificate_verified') { + $self->{_vars}{tls_certificate_verified} = 1; + } elsif ($tag eq '-tls_cipher') { + $self->{_vars}{tls_cipher} = $arg; + } elsif ($tag eq '-tls_peerdn') { + $self->{_vars}{tls_peerdn} = $arg; + } elsif ($tag eq '-host_address') { + $self->{_vars}{sender_host_port} = $self->_get_host_and_port(\$arg); + $self->{_vars}{sender_host_address} = $arg; + } elsif ($tag eq '-interface_address') { + $self->{_vars}{interface_port} = $self->_get_host_and_port(\$arg); + $self->{_vars}{interface_address} = $arg; + } elsif ($tag eq '-host_auth') { + $self->{_vars}{sender_host_authenticated} = $arg; + } elsif ($tag eq '-host_name') { + $self->{_vars}{sender_host_name} = $arg; + } elsif ($tag eq '-helo_name') { + $self->{_vars}{sender_helo_name} = $arg; + } elsif ($tag eq '-ident') { + $self->{_vars}{sender_ident} = $arg; + } elsif ($tag eq '-received_protocol') { + $self->{_vars}{received_protocol} = $arg; + } elsif ($tag eq '-N') { + $self->{_vars}{dont_deliver} = 1; + } elsif ($tag eq '-body_zerocount') { + $self->{_vars}{body_zerocount} = $arg; + } elsif ($tag eq '-allow_unqualified_recipient') { + $self->{_vars}{allow_unqualified_recipient} = 1; + } elsif ($tag eq '-allow_unqualified_sender') { + $self->{_vars}{allow_unqualified_sender} = 1; + } else { + # unrecognized tag, save it for reference + $self->{$tag} = $arg; + } + } else { + last; + } + } + + # when we drop out of the while loop, we have the first line of the + # delivered tree in $_ + do { + if ($_ eq 'XX') { + ; # noop + } elsif ($_ =~ s/^[YN][YN]\s+//) { + $self->{_del_tree}{$_} = 1; + } else { + return(0); + } + chomp($_ = ); + } while ($_ !~ /^\d+$/); + + $self->{_numrecips} = $_; + $self->{_vars}{recipients_count} = $self->{_numrecips}; + for (my $i = 0; $i < $self->{_numrecips}; $i++) { + chomp($_ = ); + return(0) if (/^$/); + my $addr = ''; + if (/^(.*)\s\d+,(\d+),\d+$/) { + #print STDERR "exim3 type (untested): $_\n"; + $self->{_recips}{$1} = { pno => $2 }; + $addr = $1; + } elsif (/^(.*)\s(\d+)$/) { + #print STDERR "exim4 original type (untested): $_\n"; + $self->{_recips}{$1} = { pno => $2 }; + $addr = $1; + } elsif (/^(.*)\s(.*)\s(\d+),(\d+)#1$/) { + #print STDERR "exim4 new type #1 (untested): $_\n"; + return($self->_error("incorrect format: $_")) if (length($2) != $3); + $self->{_recips}{$1} = { pno => $4, errors_to => $2 }; + $addr = $1; + } elsif (/^.*#(\d+)$/) { + print STDERR "exim4 #$1 style (unimplemented): $_\n"; + $self->_error("exim4 #$1 style (unimplemented): $_"); + } else { + #print STDERR "default type: $_\n"; + $self->{_recips}{$_} = {}; + $addr = $_; + } + $self->{_udel_tree}{$addr} = 1 if (!$self->{_del_tree}{$addr}); + } + $self->{_vars}{recipients} = join(', ', keys(%{$self->{_recips}})); + $self->{_vars}{recipients_del} = join(', ', keys(%{$self->{_del_tree}})); + $self->{_vars}{recipients_undel} = join(', ', keys(%{$self->{_udel_tree}})); + + # blank line + $_ = ; + return(0) if (!/^$/); + + # start reading headers + while (read(I, $_, 3) == 3) { + my $t = getc(I); + return(0) if (!length($t)); + while ($t =~ /^\d$/) { + $_ .= $t; + $t = getc(I); + } + # ok, right here $t contains the header flag and $_ contains the number of + # bytes to read. If we ever use the header flag, grab it here. + $self->{_vars}{message_size} += $_ if ($t ne '*'); + $t = getc(I); # strip the space out of the file + my $bytes = $_; + return(0) if (read(I, $_, $bytes) != $bytes); + chomp(); # may regret this later + # build the $header_ variable, following exim's rules (sort of) + if (/^([^ :]+):(.*)$/s) { + my $v = "header_" . lc($1); + my $d = $2; + $d =~ s/^\s*//; + $d =~ s/\s*$//; + $self->{_vars}{$v} .= (defined($self->{_vars}{$v}) ? "\n" : '') . $d; + $self->{_vars}{received_count}++ if ($v eq 'header_received'); + } + # push header onto $message_headers var, following exim's rules + $self->{_vars}{message_headers} .= + (defined($self->{_vars}{message_headers}) ? "\n" : '') . $_; + } + close(I); + + if (length($self->{_vars}{"header_reply-to"}) > 0) { + $self->{_vars}{reply_address} = $self->{_vars}{"header_reply-to"}; + } else { + $self->{_vars}{reply_address} = $self->{_vars}{header_from}; + } + + $self->{_vars}{message_body_size} = + (stat($self->{_path}.'/'.$self->{_message}.'-D'))[7] - 19; + if ($self->{_vars}{message_body_size} < 0) { + $self->{_vars}{message_size} = 0; + } else { + $self->{_vars}{message_size} += $self->{_vars}{message_body_size} + 1; + } + + return(1); +} + +# mimic exim's host_extract_port function - receive a ref to a scalar, +# strip it of port, return port +sub _get_host_and_port { + my $self = shift; + my $host = shift; # scalar ref, be careful + + if ($$host =~ /^\[([^\]]+)\](?:\:(\d+))?$/) { + $$host = $1; + return($2 || 0); + } elsif ($$host =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?:\.(\d+))?$/) { + $$host = $1; + return($2 || 0); + } elsif ($$host =~ /^([\d\:]+)(?:\.(\d+))?$/) { + $$host = $1; + return($2 || 0); + } + # implicit else + return(0); +} + +sub print_message { + my $self = shift; + my $fh = shift || \*STDOUT; + return if ($self->{_delivered}); + + if ($self->{_output_idonly}) { + print $fh $self->{_message}, "\n"; + return; + } + + if ($self->{_output_long} || $self->{_output_flatq}) { + my $i = int($self->{_vars}{message_age} / 60); + if ($i > 90) { + $i = int(($i+30)/60); + if ($i > 72) { printf $fh "%2dd ", int(($i+12)/24); } + else { printf $fh "%2dh ", $i; } + } else { printf $fh "%2dm ", $i; } + + $i = $self->{_vars}{message_size}; + if ($i == 0) { $i = " "; } + elsif ($i < 1024) { $i = sprintf("%5d", $i); } + elsif ($i < 10*1024) { $i = sprintf("%4.1fK", $i / 1024); } + elsif ($i < 1024*1024) { $i = sprintf("%4dK", ($i+512)/1024); } + elsif ($i < 10*1024*1024) { $i = sprintf("%4.1fM", $i/(1024*1024)); } + else { $i = sprintf("%4dM", ($i + 512 * 1024)/(1024*1024)); } + print $fh "$i "; + } + print $fh "$self->{_message} "; + print $fh "From: " if ($self->{_output_brief}); + print $fh "<$self->{_vars}{sender_address}>"; + + if ($self->{_output_long}) { + print $fh " ($self->{_vars}{originator_login})" + if ($self->{_vars}{sender_set_untrusted}); + + # XXX exim contains code here to print spool format errors + print $fh " *** frozen ***" if ($self->{_vars}{deliver_freeze}); + print $fh "\n"; + + foreach my $v (keys(%{$self->{_show_vars}})) { + printf $fh " %25s = '%s'\n", $v, $self->get_var($v); + } + + foreach my $r (keys %{$self->{_recips}}) { + next if ($self->{_del_tree}{$r} && $self->{_undelivered_only}); + printf $fh " %s %s\n", $self->{_del_tree}{$r} ? "D" : " ", $r; + } + if ($self->{_show_generated}) { + foreach my $r (keys %{$self->{_del_tree}}) { + next if ($self->{_recips}{$r}); + printf $fh " +D %s\n", $r; + } + } + } elsif ($self->{_output_brief}) { + my @r = (); + foreach my $r (keys %{$self->{_recips}}) { + next if ($self->{_del_tree}{$r}); + push(@r, $r); + } + print $fh " To: ", join(';', @r); + } elsif ($self->{_output_flatq}) { + print $fh " *** frozen ***" if ($self->{_vars}{deliver_freeze}); + my @r = (); + foreach my $r (keys %{$self->{_recips}}) { + next if ($self->{_del_tree}{$r}); + push(@r, $r); + } + print $fh " ", join(' ', @r); + } + + print $fh "\n"; +} + +sub dump { + my $self = shift; + + foreach my $k (sort keys %$self) { + my $r = ref($self->{$k}); + if ($r eq 'ARRAY') { + printf "%20s <{$k}}, "EOM\n"; + } elsif ($r eq 'HASH') { + printf "%20s <{$k}}) { + printf "%20s %s\n", $_, $self->{$k}{$_}; + } + print "EOM\n"; + } else { + printf "%20s %s\n", $k, $self->{$k}; + } + } +} + +} # BEGIN + +sub ext_usage { + if ($ARGV[0] =~ /^--help$/i) { + require Config; + $ENV{PATH} .= ":" unless $ENV{PATH} eq ""; + $ENV{PATH} = "$ENV{PATH}$Config::Config{'installscript'}"; + #exec("perldoc", "-F", "-U", $0) || exit 1; + $< = $> = 1 if ($> == 0 || $< == 0); + exec("perldoc", $0) || exit 1; + # make parser happy + %Config::Config = (); + } elsif ($ARGV[0] =~ /^--version$/i) { + print "$p_name version $p_version\n\n$p_cp\n"; + } else { + return; + } + + exit(0); +} + +__END__ + +=head1 NAME + +exipick - display messages from Exim queue based on a variety of criteria + +=head1 USAGE + +exipick [--help|--version] | [-spool ] [-and|-or] [-bp|-bpa|-bpc|-bpr|-bpra|-bpru|-bpu] [ [ ...]] + +=head1 DESCRIPTION + +exipick is designed to display the contents of a Exim mail spool based on user-specified criteria. It is designed to mimic the output of 'exim -bp' (or any of the other -bp* options) and Exim's spec.txt should be used to learn more about the exact format of the output. The criteria are formed by creating comparisons against characteristics of the messages, for instance message_size, sender_helo_name, or message_headers. + +=head1 OPTIONS + +=over 4 + +=item -spool + +The path to exim's spool directory. In general usage you should set the $spool variable in the script to your site's main spool directory, but this option is useful for alternate installs, or installs on NFS servers, etc. + +=item -and + +A message will be displayed only if it matches all of the specified criteria. This is the default. + +=item -or + +A message will be displayed if it matches any of the specified criteria. + +=item The -bp* options all control how much information is displayed and in what manner. They all match the functionality of the options of the same name in Exim. Briefly: + +=item -bp display the matching messages in 'mailq' format. + +=item -bpa ... with generated addresses as well. + +=item -bpc ... just show a count of messages. + +=item -bpr ... do not sort. + +=item -bpra ... with generated addresses, unsorted. + +=item -bpru ... only undelivered addresses, unsorted. + +=item -bpu ... only undelivered addresses. + +Please see Exim's spec.txt for details on the format and information displayed with each option. + +=item The following options are included for compatibility with the 'exiqgrep' utility: + +=item -f Same as '$sender_address = ' + +=item -r Same as '$recipients = ' + +=item -y Same as '$message_age < ' + +=item -o Same as '$message_age > ' + +=item -z Same as '$deliver_freeze' + +=item -x Same as '!$deliver_freeze' + +=item -c Display count of matches only + +=item -l Display in long format (default) + +=item -i Display message IDs only + +=item -b Display brief format only + +Please see the 'exiqgrep' documentation for more details on the behaviour and output format produced by these options + +=item + +The criteria are used to determine whether or not a given message should be displayed. The criteria are built using variables containing information about the individual messages (see VARIABLES section for list and descriptions of available variables). Each criterion is evaluated for each message in the spool and if all (by default) criteria match or (if -or option is specified) any criterion matches, the message is displayed. See VARIABLE TYPES for explanation of types of variables and the evaluations that can be performed on them and EXAMPLES section for complete examples. + +The format of a criterion is explained in detail below, but a key point to make is that the variable being compared must always be on the left side of the comparison. + +If no criteria are provided all messages in the queue are displayed (in this case the output of exipick should be identical to the output of 'exim -bp') + +=item --help + +This screen. + +=item --version + +Version info. + +=back + +=head1 VARIABLE TYPES + +Although there are variable types defined, they are defined only by the type of data that gets put into them. They are internally typeless. Because of this it is perfectly legal to perform a numeric comparison against a string variable, although the results will probably be meaningless. + +=over 4 + +=item NUMERIC + +Variable of the numeric type can be of integer or float. Valid comparisons are <, <=, >, >=, ==, and !=. + +The numbers specified in the criteria can have a suffix of d, h, m, s, M, K, or B, in which case the number will be mulitplied by 86400, 3600, 60, 1, 1048576, 1024, or 1 respectively. These suffixes are case sensitive. While these are obviously designed to aid in date and size calculations, they are not restricted to variables of their respective types. + +=item BOOLEAN + +Variables of the boolean type are very easy to use in criteria. The format is either the variable by itself or the variable negated with a ! sign. For instance, 'deliver_freeze' matches if the message in question is frozen, '!deliver_freeze' matches if message is not frozen. + +=item STRING + +String variables are basically defined as those that are neither numeric nor boolean and can contain any data. There are several types of comparisons that can be made against string variables. With the exception of '=', the operators all match the functionality of the like-named perl operators. + +The simplest form is a bare string regular expression, represented by the operator '='. The value used for the comparison will be evaluated as a regular expression and can be as simple or as complex as desired. For instance 'sender_helo_name = example' on the simple end or 'sender_helo_name = ^aol\.com$' on the more complex end. + +Slightly more complex is the string comparison with the operators 'eq' and 'ne' for equal and not equal, respectively. 'sender_helo_name eq hotmail.com' is true for messages with the exact helo string "hotmail.com", while 'sender_helo_name ne hotmail.com' is true for any message any helo string other than hotmail.com. + +The most complex and the most flexible format are straight regular expressions with the operators =~ and !~. The value in the criteria is expected to be a correctly formatted perl regular expression B. The criterion 'sender_helo_name !~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/' matches for any message which does not have an IP address for its helo string. + +=back + +=head1 VARIABLES + +With a few exceptions the available variables match Exim's internal expansion variables in both name and exact contents. There are a few notable additions and format deviations which are noted below. Although a brief explanation is offered below, Exim's spec.txt should be consulted for full details. It is important to remember that not every variable will be defined for every message. For example, sender_host_port is not defined for messages not received from a remote host. + +In the list below, '.' denotes standard messages with contents matching Exim's variable, '#' denotes standard variables with non-standard contents, and '+' denotes a non-standard variable. + +=head2 Boolean variables + +=over 4 + +=item + allow_unqualified_recipient + +TRUE if unqualified recipient addresses are permitted in header lines. + +=item + allow_unqualified_sender + +TRUE if unqualified sender addresses are permitted in header lines. + +=item + deliver_freeze + +TRUE if the message is frozen. + +=item . first_delivery + +TRUE if the message has not been deferred. + +=item . manually_thawed + +TRUE when the message has been manually thawed. + +=item + dont_deliver + +TRUE if, under normal circumstances, Exim will not try to deliver the message. + +=item . host_lookup_failed + +TRUE if there was an attempt to look up the host's name from its IP address, but the attempt failed. + +=item + local_error_message + +TRUE if the message is a locally-generated error message. + +=item + sender_local + +TRUE if the message was locally generated. + +=item + sender_set_untrusted + +TRUE if the envelope sender of this message was set by an untrusted local caller. + +=item . tls_certificate_verified + +TRUE if a TLS certificate was verified when the message was received. + +=back + +=head2 Numeric variables + +=over 4 + +=item . body_linecount + +The number of lines in the message's body. + +=item . body_zerocount + +The number of binary zero bytes in the message's body. + +=item + deliver_frozen_at + +The epoch time at which message was frozen. + +=item . interface_port + +The local port number if network-originated messages. + +=item . message_age + +The number of seconds since the message was received. + +=item . message_body_size + +The size of the body in bytes. + +=item . message_size + +The size of the message in bytes. + +=item . originator_gid + +The group id under which the process that called Exim was running as when the message was received. + +=item . originator_uid + +The user id under which the process that called Exim was running as when the message was received. + +=item . received_count + +The number of Received: header lines in the message. + +=item + received_time + +The epoch time at which the message was received. + +=item . recipients_count + +The number of envelope recipients that came with the message. + +=item . sender_host_port + +The port number that was used on the remote host for network-originated messages. + +=item + warning_count + +The number of delay warnings which have been sent for this message. + +=back + +=head2 String variables + +=over 4 + +=item . acl_c0-acl_c9, acl_m0-acl_m9 + +User definable variables. + +=item . authenticated_id + +Optional saved information from authenticators, or the login name of the calling process for locally submitted messages. + +=item . authenticated_sender + +The value of AUTH= param for smtp messages, or a generated value from the calling processes login and qualify domain for locally submitted messages. + +=item # header_* + +The value of the same named message header, for example header_to or header_reply-to. These variables are really closer to Exim's rheader_* variables, with the exception that leading and trailing space is removed. + +=item . interface_address + +The address of the local IP interface for network-originated messages. + +=item . local_scan_data + +The text returned by the local_scan() function when a message is received. + +=item # message_body + +The message's body. Unlike Exim's variable of the same name, this variable contains the entire message body. The logic behind this is that the message body is not read unless it is specifically referenced, so under normal circumstances it is not a penalty, but when you need the entire body you need the entire body. Like Exim's copy, newlines and nulls are replaced by spaces. + +=item . message_headers + +A concatenation of all the header lines except for lines added by routers or transports. + +=item . message_id + +The unique message id that is used by Exim to identify the message. + +=item + originator_login + +The login of the process which called Exim. + +=item . received_protocol + +The name of the protocol by which the message was received. + +=item # recipients + +The list of envelope recipients for a message. Unlike Exim's version, this variable always contains every envelope recipient of the message. The recipients are separated by a comma and a space. + +=item + recipients_del + +The list of delivered envelope recipients for a message. This non-standard variable is in the same format as recipients and contains the list of already-delivered recipients. + +=item + recipients_undel + +The list of undelivered envelope recipients for a message. This non-standard variable is in the same format as recipients and contains the list of undelivered recipients. + +=item . reply_address + +The contents of the Reply-To: header line if one exists and it is not empty, or otherwise the contents of the From: header line. + +=item . sender_address + +The sender's address that was received in the message's envelope. For bounce messages, the value of this variable is the empty string. + +=item . sender_address_domain + +The domain part of sender_address. + +=item . sender_address_local_part + +The local part of sender_address. + +=item . sender_helo_name + +The HELO or EHLO value supplied for smtp or bsmtp messages. + +=item . sender_host_address + +The remote host's IP address. + +=item . sender_host_authenticated + +The name of the authenticator driver which successfully authenticated the client from which the message was received. + +=item . sender_host_name + +The remote host's name as obtained by looking up its IP address. + +=item . sender_ident + +The identification received in response to an RFC 1413 request for remote messages, the login name of the user that called Exim for locally generated messages. + +=item . tls_cipher + +The cipher suite that was negotiated for encrypted SMTP connections. + +=item . tls_peerdn + +The value of the Distinguished Name of the certificate if Exim is configured to request one. + +=back + +=head1 EXAMPLES + +=over 4 + +=item exipick 'deliver_freeze' + +Display only frozen messages. + +=item exipick 'received_protocol eq asmtp' 'message_age < 20m' + +Display only messages wich were delivered over an authenticated smtp session in the last 20 minutes. + +=item exipick -bpc 'message_size > 200K' + +Display a count of messages in the queue which are over 200 kilobytes in size. + +=item exipick -or 'sender_helo_name =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/' 'sender_helo_name = _' + +Display message which have a HELO string which either is an IP address or contains an underscore. + +=back + +=head1 REQUIREMENTS + +None that I know of, except an Exim installation. Your life will also be a lot easier if you set $spool at the top of the script to your install's spool directory. + +=head1 ACKNOWLEDGEMENTS + +Although I conceived of the concept for this program independently, the name 'exipick' was taken from the Exim WishList and was suggested by Jeffrey Goldberg. + +Thank you to Philip Hazel for writing Exim. Of course this program exists because of Exim, but more specifically the message parsing code is based on Exim's and some of this documentation was copy/pasted from Exim's. + +=head1 CONTACT + +=over 4 + +=item EMAIL: proj-exipick@jetmore.net + +=item HOME: jetmore.org/john/code/#exipick + +=back + +=cut diff --git a/src/src/exiqgrep.src b/src/src/exiqgrep.src new file mode 100644 index 000000000..9abaab787 --- /dev/null +++ b/src/src/exiqgrep.src @@ -0,0 +1,195 @@ +#!PERL_COMMAND +# $Cambridge: exim/src/src/exiqgrep.src,v 1.1 2004/10/07 10:39:01 ph10 Exp $ + +# Utility for searching and displaying queue information. +# Written by Matt Hubbard 15 August 2002 + +# Except when they appear in comments, the following placeholders in this +# source are replaced when it is turned into a runnable script: +# +# BIN_DIRECTORY +# PERL_COMMAND + +# PROCESSED_FLAG + + +# Routine for extracting the UTC timestamp from message ID +# lifted from eximstat utility + +# Version 1.1 + +use strict; +use Getopt::Std; + +# Have this variable point to your exim binary. +my $exim = 'BIN_DIRECTORY/exim'; +my $eargs = '-bpu'; +my %id; +my %opt; +my $count = 0; +my $mcount = 0; +my @tab62 = + (0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, # 0-9 + 0,10,11,12,13,14,15,16,17,18,19,20, # A-K + 21,22,23,24,25,26,27,28,29,30,31,32, # L-W + 33,34,35, 0, 0, 0, 0, 0, # X-Z + 0,36,37,38,39,40,41,42,43,44,45,46, # a-k + 47,48,49,50,51,52,53,54,55,56,57,58, # l-w + 59,60,61); # x-z + +my $base; +if ($^O eq 'darwin') { # aka MacOS X + $base = 36; + } else { + $base = 62; +}; + +getopts('hf:r:y:o:s:zxlibRc',\%opt); +if ($opt{h}) { &help; exit;} + +# Read message queue output into hash +&collect(); +# Identify which messages match selection criteria +&selection(); +# Print matching data according to display option. +&display(); +exit; + + +sub help() { + print <<'EOF' +Exim message queue display utility. + + -h This help message. + +Selection criteria: + -f Match sender address sender (field is "< >" wrapped) + -r Match recipient address + -s Match against the size field from long output + -y Message younger than + -o Message older than + -z Frozen messages only (exclude non-frozen) + -x Non-frozen messages only (exclude frozen) + +[ NB: for regexps, provided string sits in // ] + +Display options: + -c Display match count + -l Long Format [Default] + -i Message IDs only + -b Brief Format + -R Reverse order +EOF +} + +sub collect() { + open(QUEUE,"$exim $eargs |") or die("Error openning pipe: $!\n"); + while() { + chomp(); + my $line = $_; + #Should be 1st line of record, if not error. + if ($line =~ /^\s*(\w+)\s+(\S+)\s+(\w{6}-\w{6}-\w{2})\s+(<.*?>)/) { + my $msg = $3; + $id{$msg}{age} = $1; + $id{$msg}{size} = $2; + $id{$msg}{from} = $4; + $id{$msg}{birth} = &msg_utc($msg); + $id{$msg}{ages} = time - $id{$msg}{birth}; + if ($line =~ /\*\*\* frozen \*\*\*$/) { + $id{$msg}{frozen} = 1; + } else { + $id{$msg}{frozen} = 0; + } + while( =~ /\s+(.*?\@.*)$/) { + push(@{$id{$msg}{rcpt}},$1); + } + # Increment message counter. + $count++; + } else { + print STDERR "Line mismatch: $line\n"; exit 1; + } + } + close(QUEUE) or die("Error closing pipe: $!\n"); +} + +sub selection() { + foreach my $msg (keys(%id)) { + if ($opt{f}) { + # Match sender address + next unless ($id{$msg}{from} =~ /$opt{f}/); + } + if ($opt{r}) { + # Match any recipient address + my $match = 0; + foreach my $rcpt (@{$id{$msg}{rcpt}}) { + $match++ if ($rcpt =~ /$opt{r}/); + } + next unless ($match); + } + if ($opt{s}) { + # Match against the size string. + next unless ($id{$msg}{size} =~ /$opt{s}/); + } + if ($opt{y}) { + # Match younger than + next unless ($id{$msg}{ages} < $opt{y}); + } + if ($opt{o}) { + # Match older than + next unless ($id{$msg}{ages} > $opt{o}); + } + if ($opt{z}) { + # Exclude non frozen + next unless ($id{$msg}{frozen}); + } + if ($opt{x}) { + # Exclude frozen + next if ($id{$msg}{frozen}); + } + # Here's what we do to select the record. + # Should only get this far if the message passed all of + # the active tests. + $id{$msg}{d} = 1; + # Increment match counter. + $mcount++; + } +} + +sub display() { + if ($opt{c}) { + printf("%d matches out of %d messages\n",$mcount,$count); + exit; + } + foreach my $msg (sort { $opt{R} ? $id{$b}{birth} <=> $id{$a}{birth} : $id{$a}{birth} <=> $id{$b}{birth} } keys(%id) ) { + if (exists($id{$msg}{d})) { + if ($opt{i}) { + # Just the msg ID + print $msg, "\n"; + } elsif ($opt{b}) { + # Brief format + printf("%s From: %s To: %s\n",$msg,$id{$msg}{from},join(';',@{$id{$msg}{rcpt}})) + } else { + # Otherwise Long format attempted duplication of original format. + printf("%3s %5s %s %s%s\n",$id{$msg}{age},$id{$msg}{size},$msg,$id{$msg}{from},$id{$msg}{frozen} ? " *** frozen ***" : ""); + foreach my $rcpt (@{$id{$msg}{rcpt}}) { + printf(" %s\n",$rcpt); + } + print "\n"; + } + } + } +} + +sub report() { + foreach my $msg (keys(%id)) { + print "$id{$msg}{birth} $msg\tAge: $id{$msg}{age}\tSize: $id{$msg}{size}\tFrom: $id{$msg}{from}\tTo: " . join(" ",@{$id{$msg}{rcpt}}). "\n"; + } +} + +sub msg_utc() { + my $id = substr((pop @_), 0, 6); + my $s = 0; + my @c = split(//, $id); + while($#c >= 0) { $s = $s * $base + $tab62[ord(shift @c) - ord('0')] } + return $s; +} diff --git a/src/src/exiqsumm.src b/src/src/exiqsumm.src new file mode 100644 index 000000000..00cd23152 --- /dev/null +++ b/src/src/exiqsumm.src @@ -0,0 +1,146 @@ +#! PERL_COMMAND -w +# $Cambridge: exim/src/src/exiqsumm.src,v 1.1 2004/10/07 10:39:01 ph10 Exp $ + +# Mail Queue Summary +# Christoph Lameter, 21 May 1997 +# Modified by Philip Hazel, June 1997 +# Bug fix: June 1998 by Philip Hazel +# Message sizes not listed by -bp with K or M +# suffixes were getting divided by 10. +# Bug fix: October 1998 by Philip Hazel +# Sorting wasn't working right with Perl 5.005 +# Fix provided by John Horne +# Bug fix: November 1998 by Philip Hazel +# Failing to recognize domain literals in recipient addresses +# Fix provided by Malcolm Ray +# Bug fix: July 2002 by Philip Hazel +# Not handling time periods of more than 100 days +# Fix provided by Randy Banks +# Added summary line: September 2002 by Philip Hazel +# Code provided by Joachim Wieland +# June 2003 by Philip Hazel +# Initialize $size, $age, $id to avoid warnings when bad +# data is provided +# Bug fix: July 2003 by Philip Hazel +# Incorrectly skipping the first lines of messages whose +# message ID ends in 'D'! Before Exim 4.14 this didn't +# matter because they never did. Looks like an original +# typo. Fix provided by Chris Liddiard. +# +# Usage: mailq | exiqsumm [-a] [-c] +# Default sorting is by domain name +# -a sorts by age of oldest message +# -c sorts by count of message + +# Slightly modified sub from eximstats + +sub print_volume_rounded { +my($x) = pop @_; +if ($x < 10000) + { + return sprintf("%6d", $x); + } +elsif ($x < 10000000) + { + return sprintf("%4dKB", ($x + 512)/1024); + } +else + { + return sprintf("%4dMB", ($x + 512*1024)/(1024*1024)); + } +} + +sub s_conv { + my($x) = @_; + my($v,$s) = $x =~ /([\d\.]+)([A-Z]|)/; + if ($s eq "K") { return $v * 1024 }; + if ($s eq "M") { return $v * 1024 * 1024 }; + return $v; +} + +sub older { + my($x1,$x2) = @_; + my($v1,$s1) = $x1 =~ /(\d+)(\w)/; + my($v2,$s2) = $x2 =~ /(\d+)(\w)/; + return $v1 <=> $v2 if ($s1 eq $s2); + return (($s2 eq "m") || + ($s2 eq "h" && $s1 eq "d") || + ($s2 eq "d" && $s1 eq "w"))? 1 : -1; +} + +# +# Main Program +# + +$sort_by_count = 0; +$sort_by_age = 0; + +$size = "0"; +$age = "0d"; +$id = ""; + + +while (@ARGV > 0 && substr($ARGV[0], 0, 1) eq "-") + { + if ($ARGV[0] eq "-a") { $sort_by_age = 1; } + if ($ARGV[0] eq "-c") { $sort_by_count = 1; } + shift @ARGV; + } + +while (<>) +{ +# Skip already delivered lines + +if (/^\s*D\s\S+/) { next; } + +# If it's the first line of a message, pick out the data. Note: it may +# have text after the final > (e.g. frozen) so don't insist that it ends >. + +if (/^([\d\s]{2,3}\w)\s+(\S+)\s(\S+)\s\<(\S*)\>/) + { + ($age,$size,$id)=($1,$2,$3); + } + +# Else check for a recipient line: to handle source-routed addresses, just +# pick off the first domain. + +elsif (/^\s+[^@]*\@([\w\.\-]+|\[(\d+\.){3}\d+\])/) + { + $domain = "\L$1"; + $queue{$domain}++; + $q_oldest{$domain} = $age + if (!defined $q_oldest{$domain} || &older($age,$q_oldest{$domain}) > 0); + $q_recent{$domain} = $age + if (!defined $q_recent{$domain} || &older($q_recent{$domain},$age) > 0); + $q_size{$domain} = 0 if (!defined $q_size{$domain}); + $q_size{$domain} += &s_conv($size); + } +} + +print "\nCount Volume Oldest Newest Domain"; +print "\n----- ------ ------ ------ ------\n\n"; + +my ($count, $volume, $max_age, $min_age) = (0, 0, "0m", "0000d"); + +foreach $id (sort + { + $sort_by_age? &older($q_oldest{$b}, $q_oldest{$a}) : + $sort_by_count? ($queue{$b} <=> $queue{$a}) : + $a cmp $b + } + keys %queue) + { + printf("%5d %.6s %6s %6s %.80s\n", + $queue{$id}, &print_volume_rounded($q_size{$id}), $q_oldest{$id}, + $q_recent{$id}, $id); + $max_age = $q_oldest{$id} if &older($q_oldest{$id}, $max_age) > 0; + $min_age = $q_recent{$id} if &older($min_age, $q_recent{$id}) > 0; + $volume += $q_size{$id}; + $count += $queue{$id}; + } +printf("---------------------------------------------------------------\n"); +printf("%5d %.6s %6s %6s %.80s\n", + $count, &print_volume_rounded($volume), $max_age, $min_age, "TOTAL"); +print "\n"; + +# End diff --git a/src/src/exiwhat.src b/src/src/exiwhat.src new file mode 100644 index 000000000..77d7f16b9 --- /dev/null +++ b/src/src/exiwhat.src @@ -0,0 +1,130 @@ +#! /bin/sh +# $Cambridge: exim/src/src/exiwhat.src,v 1.1 2004/10/07 10:39:01 ph10 Exp $ + +# Copyright (c) 2003 University of Cambridge. +# See the file NOTICE for conditions of use and distribution. + +# Except when they appear in comments, the following placeholders in this +# source are replaced when it is turned into a runnable script: +# +# CONFIGURE_FILE_USE_NODE +# CONFIGURE_FILE +# BIN_DIRECTORY +# EXIWHAT_PS_CMD +# EXIWHAT_PS_ARG +# EXIWHAT_KILL_SIGNAL +# EXIWHAT_EGREP_ARG +# EXIWHAT_MULTIKILL_CMD +# EXIWHAT_MULTIKILL_ARG + +# PROCESSED_FLAG + +# Shell script for seeing what the exim processes are doing. It gets rid +# of the old process log, then sends SIGUSR1 to all exim processes to get +# them to write their state to the log. Then it displays the contents of +# the log. + +# The following lines are generated from Exim's configuration file when +# this source is built into a script, but you can subsequently edit them +# without rebuilding things, as long are you are careful not to overwrite +# the script in the next Exim rebuild/install. However, it's best to +# arrange your build-time configuration file to get the correct values. + +# Some operating systems have a command that finds processes that match +# certain conditions (by default usually those running specific commands) +# and sends them signals. If such a command is defined for your OS, the +# following variables are set and used. + +multikill_cmd=EXIWHAT_MULTIKILL_CMD +multikill_arg=EXIWHAT_MULTIKILL_ARG + +# In other operating systems, Exim has to use "ps" and "egrep" to find the +# processes itself. In those cases, the next three variables are used: + +ps_cmd=EXIWHAT_PS_CMD +ps_arg=EXIWHAT_PS_ARG +egrep_arg=EXIWHAT_EGREP_ARG + +# In both cases, kill_arg is the argument for the (multi)kill command to send +# SIGUSR1 (at least one OS requires a numeric value). + +signal=EXIWHAT_KILL_SIGNAL + +# See if this installation is using the esoteric "USE_NODE" feature of Exim, +# in which it uses the host's name as a suffix for the configuration file name. + +if [ "CONFIGURE_FILE_USE_NODE" = "yes" ]; then + hostsuffix=.`uname -n` +fi + +# Now find the configuration file name. This has got complicated because +# CONFIGURE_FILE may now be a list of files. The one that is used is the first +# one that exists. Mimic the code in readconf.c by testing first for the +# suffixed file in each case. + +set `awk -F: '{ for (i = 1; i <= NF; i++) print $i }' < +#endif +#ifndef HAVE_CRYPT16 +extern char* crypt16(char*, char*); +#endif +#endif + +#ifdef LOOKUP_LDAP +#include "lookups/ldap.h" +#endif + + + +/* Recursively called function */ + +static uschar *expand_string_internal(uschar *, BOOL, uschar **, BOOL); + + + +/************************************************* +* Local statics and tables * +*************************************************/ + +/* Table of item names, and corresponding switch numbers. The names must be in +alphabetical order. */ + +static uschar *item_table[] = { + US"extract", + US"hash", + US"hmac", + US"if", + US"length", + US"lookup", + US"nhash", + #ifdef EXIM_PERL + US"perl", + #endif + US"readfile", + US"readsocket", + US"run", + US"sg", + US"substr", + US"tr" }; + +enum { + EITEM_EXTRACT, + EITEM_HASH, + EITEM_HMAC, + EITEM_IF, + EITEM_LENGTH, + EITEM_LOOKUP, + EITEM_NHASH, + #ifdef EXIM_PERL + EITEM_PERL, + #endif + EITEM_READFILE, + EITEM_READSOCK, + EITEM_RUN, + EITEM_SG, + EITEM_SUBSTR, + EITEM_TR }; + +/* Tables of operator names, and corresponding switch numbers. The names must be +in alphabetical order. There are two tables, because underscore is used in some +cases to introduce arguments, whereas for other it is part of the name. This is +an historical mis-design. */ + +static uschar *op_table_underscore[] = { + US"from_utf8", + US"local_part", + US"quote_local_part", + US"time_interval"}; + +enum { + EOP_FROM_UTF8, + EOP_LOCAL_PART, + EOP_QUOTE_LOCAL_PART, + EOP_TIME_INTERVAL }; + +static uschar *op_table_main[] = { + US"address", + US"base62", + US"base62d", + US"domain", + US"escape", + US"eval", + US"eval10", + US"expand", + US"h", + US"hash", + US"hex2b64", + US"l", + US"lc", + US"length", + US"mask", + US"md5", + US"nh", + US"nhash", + US"quote", + US"rfc2047", + US"rxquote", + US"s", + US"sha1", + US"stat", + US"str2b64", + US"strlen", + US"substr", + US"uc" }; + +enum { + EOP_ADDRESS = sizeof(op_table_underscore)/sizeof(uschar *), + EOP_BASE62, + EOP_BASE62D, + EOP_DOMAIN, + EOP_ESCAPE, + EOP_EVAL, + EOP_EVAL10, + EOP_EXPAND, + EOP_H, + EOP_HASH, + EOP_HEX2B64, + EOP_L, + EOP_LC, + EOP_LENGTH, + EOP_MASK, + EOP_MD5, + EOP_NH, + EOP_NHASH, + EOP_QUOTE, + EOP_RFC2047, + EOP_RXQUOTE, + EOP_S, + EOP_SHA1, + EOP_STAT, + EOP_STR2B64, + EOP_STRLEN, + EOP_SUBSTR, + EOP_UC }; + + +/* Table of condition names, and corresponding switch numbers. The names must +be in alphabetical order. */ + +static uschar *cond_table[] = { + US"<", + US"<=", + US"=", + US"==", /* Backward compatibility */ + US">", + US">=", + US"and", + US"crypteq", + US"def", + US"eq", + US"eqi", + US"exists", + US"first_delivery", + US"ge", + US"gei", + US"gt", + US"gti", + US"isip", + US"isip4", + US"isip6", + US"ldapauth", + US"le", + US"lei", + US"lt", + US"lti", + US"match", + US"match_address", + US"match_domain", + US"match_local_part", + US"or", + US"pam", + US"pwcheck", + US"queue_running", + US"radius", + US"saslauthd" +}; + +enum { + ECOND_NUM_L, + ECOND_NUM_LE, + ECOND_NUM_E, + ECOND_NUM_EE, + ECOND_NUM_G, + ECOND_NUM_GE, + ECOND_AND, + ECOND_CRYPTEQ, + ECOND_DEF, + ECOND_STR_EQ, + ECOND_STR_EQI, + ECOND_EXISTS, + ECOND_FIRST_DELIVERY, + ECOND_STR_GE, + ECOND_STR_GEI, + ECOND_STR_GT, + ECOND_STR_GTI, + ECOND_ISIP, + ECOND_ISIP4, + ECOND_ISIP6, + ECOND_LDAPAUTH, + ECOND_STR_LE, + ECOND_STR_LEI, + ECOND_STR_LT, + ECOND_STR_LTI, + ECOND_MATCH, + ECOND_MATCH_ADDRESS, + ECOND_MATCH_DOMAIN, + ECOND_MATCH_LOCAL_PART, + ECOND_OR, + ECOND_PAM, + ECOND_PWCHECK, + ECOND_QUEUE_RUNNING, + ECOND_RADIUS, + ECOND_SASLAUTHD +}; + + +/* Type for main variable table */ + +typedef struct { + char *name; + int type; + void *value; +} var_entry; + +/* Type for entries pointing to address/length pairs. Not currently +in use. */ + +typedef struct { + uschar **address; + int *length; +} alblock; + +/* Types of table entry */ + +enum { + vtype_int, /* value is address of int */ + vtype_filter_int, /* ditto, but recognized only when filtering */ + vtype_ino, /* value is address of ino_t (not always an int) */ + vtype_uid, /* value is address of uid_t (not always an int) */ + vtype_gid, /* value is address of gid_t (not always an int) */ + vtype_stringptr, /* value is address of pointer to string */ + vtype_msgbody, /* as stringptr, but read when first required */ + vtype_msgbody_end, /* ditto, the end of the message */ + vtype_msgheaders, /* the message's headers */ + vtype_localpart, /* extract local part from string */ + vtype_domain, /* extract domain from string */ + vtype_recipients, /* extract recipients from recipients list */ + /* (enabled only during system filtering */ + vtype_todbsdin, /* value not used; generate BSD inbox tod */ + vtype_tode, /* value not used; generate tod in epoch format */ + vtype_todf, /* value not used; generate full tod */ + vtype_todl, /* value not used; generate log tod */ + vtype_todlf, /* value not used; generate log file datestamp tod */ + vtype_todzone, /* value not used; generate time zone only */ + vtype_todzulu, /* value not used; generate zulu tod */ + vtype_reply, /* value not used; get reply from headers */ + vtype_pid, /* value not used; result is pid */ + vtype_host_lookup, /* value not used; get host name */ + vtype_load_avg /* value not used; result is int from os_getloadavg */ + }; + +/* This table must be kept in alphabetical order. */ + +static var_entry var_table[] = { + { "acl_c0", vtype_stringptr, &acl_var[0] }, + { "acl_c1", vtype_stringptr, &acl_var[1] }, + { "acl_c2", vtype_stringptr, &acl_var[2] }, + { "acl_c3", vtype_stringptr, &acl_var[3] }, + { "acl_c4", vtype_stringptr, &acl_var[4] }, + { "acl_c5", vtype_stringptr, &acl_var[5] }, + { "acl_c6", vtype_stringptr, &acl_var[6] }, + { "acl_c7", vtype_stringptr, &acl_var[7] }, + { "acl_c8", vtype_stringptr, &acl_var[8] }, + { "acl_c9", vtype_stringptr, &acl_var[9] }, + { "acl_m0", vtype_stringptr, &acl_var[10] }, + { "acl_m1", vtype_stringptr, &acl_var[11] }, + { "acl_m2", vtype_stringptr, &acl_var[12] }, + { "acl_m3", vtype_stringptr, &acl_var[13] }, + { "acl_m4", vtype_stringptr, &acl_var[14] }, + { "acl_m5", vtype_stringptr, &acl_var[15] }, + { "acl_m6", vtype_stringptr, &acl_var[16] }, + { "acl_m7", vtype_stringptr, &acl_var[17] }, + { "acl_m8", vtype_stringptr, &acl_var[18] }, + { "acl_m9", vtype_stringptr, &acl_var[19] }, + { "acl_verify_message", vtype_stringptr, &acl_verify_message }, + { "address_data", vtype_stringptr, &deliver_address_data }, + { "address_file", vtype_stringptr, &address_file }, + { "address_pipe", vtype_stringptr, &address_pipe }, + { "authenticated_id", vtype_stringptr, &authenticated_id }, + { "authenticated_sender",vtype_stringptr, &authenticated_sender }, + { "authentication_failed",vtype_int, &authentication_failed }, + { "body_linecount", vtype_int, &body_linecount }, + { "body_zerocount", vtype_int, &body_zerocount }, + { "bounce_recipient", vtype_stringptr, &bounce_recipient }, + { "bounce_return_size_limit", vtype_int, &bounce_return_size_limit }, + { "caller_gid", vtype_gid, &real_gid }, + { "caller_uid", vtype_uid, &real_uid }, + { "compile_date", vtype_stringptr, &version_date }, + { "compile_number", vtype_stringptr, &version_cnumber }, + { "dnslist_domain", vtype_stringptr, &dnslist_domain }, + { "dnslist_text", vtype_stringptr, &dnslist_text }, + { "dnslist_value", vtype_stringptr, &dnslist_value }, + { "domain", vtype_stringptr, &deliver_domain }, + { "domain_data", vtype_stringptr, &deliver_domain_data }, + { "exim_gid", vtype_gid, &exim_gid }, + { "exim_path", vtype_stringptr, &exim_path }, + { "exim_uid", vtype_uid, &exim_uid }, + { "home", vtype_stringptr, &deliver_home }, + { "host", vtype_stringptr, &deliver_host }, + { "host_address", vtype_stringptr, &deliver_host_address }, + { "host_data", vtype_stringptr, &host_data }, + { "host_lookup_failed", vtype_int, &host_lookup_failed }, + { "inode", vtype_ino, &deliver_inode }, + { "interface_address", vtype_stringptr, &interface_address }, + { "interface_port", vtype_int, &interface_port }, + #ifdef LOOKUP_LDAP + { "ldap_dn", vtype_stringptr, &eldap_dn }, + #endif + { "load_average", vtype_load_avg, NULL }, + { "local_part", vtype_stringptr, &deliver_localpart }, + { "local_part_data", vtype_stringptr, &deliver_localpart_data }, + { "local_part_prefix", vtype_stringptr, &deliver_localpart_prefix }, + { "local_part_suffix", vtype_stringptr, &deliver_localpart_suffix }, + { "local_scan_data", vtype_stringptr, &local_scan_data }, + { "local_user_gid", vtype_gid, &local_user_gid }, + { "local_user_uid", vtype_uid, &local_user_uid }, + { "localhost_number", vtype_int, &host_number }, + { "mailstore_basename", vtype_stringptr, &mailstore_basename }, + { "message_age", vtype_int, &message_age }, + { "message_body", vtype_msgbody, &message_body }, + { "message_body_end", vtype_msgbody_end, &message_body_end }, + { "message_body_size", vtype_int, &message_body_size }, + { "message_headers", vtype_msgheaders, NULL }, + { "message_id", vtype_stringptr, &message_id }, + { "message_size", vtype_int, &message_size }, + { "n0", vtype_filter_int, &filter_n[0] }, + { "n1", vtype_filter_int, &filter_n[1] }, + { "n2", vtype_filter_int, &filter_n[2] }, + { "n3", vtype_filter_int, &filter_n[3] }, + { "n4", vtype_filter_int, &filter_n[4] }, + { "n5", vtype_filter_int, &filter_n[5] }, + { "n6", vtype_filter_int, &filter_n[6] }, + { "n7", vtype_filter_int, &filter_n[7] }, + { "n8", vtype_filter_int, &filter_n[8] }, + { "n9", vtype_filter_int, &filter_n[9] }, + { "original_domain", vtype_stringptr, &deliver_domain_orig }, + { "original_local_part", vtype_stringptr, &deliver_localpart_orig }, + { "originator_gid", vtype_gid, &originator_gid }, + { "originator_uid", vtype_uid, &originator_uid }, + { "parent_domain", vtype_stringptr, &deliver_domain_parent }, + { "parent_local_part", vtype_stringptr, &deliver_localpart_parent }, + { "pid", vtype_pid, NULL }, + { "primary_hostname", vtype_stringptr, &primary_hostname }, + { "qualify_domain", vtype_stringptr, &qualify_domain_sender }, + { "qualify_recipient", vtype_stringptr, &qualify_domain_recipient }, + { "rcpt_count", vtype_int, &rcpt_count }, + { "rcpt_defer_count", vtype_int, &rcpt_defer_count }, + { "rcpt_fail_count", vtype_int, &rcpt_fail_count }, + { "received_count", vtype_int, &received_count }, + { "received_for", vtype_stringptr, &received_for }, + { "received_protocol", vtype_stringptr, &received_protocol }, + { "recipient_data", vtype_stringptr, &recipient_data }, + { "recipients", vtype_recipients, NULL }, + { "recipients_count", vtype_int, &recipients_count }, + { "reply_address", vtype_reply, NULL }, + { "return_path", vtype_stringptr, &return_path }, + { "return_size_limit", vtype_int, &bounce_return_size_limit }, + { "runrc", vtype_int, &runrc }, + { "self_hostname", vtype_stringptr, &self_hostname }, + { "sender_address", vtype_stringptr, &sender_address }, + { "sender_address_domain", vtype_domain, &sender_address }, + { "sender_address_local_part", vtype_localpart, &sender_address }, + { "sender_data", vtype_stringptr, &sender_data }, + { "sender_fullhost", vtype_stringptr, &sender_fullhost }, + { "sender_helo_name", vtype_stringptr, &sender_helo_name }, + { "sender_host_address", vtype_stringptr, &sender_host_address }, + { "sender_host_authenticated",vtype_stringptr, &sender_host_authenticated }, + { "sender_host_name", vtype_host_lookup, NULL }, + { "sender_host_port", vtype_int, &sender_host_port }, + { "sender_ident", vtype_stringptr, &sender_ident }, + { "sender_rcvhost", vtype_stringptr, &sender_rcvhost }, + { "smtp_command_argument", vtype_stringptr, &smtp_command_argument }, + { "sn0", vtype_filter_int, &filter_sn[0] }, + { "sn1", vtype_filter_int, &filter_sn[1] }, + { "sn2", vtype_filter_int, &filter_sn[2] }, + { "sn3", vtype_filter_int, &filter_sn[3] }, + { "sn4", vtype_filter_int, &filter_sn[4] }, + { "sn5", vtype_filter_int, &filter_sn[5] }, + { "sn6", vtype_filter_int, &filter_sn[6] }, + { "sn7", vtype_filter_int, &filter_sn[7] }, + { "sn8", vtype_filter_int, &filter_sn[8] }, + { "sn9", vtype_filter_int, &filter_sn[9] }, + { "spool_directory", vtype_stringptr, &spool_directory }, + { "thisaddress", vtype_stringptr, &filter_thisaddress }, + { "tls_certificate_verified", vtype_int, &tls_certificate_verified }, + { "tls_cipher", vtype_stringptr, &tls_cipher }, + { "tls_peerdn", vtype_stringptr, &tls_peerdn }, + { "tod_bsdinbox", vtype_todbsdin, NULL }, + { "tod_epoch", vtype_tode, NULL }, + { "tod_full", vtype_todf, NULL }, + { "tod_log", vtype_todl, NULL }, + { "tod_logfile", vtype_todlf, NULL }, + { "tod_zone", vtype_todzone, NULL }, + { "tod_zulu", vtype_todzulu, NULL }, + { "value", vtype_stringptr, &lookup_value }, + { "version_number", vtype_stringptr, &version_string }, + { "warn_message_delay", vtype_stringptr, &warnmsg_delay }, + { "warn_message_recipient",vtype_stringptr, &warnmsg_recipients }, + { "warn_message_recipients",vtype_stringptr,&warnmsg_recipients }, + { "warnmsg_delay", vtype_stringptr, &warnmsg_delay }, + { "warnmsg_recipient", vtype_stringptr, &warnmsg_recipients }, + { "warnmsg_recipients", vtype_stringptr, &warnmsg_recipients } +}; + +static int var_table_size = sizeof(var_table)/sizeof(var_entry); +static uschar var_buffer[256]; +static BOOL malformed_header; + +/* For textual hashes */ + +static char *hashcodes = "abcdefghijklmnopqrtsuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789"; + +enum { HMAC_MD5, HMAC_SHA1 }; + +/* For numeric hashes */ + +static unsigned int prime[] = { + 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, + 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, + 73, 79, 83, 89, 97, 101, 103, 107, 109, 113}; + +/* For printing modes in symbolic form */ + +static uschar *mtable_normal[] = + { US"---", US"--x", US"-w-", US"-wx", US"r--", US"r-x", US"rw-", US"rwx" }; + +static uschar *mtable_setid[] = + { US"--S", US"--s", US"-wS", US"-ws", US"r-S", US"r-s", US"rwS", US"rws" }; + +static uschar *mtable_sticky[] = + { US"--T", US"--t", US"-wT", US"-wt", US"r-T", US"r-t", US"rwT", US"rwt" }; + + + +/************************************************* +* Tables for UTF-8 support * +*************************************************/ + +/* Table of the number of extra characters, indexed by the first character +masked with 0x3f. The highest number for a valid UTF-8 character is in fact +0x3d. */ + +static uschar utf8_table1[] = { + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, + 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, + 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5 }; + +/* These are the masks for the data bits in the first byte of a character, +indexed by the number of additional bytes. */ + +static int utf8_table2[] = { 0xff, 0x1f, 0x0f, 0x07, 0x03, 0x01}; + +/* Get the next UTF-8 character, advancing the pointer. */ + +#define GETUTF8INC(c, ptr) \ + c = *ptr++; \ + if ((c & 0xc0) == 0xc0) \ + { \ + int a = utf8_table1[c & 0x3f]; /* Number of additional bytes */ \ + int s = 6*a; \ + c = (c & utf8_table2[a]) << s; \ + while (a-- > 0) \ + { \ + s -= 6; \ + c |= (*ptr++ & 0x3f) << s; \ + } \ + } + + +/************************************************* +* Binary chop search on a table * +*************************************************/ + +/* This is used for matching expansion items and operators. + +Arguments: + name the name that is being sought + table the table to search + table_size the number of items in the table + +Returns: the offset in the table, or -1 +*/ + +static int +chop_match(uschar *name, uschar **table, int table_size) +{ +uschar **bot = table; +uschar **top = table + table_size; + +while (top > bot) + { + uschar **mid = bot + (top - bot)/2; + int c = Ustrcmp(name, *mid); + if (c == 0) return mid - table; + if (c > 0) bot = mid + 1; else top = mid; + } + +return -1; +} + + + +/************************************************* +* Check a condition string * +*************************************************/ + +/* This function is called to expand a string, and test the result for a "true" +or "false" value. Failure of the expansion yields FALSE; logged unless it was a +forced fail or lookup defer. All store used by the function can be released on +exit. + +Arguments: + condition the condition string + m1 text to be incorporated in panic error + m2 ditto + +Returns: TRUE if condition is met, FALSE if not +*/ + +BOOL +expand_check_condition(uschar *condition, uschar *m1, uschar *m2) +{ +int rc; +void *reset_point = store_get(0); +uschar *ss = expand_string(condition); +if (ss == NULL) + { + if (!expand_string_forcedfail && !search_find_defer) + log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand condition \"%s\" " + "for %s %s: %s", condition, m1, m2, expand_string_message); + return FALSE; + } +rc = ss[0] != 0 && Ustrcmp(ss, "0") != 0 && strcmpic(ss, US"no") != 0 && + strcmpic(ss, US"false") != 0; +store_reset(reset_point); +return rc; +} + + + +/************************************************* +* Pick out a name from a string * +*************************************************/ + +/* If the name is too long, it is silently truncated. + +Arguments: + name points to a buffer into which to put the name + max is the length of the buffer + s points to the first alphabetic character of the name + extras chars other than alphanumerics to permit + +Returns: pointer to the first character after the name + +Note: The test for *s != 0 in the while loop is necessary because +Ustrchr() yields non-NULL if the character is zero (which is not something +I expected). */ + +static uschar * +read_name(uschar *name, int max, uschar *s, uschar *extras) +{ +int ptr = 0; +while (*s != 0 && (isalnum(*s) || Ustrchr(extras, *s) != NULL)) + { + if (ptr < max-1) name[ptr++] = *s; + s++; + } +name[ptr] = 0; +return s; +} + + + +/************************************************* +* Pick out the rest of a header name * +*************************************************/ + +/* A variable name starting $header_ (or just $h_ for those who like +abbreviations) might not be the complete header name because headers can +contain any printing characters in their names, except ':'. This function is +called to read the rest of the name, chop h[eader]_ off the front, and put ':' +on the end, if the name was terminated by white space. + +Arguments: + name points to a buffer in which the name read so far exists + max is the length of the buffer + s points to the first character after the name so far, i.e. the + first non-alphameric character after $header_xxxxx + +Returns: a pointer to the first character after the header name +*/ + +static uschar * +read_header_name(uschar *name, int max, uschar *s) +{ +int prelen = Ustrchr(name, '_') - name + 1; +int ptr = Ustrlen(name) - prelen; +if (ptr > 0) memmove(name, name+prelen, ptr); +while (mac_isgraph(*s) && *s != ':') + { + if (ptr < max-1) name[ptr++] = *s; + s++; + } +if (*s == ':') s++; +name[ptr++] = ':'; +name[ptr] = 0; +return s; +} + + + +/************************************************* +* Pick out a number from a string * +*************************************************/ + +/* Arguments: + n points to an integer into which to put the number + s points to the first digit of the number + +Returns: a pointer to the character after the last digit +*/ + +static uschar * +read_number(int *n, uschar *s) +{ +*n = 0; +while (isdigit(*s)) *n = *n * 10 + (*s++ - '0'); +return s; +} + + + +/************************************************* +* Extract keyed subfield from a string * +*************************************************/ + +/* The yield is in dynamic store; NULL means that the key was not found. + +Arguments: + key points to the name of the key + s points to the string from which to extract the subfield + +Returns: NULL if the subfield was not found, or + a pointer to the subfield's data +*/ + +static uschar * +expand_getkeyed(uschar *key, uschar *s) +{ +int length = Ustrlen(key); +while (isspace(*s)) s++; + +/* Loop to search for the key */ + +while (*s != 0) + { + int dkeylength; + uschar *data; + uschar *dkey = s; + + while (*s != 0 && *s != '=' && !isspace(*s)) s++; + dkeylength = s - dkey; + while (isspace(*s)) s++; + if (*s == '=') while (isspace((*(++s)))); + + data = string_dequote(&s); + if (length == dkeylength && strncmpic(key, dkey, length) == 0) + return data; + + while (isspace(*s)) s++; + } + +return NULL; +} + + + + +/************************************************* +* Extract numbered subfield from string * +*************************************************/ + +/* Extracts a numbered field from a string that is divided by tokens - for +example a line from /etc/passwd is divided by colon characters. First field is +numbered one. Negative arguments count from the right. Zero returns the whole +string. Returns NULL if there are insufficient tokens in the string + +***WARNING*** +Modifies final argument - this is a dynamically generated string, so that's OK. + +Arguments: + field number of field to be extracted, + first field = 1, whole string = 0, last field = -1 + separators characters that are used to break string into tokens + s points to the string from which to extract the subfield + +Returns: NULL if the field was not found, + a pointer to the field's data inside s (modified to add 0) +*/ + +static uschar * +expand_gettokened (int field, uschar *separators, uschar *s) +{ +int sep = 1; +int count; +uschar *ss = s; +uschar *fieldtext = NULL; + +if (field == 0) return s; + +/* Break the line up into fields in place; for field > 0 we stop when we have +done the number of fields we want. For field < 0 we continue till the end of +the string, counting the number of fields. */ + +count = (field > 0)? field : INT_MAX; + +while (count-- > 0) + { + size_t len; + + /* Previous field was the last one in the string. For a positive field + number, this means there are not enough fields. For a negative field number, + check that there are enough, and scan back to find the one that is wanted. */ + + if (sep == 0) + { + if (field > 0 || (-field) > (INT_MAX - count - 1)) return NULL; + if ((-field) == (INT_MAX - count - 1)) return s; + while (field++ < 0) + { + ss--; + while (ss[-1] != 0) ss--; + } + fieldtext = ss; + break; + } + + /* Previous field was not last in the string; save its start and put a + zero at its end. */ + + fieldtext = ss; + len = Ustrcspn(ss, separators); + sep = ss[len]; + ss[len] = 0; + ss += len + 1; + } + +return fieldtext; +} + + + +/************************************************* +* Extract a substring from a string * +*************************************************/ + +/* Perform the ${substr or ${length expansion operations. + +Arguments: + subject the input string + value1 the offset from the start of the input string to the start of + the output string; if negative, count from the right. + value2 the length of the output string, or negative (-1) for unset + if value1 is positive, unset means "all after" + if value1 is negative, unset means "all before" + len set to the length of the returned string + +Returns: pointer to the output string, or NULL if there is an error +*/ + +static uschar * +extract_substr(uschar *subject, int value1, int value2, int *len) +{ +int sublen = Ustrlen(subject); + +if (value1 < 0) /* count from right */ + { + value1 += sublen; + + /* If the position is before the start, skip to the start, and adjust the + length. If the length ends up negative, the substring is null because nothing + can precede. This falls out naturally when the length is unset, meaning "all + to the left". */ + + if (value1 < 0) + { + value2 += value1; + if (value2 < 0) value2 = 0; + value1 = 0; + } + + /* Otherwise an unset length => characters before value1 */ + + else if (value2 < 0) + { + value2 = value1; + value1 = 0; + } + } + +/* For a non-negative offset, if the starting position is past the end of the +string, the result will be the null string. Otherwise, an unset length means +"rest"; just set it to the maximum - it will be cut down below if necessary. */ + +else + { + if (value1 > sublen) + { + value1 = sublen; + value2 = 0; + } + else if (value2 < 0) value2 = sublen; + } + +/* Cut the length down to the maximum possible for the offset value, and get +the required characters. */ + +if (value1 + value2 > sublen) value2 = sublen - value1; +*len = value2; +return subject + value1; +} + + + + +/************************************************* +* Old-style hash of a string * +*************************************************/ + +/* Perform the ${hash expansion operation. + +Arguments: + subject the input string (an expanded substring) + value1 the length of the output string; if greater or equal to the + length of the input string, the input string is returned + value2 the number of hash characters to use, or 26 if negative + len set to the length of the returned string + +Returns: pointer to the output string, or NULL if there is an error +*/ + +static uschar * +compute_hash(uschar *subject, int value1, int value2, int *len) +{ +int sublen = Ustrlen(subject); + +if (value2 < 0) value2 = 26; +else if (value2 > Ustrlen(hashcodes)) + { + expand_string_message = + string_sprintf("hash count \"%d\" too big", value2); + return NULL; + } + +/* Calculate the hash text. We know it is shorter than the original string, so +can safely place it in subject[] (we know that subject is always itself an +expanded substring). */ + +if (value1 < sublen) + { + int c; + int i = 0; + int j = value1; + while ((c = (subject[j])) != 0) + { + int shift = (c + j++) & 7; + subject[i] ^= (c << shift) | (c >> (8-shift)); + if (++i >= value1) i = 0; + } + for (i = 0; i < value1; i++) + subject[i] = hashcodes[(subject[i]) % value2]; + } +else value1 = sublen; + +*len = value1; +return subject; +} + + + + +/************************************************* +* Numeric hash of a string * +*************************************************/ + +/* Perform the ${nhash expansion operation. The first characters of the +string are treated as most important, and get the highest prime numbers. + +Arguments: + subject the input string + value1 the maximum value of the first part of the result + value2 the maximum value of the second part of the result, + or negative to produce only a one-part result + len set to the length of the returned string + +Returns: pointer to the output string, or NULL if there is an error. +*/ + +static uschar * +compute_nhash (uschar *subject, int value1, int value2, int *len) +{ +uschar *s = subject; +int i = 0; +unsigned long int total = 0; /* no overflow */ + +while (*s != 0) + { + if (i == 0) i = sizeof(prime)/sizeof(int) - 1; + total += prime[i--] * (unsigned int)(*s++); + } + +/* If value2 is unset, just compute one number */ + +if (value2 < 0) + { + s = string_sprintf("%d", total % value1); + } + +/* Otherwise do a div/mod hash */ + +else + { + total = total % (value1 * value2); + s = string_sprintf("%d/%d", total/value2, total % value2); + } + +*len = Ustrlen(s); +return s; +} + + + + + +/************************************************* +* Find the value of a header or headers * +*************************************************/ + +/* Multiple instances of the same header get concatenated, and this function +can also return a concatenation of all the header lines. When concatenating +specific headers that contain lists of addresses, a comma is inserted between +them. Otherwise we use a straight concatenation. Because some messages can have +pathologically large number of lines, there is a limit on the length that is +returned. Also, to avoid massive store use which would result from using +string_cat() as it copies and extends strings, we do a preliminary pass to find +out exactly how much store will be needed. On "normal" messages this will be +pretty trivial. + +Arguments: + name the name of the header, without the leading $header_ or $h_, + or NULL if a concatenation of all headers is required + exists_only TRUE if called from a def: test; don't need to build a string; + just return a string that is not "" and not "0" if the header + exists + newsize return the size of memory block that was obtained; may be NULL + if exists_only is TRUE + want_raw TRUE if called for $rh_ or $rheader_ variables; no processing, + other than concatenating, will be done on the header + charset name of charset to translate MIME words to; used only if + want_raw is false; if NULL, no translation is done (this is + used for $bh_ and $bheader_) + +Returns: NULL if the header does not exist, else a pointer to a new + store block +*/ + +static uschar * +find_header(uschar *name, BOOL exists_only, int *newsize, BOOL want_raw, + uschar *charset) +{ +BOOL found = name == NULL; +int comma = 0; +int len = found? 0 : Ustrlen(name); +int i; +uschar *yield = NULL; +uschar *ptr = NULL; + +/* Loop for two passes - saves code repetition */ + +for (i = 0; i < 2; i++) + { + int size = 0; + header_line *h; + + for (h = header_list; size < header_insert_maxlen && h != NULL; h = h->next) + { + if (h->type != htype_old && h->text != NULL) /* NULL => Received: placeholder */ + { + if (name == NULL || (len <= h->slen && strncmpic(name, h->text, len) == 0)) + { + int ilen; + uschar *t; + + if (exists_only) return US"1"; /* don't need actual string */ + found = TRUE; + t = h->text + len; /* text to insert */ + if (!want_raw) /* unless wanted raw, */ + while (isspace(*t)) t++; /* remove leading white space */ + ilen = h->slen - (t - h->text); /* length to insert */ + + /* Set comma = 1 if handling a single header and it's one of those + that contains an address list, except when asked for raw headers. Only + need to do this once. */ + + if (!want_raw && name != NULL && comma == 0 && + Ustrchr("BCFRST", h->type) != NULL) + comma = 1; + + /* First pass - compute total store needed; second pass - compute + total store used, including this header. */ + + size += ilen + comma; + + /* Second pass - concatentate the data, up to a maximum. Note that + the loop stops when size hits the limit. */ + + if (i != 0) + { + if (size > header_insert_maxlen) + { + ilen -= size - header_insert_maxlen; + comma = 0; + } + Ustrncpy(ptr, t, ilen); + ptr += ilen; + if (comma != 0 && ilen > 0) + { + ptr[-1] = ','; + *ptr++ = '\n'; + } + } + } + } + } + + /* At end of first pass, truncate size if necessary, and get the buffer + to hold the data, returning the buffer size. */ + + if (i == 0) + { + if (!found) return NULL; + if (size > header_insert_maxlen) size = header_insert_maxlen; + *newsize = size + 1; + ptr = yield = store_get(*newsize); + } + } + +/* Remove a redundant added comma if present */ + +if (comma != 0 && ptr > yield) ptr -= 2; + +/* That's all we do for raw header expansion. */ + +if (want_raw) + { + *ptr = 0; + } + +/* Otherwise, we remove trailing whitespace, including newlines. Then we do RFC +2047 decoding, translating the charset if requested. The rfc2047_decode2() +function can return an error with decoded data if the charset translation +fails. If decoding fails, it returns NULL. */ + +else + { + uschar *decoded, *error; + while (ptr > yield && isspace(ptr[-1])) ptr--; + *ptr = 0; + decoded = rfc2047_decode2(yield, TRUE, charset, '?', NULL, newsize, &error); + if (error != NULL) + { + DEBUG(D_any) debug_printf("*** error in RFC 2047 decoding: %s\n" + " input was: %s\n", error, yield); + } + if (decoded != NULL) yield = decoded; + } + +return yield; +} + + + + +/************************************************* +* Find value of a variable * +*************************************************/ + +/* The table of variables is kept in alphabetic order, so we can search it +using a binary chop. The "choplen" variable is nothing to do with the binary +chop. + +Arguments: + name the name of the variable being sought + exists_only TRUE if this is a def: test; passed on to find_header() + skipping TRUE => skip any processing evaluation; this is not the same as + exists_only because def: may test for values that are first + evaluated here + newsize pointer to an int which is initially zero; if the answer is in + a new memory buffer, *newsize is set to its size + +Returns: NULL if the variable does not exist, or + a pointer to the variable's contents, or + something non-NULL if exists_only is TRUE +*/ + +static uschar * +find_variable(uschar *name, BOOL exists_only, BOOL skipping, int *newsize) +{ +int first = 0; +int last = var_table_size; + +while (last > first) + { + uschar *s, *domain; + uschar **ss; + int middle = (first + last)/2; + int c = Ustrcmp(name, var_table[middle].name); + + if (c > 0) { first = middle + 1; continue; } + if (c < 0) { last = middle; continue; } + + /* Found an existing variable. If in skipping state, the value isn't needed, + and we want to avoid processing (such as looking up up the host name). */ + + if (skipping) return US""; + + switch (var_table[middle].type) + { + case vtype_filter_int: + if (!filter_running) return NULL; + /* Fall through */ + + case vtype_int: + sprintf(CS var_buffer, "%d", *(int *)(var_table[middle].value)); /* Integer */ + return var_buffer; + + case vtype_ino: + sprintf(CS var_buffer, "%ld", (long int)(*(ino_t *)(var_table[middle].value))); /* Inode */ + return var_buffer; + + case vtype_gid: + sprintf(CS var_buffer, "%ld", (long int)(*(gid_t *)(var_table[middle].value))); /* gid */ + return var_buffer; + + case vtype_uid: + sprintf(CS var_buffer, "%ld", (long int)(*(uid_t *)(var_table[middle].value))); /* uid */ + return var_buffer; + + case vtype_stringptr: /* Pointer to string */ + s = *((uschar **)(var_table[middle].value)); + return (s == NULL)? US"" : s; + + case vtype_pid: + sprintf(CS var_buffer, "%d", (int)getpid()); /* pid */ + return var_buffer; + + case vtype_load_avg: + sprintf(CS var_buffer, "%d", os_getloadavg()); /* load_average */ + return var_buffer; + + case vtype_host_lookup: /* Lookup if not done so */ + if (sender_host_name == NULL && sender_host_address != NULL && + !host_lookup_failed && host_name_lookup() == OK) + host_build_sender_fullhost(); + return (sender_host_name == NULL)? US"" : sender_host_name; + + case vtype_localpart: /* Get local part from address */ + s = *((uschar **)(var_table[middle].value)); + if (s == NULL) return US""; + domain = Ustrrchr(s, '@'); + if (domain == NULL) return s; + if (domain - s > sizeof(var_buffer) - 1) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "local part longer than %d in " + "string expansion", sizeof(var_buffer)); + Ustrncpy(var_buffer, s, domain - s); + var_buffer[domain - s] = 0; + return var_buffer; + + case vtype_domain: /* Get domain from address */ + s = *((uschar **)(var_table[middle].value)); + if (s == NULL) return US""; + domain = Ustrrchr(s, '@'); + return (domain == NULL)? US"" : domain + 1; + + case vtype_msgheaders: + return find_header(NULL, exists_only, newsize, FALSE, NULL); + + case vtype_msgbody: /* Pointer to msgbody string */ + case vtype_msgbody_end: /* Ditto, the end of the msg */ + ss = (uschar **)(var_table[middle].value); + if (*ss == NULL && deliver_datafile >= 0) /* Read body when needed */ + { + uschar *body; + int start_offset = SPOOL_DATA_START_OFFSET; + int len = message_body_visible; + if (len > message_size) len = message_size; + *ss = body = store_malloc(len+1); + body[0] = 0; + if (var_table[middle].type == vtype_msgbody_end) + { + struct stat statbuf; + if (fstat(deliver_datafile, &statbuf) == 0) + { + start_offset = statbuf.st_size - len; + if (start_offset < SPOOL_DATA_START_OFFSET) + start_offset = SPOOL_DATA_START_OFFSET; + } + } + lseek(deliver_datafile, start_offset, SEEK_SET); + len = read(deliver_datafile, body, len); + if (len > 0) + { + body[len] = 0; + while (len > 0) + { + if (body[--len] == '\n' || body[len] == 0) body[len] = ' '; + } + } + } + return (*ss == NULL)? US"" : *ss; + + case vtype_todbsdin: /* BSD inbox time of day */ + return tod_stamp(tod_bsdin); + + case vtype_tode: /* Unix epoch time of day */ + return tod_stamp(tod_epoch); + + case vtype_todf: /* Full time of day */ + return tod_stamp(tod_full); + + case vtype_todl: /* Log format time of day */ + return tod_stamp(tod_log_bare); /* (without timezone) */ + + case vtype_todzone: /* Time zone offset only */ + return tod_stamp(tod_zone); + + case vtype_todzulu: /* Zulu time */ + return tod_stamp(tod_zulu); + + case vtype_todlf: /* Log file datestamp tod */ + return tod_stamp(tod_log_datestamp); + + case vtype_reply: /* Get reply address */ + s = find_header(US"reply-to:", exists_only, newsize, FALSE, + headers_charset); + if (s == NULL || *s == 0) + s = find_header(US"from:", exists_only, newsize, FALSE, headers_charset); + return (s == NULL)? US"" : s; + + /* A recipients list is available only during system message filtering, + during ACL processing after DATA, and while expanding pipe commands + generated from a system filter, but not elsewhere. */ + + case vtype_recipients: + if (!enable_dollar_recipients) return NULL; else + { + int size = 128; + int ptr = 0; + int i; + s = store_get(size); + for (i = 0; i < recipients_count; i++) + { + if (i != 0) s = string_cat(s, &size, &ptr, US", ", 2); + s = string_cat(s, &size, &ptr, recipients_list[i].address, + Ustrlen(recipients_list[i].address)); + } + s[ptr] = 0; /* string_cat() leaves room */ + } + return s; + } + } + +return NULL; /* Unknown variable name */ +} + + + + +/************************************************* +* Read and expand substrings * +*************************************************/ + +/* This function is called to read and expand argument substrings for various +expansion items. Some have a minimum requirement that is less than the maximum; +in these cases, the first non-present one is set to NULL. + +Arguments: + sub points to vector of pointers to set + n maximum number of substrings + m minimum required + sptr points to current string pointer + skipping the skipping flag + check_end if TRUE, check for final '}' + name name of item, for error message + +Returns: 0 OK; string pointer updated + 1 curly bracketing error (too few arguments) + 2 too many arguments (only if check_end is set); message set + 3 other error (expansion failure) +*/ + +static int +read_subs(uschar **sub, int n, int m, uschar **sptr, BOOL skipping, + BOOL check_end, uschar *name) +{ +int i; +uschar *s = *sptr; + +while (isspace(*s)) s++; +for (i = 0; i < n; i++) + { + if (*s != '{') + { + if (i < m) return 1; + sub[i] = NULL; + break; + } + sub[i] = expand_string_internal(s+1, TRUE, &s, skipping); + if (sub[i] == NULL) return 3; + if (*s++ != '}') return 1; + while (isspace(*s)) s++; + } +if (check_end && *s++ != '}') + { + if (s[-1] == '{') + { + expand_string_message = string_sprintf("Too many arguments for \"%s\" " + "(max is %d)", name, n); + return 2; + } + return 1; + } + +*sptr = s; +return 0; +} + + + + +/************************************************* +* Read and evaluate a condition * +*************************************************/ + +/* +Arguments: + s points to the start of the condition text + yield points to a BOOL to hold the result of the condition test; + if NULL, we are just reading through a condition that is + part of an "or" combination to check syntax, or in a state + where the answer isn't required + +Returns: a pointer to the first character after the condition, or + NULL after an error +*/ + +static uschar * +eval_condition(uschar *s, BOOL *yield) +{ +BOOL testfor = TRUE; +BOOL tempcond, combined_cond; +BOOL *subcondptr; +int i, rc, cond_type, roffset; +int num[2]; +struct stat statbuf; +uschar name[256]; +uschar *sub[4]; + +const pcre *re; +const uschar *rerror; + +for (;;) + { + while (isspace(*s)) s++; + if (*s == '!') { testfor = !testfor; s++; } else break; + } + +/* Numeric comparisons are symbolic */ + +if (*s == '=' || *s == '>' || *s == '<') + { + int p = 0; + name[p++] = *s++; + if (*s == '=') + { + name[p++] = '='; + s++; + } + name[p] = 0; + } + +/* All other conditions are named */ + +else s = read_name(name, 256, s, US"_"); + +/* If we haven't read a name, it means some non-alpha character is first. */ + +if (name[0] == 0) + { + expand_string_message = string_sprintf("condition name expected, " + "but found \"%.16s\"", s); + return NULL; + } + +/* Find which condition we are dealing with, and switch on it */ + +cond_type = chop_match(name, cond_table, sizeof(cond_table)/sizeof(uschar *)); +switch(cond_type) + { + /* def: tests for a non-zero or non-NULL variable, or for an existing + header */ + + case ECOND_DEF: + if (*s != ':') + { + expand_string_message = US"\":\" expected after \"def\""; + return NULL; + } + + s = read_name(name, 256, s+1, US"_"); + + /* Test for a header's existence */ + + if (Ustrncmp(name, "h_", 2) == 0 || + Ustrncmp(name, "rh_", 3) == 0 || + Ustrncmp(name, "bh_", 3) == 0 || + Ustrncmp(name, "header_", 7) == 0 || + Ustrncmp(name, "rheader_", 8) == 0 || + Ustrncmp(name, "bheader_", 8) == 0) + { + s = read_header_name(name, 256, s); + if (yield != NULL) *yield = + (find_header(name, TRUE, NULL, FALSE, NULL) != NULL) == testfor; + } + + /* Test for a variable's having a non-empty value. If yield == NULL we + are in a skipping state, and don't care about the answer. */ + + else + { + uschar *value = find_variable(name, TRUE, yield == NULL, NULL); + if (value == NULL) + { + expand_string_message = (name[0] == 0)? + string_sprintf("variable name omitted after \"def:\"") : + string_sprintf("unknown variable \"%s\" after \"def:\"", name); + return NULL; + } + if (yield != NULL) + *yield = (value[0] != 0 && Ustrcmp(value, "0") != 0) == testfor; + } + + return s; + + + /* first_delivery tests for first delivery attempt */ + + case ECOND_FIRST_DELIVERY: + if (yield != NULL) *yield = deliver_firsttime == testfor; + return s; + + + /* queue_running tests for any process started by a queue runner */ + + case ECOND_QUEUE_RUNNING: + if (yield != NULL) *yield = (queue_run_pid != (pid_t)0) == testfor; + return s; + + + /* exists: tests for file existence + isip: tests for any IP address + isip4: tests for an IPv4 address + isip6: tests for an IPv6 address + pam: does PAM authentication + radius: does RADIUS authentication + ldapauth: does LDAP authentication + pwcheck: does Cyrus SASL pwcheck authentication + */ + + case ECOND_EXISTS: + case ECOND_ISIP: + case ECOND_ISIP4: + case ECOND_ISIP6: + case ECOND_PAM: + case ECOND_RADIUS: + case ECOND_LDAPAUTH: + case ECOND_PWCHECK: + + while (isspace(*s)) s++; + if (*s != '{') goto COND_FAILED_CURLY_START; + + sub[0] = expand_string_internal(s+1, TRUE, &s, yield == NULL); + if (sub[0] == NULL) return NULL; + if (*s++ != '}') goto COND_FAILED_CURLY_END; + + if (yield == NULL) return s; /* No need to run the test if skipping */ + + switch(cond_type) + { + case ECOND_EXISTS: + if ((expand_forbid & RDO_EXISTS) != 0) + { + expand_string_message = US"File existence tests are not permitted"; + return NULL; + } + *yield = (Ustat(sub[0], &statbuf) == 0) == testfor; + break; + + case ECOND_ISIP: + case ECOND_ISIP4: + case ECOND_ISIP6: + rc = string_is_ip_address(sub[0], NULL); + *yield = ((cond_type == ECOND_ISIP)? (rc > 0) : + (cond_type == ECOND_ISIP4)? (rc == 4) : (rc == 6)) == testfor; + break; + + /* Various authentication tests - all optionally compiled */ + + case ECOND_PAM: + #ifdef SUPPORT_PAM + rc = auth_call_pam(sub[0], &expand_string_message); + goto END_AUTH; + #else + goto COND_FAILED_NOT_COMPILED; + #endif /* SUPPORT_PAM */ + + case ECOND_RADIUS: + #ifdef RADIUS_CONFIG_FILE + rc = auth_call_radius(sub[0], &expand_string_message); + goto END_AUTH; + #else + goto COND_FAILED_NOT_COMPILED; + #endif /* RADIUS_CONFIG_FILE */ + + case ECOND_LDAPAUTH: + #ifdef LOOKUP_LDAP + { + /* Just to keep the interface the same */ + BOOL do_cache; + int old_pool = store_pool; + store_pool = POOL_SEARCH; + rc = eldapauth_find((void *)(-1), NULL, sub[0], Ustrlen(sub[0]), NULL, + &expand_string_message, &do_cache); + store_pool = old_pool; + } + goto END_AUTH; + #else + goto COND_FAILED_NOT_COMPILED; + #endif /* LOOKUP_LDAP */ + + case ECOND_PWCHECK: + #ifdef CYRUS_PWCHECK_SOCKET + rc = auth_call_pwcheck(sub[0], &expand_string_message); + goto END_AUTH; + #else + goto COND_FAILED_NOT_COMPILED; + #endif /* CYRUS_PWCHECK_SOCKET */ + + #if defined(SUPPORT_PAM) || defined(RADIUS_CONFIG_FILE) || \ + defined(LOOKUP_LDAP) || defined(CYRUS_PWCHECK_SOCKET) + END_AUTH: + if (rc == ERROR || rc == DEFER) return NULL; + *yield = (rc == OK) == testfor; + #endif + } + return s; + + + /* saslauthd: does Cyrus saslauthd authentication. Four parameters are used: + + ${if saslauthd {{username}{password}{service}{realm}} {yes}[no}} + + However, the last two are optional. That is why the whole set is enclosed + in their own set or braces. */ + + case ECOND_SASLAUTHD: + #ifndef CYRUS_SASLAUTHD_SOCKET + goto COND_FAILED_NOT_COMPILED; + #else + while (isspace(*s)) s++; + if (*s++ != '{') goto COND_FAILED_CURLY_START; + switch(read_subs(sub, 4, 2, &s, yield == NULL, TRUE, US"saslauthd")) + { + case 1: expand_string_message = US"too few arguments or bracketing " + "error for saslauthd"; + case 2: + case 3: return NULL; + } + if (sub[2] == NULL) sub[3] = NULL; /* realm if no service */ + if (yield != NULL) + { + int rc; + rc = auth_call_saslauthd(sub[0], sub[1], sub[2], sub[3], + &expand_string_message); + if (rc == ERROR || rc == DEFER) return NULL; + *yield = (rc == OK) == testfor; + } + return s; + #endif /* CYRUS_SASLAUTHD_SOCKET */ + + + /* symbolic operators for numeric and string comparison, and a number of + other operators, all requiring two arguments. + + match: does a regular expression match and sets up the numerical + variables if it succeeds + match_address: matches in an address list + match_domain: matches in a domain list + match_local_part: matches in a local part list + crypteq: encrypts plaintext and compares against an encrypted text, + using crypt(), crypt16(), MD5 or SHA-1 + */ + + case ECOND_MATCH: + case ECOND_MATCH_ADDRESS: + case ECOND_MATCH_DOMAIN: + case ECOND_MATCH_LOCAL_PART: + case ECOND_CRYPTEQ: + + case ECOND_NUM_L: /* Numerical comparisons */ + case ECOND_NUM_LE: + case ECOND_NUM_E: + case ECOND_NUM_EE: + case ECOND_NUM_G: + case ECOND_NUM_GE: + + case ECOND_STR_LT: /* String comparisons */ + case ECOND_STR_LTI: + case ECOND_STR_LE: + case ECOND_STR_LEI: + case ECOND_STR_EQ: + case ECOND_STR_EQI: + case ECOND_STR_GT: + case ECOND_STR_GTI: + case ECOND_STR_GE: + case ECOND_STR_GEI: + + for (i = 0; i < 2; i++) + { + while (isspace(*s)) s++; + if (*s != '{') + { + if (i == 0) goto COND_FAILED_CURLY_START; + expand_string_message = string_sprintf("missing 2nd string in {} " + "after \"%s\"", name); + return NULL; + } + sub[i] = expand_string_internal(s+1, TRUE, &s, yield == NULL); + if (sub[i] == NULL) return NULL; + if (*s++ != '}') goto COND_FAILED_CURLY_END; + + /* Convert to numerical if required; we know that the names of all the + conditions that compare numbers do not start with a letter. This just saves + checking for them individually. */ + + if (!isalpha(name[0])) + { + uschar *endptr; + num[i] = (int)Ustrtol((const uschar *)sub[i], &endptr, 10); + if (tolower(*endptr) == 'k') + { + num[i] *= 1024; + endptr++; + } + else if (tolower(*endptr) == 'm') + { + num[i] *= 1024*1024; + endptr++; + } + while (isspace(*endptr)) endptr++; + if (*endptr != 0) + { + expand_string_message = string_sprintf("\"%s\" is not a number", + sub[i]); + return NULL; + } + } + } + + /* Result not required */ + + if (yield == NULL) return s; + + /* Do an appropriate comparison */ + + switch(cond_type) + { + case ECOND_NUM_E: + case ECOND_NUM_EE: + *yield = (num[0] == num[1]) == testfor; + break; + + case ECOND_NUM_G: + *yield = (num[0] > num[1]) == testfor; + break; + + case ECOND_NUM_GE: + *yield = (num[0] >= num[1]) == testfor; + break; + + case ECOND_NUM_L: + *yield = (num[0] < num[1]) == testfor; + break; + + case ECOND_NUM_LE: + *yield = (num[0] <= num[1]) == testfor; + break; + + case ECOND_STR_LT: + *yield = (Ustrcmp(sub[0], sub[1]) < 0) == testfor; + break; + + case ECOND_STR_LTI: + *yield = (strcmpic(sub[0], sub[1]) < 0) == testfor; + break; + + case ECOND_STR_LE: + *yield = (Ustrcmp(sub[0], sub[1]) <= 0) == testfor; + break; + + case ECOND_STR_LEI: + *yield = (strcmpic(sub[0], sub[1]) <= 0) == testfor; + break; + + case ECOND_STR_EQ: + *yield = (Ustrcmp(sub[0], sub[1]) == 0) == testfor; + break; + + case ECOND_STR_EQI: + *yield = (strcmpic(sub[0], sub[1]) == 0) == testfor; + break; + + case ECOND_STR_GT: + *yield = (Ustrcmp(sub[0], sub[1]) > 0) == testfor; + break; + + case ECOND_STR_GTI: + *yield = (strcmpic(sub[0], sub[1]) > 0) == testfor; + break; + + case ECOND_STR_GE: + *yield = (Ustrcmp(sub[0], sub[1]) >= 0) == testfor; + break; + + case ECOND_STR_GEI: + *yield = (strcmpic(sub[0], sub[1]) >= 0) == testfor; + break; + + case ECOND_MATCH: /* Regular expression match */ + re = pcre_compile(CS sub[1], PCRE_COPT, (const char **)&rerror, &roffset, + NULL); + if (re == NULL) + { + expand_string_message = string_sprintf("regular expression error in " + "\"%s\": %s at offset %d", sub[1], rerror, roffset); + return NULL; + } + *yield = regex_match_and_setup(re, sub[0], 0, -1) == testfor; + break; + + case ECOND_MATCH_ADDRESS: /* Match in an address list */ + rc = match_address_list(sub[0], TRUE, FALSE, &(sub[1]), NULL, -1, 0, NULL); + goto MATCHED_SOMETHING; + + case ECOND_MATCH_DOMAIN: /* Match in a domain list */ + rc = match_isinlist(sub[0], &(sub[1]), 0, &domainlist_anchor, NULL, + MCL_DOMAIN + MCL_NOEXPAND, TRUE, NULL); + goto MATCHED_SOMETHING; + + case ECOND_MATCH_LOCAL_PART: + rc = match_isinlist(sub[0], &(sub[1]), 0, &localpartlist_anchor, NULL, + MCL_LOCALPART + MCL_NOEXPAND, TRUE, NULL); + /* Fall through */ + + MATCHED_SOMETHING: + switch(rc) + { + case OK: + *yield = testfor; + break; + + case FAIL: + *yield = !testfor; + break; + + case DEFER: + expand_string_message = string_sprintf("unable to complete match " + "against \"%s\": %s", sub[1], search_error_message); + return NULL; + } + + break; + + /* Various "encrypted" comparisons. If the second string starts with + "{" then an encryption type is given. Default to crypt() or crypt16() + (build-time choice). */ + + case ECOND_CRYPTEQ: + #ifndef SUPPORT_CRYPTEQ + goto COND_FAILED_NOT_COMPILED; + #else + if (strncmpic(sub[1], US"{md5}", 5) == 0) + { + int sublen = Ustrlen(sub[1]+5); + md5 base; + uschar digest[16]; + + md5_start(&base); + md5_end(&base, (uschar *)sub[0], Ustrlen(sub[0]), digest); + + /* If the length that we are comparing against is 24, the MD5 digest + is expressed as a base64 string. This is the way LDAP does it. However, + some other software uses a straightforward hex representation. We assume + this if the length is 32. Other lengths fail. */ + + if (sublen == 24) + { + uschar *coded = auth_b64encode((uschar *)digest, 16); + DEBUG(D_auth) debug_printf("crypteq: using MD5+B64 hashing\n" + " subject=%s\n crypted=%s\n", coded, sub[1]+5); + *yield = (Ustrcmp(coded, sub[1]+5) == 0) == testfor; + } + else if (sublen == 32) + { + int i; + uschar coded[36]; + for (i = 0; i < 16; i++) sprintf(CS (coded+2*i), "%02X", digest[i]); + coded[32] = 0; + DEBUG(D_auth) debug_printf("crypteq: using MD5+hex hashing\n" + " subject=%s\n crypted=%s\n", coded, sub[1]+5); + *yield = (strcmpic(coded, sub[1]+5) == 0) == testfor; + } + else + { + DEBUG(D_auth) debug_printf("crypteq: length for MD5 not 24 or 32: " + "fail\n crypted=%s\n", sub[1]+5); + *yield = !testfor; + } + } + + else if (strncmpic(sub[1], US"{sha1}", 6) == 0) + { + int sublen = Ustrlen(sub[1]+6); + sha1 base; + uschar digest[20]; + + sha1_start(&base); + sha1_end(&base, (uschar *)sub[0], Ustrlen(sub[0]), digest); + + /* If the length that we are comparing against is 28, assume the SHA1 + digest is expressed as a base64 string. If the length is 40, assume a + straightforward hex representation. Other lengths fail. */ + + if (sublen == 28) + { + uschar *coded = auth_b64encode((uschar *)digest, 20); + DEBUG(D_auth) debug_printf("crypteq: using SHA1+B64 hashing\n" + " subject=%s\n crypted=%s\n", coded, sub[1]+6); + *yield = (Ustrcmp(coded, sub[1]+6) == 0) == testfor; + } + else if (sublen == 40) + { + int i; + uschar coded[44]; + for (i = 0; i < 20; i++) sprintf(CS (coded+2*i), "%02X", digest[i]); + coded[40] = 0; + DEBUG(D_auth) debug_printf("crypteq: using SHA1+hex hashing\n" + " subject=%s\n crypted=%s\n", coded, sub[1]+6); + *yield = (strcmpic(coded, sub[1]+6) == 0) == testfor; + } + else + { + DEBUG(D_auth) debug_printf("crypteq: length for SHA-1 not 28 or 40: " + "fail\n crypted=%s\n", sub[1]+6); + *yield = !testfor; + } + } + + else /* {crypt} or {crypt16} and non-{ at start */ + { + int which = 0; + uschar *coded; + + if (strncmpic(sub[1], US"{crypt}", 7) == 0) + { + sub[1] += 7; + which = 1; + } + else if (strncmpic(sub[1], US"{crypt16}", 9) == 0) + { + sub[1] += 9; + which = 2; + } + else if (sub[1][0] == '{') + { + expand_string_message = string_sprintf("unknown encryption mechanism " + "in \"%s\"", sub[1]); + return NULL; + } + + switch(which) + { + case 0: coded = US DEFAULT_CRYPT(CS sub[0], CS sub[1]); break; + case 1: coded = US crypt(CS sub[0], CS sub[1]); break; + default: coded = US crypt16(CS sub[0], CS sub[1]); break; + } + + #define STR(s) # s + #define XSTR(s) STR(s) + DEBUG(D_auth) debug_printf("crypteq: using %s()\n" + " subject=%s\n crypted=%s\n", + (which == 0)? XSTR(DEFAULT_CRYPT) : (which == 1)? "crypt" : "crypt16", + coded, sub[1]); + #undef STR + #undef XSTR + + /* If the encrypted string contains fewer than two characters (for the + salt), force failure. Otherwise we get false positives: with an empty + string the yield of crypt() is an empty string! */ + + *yield = (Ustrlen(sub[1]) < 2)? !testfor : + (Ustrcmp(coded, sub[1]) == 0) == testfor; + } + break; + #endif /* SUPPORT_CRYPTEQ */ + } /* Switch for comparison conditions */ + + return s; /* End of comparison conditions */ + + + /* and/or: computes logical and/or of several conditions */ + + case ECOND_AND: + case ECOND_OR: + subcondptr = (yield == NULL)? NULL : &tempcond; + combined_cond = (cond_type == ECOND_AND); + + while (isspace(*s)) s++; + if (*s++ != '{') goto COND_FAILED_CURLY_START; + + for (;;) + { + while (isspace(*s)) s++; + if (*s == '}') break; + if (*s != '{') + { + expand_string_message = string_sprintf("each subcondition " + "inside an \"%s{...}\" condition must be in its own {}", name); + return NULL; + } + + s = eval_condition(s+1, subcondptr); + if (s == NULL) + { + expand_string_message = string_sprintf("%s inside \"%s{...}\" condition", + expand_string_message, name); + return NULL; + } + while (isspace(*s)) s++; + + if (*s++ != '}') + { + expand_string_message = string_sprintf("missing } at end of condition " + "inside \"%s\" group", name); + return NULL; + } + + if (yield != NULL) + { + if (cond_type == ECOND_AND) + { + combined_cond &= tempcond; + if (!combined_cond) subcondptr = NULL; /* once false, don't */ + } /* evaluate any more */ + else + { + combined_cond |= tempcond; + if (combined_cond) subcondptr = NULL; /* once true, don't */ + } /* evaluate any more */ + } + } + + if (yield != NULL) *yield = (combined_cond == testfor); + return ++s; + + + /* Unknown condition */ + + default: + expand_string_message = string_sprintf("unknown condition \"%s\"", name); + return NULL; + } /* End switch on condition type */ + +/* Missing braces at start and end of data */ + +COND_FAILED_CURLY_START: +expand_string_message = string_sprintf("missing { after \"%s\"", name); +return NULL; + +COND_FAILED_CURLY_END: +expand_string_message = string_sprintf("missing } at end of \"%s\" condition", + name); +return NULL; + +/* A condition requires code that is not compiled */ + +#if !defined(SUPPORT_PAM) || !defined(RADIUS_CONFIG_FILE) || \ + !defined(LOOKUP_LDAP) || !defined(CYRUS_PWCHECK_SOCKET) || \ + !defined(SUPPORT_CRYPTEQ) || !defined(CYRUS_SASLAUTHD_SOCKET) +COND_FAILED_NOT_COMPILED: +expand_string_message = string_sprintf("support for \"%s\" not compiled", + name); +return NULL; +#endif +} + + + + +/************************************************* +* Save numerical variables * +*************************************************/ + +/* This function is called from items such as "if" that want to preserve and +restore the numbered variables. + +Arguments: + save_expand_string points to an array of pointers to set + save_expand_nlength points to an array of ints for the lengths + +Returns: the value of expand max to save +*/ + +static int +save_expand_strings(uschar **save_expand_nstring, int *save_expand_nlength) +{ +int i; +for (i = 0; i <= expand_nmax; i++) + { + save_expand_nstring[i] = expand_nstring[i]; + save_expand_nlength[i] = expand_nlength[i]; + } +return expand_nmax; +} + + + +/************************************************* +* Restore numerical variables * +*************************************************/ + +/* This function restored saved values of numerical strings. + +Arguments: + save_expand_nmax the number of strings to restore + save_expand_string points to an array of pointers + save_expand_nlength points to an array of ints + +Returns: nothing +*/ + +static void +restore_expand_strings(int save_expand_nmax, uschar **save_expand_nstring, + int *save_expand_nlength) +{ +int i; +expand_nmax = save_expand_nmax; +for (i = 0; i <= expand_nmax; i++) + { + expand_nstring[i] = save_expand_nstring[i]; + expand_nlength[i] = save_expand_nlength[i]; + } +} + + + + + +/************************************************* +* Handle yes/no substrings * +*************************************************/ + +/* This function is used by ${if}, ${lookup} and ${extract} to handle the +alternative substrings that depend on whether or not the condition was true, +or the lookup or extraction succeeded. The substrings always have to be +expanded, to check their syntax, but "skipping" is set when the result is not +needed - this avoids unnecessary nested lookups. + +Arguments: + skipping TRUE if we were skipping when this item was reached + yes TRUE if the first string is to be used, else use the second + save_lookup a value to put back into lookup_value before the 2nd expansion + sptr points to the input string pointer + yieldptr points to the output string pointer + sizeptr points to the output string size + ptrptr points to the output string pointer + type "lookup" or "if" or "extract" or "run", for error message + +Returns: 0 OK; lookup_value has been reset to save_lookup + 1 expansion failed + 2 expansion failed because of bracketing error +*/ + +static int +process_yesno(BOOL skipping, BOOL yes, uschar *save_lookup, uschar **sptr, + uschar **yieldptr, int *sizeptr, int *ptrptr, uschar *type) +{ +int rc = 0; +uschar *s = *sptr; /* Local value */ +uschar *sub1, *sub2; + +/* If there are no following strings, we substitute the contents of $value for +lookups and for extractions in the success case. In the fail case, nothing is +substituted. In the case of "if", lack of following strings is an error. */ + +while (isspace(*s)) s++; +if (*s == '}') + { + if (type[0] == 'i') goto FAILED_CURLY; + if (yes && lookup_value != NULL) + *yieldptr = string_cat(*yieldptr, sizeptr, ptrptr, lookup_value, + Ustrlen(lookup_value)); + lookup_value = save_lookup; + s++; + goto RETURN; + } + +/* Expand the first substring. Forced failures are noticed only if we actually +want this string. Set skipping in the call in the fail case (this will always +be the case if we were already skipping). */ + +sub1 = expand_string_internal(s+1, TRUE, &s, !yes); +if (sub1 == NULL && (yes || !expand_string_forcedfail)) goto FAILED; +expand_string_forcedfail = FALSE; +if (*s++ != '}') goto FAILED_CURLY; + +/* If we want the first string, add it to the output */ + +if (yes) + *yieldptr = string_cat(*yieldptr, sizeptr, ptrptr, sub1, Ustrlen(sub1)); + +/* If this is called from a lookup or an extract, we want to restore $value to +what it was at the start of the item, so that it has this value during the +second string expansion. For the call from "if" to this function, save_lookup +is set to lookup_value, so that this statement does nothing. */ + +lookup_value = save_lookup; + +/* There now follows either another substring, or "fail", or nothing. This +time, forced failures are noticed only if we want the second string. We must +set skipping in the nested call if we don't want this string, or if we were +already skipping. */ + +while (isspace(*s)) s++; +if (*s == '{') + { + sub2 = expand_string_internal(s+1, TRUE, &s, yes || skipping); + if (sub2 == NULL && (!yes || !expand_string_forcedfail)) goto FAILED; + expand_string_forcedfail = FALSE; + if (*s++ != '}') goto FAILED_CURLY; + + /* If we want the second string, add it to the output */ + + if (!yes) + *yieldptr = string_cat(*yieldptr, sizeptr, ptrptr, sub2, Ustrlen(sub2)); + } + +/* If there is no second string, but the word "fail" is present when the use of +the second string is wanted, set a flag indicating it was a forced failure +rather than a syntactic error. Swallow the terminating } in case this is nested +inside another lookup or if or extract. */ + +else if (*s != '}') + { + uschar name[256]; + s = read_name(name, sizeof(name), s, US"_"); + if (Ustrcmp(name, "fail") == 0) + { + if (!yes && !skipping) + { + while (isspace(*s)) s++; + if (*s++ != '}') goto FAILED_CURLY; + expand_string_message = + string_sprintf("\"%s\" failed and \"fail\" requested", type); + expand_string_forcedfail = TRUE; + goto FAILED; + } + } + else + { + expand_string_message = + string_sprintf("syntax error in \"%s\" item - \"fail\" expected", type); + goto FAILED; + } + } + +/* All we have to do now is to check on the final closing brace. */ + +while (isspace(*s)) s++; +if (*s++ == '}') goto RETURN; + +/* Get here if there is a bracketing failure */ + +FAILED_CURLY: +rc++; + +/* Get here for other failures */ + +FAILED: +rc++; + +/* Update the input pointer value before returning */ + +RETURN: +*sptr = s; +return rc; +} + + + + + + +/************************************************* +* Handle MD5 or SHA-1 computation for HMAC * +*************************************************/ + +/* These are some wrapping functions that enable the HMAC code to be a bit +cleaner. A good compiler will spot the tail recursion. + +Arguments: + type HMAC_MD5 or HMAC_SHA1 + remaining are as for the cryptographic hash functions + +Returns: nothing +*/ + +static void +chash_start(int type, void *base) +{ +if (type == HMAC_MD5) + md5_start((md5 *)base); +else + sha1_start((sha1 *)base); +} + +static void +chash_mid(int type, void *base, uschar *string) +{ +if (type == HMAC_MD5) + md5_mid((md5 *)base, string); +else + sha1_mid((sha1 *)base, string); +} + +static void +chash_end(int type, void *base, uschar *string, int length, uschar *digest) +{ +if (type == HMAC_MD5) + md5_end((md5 *)base, string, length, digest); +else + sha1_end((sha1 *)base, string, length, digest); +} + + + + + +/************************************************* +* Join a file onto the output string * +*************************************************/ + +/* This is used for readfile and after a run expansion. It joins the contents +of a file onto the output string, globally replacing newlines with a given +string (optionally). The file is closed at the end. + +Arguments: + f the FILE + yield pointer to the expandable string + sizep pointer to the current size + ptrp pointer to the current position + eol newline replacement string, or NULL + +Returns: new value of string pointer +*/ + +static uschar * +cat_file(FILE *f, uschar *yield, int *sizep, int *ptrp, uschar *eol) +{ +int eollen; +uschar buffer[1024]; + +eollen = (eol == NULL)? 0 : Ustrlen(eol); + +while (Ufgets(buffer, sizeof(buffer), f) != NULL) + { + int len = Ustrlen(buffer); + if (eol != NULL && buffer[len-1] == '\n') len--; + yield = string_cat(yield, sizep, ptrp, buffer, len); + if (buffer[len] != 0) + yield = string_cat(yield, sizep, ptrp, eol, eollen); + } + +if (yield != NULL) yield[*ptrp] = 0; + +return yield; +} + + + + +/************************************************* +* Evaluate numeric expression * +*************************************************/ + +/* This is a set of mutually recursive functions that evaluate a simple +arithmetic expression involving only + - * / and parentheses. The only one that +is called from elsewhere is eval_expr, whose interface is: + +Arguments: + sptr pointer to the pointer to the string - gets updated + decimal TRUE if numbers are to be assumed decimal + error pointer to where to put an error message - must be NULL on input + endket TRUE if ')' must terminate - FALSE for external call + + +Returns: on success: the value of the expression, with *error still NULL + on failure: an undefined value, with *error = a message +*/ + +static int eval_sumterm(uschar **, BOOL, uschar **); + +static int +eval_expr(uschar **sptr, BOOL decimal, uschar **error, BOOL endket) +{ +uschar *s = *sptr; +int x = eval_sumterm(&s, decimal, error); +if (*error == NULL) + { + while (*s == '+' || *s == '-') + { + int op = *s++; + int y = eval_sumterm(&s, decimal, error); + if (*error != NULL) break; + if (op == '+') x += y; else x -= y; + } + if (*error == NULL) + { + if (endket) + { + if (*s != ')') + *error = US"expecting closing parenthesis"; + else + while (isspace(*(++s))); + } + else if (*s != 0) *error = US"expecting + or -"; + } + } + +*sptr = s; +return x; +} + +static int +eval_term(uschar **sptr, BOOL decimal, uschar **error) +{ +register int c; +int n; +uschar *s = *sptr; +while (isspace(*s)) s++; +c = *s; +if (isdigit(c) || ((c == '-' || c == '+') && isdigit(s[1]))) + { + int count; + (void)sscanf(CS s, (decimal? "%d%n" : "%i%n"), &n, &count); + s += count; + if (tolower(*s) == 'k') { n *= 1024; s++; } + else if (tolower(*s) == 'm') { n *= 1024*1024; s++; } + while (isspace (*s)) s++; + } +else if (c == '(') + { + s++; + n = eval_expr(&s, decimal, error, 1); + } +else + { + *error = US"expecting number or opening parenthesis"; + n = 0; + } +*sptr = s; +return n; +} + +static int eval_sumterm(uschar **sptr, BOOL decimal, uschar **error) +{ +uschar *s = *sptr; +int x = eval_term(&s, decimal, error); +if (*error == NULL) + { + while (*s == '*' || *s == '/') + { + int op = *s++; + int y = eval_term(&s, decimal, error); + if (*error != NULL) break; + if (op == '*') x *= y; else x /= y; + } + } +*sptr = s; +return x; +} + + + + +/************************************************* +* Expand string * +*************************************************/ + +/* Returns either an unchanged string, or the expanded string in stacking pool +store. Interpreted sequences are: + + \... normal escaping rules + $name substitutes the variable + ${name} ditto + ${op:string} operates on the expanded string value + ${item{arg1}{arg2}...} expands the args and then does the business + some literal args are not enclosed in {} + +There are now far too many operators and item types to make it worth listing +them here in detail any more. + +We use an internal routine recursively to handle embedded substrings. The +external function follows. The yield is NULL if the expansion failed, and there +are two cases: if something collapsed syntactically, or if "fail" was given +as the action on a lookup failure. These can be distinguised by looking at the +variable expand_string_forcedfail, which is TRUE in the latter case. + +The skipping flag is set true when expanding a substring that isn't actually +going to be used (after "if" or "lookup") and it prevents lookups from +happening lower down. + +Store usage: At start, a store block of the length of the input plus 64 +is obtained. This is expanded as necessary by string_cat(), which might have to +get a new block, or might be able to expand the original. At the end of the +function we can release any store above that portion of the yield block that +was actually used. In many cases this will be optimal. + +However: if the first item in the expansion is a variable name or header name, +we reset the store before processing it; if the result is in fresh store, we +use that without copying. This is helpful for expanding strings like +$message_headers which can get very long. + +Arguments: + string the string to be expanded + ket_ends true if expansion is to stop at } + left if not NULL, a pointer to the first character after the + expansion is placed here (typically used with ket_ends) + skipping TRUE for recursive calls when the value isn't actually going + to be used (to allow for optimisation) + +Returns: NULL if expansion fails: + expand_string_forcedfail is set TRUE if failure was forced + expand_string_message contains a textual error message + a pointer to the expanded string on success +*/ + +static uschar * +expand_string_internal(uschar *string, BOOL ket_ends, uschar **left, + BOOL skipping) +{ +int ptr = 0; +int size = Ustrlen(string)+ 64; +int item_type; +uschar *yield = store_get(size); +uschar *s = string; +uschar *save_expand_nstring[EXPAND_MAXN+1]; +int save_expand_nlength[EXPAND_MAXN+1]; + +expand_string_forcedfail = FALSE; +expand_string_message = US""; + +while (*s != 0) + { + uschar *value; + uschar name[256]; + + /* \ escapes the next character, which must exist, or else + the expansion fails. There's a special escape, \N, which causes + copying of the subject verbatim up to the next \N. Otherwise, + the escapes are the standard set. */ + + if (*s == '\\') + { + if (s[1] == 0) + { + expand_string_message = US"\\ at end of string"; + goto EXPAND_FAILED; + } + + if (s[1] == 'N') + { + uschar *t = s + 2; + for (s = t; *s != 0; s++) if (*s == '\\' && s[1] == 'N') break; + yield = string_cat(yield, &size, &ptr, t, s - t); + if (*s != 0) s += 2; + } + + else + { + uschar ch[1]; + ch[0] = string_interpret_escape(&s); + s++; + yield = string_cat(yield, &size, &ptr, ch, 1); + } + + continue; + } + + /* Anything other than $ is just copied verbatim, unless we are + looking for a terminating } character. */ + + if (ket_ends && *s == '}') break; + + if (*s != '$') + { + yield = string_cat(yield, &size, &ptr, s++, 1); + continue; + } + + /* No { after the $ - must be a plain name or a number for string + match variable. There has to be a fudge for variables that are the + names of header fields preceded by "$header_" because header field + names can contain any printing characters except space and colon. + For those that don't like typing this much, "$h_" is a synonym for + "$header_". A non-existent header yields a NULL value; nothing is + inserted. */ + + if (isalpha((*(++s)))) + { + int len; + int newsize = 0; + + s = read_name(name, sizeof(name), s, US"_"); + + /* If this is the first thing to be expanded, release the pre-allocated + buffer. */ + + if (ptr == 0 && yield != NULL) + { + store_reset(yield); + yield = NULL; + size = 0; + } + + /* Header */ + + if (Ustrncmp(name, "h_", 2) == 0 || + Ustrncmp(name, "rh_", 3) == 0 || + Ustrncmp(name, "bh_", 3) == 0 || + Ustrncmp(name, "header_", 7) == 0 || + Ustrncmp(name, "rheader_", 8) == 0 || + Ustrncmp(name, "bheader_", 8) == 0) + { + BOOL want_raw = (name[0] == 'r')? TRUE : FALSE; + uschar *charset = (name[0] == 'b')? NULL : headers_charset; + s = read_header_name(name, sizeof(name), s); + value = find_header(name, FALSE, &newsize, want_raw, charset); + + /* If we didn't find the header, and the header contains a closing brace + characters, this may be a user error where the terminating colon + has been omitted. Set a flag to adjust the error message in this case. + But there is no error here - nothing gets inserted. */ + + if (value == NULL) + { + if (Ustrchr(name, '}') != NULL) malformed_header = TRUE; + continue; + } + } + + /* Variable */ + + else + { + value = find_variable(name, FALSE, skipping, &newsize); + if (value == NULL) + { + expand_string_message = + string_sprintf("unknown variable name \"%s\"", name); + goto EXPAND_FAILED; + } + } + + /* If the data is known to be in a new buffer, newsize will be set to the + size of that buffer. If this is the first thing in an expansion string, + yield will be NULL; just point it at the new store instead of copying. Many + expansion strings contain just one reference, so this is a useful + optimization, especially for humungous headers. */ + + len = Ustrlen(value); + if (yield == NULL && newsize != 0) + { + yield = value; + size = newsize; + ptr = len; + } + else yield = string_cat(yield, &size, &ptr, value, len); + + continue; + } + + if (isdigit(*s)) + { + int n; + s = read_number(&n, s); + if (n >= 0 && n <= expand_nmax) + yield = string_cat(yield, &size, &ptr, expand_nstring[n], + expand_nlength[n]); + continue; + } + + /* Otherwise, if there's no '{' after $ it's an error. */ + + if (*s != '{') + { + expand_string_message = US"$ not followed by letter, digit, or {"; + goto EXPAND_FAILED; + } + + /* After { there can be various things, but they all start with + an initial word, except for a number for a string match variable. */ + + if (isdigit((*(++s)))) + { + int n; + s = read_number(&n, s); + if (*s++ != '}') + { + expand_string_message = US"} expected after number"; + goto EXPAND_FAILED; + } + if (n >= 0 && n <= expand_nmax) + yield = string_cat(yield, &size, &ptr, expand_nstring[n], + expand_nlength[n]); + continue; + } + + if (!isalpha(*s)) + { + expand_string_message = US"letter or digit expected after ${"; + goto EXPAND_FAILED; + } + + /* Allow "-" in names to cater for substrings with negative + arguments. Since we are checking for known names after { this is + OK. */ + + s = read_name(name, sizeof(name), s, US"_-"); + item_type = chop_match(name, item_table, sizeof(item_table)/sizeof(uschar *)); + + switch(item_type) + { + /* Handle conditionals - preserve the values of the numerical expansion + variables in case they get changed by a regular expression match in the + condition. If not, they retain their external settings. At the end + of this "if" section, they get restored to their previous values. */ + + case EITEM_IF: + { + BOOL cond = FALSE; + uschar *next_s; + int save_expand_nmax = + save_expand_strings(save_expand_nstring, save_expand_nlength); + + while (isspace(*s)) s++; + next_s = eval_condition(s, skipping? NULL : &cond); + if (next_s == NULL) goto EXPAND_FAILED; /* message already set */ + + DEBUG(D_expand) + debug_printf("condition: %.*s\n result: %s\n", (int)(next_s - s), s, + cond? "true" : "false"); + + s = next_s; + + /* The handling of "yes" and "no" result strings is now in a separate + function that is also used by ${lookup} and ${extract} and ${run}. */ + + switch(process_yesno( + skipping, /* were previously skipping */ + cond, /* success/failure indicator */ + lookup_value, /* value to reset for string2 */ + &s, /* input pointer */ + &yield, /* output pointer */ + &size, /* output size */ + &ptr, /* output current point */ + US"if")) /* condition type */ + { + case 1: goto EXPAND_FAILED; /* when all is well, the */ + case 2: goto EXPAND_FAILED_CURLY; /* returned value is 0 */ + } + + /* Restore external setting of expansion variables for continuation + at this level. */ + + restore_expand_strings(save_expand_nmax, save_expand_nstring, + save_expand_nlength); + continue; + } + + /* Handle database lookups unless locked out. If "skipping" is TRUE, we are + expanding an internal string that isn't actually going to be used. All we + need to do is check the syntax, so don't do a lookup at all. Preserve the + values of the numerical expansion variables in case they get changed by a + partial lookup. If not, they retain their external settings. At the end + of this "lookup" section, they get restored to their previous values. */ + + case EITEM_LOOKUP: + { + int stype, partial, affixlen, starflags; + int expand_setup = 0; + int nameptr = 0; + uschar *key, *filename, *affix; + uschar *save_lookup_value = lookup_value; + int save_expand_nmax = + save_expand_strings(save_expand_nstring, save_expand_nlength); + + if ((expand_forbid & RDO_LOOKUP) != 0) + { + expand_string_message = US"lookup expansions are not permitted"; + goto EXPAND_FAILED; + } + + /* Get the key we are to look up for single-key+file style lookups. + Otherwise set the key NULL pro-tem. */ + + while (isspace(*s)) s++; + if (*s == '{') + { + key = expand_string_internal(s+1, TRUE, &s, skipping); + if (key == NULL) goto EXPAND_FAILED; + if (*s++ != '}') goto EXPAND_FAILED_CURLY; + while (isspace(*s)) s++; + } + else key = NULL; + + /* Find out the type of database */ + + if (!isalpha(*s)) + { + expand_string_message = US"missing lookup type"; + goto EXPAND_FAILED; + } + + /* The type is a string that may contain special characters of various + kinds. Allow everything except space or { to appear; the actual content + is checked by search_findtype_partial. */ + + while (*s != 0 && *s != '{' && !isspace(*s)) + { + if (nameptr < sizeof(name) - 1) name[nameptr++] = *s; + s++; + } + name[nameptr] = 0; + while (isspace(*s)) s++; + + /* Now check for the individual search type and any partial or default + options. Only those types that are actually in the binary are valid. */ + + stype = search_findtype_partial(name, &partial, &affix, &affixlen, + &starflags); + if (stype < 0) + { + expand_string_message = search_error_message; + goto EXPAND_FAILED; + } + + /* Check that a key was provided for those lookup types that need it, + and was not supplied for those that use the query style. */ + + if (!mac_islookup(stype, lookup_querystyle)) + { + if (key == NULL) + { + expand_string_message = string_sprintf("missing {key} for single-" + "key \"%s\" lookup", name); + goto EXPAND_FAILED; + } + } + else + { + if (key != NULL) + { + expand_string_message = string_sprintf("a single key was given for " + "lookup type \"%s\", which is not a single-key lookup type", name); + goto EXPAND_FAILED; + } + } + + /* Get the next string in brackets and expand it. It is the file name for + single-key+file lookups, and the whole query otherwise. */ + + if (*s != '{') goto EXPAND_FAILED_CURLY; + filename = expand_string_internal(s+1, TRUE, &s, skipping); + if (filename == NULL) goto EXPAND_FAILED; + if (*s++ != '}') goto EXPAND_FAILED_CURLY; + while (isspace(*s)) s++; + + /* If this isn't a single-key+file lookup, re-arrange the variables + to be appropriate for the search_ functions. */ + + if (key == NULL) + { + key = filename; + filename = NULL; + } + + /* If skipping, don't do the next bit - just lookup_value == NULL, as if + the entry was not found. Note that there is no search_close() function. + Files are left open in case of re-use. At suitable places in higher logic, + search_tidyup() is called to tidy all open files. This can save opening + the same file several times. However, files may also get closed when + others are opened, if too many are open at once. The rule is that a + handle should not be used after a second search_open(). + + Request that a partial search sets up $1 and maybe $2 by passing + expand_setup containing zero. If its value changes, reset expand_nmax, + since new variables will have been set. Note that at the end of this + "lookup" section, the old numeric variables are restored. */ + + if (skipping) + lookup_value = NULL; + else + { + void *handle = search_open(filename, stype, 0, NULL, NULL); + if (handle == NULL) + { + expand_string_message = search_error_message; + goto EXPAND_FAILED; + } + lookup_value = search_find(handle, filename, key, partial, affix, + affixlen, starflags, &expand_setup); + if (search_find_defer) + { + expand_string_message = + string_sprintf("lookup of \"%s\" gave DEFER: %s", key, + search_error_message); + goto EXPAND_FAILED; + } + if (expand_setup > 0) expand_nmax = expand_setup; + } + + /* The handling of "yes" and "no" result strings is now in a separate + function that is also used by ${if} and ${extract}. */ + + switch(process_yesno( + skipping, /* were previously skipping */ + lookup_value != NULL, /* success/failure indicator */ + save_lookup_value, /* value to reset for string2 */ + &s, /* input pointer */ + &yield, /* output pointer */ + &size, /* output size */ + &ptr, /* output current point */ + US"lookup")) /* condition type */ + { + case 1: goto EXPAND_FAILED; /* when all is well, the */ + case 2: goto EXPAND_FAILED_CURLY; /* returned value is 0 */ + } + + /* Restore external setting of expansion variables for carrying on + at this level, and continue. */ + + restore_expand_strings(save_expand_nmax, save_expand_nstring, + save_expand_nlength); + continue; + } + + /* If Perl support is configured, handle calling embedded perl subroutines, + unless locked out at this time. Syntax is ${perl{sub}} or ${perl{sub}{arg}} + or ${perl{sub}{arg1}{arg2}} or up to a maximum of EXIM_PERL_MAX_ARGS + arguments (defined below). */ + + #ifdef EXIM_PERL + #define EXIM_PERL_MAX_ARGS 8 + + case EITEM_PERL: + { + uschar *sub_arg[EXIM_PERL_MAX_ARGS + 2]; + uschar *new_yield; + + if ((expand_forbid & RDO_PERL) != 0) + { + expand_string_message = US"Perl calls are not permitted"; + goto EXPAND_FAILED; + } + + switch(read_subs(sub_arg, EXIM_PERL_MAX_ARGS + 1, 1, &s, skipping, TRUE, + US"perl")) + { + case 1: goto EXPAND_FAILED_CURLY; + case 2: + case 3: goto EXPAND_FAILED; + } + + /* If skipping, we don't actually do anything */ + + if (skipping) continue; + + /* Start the interpreter if necessary */ + + if (!opt_perl_started) + { + uschar *initerror; + if (opt_perl_startup == NULL) + { + expand_string_message = US"A setting of perl_startup is needed when " + "using the Perl interpreter"; + goto EXPAND_FAILED; + } + DEBUG(D_any) debug_printf("Starting Perl interpreter\n"); + initerror = init_perl(opt_perl_startup); + if (initerror != NULL) + { + expand_string_message = + string_sprintf("error in perl_startup code: %s\n", initerror); + goto EXPAND_FAILED; + } + opt_perl_started = TRUE; + } + + /* Call the function */ + + sub_arg[EXIM_PERL_MAX_ARGS + 1] = NULL; + new_yield = call_perl_cat(yield, &size, &ptr, &expand_string_message, + sub_arg[0], sub_arg + 1); + + /* NULL yield indicates failure; if the message pointer has been set to + NULL, the yield was undef, indicating a forced failure. Otherwise the + message will indicate some kind of Perl error. */ + + if (new_yield == NULL) + { + if (expand_string_message == NULL) + { + expand_string_message = + string_sprintf("Perl subroutine \"%s\" returned undef to force " + "failure", sub_arg[0]); + expand_string_forcedfail = TRUE; + } + goto EXPAND_FAILED; + } + + /* Yield succeeded. Ensure forcedfail is unset, just in case it got + set during a callback from Perl. */ + + expand_string_forcedfail = FALSE; + yield = new_yield; + continue; + } + #endif /* EXIM_PERL */ + + /* Handle "readfile" to insert an entire file */ + + case EITEM_READFILE: + { + FILE *f; + uschar *sub_arg[2]; + + if ((expand_forbid & RDO_READFILE) != 0) + { + expand_string_message = US"file insertions are not permitted"; + goto EXPAND_FAILED; + } + + switch(read_subs(sub_arg, 2, 1, &s, skipping, TRUE, US"readfile")) + { + case 1: goto EXPAND_FAILED_CURLY; + case 2: + case 3: goto EXPAND_FAILED; + } + + /* If skipping, we don't actually do anything */ + + if (skipping) continue; + + /* Open the file and read it */ + + f = Ufopen(sub_arg[0], "rb"); + if (f == NULL) + { + expand_string_message = string_open_failed(errno, "%s", sub_arg[0]); + goto EXPAND_FAILED; + } + + yield = cat_file(f, yield, &size, &ptr, sub_arg[1]); + fclose(f); + continue; + } + + /* Handle "readsocket" to insert data from a Unix domain socket */ + + case EITEM_READSOCK: + { + int fd; + int timeout = 5; + int save_ptr = ptr; + FILE *f; + struct sockaddr_un sockun; /* don't call this "sun" ! */ + uschar *arg; + uschar *sub_arg[4]; + + if ((expand_forbid & RDO_READSOCK) != 0) + { + expand_string_message = US"socket insertions are not permitted"; + goto EXPAND_FAILED; + } + + /* Read up to 4 arguments, but don't do the end of item check afterwards, + because there may be a string for expansion on failure. */ + + switch(read_subs(sub_arg, 4, 2, &s, skipping, FALSE, US"readsocket")) + { + case 1: goto EXPAND_FAILED_CURLY; + case 2: /* Won't occur: no end check */ + case 3: goto EXPAND_FAILED; + } + + /* Sort out timeout, if given */ + + if (sub_arg[2] != NULL) + { + timeout = readconf_readtime(sub_arg[2], 0, FALSE); + if (timeout < 0) + { + expand_string_message = string_sprintf("bad time value %s", + sub_arg[2]); + goto EXPAND_FAILED; + } + } + else sub_arg[3] = NULL; /* No eol if no timeout */ + + /* If skipping, we don't actually do anything */ + + if (!skipping) + { + /* Make a connection to the socket */ + + if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) + { + expand_string_message = string_sprintf("failed to create socket: %s", + strerror(errno)); + goto SOCK_FAIL; + } + + sockun.sun_family = AF_UNIX; + sprintf(sockun.sun_path, "%.*s", (int)(sizeof(sockun.sun_path)-1), + sub_arg[0]); + if(connect(fd, (struct sockaddr *)(&sockun), sizeof(sockun)) == -1) + { + expand_string_message = string_sprintf("failed to connect to socket " + "%s: %s", sub_arg[0], strerror(errno)); + goto SOCK_FAIL; + } + DEBUG(D_expand) debug_printf("connected to socket %s\n", sub_arg[0]); + + /* Write the request string, if not empty */ + + if (sub_arg[1][0] != 0) + { + int len = Ustrlen(sub_arg[1]); + DEBUG(D_expand) debug_printf("writing \"%s\" to socket\n", + sub_arg[1]); + if (write(fd, sub_arg[1], len) != len) + { + expand_string_message = string_sprintf("request write to socket " + "failed: %s", strerror(errno)); + goto SOCK_FAIL; + } + } + + /* Now we need to read from the socket, under a timeout. The function + that reads a file can be used. */ + + f = fdopen(fd, "rb"); + sigalrm_seen = FALSE; + alarm(timeout); + yield = cat_file(f, yield, &size, &ptr, sub_arg[3]); + alarm(0); + fclose(f); + + /* After a timeout, we restore the pointer in the result, that is, + make sure we add nothing from the socket. */ + + if (sigalrm_seen) + { + ptr = save_ptr; + expand_string_message = US"socket read timed out"; + goto SOCK_FAIL; + } + } + + /* The whole thing has worked (or we were skipping). If there is a + failure string following, we need to skip it. */ + + if (*s == '{') + { + if (expand_string_internal(s+1, TRUE, &s, TRUE) == NULL) + goto EXPAND_FAILED; + if (*s++ != '}') goto EXPAND_FAILED_CURLY; + while (isspace(*s)) s++; + } + if (*s++ != '}') goto EXPAND_FAILED_CURLY; + continue; + + /* Come here on failure to create socket, connect socket, write to the + socket, or timeout on reading. If another substring follows, expand and + use it. Otherwise, those conditions give expand errors. */ + + SOCK_FAIL: + if (*s != '{') goto EXPAND_FAILED; + DEBUG(D_any) debug_printf("%s\n", expand_string_message); + arg = expand_string_internal(s+1, TRUE, &s, FALSE); + if (arg == NULL) goto EXPAND_FAILED; + yield = string_cat(yield, &size, &ptr, arg, Ustrlen(arg)); + if (*s++ != '}') goto EXPAND_FAILED_CURLY; + while (isspace(*s)) s++; + if (*s++ != '}') goto EXPAND_FAILED_CURLY; + continue; + } + + /* Handle "run" to execute a program. */ + + case EITEM_RUN: + { + FILE *f; + uschar *old_lookup_value = NULL; + uschar *arg; + uschar **argv; + pid_t pid; + int fd_in, fd_out; + int lsize = 0; + int lptr = 0; + + if ((expand_forbid & RDO_RUN) != 0) + { + expand_string_message = US"running a command is not permitted"; + goto EXPAND_FAILED; + } + + while (isspace(*s)) s++; + if (*s != '{') goto EXPAND_FAILED_CURLY; + arg = expand_string_internal(s+1, TRUE, &s, skipping); + if (arg == NULL) goto EXPAND_FAILED; + while (isspace(*s)) s++; + if (*s++ != '}') goto EXPAND_FAILED_CURLY; + + if (skipping) /* Just pretend it worked when we're skipping */ + { + runrc = 0; + } + else + { + if (!transport_set_up_command(&argv, /* anchor for arg list */ + arg, /* raw command */ + FALSE, /* don't expand the arguments */ + 0, /* not relevant when... */ + NULL, /* no transporting address */ + US"${run} expansion", /* for error messages */ + &expand_string_message)) /* where to put error message */ + { + goto EXPAND_FAILED; + } + + /* Create the child process, making it a group leader. */ + + pid = child_open(argv, NULL, 0077, &fd_in, &fd_out, TRUE); + + if (pid < 0) + { + expand_string_message = + string_sprintf("couldn't create child process: %s", strerror(errno)); + goto EXPAND_FAILED; + } + + /* Nothing is written to the standard input. */ + + close(fd_in); + + /* Wait for the process to finish, applying the timeout, and inspect its + return code for serious disasters. Simple non-zero returns are passed on. + */ + + if ((runrc = child_close(pid, 60)) < 0) + { + if (runrc == -256) + { + expand_string_message = string_sprintf("command timed out"); + killpg(pid, SIGKILL); /* Kill the whole process group */ + } + + else if (runrc == -257) + expand_string_message = string_sprintf("wait() failed: %s", + strerror(errno)); + + else + expand_string_message = string_sprintf("command killed by signal %d", + -runrc); + + goto EXPAND_FAILED; + } + + /* Read the pipe to get the command's output into $value (which is kept + in lookup_value). */ + + f = fdopen(fd_out, "rb"); + old_lookup_value = lookup_value; + lookup_value = NULL; + lookup_value = cat_file(f, lookup_value, &lsize, &lptr, NULL); + fclose(f); + } + + /* Process the yes/no strings */ + + switch(process_yesno( + skipping, /* were previously skipping */ + runrc == 0, /* success/failure indicator */ + old_lookup_value, /* value to reset for string2 */ + &s, /* input pointer */ + &yield, /* output pointer */ + &size, /* output size */ + &ptr, /* output current point */ + US"run")) /* condition type */ + { + case 1: goto EXPAND_FAILED; /* when all is well, the */ + case 2: goto EXPAND_FAILED_CURLY; /* returned value is 0 */ + } + + continue; + } + + /* Handle character translation for "tr" */ + + case EITEM_TR: + { + int oldptr = ptr; + int o2m; + uschar *sub[3]; + + switch(read_subs(sub, 3, 3, &s, skipping, TRUE, US"tr")) + { + case 1: goto EXPAND_FAILED_CURLY; + case 2: + case 3: goto EXPAND_FAILED; + } + + yield = string_cat(yield, &size, &ptr, sub[0], Ustrlen(sub[0])); + o2m = Ustrlen(sub[2]) - 1; + + if (o2m >= 0) for (; oldptr < ptr; oldptr++) + { + uschar *m = Ustrrchr(sub[1], yield[oldptr]); + if (m != NULL) + { + int o = m - sub[1]; + yield[oldptr] = sub[2][(o < o2m)? o : o2m]; + } + } + + continue; + } + + /* Handle "hash", "length", "nhash", and "substr" when they are given with + expanded arguments. */ + + case EITEM_HASH: + case EITEM_LENGTH: + case EITEM_NHASH: + case EITEM_SUBSTR: + { + int i; + int len; + uschar *ret; + int val[2] = { 0, -1 }; + uschar *sub[3]; + + /* "length" takes only 2 arguments whereas the others take 2 or 3. + Ensure that sub[2] is set in the ${length case. */ + + sub[2] = NULL; + switch(read_subs(sub, (item_type == EITEM_LENGTH)? 2:3, 2, &s, skipping, + TRUE, name)) + { + case 1: goto EXPAND_FAILED_CURLY; + case 2: + case 3: goto EXPAND_FAILED; + } + + /* Juggle the arguments if there are only two of them: always move the + string to the last position and make ${length{n}{str}} equivalent to + ${substr{0}{n}{str}}. See the defaults for val[] above. */ + + if (sub[2] == NULL) + { + sub[2] = sub[1]; + sub[1] = NULL; + if (item_type == EITEM_LENGTH) + { + sub[1] = sub[0]; + sub[0] = NULL; + } + } + + for (i = 0; i < 2; i++) + { + if (sub[i] == NULL) continue; + val[i] = (int)Ustrtol(sub[i], &ret, 10); + if (*ret != 0 || (i != 0 && val[i] < 0)) + { + expand_string_message = string_sprintf("\"%s\" is not a%s number " + "(in \"%s\" expansion)", sub[i], (i != 0)? " positive" : "", name); + goto EXPAND_FAILED; + } + } + + ret = + (item_type == EITEM_HASH)? + compute_hash(sub[2], val[0], val[1], &len) : + (item_type == EITEM_NHASH)? + compute_nhash(sub[2], val[0], val[1], &len) : + extract_substr(sub[2], val[0], val[1], &len); + + if (ret == NULL) goto EXPAND_FAILED; + yield = string_cat(yield, &size, &ptr, ret, len); + continue; + } + + /* Handle HMAC computation: ${hmac{}{}{}} + This code originally contributed by Steve Haslam. It currently supports + the use of MD5 and SHA-1 hashes. + + We need some workspace that is large enough to handle all the supported + hash types. Use macros to set the sizes rather than be too elaborate. */ + + #define MAX_HASHLEN 20 + #define MAX_HASHBLOCKLEN 64 + + case EITEM_HMAC: + { + uschar *sub[3]; + md5 md5_base; + sha1 sha1_base; + void *use_base; + int type, i; + int hashlen; /* Number of octets for the hash algorithm's output */ + int hashblocklen; /* Number of octets the hash algorithm processes */ + uschar *keyptr, *p; + unsigned int keylen; + + uschar keyhash[MAX_HASHLEN]; + uschar innerhash[MAX_HASHLEN]; + uschar finalhash[MAX_HASHLEN]; + uschar finalhash_hex[2*MAX_HASHLEN]; + uschar innerkey[MAX_HASHBLOCKLEN]; + uschar outerkey[MAX_HASHBLOCKLEN]; + + switch (read_subs(sub, 3, 3, &s, skipping, TRUE, name)) + { + case 1: goto EXPAND_FAILED_CURLY; + case 2: + case 3: goto EXPAND_FAILED; + } + + if (Ustrcmp(sub[0], "md5") == 0) + { + type = HMAC_MD5; + use_base = &md5_base; + hashlen = 16; + hashblocklen = 64; + } + else if (Ustrcmp(sub[0], "sha1") == 0) + { + type = HMAC_SHA1; + use_base = &sha1_base; + hashlen = 20; + hashblocklen = 64; + } + else + { + expand_string_message = + string_sprintf("hmac algorithm \"%s\" is not recognised", sub[0]); + goto EXPAND_FAILED; + } + + keyptr = sub[1]; + keylen = Ustrlen(keyptr); + + /* If the key is longer than the hash block length, then hash the key + first */ + + if (keylen > hashblocklen) + { + chash_start(type, use_base); + chash_end(type, use_base, keyptr, keylen, keyhash); + keyptr = keyhash; + keylen = hashlen; + } + + /* Now make the inner and outer key values */ + + memset(innerkey, 0x36, hashblocklen); + memset(outerkey, 0x5c, hashblocklen); + + for (i = 0; i < keylen; i++) + { + innerkey[i] ^= keyptr[i]; + outerkey[i] ^= keyptr[i]; + } + + /* Now do the hashes */ + + chash_start(type, use_base); + chash_mid(type, use_base, innerkey); + chash_end(type, use_base, sub[2], Ustrlen(sub[2]), innerhash); + + chash_start(type, use_base); + chash_mid(type, use_base, outerkey); + chash_end(type, use_base, innerhash, hashlen, finalhash); + + /* Encode the final hash as a hex string */ + + p = finalhash_hex; + for (i = 0; i < hashlen; i++) + { + *p++ = hex_digits[(finalhash[i] & 0xf0) >> 4]; + *p++ = hex_digits[finalhash[i] & 0x0f]; + } + + DEBUG(D_any) debug_printf("HMAC[%s](%.*s,%.*s)=%.*s\n", sub[0], + (int)keylen, keyptr, Ustrlen(sub[2]), sub[2], hashlen*2, finalhash_hex); + + yield = string_cat(yield, &size, &ptr, finalhash_hex, hashlen*2); + } + + continue; + + /* Handle global substitution for "sg" - like Perl's s/xxx/yyy/g operator. + We have to save the numerical variables and restore them afterwards. */ + + case EITEM_SG: + { + const pcre *re; + int moffset, moffsetextra, slen; + int roffset; + int emptyopt; + const uschar *rerror; + uschar *subject; + uschar *sub[3]; + int save_expand_nmax = + save_expand_strings(save_expand_nstring, save_expand_nlength); + + switch(read_subs(sub, 3, 3, &s, skipping, TRUE, US"sg")) + { + case 1: goto EXPAND_FAILED_CURLY; + case 2: + case 3: goto EXPAND_FAILED; + } + + /* Compile the regular expression */ + + re = pcre_compile(CS sub[1], PCRE_COPT, (const char **)&rerror, &roffset, + NULL); + + if (re == NULL) + { + expand_string_message = string_sprintf("regular expression error in " + "\"%s\": %s at offset %d", sub[1], rerror, roffset); + goto EXPAND_FAILED; + } + + /* Now run a loop to do the substitutions as often as necessary. It ends + when there are no more matches. Take care over matches of the null string; + do the same thing as Perl does. */ + + subject = sub[0]; + slen = Ustrlen(sub[0]); + moffset = moffsetextra = 0; + emptyopt = 0; + + for (;;) + { + int ovector[3*(EXPAND_MAXN+1)]; + int n = pcre_exec(re, NULL, CS subject, slen, moffset + moffsetextra, + PCRE_EOPT | emptyopt, ovector, sizeof(ovector)/sizeof(int)); + int nn; + uschar *insert; + + /* No match - if we previously set PCRE_NOTEMPTY after a null match, this + is not necessarily the end. We want to repeat the match from one + character further along, but leaving the basic offset the same (for + copying below). We can't be at the end of the string - that was checked + before setting PCRE_NOTEMPTY. If PCRE_NOTEMPTY is not set, we are + finished; copy the remaining string and end the loop. */ + + if (n < 0) + { + if (emptyopt != 0) + { + moffsetextra = 1; + emptyopt = 0; + continue; + } + yield = string_cat(yield, &size, &ptr, subject+moffset, slen-moffset); + break; + } + + /* Match - set up for expanding the replacement. */ + + if (n == 0) n = EXPAND_MAXN + 1; + expand_nmax = 0; + for (nn = 0; nn < n*2; nn += 2) + { + expand_nstring[expand_nmax] = subject + ovector[nn]; + expand_nlength[expand_nmax++] = ovector[nn+1] - ovector[nn]; + } + expand_nmax--; + + /* Copy the characters before the match, plus the expanded insertion. */ + + yield = string_cat(yield, &size, &ptr, subject + moffset, + ovector[0] - moffset); + insert = expand_string(sub[2]); + if (insert == NULL) goto EXPAND_FAILED; + yield = string_cat(yield, &size, &ptr, insert, Ustrlen(insert)); + + moffset = ovector[1]; + moffsetextra = 0; + emptyopt = 0; + + /* If we have matched an empty string, first check to see if we are at + the end of the subject. If so, the loop is over. Otherwise, mimic + what Perl's /g options does. This turns out to be rather cunning. First + we set PCRE_NOTEMPTY and PCRE_ANCHORED and try the match a non-empty + string at the same point. If this fails (picked up above) we advance to + the next character. */ + + if (ovector[0] == ovector[1]) + { + if (ovector[0] == slen) break; + emptyopt = PCRE_NOTEMPTY | PCRE_ANCHORED; + } + } + + /* All done - restore numerical variables. */ + + restore_expand_strings(save_expand_nmax, save_expand_nstring, + save_expand_nlength); + continue; + } + + /* Handle keyed and numbered substring extraction. If the first argument + consists entirely of digits, then a numerical extraction is assumed. */ + + case EITEM_EXTRACT: + { + int i; + int j = 2; + int field_number = 1; + BOOL field_number_set = FALSE; + uschar *save_lookup_value = lookup_value; + uschar *sub[3]; + int save_expand_nmax = + save_expand_strings(save_expand_nstring, save_expand_nlength); + + /* Read the arguments */ + + for (i = 0; i < j; i++) + { + while (isspace(*s)) s++; + if (*s == '{') + { + sub[i] = expand_string_internal(s+1, TRUE, &s, skipping); + if (sub[i] == NULL) goto EXPAND_FAILED; + if (*s++ != '}') goto EXPAND_FAILED_CURLY; + + /* After removal of leading and trailing white space, the first + argument must not be empty; if it consists entirely of digits + (optionally preceded by a minus sign), this is a numerical + extraction, and we expect 3 arguments. */ + + if (i == 0) + { + int len; + int x = 0; + uschar *p = sub[0]; + + while (isspace(*p)) p++; + sub[0] = p; + + len = Ustrlen(p); + while (len > 0 && isspace(p[len-1])) len--; + p[len] = 0; + + if (*p == 0) + { + expand_string_message = US"first argument of \"expand\" must not " + "be empty"; + goto EXPAND_FAILED; + } + + if (*p == '-') + { + field_number = -1; + p++; + } + while (*p != 0 && isdigit(*p)) x = x * 10 + *p++ - '0'; + if (*p == 0) + { + field_number *= x; + j = 3; /* Need 3 args */ + field_number_set = TRUE; + } + } + } + else goto EXPAND_FAILED_CURLY; + } + + /* Extract either the numbered or the keyed substring into $value. If + skipping, just pretend the extraction failed. */ + + lookup_value = skipping? NULL : field_number_set? + expand_gettokened(field_number, sub[1], sub[2]) : + expand_getkeyed(sub[0], sub[1]); + + /* If no string follows, $value gets substituted; otherwise there can + be yes/no strings, as for lookup or if. */ + + switch(process_yesno( + skipping, /* were previously skipping */ + lookup_value != NULL, /* success/failure indicator */ + save_lookup_value, /* value to reset for string2 */ + &s, /* input pointer */ + &yield, /* output pointer */ + &size, /* output size */ + &ptr, /* output current point */ + US"extract")) /* condition type */ + { + case 1: goto EXPAND_FAILED; /* when all is well, the */ + case 2: goto EXPAND_FAILED_CURLY; /* returned value is 0 */ + } + + /* All done - restore numerical variables. */ + + restore_expand_strings(save_expand_nmax, save_expand_nstring, + save_expand_nlength); + + continue; + } + } + + /* Control reaches here if the name is not recognized as one of the more + complicated expansion items. Check for the "operator" syntax (name terminated + by a colon). Some of the operators have arguments, separated by _ from the + name. */ + + if (*s == ':') + { + int c; + uschar *arg = NULL; + uschar *sub = expand_string_internal(s+1, TRUE, &s, skipping); + if (sub == NULL) goto EXPAND_FAILED; + s++; + + /* Owing to an historical mis-design, an underscore may be part of the + operator name, or it may introduce arguments. We therefore first scan the + table of names that contain underscores. If there is no match, we cut off + the arguments and then scan the main table. */ + + c = chop_match(name, op_table_underscore, + sizeof(op_table_underscore)/sizeof(uschar *)); + + if (c < 0) + { + arg = Ustrchr(name, '_'); + if (arg != NULL) *arg = 0; + c = chop_match(name, op_table_main, + sizeof(op_table_main)/sizeof(uschar *)); + if (c >= 0) c += sizeof(op_table_underscore)/sizeof(uschar *); + if (arg != NULL) *arg++ = '_'; /* Put back for error messages */ + } + + /* If we are skipping, we don't need to perform the operation at all. + This matters for operations like "mask", because the data may not be + in the correct format when skipping. For example, the expression may test + for the existence of $sender_host_address before trying to mask it. For + other operations, doing them may not fail, but it is a waste of time. */ + + if (skipping && c >= 0) continue; + + /* Otherwise, switch on the operator type */ + + switch(c) + { + case EOP_BASE62: + { + uschar *t; + unsigned long int n = Ustrtoul(sub, &t, 10); + if (*t != 0) + { + expand_string_message = string_sprintf("argument for base62 " + "operator is \"%s\", which is not a decimal number", sub); + goto EXPAND_FAILED; + } + t = string_base62(n); + yield = string_cat(yield, &size, &ptr, t, Ustrlen(t)); + continue; + } + + case EOP_BASE62D: + { + uschar buf[16]; + uschar *tt = sub; + unsigned long int n = 0; + while (*tt != 0) + { + uschar *t = Ustrchr(base62_chars, *tt++); + if (t == NULL) + { + expand_string_message = string_sprintf("argument for base62d " + "operator is \"%s\", which is not a base 62 number", sub); + goto EXPAND_FAILED; + } + n = n * 62 + (t - base62_chars); + } + (void)sprintf(CS buf, "%ld", n); + yield = string_cat(yield, &size, &ptr, buf, Ustrlen(buf)); + continue; + } + + case EOP_EXPAND: + { + uschar *expanded = expand_string_internal(sub, FALSE, NULL, skipping); + if (expanded == NULL) + { + expand_string_message = + string_sprintf("internal expansion of \"%s\" failed: %s", sub, + expand_string_message); + goto EXPAND_FAILED; + } + yield = string_cat(yield, &size, &ptr, expanded, Ustrlen(expanded)); + continue; + } + + case EOP_LC: + { + int count = 0; + uschar *t = sub - 1; + while (*(++t) != 0) { *t = tolower(*t); count++; } + yield = string_cat(yield, &size, &ptr, sub, count); + continue; + } + + case EOP_UC: + { + int count = 0; + uschar *t = sub - 1; + while (*(++t) != 0) { *t = toupper(*t); count++; } + yield = string_cat(yield, &size, &ptr, sub, count); + continue; + } + + case EOP_MD5: + { + md5 base; + uschar digest[16]; + int j; + char st[33]; + md5_start(&base); + md5_end(&base, sub, Ustrlen(sub), digest); + for(j = 0; j < 16; j++) sprintf(st+2*j, "%02x", digest[j]); + yield = string_cat(yield, &size, &ptr, US st, (int)strlen(st)); + continue; + } + + case EOP_SHA1: + { + sha1 base; + uschar digest[20]; + int j; + char st[41]; + sha1_start(&base); + sha1_end(&base, sub, Ustrlen(sub), digest); + for(j = 0; j < 20; j++) sprintf(st+2*j, "%02X", digest[j]); + yield = string_cat(yield, &size, &ptr, US st, (int)strlen(st)); + continue; + } + + /* Convert hex encoding to base64 encoding */ + + case EOP_HEX2B64: + { + int c = 0; + int b = -1; + uschar *in = sub; + uschar *out = sub; + uschar *enc; + + for (enc = sub; *enc != 0; enc++) + { + if (!isxdigit(*enc)) + { + expand_string_message = string_sprintf("\"%s\" is not a hex " + "string", sub); + goto EXPAND_FAILED; + } + c++; + } + + if ((c & 1) != 0) + { + expand_string_message = string_sprintf("\"%s\" contains an odd " + "number of characters", sub); + goto EXPAND_FAILED; + } + + while ((c = *in++) != 0) + { + if (isdigit(c)) c -= '0'; + else c = toupper(c) - 'A' + 10; + if (b == -1) + { + b = c << 4; + } + else + { + *out++ = b | c; + b = -1; + } + } + + enc = auth_b64encode(sub, out - sub); + yield = string_cat(yield, &size, &ptr, enc, Ustrlen(enc)); + continue; + } + + /* mask applies a mask to an IP address; for example the result of + ${mask:131.111.10.206/28} is 131.111.10.192/28. */ + + case EOP_MASK: + { + int count; + uschar *endptr; + int binary[4]; + int mask, maskoffset; + int type = string_is_ip_address(sub, &maskoffset); + uschar buffer[64]; + + if (type == 0) + { + expand_string_message = string_sprintf("\"%s\" is not an IP address", + sub); + goto EXPAND_FAILED; + } + + if (maskoffset == 0) + { + expand_string_message = string_sprintf("missing mask value in \"%s\"", + sub); + goto EXPAND_FAILED; + } + + mask = Ustrtol(sub + maskoffset + 1, &endptr, 10); + + if (*endptr != 0 || mask < 0 || mask > ((type == 4)? 32 : 128)) + { + expand_string_message = string_sprintf("mask value too big in \"%s\"", + sub); + goto EXPAND_FAILED; + } + + /* Convert the address to binary integer(s) and apply the mask */ + + sub[maskoffset] = 0; + count = host_aton(sub, binary); + host_mask(count, binary, mask); + + /* Convert to masked textual format and add to output. */ + + yield = string_cat(yield, &size, &ptr, buffer, + host_nmtoa(count, binary, mask, buffer)); + continue; + } + + case EOP_ADDRESS: + case EOP_LOCAL_PART: + case EOP_DOMAIN: + { + uschar *error; + int start, end, domain; + uschar *t = parse_extract_address(sub, &error, &start, &end, &domain, + FALSE); + if (t != NULL) + { + if (c != EOP_DOMAIN) + { + if (c == EOP_LOCAL_PART && domain != 0) end = start + domain - 1; + yield = string_cat(yield, &size, &ptr, sub+start, end-start); + } + else if (domain != 0) + { + domain += start; + yield = string_cat(yield, &size, &ptr, sub+domain, end-domain); + } + } + continue; + } + + /* quote puts a string in quotes if it is empty or contains anything + other than alphamerics, underscore, dot, or hyphen. + + quote_local_part puts a string in quotes if RFC 2821/2822 requires it to + be quoted in order to be a valid local part. + + In both cases, newlines and carriage returns are converted into \n and \r + respectively */ + + case EOP_QUOTE: + case EOP_QUOTE_LOCAL_PART: + if (arg == NULL) + { + BOOL needs_quote = (*sub == 0); /* TRUE for empty string */ + uschar *t = sub - 1; + + if (c == EOP_QUOTE) + { + while (!needs_quote && *(++t) != 0) + needs_quote = !isalnum(*t) && !strchr("_-.", *t); + } + else /* EOP_QUOTE_LOCAL_PART */ + { + while (!needs_quote && *(++t) != 0) + needs_quote = !isalnum(*t) && + strchr("!#$%&'*+-/=?^_`{|}~", *t) == NULL && + (*t != '.' || t == sub || t[1] == 0); + } + + if (needs_quote) + { + yield = string_cat(yield, &size, &ptr, US"\"", 1); + t = sub - 1; + while (*(++t) != 0) + { + if (*t == '\n') + yield = string_cat(yield, &size, &ptr, US"\\n", 2); + else if (*t == '\r') + yield = string_cat(yield, &size, &ptr, US"\\r", 2); + else + { + if (*t == '\\' || *t == '"') + yield = string_cat(yield, &size, &ptr, US"\\", 1); + yield = string_cat(yield, &size, &ptr, t, 1); + } + } + yield = string_cat(yield, &size, &ptr, US"\"", 1); + } + else yield = string_cat(yield, &size, &ptr, sub, Ustrlen(sub)); + continue; + } + + /* quote_lookuptype does lookup-specific quoting */ + + else + { + int n; + uschar *opt = Ustrchr(arg, '_'); + + if (opt != NULL) *opt++ = 0; + + n = search_findtype(arg, Ustrlen(arg)); + if (n < 0) + { + expand_string_message = search_error_message; + goto EXPAND_FAILED; + } + + if (lookup_list[n].quote != NULL) + sub = (lookup_list[n].quote)(sub, opt); + else if (opt != NULL) sub = NULL; + + if (sub == NULL) + { + expand_string_message = string_sprintf( + "\"%s\" unrecognized after \"${quote_%s\"", + opt, arg); + goto EXPAND_FAILED; + } + + yield = string_cat(yield, &size, &ptr, sub, Ustrlen(sub)); + continue; + } + + /* rx quote sticks in \ before any non-alphameric character so that + the insertion works in a regular expression. */ + + case EOP_RXQUOTE: + { + uschar *t = sub - 1; + while (*(++t) != 0) + { + if (!isalnum(*t)) + yield = string_cat(yield, &size, &ptr, US"\\", 1); + yield = string_cat(yield, &size, &ptr, t, 1); + } + continue; + } + + /* RFC 2047 encodes, assuming headers_charset (default ISO 8859-1) as + prescribed by the RFC, if there are characters that need to be encoded */ + + case EOP_RFC2047: + { + uschar buffer[1024]; + uschar *string = parse_quote_2047(sub, Ustrlen(sub), headers_charset, + buffer, sizeof(buffer)); + yield = string_cat(yield, &size, &ptr, string, Ustrlen(string)); + continue; + } + + /* from_utf8 converts UTF-8 to 8859-1, turning non-existent chars into + underscores */ + + case EOP_FROM_UTF8: + { + while (*sub != 0) + { + int c; + uschar buff[4]; + GETUTF8INC(c, sub); + if (c > 255) c = '_'; + buff[0] = c; + yield = string_cat(yield, &size, &ptr, buff, 1); + } + continue; + } + + /* escape turns all non-printing characters into escape sequences. */ + + case EOP_ESCAPE: + { + uschar *t = string_printing(sub); + yield = string_cat(yield, &size, &ptr, t, Ustrlen(t)); + continue; + } + + /* Handle numeric expression evaluation */ + + case EOP_EVAL: + case EOP_EVAL10: + { + uschar *save_sub = sub; + uschar *error = NULL; + int n = eval_expr(&sub, (c == EOP_EVAL10), &error, FALSE); + if (error != NULL) + { + expand_string_message = string_sprintf("error in expression " + "evaluation: %s (after processing \"%.*s\")", error, sub-save_sub, + save_sub); + goto EXPAND_FAILED; + } + sprintf(CS var_buffer, "%d", n); + yield = string_cat(yield, &size, &ptr, var_buffer, Ustrlen(var_buffer)); + continue; + } + + /* Handle time period formating */ + + case EOP_TIME_INTERVAL: + { + int n; + uschar *t = read_number(&n, sub); + if (*t != 0) /* Not A Number*/ + { + expand_string_message = string_sprintf("string \"%s\" is not a " + "positive number in \"%s\" operator", sub, name); + goto EXPAND_FAILED; + } + t = readconf_printtime(n); + yield = string_cat(yield, &size, &ptr, t, Ustrlen(t)); + continue; + } + + /* Convert string to base64 encoding */ + + case EOP_STR2B64: + { + uschar *encstr = auth_b64encode(sub, Ustrlen(sub)); + yield = string_cat(yield, &size, &ptr, encstr, Ustrlen(encstr)); + continue; + } + + /* strlen returns the length of the string */ + + case EOP_STRLEN: + { + uschar buff[24]; + (void)sprintf(CS buff, "%d", Ustrlen(sub)); + yield = string_cat(yield, &size, &ptr, buff, Ustrlen(buff)); + continue; + } + + /* length_n or l_n takes just the first n characters or the whole string, + whichever is the shorter; + + substr_m_n, and s_m_n take n characters from offset m; negative m take + from the end; l_n is synonymous with s_0_n. If n is omitted in substr it + takes the rest, either to the right or to the left. + + hash_n or h_n makes a hash of length n from the string, yielding n + characters from the set a-z; hash_n_m makes a hash of length n, but + uses m characters from the set a-zA-Z0-9. + + nhash_n returns a single number between 0 and n-1 (in text form), while + nhash_n_m returns a div/mod hash as two numbers "a/b". The first lies + between 0 and n-1 and the second between 0 and m-1. */ + + case EOP_LENGTH: + case EOP_L: + case EOP_SUBSTR: + case EOP_S: + case EOP_HASH: + case EOP_H: + case EOP_NHASH: + case EOP_NH: + { + int sign = 1; + int value1 = 0; + int value2 = -1; + int *pn; + int len; + uschar *ret; + + if (arg == NULL) + { + expand_string_message = string_sprintf("missing values after %s", + name); + goto EXPAND_FAILED; + } + + /* "length" has only one argument, effectively being synonymous with + substr_0_n. */ + + if (c == EOP_LENGTH || c == EOP_L) + { + pn = &value2; + value2 = 0; + } + + /* The others have one or two arguments; for "substr" the first may be + negative. The second being negative means "not supplied". */ + + else + { + pn = &value1; + if (name[0] == 's' && *arg == '-') { sign = -1; arg++; } + } + + /* Read up to two numbers, separated by underscores */ + + ret = arg; + while (*arg != 0) + { + if (arg != ret && *arg == '_' && pn == &value1) + { + pn = &value2; + value2 = 0; + if (arg[1] != 0) arg++; + } + else if (!isdigit(*arg)) + { + expand_string_message = + string_sprintf("non-digit after underscore in \"%s\"", name); + goto EXPAND_FAILED; + } + else *pn = (*pn)*10 + *arg++ - '0'; + } + value1 *= sign; + + /* Perform the required operation */ + + ret = + (c == EOP_HASH || c == EOP_H)? + compute_hash(sub, value1, value2, &len) : + (c == EOP_NHASH || c == EOP_NH)? + compute_nhash(sub, value1, value2, &len) : + extract_substr(sub, value1, value2, &len); + + if (ret == NULL) goto EXPAND_FAILED; + yield = string_cat(yield, &size, &ptr, ret, len); + continue; + } + + /* Stat a path */ + + case EOP_STAT: + { + uschar *s; + uschar smode[12]; + uschar **modetable[3]; + int i; + mode_t mode; + struct stat st; + + if (stat(CS sub, &st) < 0) + { + expand_string_message = string_sprintf("stat(%s) failed: %s", + sub, strerror(errno)); + goto EXPAND_FAILED; + } + mode = st.st_mode; + switch (mode & S_IFMT) + { + case S_IFIFO: smode[0] = 'p'; break; + case S_IFCHR: smode[0] = 'c'; break; + case S_IFDIR: smode[0] = 'd'; break; + case S_IFBLK: smode[0] = 'b'; break; + case S_IFREG: smode[0] = '-'; break; + default: smode[0] = '?'; break; + } + + modetable[0] = ((mode & 01000) == 0)? mtable_normal : mtable_sticky; + modetable[1] = ((mode & 02000) == 0)? mtable_normal : mtable_setid; + modetable[2] = ((mode & 04000) == 0)? mtable_normal : mtable_setid; + + for (i = 0; i < 3; i++) + { + memcpy(CS(smode + 7 - i*3), CS(modetable[i][mode & 7]), 3); + mode >>= 3; + } + + smode[10] = 0; + s = string_sprintf("mode=%04lo smode=%s inode=%ld device=%ld links=%ld " + "uid=%ld gid=%ld size=%ld atime=%ld mtime=%ld ctime=%ld", + (long)(st.st_mode & 077777), smode, (long)st.st_ino, + (long)st.st_dev, (long)st.st_nlink, (long)st.st_uid, + (long)st.st_gid, (long)st.st_size, (long)st.st_atime, + (long)st.st_mtime, (long)st.st_ctime); + yield = string_cat(yield, &size, &ptr, s, Ustrlen(s)); + continue; + } + + /* Unknown operator */ + + default: + expand_string_message = + string_sprintf("unknown expansion operator \"%s\"", name); + goto EXPAND_FAILED; + } + } + + /* Handle a plain name. If this is the first thing in the expansion, release + the pre-allocated buffer. If the result data is known to be in a new buffer, + newsize will be set to the size of that buffer, and we can just point at that + store instead of copying. Many expansion strings contain just one reference, + so this is a useful optimization, especially for humungous headers + ($message_headers). */ + + if (*s++ == '}') + { + int len; + int newsize = 0; + if (ptr == 0) + { + store_reset(yield); + yield = NULL; + size = 0; + } + value = find_variable(name, FALSE, skipping, &newsize); + if (value == NULL) + { + expand_string_message = + string_sprintf("unknown variable in \"${%s}\"", name); + goto EXPAND_FAILED; + } + len = Ustrlen(value); + if (yield == NULL && newsize != 0) + { + yield = value; + size = newsize; + ptr = len; + } + else yield = string_cat(yield, &size, &ptr, value, len); + continue; + } + + /* Else there's something wrong */ + + expand_string_message = + string_sprintf("\"${%s\" is not a known operator (or a } is missing " + "in a variable reference)", name); + goto EXPAND_FAILED; + } + +/* If we hit the end of the string when ket_ends is set, there is a missing +terminating brace. */ + +if (ket_ends && *s == 0) + { + expand_string_message = malformed_header? + US"missing } at end of string - could be header name not terminated by colon" + : + US"missing } at end of string"; + goto EXPAND_FAILED; + } + +/* Expansion succeeded; yield may still be NULL here if nothing was actually +added to the string. If so, set up an empty string. Add a terminating zero. If +left != NULL, return a pointer to the terminator. */ + +if (yield == NULL) yield = store_get(1); +yield[ptr] = 0; +if (left != NULL) *left = s; + +/* Any stacking store that was used above the final string is no longer needed. +In many cases the final string will be the first one that was got and so there +will be optimal store usage. */ + +store_reset(yield + ptr + 1); +DEBUG(D_expand) + { + debug_printf("expanding: %.*s\n result: %s\n", (int)(s - string), string, + yield); + if (skipping) debug_printf("skipping: result is not used\n"); + } +return yield; + +/* This is the failure exit: easiest to program with a goto. We still need +to update the pointer to the terminator, for cases of nested calls with "fail". +*/ + +EXPAND_FAILED_CURLY: +expand_string_message = malformed_header? + US"missing or misplaced { or } - could be header name not terminated by colon" + : + US"missing or misplaced { or }"; + +/* At one point, Exim reset the store to yield (if yield was not NULL), but +that is a bad idea, because expand_string_message is in dynamic store. */ + +EXPAND_FAILED: +if (left != NULL) *left = s; +DEBUG(D_expand) + { + debug_printf("failed to expand: %s\n", string); + debug_printf(" error message: %s\n", expand_string_message); + if (expand_string_forcedfail) debug_printf("failure was forced\n"); + } +return NULL; +} + + +/* This is the external function call. Do a quick check for any expansion +metacharacters, and if there are none, just return the input string. + +Argument: the string to be expanded +Returns: the expanded string, or NULL if expansion failed; if failure was + due to a lookup deferring, search_find_defer will be TRUE +*/ + +uschar * +expand_string(uschar *string) +{ +search_find_defer = FALSE; +malformed_header = FALSE; +return (Ustrpbrk(string, "$\\") == NULL)? string : + expand_string_internal(string, FALSE, NULL, FALSE); +} + + + +/************************************************* +* Expand and copy * +*************************************************/ + +/* Now and again we want to expand a string and be sure that the result is in a +new bit of store. This function does that. + +Argument: the string to be expanded +Returns: the expanded string, always in a new bit of store, or NULL +*/ + +uschar * +expand_string_copy(uschar *string) +{ +uschar *yield = expand_string(string); +if (yield == string) yield = string_copy(string); +return yield; +} + + + +/************************************************* +* Expand and interpret as an integer * +*************************************************/ + +/* Expand a string, and convert the result into an integer. + +Argument: the string to be expanded + +Returns: the integer value, or + -1 for an expansion error ) in both cases, message in + -2 for an integer interpretation error ) expand_string_message + +*/ + +int +expand_string_integer(uschar *string) +{ +long int value; +uschar *s = expand_string(string); +uschar *msg = US"invalid integer \"%s\""; +uschar *endptr; + +if (s == NULL) return -1; + +/* On an overflow, strtol() returns LONG_MAX or LONG_MIN, and sets errno +to ERANGE. When there isn't an overflow, errno is not changed, at least on some +systems, so we set it zero ourselves. */ + +errno = 0; +value = strtol(CS s, CSS &endptr, 0); + +if (endptr == s) + { + msg = US"integer expected but \"%s\" found"; + } +else + { + /* Ensure we can cast this down to an int */ + if (value > INT_MAX || value < INT_MIN) errno = ERANGE; + + if (errno != ERANGE) + { + if (tolower(*endptr) == 'k') + { + if (value > INT_MAX/1024 || value < INT_MIN/1024) errno = ERANGE; + else value *= 1024; + endptr++; + } + else if (tolower(*endptr) == 'm') + { + if (value > INT_MAX/(1024*1024) || value < INT_MIN/(1024*1024)) + errno = ERANGE; + else value *= 1024*1024; + endptr++; + } + } + if (errno == ERANGE) + msg = US"absolute value of integer \"%s\" is too large (overflow)"; + else + { + while (isspace(*endptr)) endptr++; + if (*endptr == 0) return (int)value; + } + } + +expand_string_message = string_sprintf(CS msg, s); +return -2; +} + + + +/************************************************* +************************************************** +* Stand-alone test program * +************************************************** +*************************************************/ + +#ifdef STAND_ALONE + + +BOOL +regex_match_and_setup(const pcre *re, uschar *subject, int options, int setup) +{ +int ovector[3*(EXPAND_MAXN+1)]; +int n = pcre_exec(re, NULL, subject, Ustrlen(subject), 0, PCRE_EOPT|options, + ovector, sizeof(ovector)/sizeof(int)); +BOOL yield = n >= 0; +if (n == 0) n = EXPAND_MAXN + 1; +if (yield) + { + int nn; + expand_nmax = (setup < 0)? 0 : setup + 1; + for (nn = (setup < 0)? 0 : 2; nn < n*2; nn += 2) + { + expand_nstring[expand_nmax] = subject + ovector[nn]; + expand_nlength[expand_nmax++] = ovector[nn+1] - ovector[nn]; + } + expand_nmax--; + } +return yield; +} + + +int main(int argc, uschar **argv) +{ +int i; +uschar buffer[1024]; + +debug_selector = D_v; +debug_file = stderr; +debug_fd = fileno(debug_file); +big_buffer = malloc(big_buffer_size); + +for (i = 1; i < argc; i++) + { + if (argv[i][0] == '+') + { + debug_trace_memory = 2; + argv[i]++; + } + if (isdigit(argv[i][0])) + debug_selector = Ustrtol(argv[i], NULL, 0); + else + if (Ustrspn(argv[i], "abcdefghijklmnopqrtsuvwxyz0123456789-.:/") == + Ustrlen(argv[i])) + { + #ifdef LOOKUP_LDAP + eldap_default_servers = argv[i]; + #endif + #ifdef LOOKUP_MYSQL + mysql_servers = argv[i]; + #endif + #ifdef LOOKUP_PGSQL + pgsql_servers = argv[i]; + #endif + } + #ifdef EXIM_PERL + else opt_perl_startup = argv[i]; + #endif + } + +printf("Testing string expansion: debug_level = %d\n\n", debug_level); + +expand_nstring[1] = US"string 1...."; +expand_nlength[1] = 8; +expand_nmax = 1; + +#ifdef EXIM_PERL +if (opt_perl_startup != NULL) + { + uschar *errstr; + printf("Starting Perl interpreter\n"); + errstr = init_perl(opt_perl_startup); + if (errstr != NULL) + { + printf("** error in perl_startup code: %s\n", errstr); + return EXIT_FAILURE; + } + } +#endif /* EXIM_PERL */ + +while (fgets(buffer, sizeof(buffer), stdin) != NULL) + { + void *reset_point = store_get(0); + uschar *yield = expand_string(buffer); + if (yield != NULL) + { + printf("%s\n", yield); + store_reset(reset_point); + } + else + { + if (search_find_defer) printf("search_find deferred\n"); + printf("Failed: %s\n", expand_string_message); + if (expand_string_forcedfail) printf("Forced failure\n"); + printf("\n"); + } + } + +search_tidyup(); + +return 0; +} + +#endif + +/* End of expand.c */ diff --git a/src/src/filter.c b/src/src/filter.c new file mode 100644 index 000000000..0e8e790a4 --- /dev/null +++ b/src/src/filter.c @@ -0,0 +1,2513 @@ +/* $Cambridge: exim/src/src/filter.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + + +/* Code for mail filtering functions. */ + +#include "exim.h" + + +/* Command arguments and left/right points in conditions can contain different +types of data, depending on the particular command or condition. Originally, +(void *) was used as "any old type", with casts, but this gives trouble and +warnings in some environments. So now it is done "properly", with a union. We +need to declare the structures first because some of them are recursive. */ + +struct filter_cmd; +struct condition_block; + +union argtypes { + struct string_item *a; + BOOL b; + struct condition_block *c; + struct filter_cmd *f; + int i; + uschar *u; +}; + +/* Local structures used in this module */ + +typedef struct filter_cmd { + struct filter_cmd *next; + int command; + BOOL seen; + BOOL noerror; + union argtypes args[1]; +} filter_cmd; + +typedef struct condition_block { + struct condition_block *parent; + int type; + BOOL testfor; + union argtypes left; + union argtypes right; +} condition_block; + +/* Miscellaneous other declarations */ + +static uschar **error_pointer; +static uschar *log_filename; +static int filter_options; +static int line_number; +static int expect_endif; +static int had_else_endif; +static int log_fd; +static int log_mode; +static int output_indent; +static BOOL filter_delivered; +static BOOL finish_obeyed; +static BOOL seen_force; +static BOOL seen_value; +static BOOL noerror_force; + +enum { had_neither, had_else, had_elif, had_endif }; + +static BOOL read_command_list(uschar **, filter_cmd ***, BOOL); + + +/* The string arguments for the mail command. The header line ones (that are +permitted to include \n followed by white space) first, and then the body text +one (it can have \n anywhere). Then the file names and once_repeat, which may +not contain \n. */ + +static char *mailargs[] = { /* "to" must be first, and */ + "to", /* "cc" and "bcc" must follow */ + "cc", + "bcc", + "from", + "reply_to", + "subject", + "extra_headers", /* miscellaneous added header lines */ + "text", + "file", + "log", + "once", + "once_repeat" +}; + +/* The count of string arguments */ + +#define MAILARGS_STRING_COUNT (sizeof(mailargs)/sizeof(uschar *)) + +/* The count of string arguments that are actually passed over as strings +(once_repeat is converted to an int). */ + +#define mailargs_string_passed (MAILARGS_STRING_COUNT - 1) + +/* This defines the offsets for the arguments; first the string ones, and +then the non-string ones. The order must be as above. */ + +enum { mailarg_index_to, + mailarg_index_cc, + mailarg_index_bcc, + mailarg_index_from, + mailarg_index_reply_to, + mailarg_index_subject, + mailarg_index_headers, /* misc headers must be last */ + mailarg_index_text, /* text is first after headers */ + mailarg_index_file, /* between text and expand are filenames */ + mailarg_index_log, + mailarg_index_once, + mailarg_index_once_repeat, /* a time string */ + mailarg_index_expand, /* first non-string argument */ + mailarg_index_return, + mailargs_total /* total number of arguments */ + }; + +/* Offsets in the data structure for the string arguments (note that +once_repeat isn't a string argument at this point.) */ + +static int reply_offsets[] = { /* must be in same order as above */ + offsetof(reply_item, to), + offsetof(reply_item, cc), + offsetof(reply_item, bcc), + offsetof(reply_item, from), + offsetof(reply_item, reply_to), + offsetof(reply_item, subject), + offsetof(reply_item, headers), + offsetof(reply_item, text), + offsetof(reply_item, file), + offsetof(reply_item, logfile), + offsetof(reply_item, oncelog), +}; + +/* Condition identities and names, with negated versions for some +of them. */ + +enum { cond_and, cond_or, cond_personal, cond_begins, cond_BEGINS, + cond_ends, cond_ENDS, cond_is, cond_IS, cond_matches, + cond_MATCHES, cond_contains, cond_CONTAINS, cond_delivered, + cond_above, cond_below, cond_errormsg, cond_firsttime, + cond_manualthaw, cond_foranyaddress }; + +static char *cond_names[] = { + "and", "or", "personal", + "begins", "BEGINS", "ends", "ENDS", + "is", "IS", "matches", "MATCHES", "contains", + "CONTAINS", "delivered", "above", "below", "error_message", + "first_delivery", "manually_thawed", "foranyaddress" }; + +static char *cond_not_names[] = { + "", "", "not personal", + "does not begin", "does not BEGIN", + "does not end", "does not END", + "is not", "IS not", "does not match", + "does not MATCH", "does not contain", "does not CONTAIN", + "not delivered", "not above", "not below", "not error_message", + "not first_delivery", "not manually_thawed", "not foranyaddress" }; + +/* Tables of binary condition words and their corresponding types. Not easy +to amalgamate with the above because of the different variants. */ + +static char *cond_words[] = { + "BEGIN", + "BEGINS", + "CONTAIN", + "CONTAINS", + "END", + "ENDS", + "IS", + "MATCH", + "MATCHES", + "above", + "begin", + "begins", + "below", + "contain", + "contains", + "end", + "ends", + "is", + "match", + "matches"}; + +static int cond_word_count = (sizeof(cond_words)/sizeof(uschar *)); + +static int cond_types[] = { cond_BEGINS, cond_BEGINS, cond_CONTAINS, + cond_CONTAINS, cond_ENDS, cond_ENDS, cond_IS, cond_MATCHES, cond_MATCHES, + cond_above, cond_begins, cond_begins, cond_below, cond_contains, + cond_contains, cond_ends, cond_ends, cond_is, cond_matches, cond_matches }; + +/* Command identities: must be kept in step with the list of command words +and the list of expanded argument counts which follow. */ + +enum { add_command, defer_command, deliver_command, elif_command, else_command, + endif_command, finish_command, fail_command, freeze_command, + headers_command, if_command, logfile_command, logwrite_command, + mail_command, noerror_command, pipe_command, save_command, seen_command, + testprint_command, unseen_command, vacation_command }; + +static char *command_list[] = { + "add", "defer", "deliver", "elif", "else", "endif", "finish", + "fail", "freeze", "headers", "if", "logfile", "logwrite", "mail", + "noerror", "pipe", "save", "seen", "testprint", "unseen", "vacation" +}; + +static int command_list_count = sizeof(command_list)/sizeof(uschar *); + +/* This table contains the number of expanded arguments in the bottom 4 bits. +If the top bit is set, it means that the default for the command is "seen". */ + +static uschar command_exparg_count[] = { + 2, /* add */ + 1, /* defer */ + 128+2, /* deliver */ + 0, /* elif */ + 0, /* else */ + 0, /* endif */ + 0, /* finish */ + 1, /* fail */ + 1, /* freeze */ + 1, /* headers */ + 0, /* if */ + 1, /* logfile */ + 1, /* logwrite */ + MAILARGS_STRING_COUNT, /* mail */ + 0, /* noerror */ + 128+0, /* pipe */ + 128+1, /* save */ + 0, /* seen */ + 1, /* testprint */ + 0, /* unseen */ + MAILARGS_STRING_COUNT /* vacation */ +}; + + + +/************************************************* +* Find next significant uschar * +*************************************************/ + +/* Function to skip over white space and, optionally, comments. + +Arguments: + ptr pointer to next character + comment_allowed if TRUE, comments (# to \n) are skipped + +Returns: pointer to next non-whitespace character +*/ + +static uschar * +nextsigchar(uschar *ptr, BOOL comment_allowed) +{ +for (;;) + { + while (isspace(*ptr)) + { + if (*ptr == '\n') line_number++; + ptr++; + } + if (comment_allowed && *ptr == '#') + { + while (*(++ptr) != '\n' && *ptr != 0); + continue; + } + else break; + } +return ptr; +} + + + +/************************************************* +* Read one word * +*************************************************/ + +/* The terminator is white space unless bracket is TRUE, in which +case ( and ) terminate. + +Arguments + ptr pointer to next character + buffer where to put the word + size size of buffer + bracket if TRUE, terminate on ( and ) as well as space + +Returns: pointer to the next significant character after the word +*/ + +static uschar * +nextword(uschar *ptr, uschar *buffer, int size, BOOL bracket) +{ +uschar *bp = buffer; +while (*ptr != 0 && !isspace(*ptr) && + (!bracket || (*ptr != '(' && *ptr != ')'))) + { + if (bp - buffer < size - 1) *bp++ = *ptr++; else + { + *error_pointer = string_sprintf("word is too long in line %d of " + "filter file (max = %d chars)", line_number, size); + break; + } + } +*bp = 0; +return nextsigchar(ptr, TRUE); +} + + + +/************************************************* +* Read one item * +*************************************************/ + +/* Might be a word, or might be a quoted string; in the latter case +do the escape stuff. + +Arguments: + ptr pointer to next character + buffer where to put the item + size size of buffer + bracket if TRUE, terminate non-quoted on ( and ) as well as space + +Returns: the next significant character after the item +*/ + +static uschar * +nextitem(uschar *ptr, uschar *buffer, int size, BOOL bracket) +{ +uschar *bp = buffer; +if (*ptr != '\"') return nextword(ptr, buffer, size, bracket); + +while (*(++ptr) != 0 && *ptr != '\"' && *ptr != '\n') + { + if (bp - buffer >= size - 1) + { + *error_pointer = string_sprintf("string is too long in line %d of " + "filter file (max = %d chars)", line_number, size); + break; + } + + if (*ptr != '\\') *bp++ = *ptr; else + { + if (isspace(ptr[1])) /* \NL ignored */ + { + uschar *p = ptr + 1; + while (*p != '\n' && isspace(*p)) p++; + if (*p == '\n') + { + line_number++; + ptr = p; + while (ptr[1] != '\n' && isspace(ptr[1])) ptr++; + continue; + } + } + + *bp++ = string_interpret_escape(&ptr); + } + } + +if (*ptr == '\"') ptr++; + else if (*error_pointer == NULL) + *error_pointer = string_sprintf("quote missing at end of string " + "in line %d", line_number); + +*bp = 0; +return nextsigchar(ptr, TRUE); +} + + + + +/************************************************* +* Convert a string + K|M to a number * +*************************************************/ + +/* +Arguments: + s points to text string + OK set TRUE if a valid number was read + +Returns: the number, or 0 on error (with *OK FALSE) +*/ + +static int +get_number(uschar *s, BOOL *ok) +{ +int value, count; +*ok = FALSE; +if (sscanf(CS s, "%i%n", &value, &count) != 1) return 0; +if (tolower(s[count]) == 'k') { value *= 1024; count++; } +if (tolower(s[count]) == 'm') { value *= 1024*1024; count++; } +while (isspace((s[count]))) count++; +if (s[count] != 0) return 0; +*ok = TRUE; +return value; +} + + + +/************************************************* +* Read one condition * +*************************************************/ + +/* A complete condition must be terminated by "then"; bracketed internal +conditions must be terminated by a closing bracket. They are read by calling +this function recursively. + +Arguments: + ptr points to start of condition + condition_block where to hang the created condition block + toplevel TRUE when called at the top level + +Returns: points to next character after "then" +*/ + +static uschar * +read_condition(uschar *ptr, condition_block **cond, BOOL toplevel) +{ +uschar buffer[1024]; +BOOL testfor = TRUE; +condition_block *current_parent = NULL; +condition_block **current = cond; + +*current = NULL; + +/* Loop to read next condition */ + +for (;;) + { + condition_block *c; + + /* reaching the end of the input is an error. */ + + if (*ptr == 0) + { + *error_pointer = string_sprintf("\"then\" missing at end of filter file"); + break; + } + + /* Opening bracket at the start of a condition introduces a nested + condition, which must be terminated by a closing bracket. */ + + if (*ptr == '(') + { + ptr = read_condition(nextsigchar(ptr+1, TRUE), &c, FALSE); + if (*error_pointer != NULL) break; + if (*ptr != ')') + { + *error_pointer = string_sprintf("expected \")\" in line %d of " + "filter file", line_number); + break; + } + if (!testfor) + { + c->testfor = !c->testfor; + testfor = TRUE; + } + ptr = nextsigchar(ptr+1, TRUE); + } + + + /* Closing bracket at the start of a condition is an error. Give an + explicit message, as otherwise "unknown condition" would be confusing. */ + + else if (*ptr == ')') + { + *error_pointer = string_sprintf("unexpected \")\" in line %d of " + "filter file", line_number); + break; + } + + /* Otherwise we expect a word or a string. */ + + else + { + ptr = nextitem(ptr, buffer, sizeof(buffer), TRUE); + if (*error_pointer != NULL) break; + + /* "Then" at the start of a condition is an error */ + + if (Ustrcmp(buffer, "then") == 0) + { + *error_pointer = string_sprintf("unexpected \"then\" near line %d of " + "filter file", line_number); + break; + } + + /* "Not" at the start of a condition negates the testing condition. */ + + if (Ustrcmp(buffer, "not") == 0) + { + testfor = !testfor; + continue; + } + + /* Build a condition block from the specific word. */ + + c = store_get(sizeof(condition_block)); + c->left.u = c->right.u = NULL; + c->testfor = testfor; + testfor = TRUE; + + /* Check for conditions that start with a keyword */ + + if (Ustrcmp(buffer, "delivered") == 0) c->type = cond_delivered; + else if (Ustrcmp(buffer, "error_message") == 0) c->type = cond_errormsg; + else if (Ustrcmp(buffer, "first_delivery") == 0) c->type = cond_firsttime; + else if (Ustrcmp(buffer, "manually_thawed") == 0) c->type = cond_manualthaw; + + /* Personal can be followed by any number of aliases */ + + else if (Ustrcmp(buffer, "personal") == 0) + { + c->type = cond_personal; + for (;;) + { + string_item *aa; + uschar *saveptr = ptr; + ptr = nextword(ptr, buffer, sizeof(buffer), TRUE); + if (*error_pointer != NULL) break; + if (Ustrcmp(buffer, "alias") != 0) + { + ptr = saveptr; + break; + } + ptr = nextitem(ptr, buffer, sizeof(buffer), TRUE); + if (*error_pointer != NULL) break; + aa = store_get(sizeof(string_item)); + aa->text = string_copy(buffer); + aa->next = c->left.a; + c->left.a = aa; + } + } + + /* Foranyaddress must be followed by a string and a condition enclosed + in parentheses, which is handled as a subcondition. */ + + else if (Ustrcmp(buffer, "foranyaddress") == 0) + { + ptr = nextitem(ptr, buffer, sizeof(buffer), TRUE); + if (*error_pointer != NULL) break; + if (*ptr != '(') + { + *error_pointer = string_sprintf("\"(\" expected after \"foranyaddress\" " + "near line %d of filter file", line_number); + break; + } + + c->type = cond_foranyaddress; + c->left.u = string_copy(buffer); + + ptr = read_condition(nextsigchar(ptr+1, TRUE), &(c->right.c), FALSE); + if (*error_pointer != NULL) break; + if (*ptr != ')') + { + *error_pointer = string_sprintf("expected \")\" in line %d of " + "filter file", line_number); + break; + } + if (!testfor) + { + c->testfor = !c->testfor; + testfor = TRUE; + } + ptr = nextsigchar(ptr+1, TRUE); + } + + /* If it's not a word we recognize, then it must be the lefthand + operand of one of the comparison words. */ + + else + { + int i; + uschar *isptr = NULL; + + c->left.u = string_copy(buffer); + ptr = nextword(ptr, buffer, sizeof(buffer), TRUE); + if (*error_pointer != NULL) break; + + /* Handle "does|is [not]", preserving the pointer after "is" in + case it isn't that, but the form "is ". */ + + if (strcmpic(buffer, US"does") == 0 || strcmpic(buffer, US"is") == 0) + { + if (buffer[0] == 'i') { c->type = cond_is; isptr = ptr; } + if (buffer[0] == 'I') { c->type = cond_IS; isptr = ptr; } + + ptr = nextword(ptr, buffer, sizeof(buffer), TRUE); + if (*error_pointer != NULL) break; + if (strcmpic(buffer, US"not") == 0) + { + c->testfor = !c->testfor; + if (isptr != NULL) isptr = ptr; + ptr = nextword(ptr, buffer, sizeof(buffer), TRUE); + if (*error_pointer != NULL) break; + } + } + + for (i = 0; i < cond_word_count; i++) + { + if (Ustrcmp(buffer, cond_words[i]) == 0) + { + c->type = cond_types[i]; + break; + } + } + + /* If an unknown word follows "is" or "is not" + it's actually the argument. Reset to read it. */ + + if (i >= cond_word_count) + { + if (isptr != NULL) + { + ptr = isptr; + } + else + { + *error_pointer = string_sprintf("unrecognized condition word \"%s\" " + "near line %d of filter file", buffer, line_number); + break; + } + } + + /* Get the RH argument. */ + + ptr = nextitem(ptr, buffer, sizeof(buffer), TRUE); + if (*error_pointer != NULL) break; + c->right.u = string_copy(buffer); + } + } + + /* We have read some new condition and set it up in the condition block + c; point the current pointer at it, and then deal with what follows. */ + + *current = c; + + /* Closing bracket terminates if this is a lower-level condition. Otherwise + it is unexpected. */ + + if (*ptr == ')') + { + if (toplevel) + *error_pointer = string_sprintf("unexpected \")\" in line %d of " + "filter file", line_number); + break; + } + + /* Opening bracket following a condition is an error; give an explicit + message to make it clearer what is wrong. */ + + else if (*ptr == '(') + { + *error_pointer = string_sprintf("unexpected \"(\" in line %d of " + "filter file", line_number); + break; + } + + /* Otherwise the next thing must be one of the words "and", "or" or "then" */ + + else + { + uschar *saveptr = ptr; + ptr = nextword(ptr, buffer, sizeof(buffer), FALSE); + if (*error_pointer != NULL) break; + + /* "Then" terminates a toplevel condition; otherwise a closing bracket + has been omitted. Put a string terminator at the start of "then" so + that reflecting the condition can be done when testing. */ + + if (Ustrcmp(buffer, "then") == 0) + { + if (toplevel) *saveptr = 0; + else *error_pointer = string_sprintf("missing \")\" at end of " + "condition near line %d of filter file", line_number); + break; + } + + /* "And" causes a new condition block to replace the one we have + just read, which becomes the left sub-condition. The current pointer + is reset to the pointer for the right sub-condition. We have to keep + track of the tree of sequential "ands", so as to traverse back up it + if an "or" is met. */ + + else if (Ustrcmp(buffer, "and") == 0) + { + condition_block *andc = store_get(sizeof(condition_block)); + andc->parent = current_parent; + andc->type = cond_and; + andc->testfor = TRUE; + andc->left.c = c; + andc->right.u = NULL; /* insurance */ + *current = andc; + current = &(andc->right.c); + current_parent = andc; + } + + /* "Or" is similar, but has to be done a bit more carefully to + ensure that "and" is more binding. If there's a parent set, we + are following a sequence of "and"s and must track back to their + start. */ + + else if (Ustrcmp(buffer, "or") == 0) + { + condition_block *orc = store_get(sizeof(condition_block)); + condition_block *or_parent = NULL; + + if (current_parent != NULL) + { + while (current_parent->parent != NULL && + current_parent->parent->type == cond_and) + current_parent = current_parent->parent; + + /* If the parent has a parent, it must be an "or" parent. */ + + if (current_parent->parent != NULL) + or_parent = current_parent->parent; + } + + orc->parent = or_parent; + if (or_parent == NULL) *cond = orc; else + or_parent->right.c = orc; + orc->type = cond_or; + orc->testfor = TRUE; + orc->left.c = (current_parent == NULL)? c : current_parent; + orc->right.c = NULL; /* insurance */ + current = &(orc->right.c); + current_parent = orc; + } + + /* Otherwise there is a disaster */ + + else + { + *error_pointer = string_sprintf("\"and\" or \"or\" or \"%s\" " + "expected near line %d of filter file, but found \"%s\"", + toplevel? "then" : ")", line_number, buffer); + break; + } + } + } + +return nextsigchar(ptr, TRUE); +} + + + +/************************************************* +* Ouput the current indent * +*************************************************/ + +static void +indent(void) +{ +int i; +for (i = 0; i < output_indent; i++) debug_printf(" "); +} + + + +/************************************************* +* Condition printer: for debugging * +*************************************************/ + +/* +Arguments: + c the block at the top of the tree + toplevel TRUE at toplevel - stops overall brackets + +Returns: nothing +*/ + +static void +print_condition(condition_block *c, BOOL toplevel) +{ +char *name = (c->testfor)? cond_names[c->type] : cond_not_names[c->type]; +switch(c->type) + { + case cond_personal: + case cond_delivered: + case cond_errormsg: + case cond_firsttime: + case cond_manualthaw: + debug_printf("%s", name); + break; + + case cond_is: + case cond_IS: + case cond_matches: + case cond_MATCHES: + case cond_contains: + case cond_CONTAINS: + case cond_begins: + case cond_BEGINS: + case cond_ends: + case cond_ENDS: + case cond_above: + case cond_below: + debug_printf("%s %s %s", c->left.u, (char *)name, c->right.u); + break; + + case cond_and: + if (!c->testfor) debug_printf("not ("); + print_condition(c->left.c, FALSE); + debug_printf(" %s ", cond_names[c->type]); + print_condition(c->right.c, FALSE); + if (!c->testfor) debug_printf(")"); + break; + + case cond_or: + if (!c->testfor) debug_printf("not ("); + else if (!toplevel) debug_printf("("); + print_condition(c->left.c, FALSE); + debug_printf(" %s ", cond_names[c->type]); + print_condition(c->right.c, FALSE); + if (!toplevel || !c->testfor) debug_printf(")"); + break; + + case cond_foranyaddress: + debug_printf("%s %s (", name, c->left.u); + print_condition(c->right.c, FALSE); + debug_printf(")"); + break; + } +} + + + + +/************************************************* +* Read one filtering command * +*************************************************/ + +/* +Arguments: + pptr points to pointer to first character of command; the pointer + is updated to point after the last character read + lastcmdptr points to pointer to pointer to last command; used for hanging + on the newly read command + +Returns: TRUE if command successfully read, else FALSE +*/ + +static BOOL +read_command(uschar **pptr, filter_cmd ***lastcmdptr) +{ +int command, i, cmd_bit; +filter_cmd *new, **newlastcmdptr; +BOOL yield = TRUE; +BOOL was_seen_or_unseen = FALSE; +BOOL was_noerror = FALSE; +uschar buffer[1024]; +uschar *ptr = *pptr; +uschar *saveptr; +uschar *fmsg = NULL; + +/* Read the next word and find which command it is. Command words are normally +terminated by white space, but there are two exceptions, which are the "if" and +"elif" commands. We must allow for them to be terminated by an opening bracket, +as brackets are allowed in conditions and users will expect not to require +white space here. */ + +if (Ustrncmp(ptr, "if(", 3) == 0) + { + Ustrcpy(buffer, "if"); + ptr += 2; + } +else if (Ustrncmp(ptr, "elif(", 5) == 0) + { + Ustrcpy(buffer, "elif"); + ptr += 4; + } +else + { + ptr = nextword(ptr, buffer, sizeof(buffer), FALSE); + if (*error_pointer != NULL) return FALSE; + } + +for (command = 0; command < command_list_count; command++) + if (Ustrcmp(buffer, command_list[command]) == 0) break; + +/* Handle the individual commands */ + +switch (command) + { + /* Add takes two arguments, separated by the word "to". Headers has two + arguments, but the first must be "add", "remove", or "charset", and it gets + stored in the second argument slot. Neither may be preceded by seen, unseen + or noerror. */ + + case add_command: + case headers_command: + if (seen_force || noerror_force) + { + *error_pointer = string_sprintf("\"seen\", \"unseen\", or \"noerror\" " + "found before an \"%s\" command near line %d", + command_list[command], line_number); + yield = FALSE; + } + /* Fall through */ + + /* Logwrite, logfile, pipe, and testprint all take a single argument, save + and logfile can have an option second argument for the mode, and deliver can + have "errors_to
    " in a system filter, or in a user filter if the + address is the current one. */ + + case deliver_command: + case logfile_command: + case logwrite_command: + case pipe_command: + case save_command: + case testprint_command: + + ptr = nextitem(ptr, buffer, sizeof(buffer), FALSE); + if (*buffer == 0) + *error_pointer = string_sprintf("\"%s\" requires an argument " + "near line %d of filter file", command_list[command], line_number); + + if (*error_pointer != NULL) yield = FALSE; else + { + union argtypes argument, second_argument; + + argument.u = second_argument.u = NULL; + + if (command == add_command) + { + argument.u = string_copy(buffer); + ptr = nextitem(ptr, buffer, sizeof(buffer), FALSE); + if (*buffer == 0 || Ustrcmp(buffer, "to") != 0) + *error_pointer = string_sprintf("\"to\" expected in \"add\" command " + "near line %d of filter file", line_number); + else + { + ptr = nextitem(ptr, buffer, sizeof(buffer), FALSE); + if (*buffer == 0) + *error_pointer = string_sprintf("value missing after \"to\" " + "near line %d of filter file", line_number); + else second_argument.u = string_copy(buffer); + } + } + + else if (command == headers_command) + { + if (Ustrcmp(buffer, "add") == 0) + second_argument.b = TRUE; + else + if (Ustrcmp(buffer, "remove") == 0) second_argument.b = FALSE; + else + if (Ustrcmp(buffer, "charset") == 0) + second_argument.b = TRUE_UNSET; + else + { + *error_pointer = string_sprintf("\"add\", \"remove\", or \"charset\" " + "expected after \"headers\" near line %d of filter file", + line_number); + yield = FALSE; + } + + if (!system_filtering && second_argument.b != TRUE_UNSET) + { + *error_pointer = string_sprintf("header addition and removal is " + "available only in system filters: near line %d of filter file", + line_number); + yield = FALSE; + break; + } + + if (yield) + { + ptr = nextitem(ptr, buffer, sizeof(buffer), FALSE); + if (*buffer == 0) + *error_pointer = string_sprintf("value missing after \"add\", " + "\"remove\", or \"charset\" near line %d of filter file", + line_number); + else argument.u = string_copy(buffer); + } + } + + /* The argument for the logwrite command must end in a newline, and the save + and logfile commands can have an optional mode argument. The deliver + command can have an optional "errors_to
    " for a system filter, + or for a user filter if the address is the user's address. Accept the + syntax here - the check is later. */ + + else + { + if (command == logwrite_command) + { + int len = Ustrlen(buffer); + if (len == 0 || buffer[len-1] != '\n') Ustrcat(buffer, "\n"); + } + + argument.u = string_copy(buffer); + + if (command == save_command || command == logfile_command) + { + if (isdigit(*ptr)) + { + ptr = nextword(ptr, buffer, sizeof(buffer), FALSE); + second_argument.i = (int)Ustrtol(buffer, NULL, 8); + } + else second_argument.i = -1; + } + + else if (command == deliver_command) + { + uschar *save_ptr = ptr; + ptr = nextword(ptr, buffer, sizeof(buffer), FALSE); + if (Ustrcmp(buffer, "errors_to") == 0) + { + ptr = nextword(ptr, buffer, sizeof(buffer), FALSE); + second_argument.u = string_copy(buffer); + } + else ptr = save_ptr; + } + } + + /* Set up the command block. Seen defaults TRUE for delivery commands, + FALSE for logging commands, and it doesn't matter for testprint, as + that doesn't change the "delivered" status. */ + + if (*error_pointer != NULL) yield = FALSE; else + { + new = store_get(sizeof(filter_cmd) + sizeof(union argtypes)); + new->next = NULL; + **lastcmdptr = new; + *lastcmdptr = &(new->next); + new->command = command; + new->seen = seen_force? seen_value : command_exparg_count[command] >= 128; + new->noerror = noerror_force; + new->args[0] = argument; + new->args[1] = second_argument; + } + } + break; + + + /* Elif, else and endif just set a flag if expected. */ + + case elif_command: + case else_command: + case endif_command: + if (expect_endif > 0) + had_else_endif = (command == elif_command)? had_elif : + (command == else_command)? had_else : had_endif; + else + { + *error_pointer = string_sprintf("unexpected \"%s\" command near " + "line %d of filter file", buffer, line_number); + yield = FALSE; + } + break; + + + /* Defer, freeze, and fail are available only if permitted. */ + + case defer_command: + cmd_bit = RDO_DEFER; + goto DEFER_FREEZE_FAIL; + + case fail_command: + cmd_bit = RDO_FAIL; + goto DEFER_FREEZE_FAIL; + + case freeze_command: + cmd_bit = RDO_FREEZE; + + DEFER_FREEZE_FAIL: + if ((filter_options & cmd_bit) == 0) + { + *error_pointer = string_sprintf("filtering command \"%s\" is disabled: " + "near line %d of filter file", buffer, line_number); + yield = FALSE; + break; + } + + /* A text message can be provided after the "text" keyword, or + as a string in quotes. */ + + saveptr = ptr; + ptr = nextitem(ptr, buffer, sizeof(buffer), FALSE); + if (*saveptr != '\"' && (*buffer == 0 || Ustrcmp(buffer, "text") != 0)) + { + ptr = saveptr; + fmsg = US""; + } + else + { + if (*saveptr != '\"') + ptr = nextitem(ptr, buffer, sizeof(buffer), FALSE); + fmsg = string_copy(buffer); + } + + /* Drop through and treat as "finish", but never set "seen". */ + + seen_value = FALSE; + + /* Finish has no arguments; fmsg defaults to NULL */ + + case finish_command: + new = store_get(sizeof(filter_cmd)); + new->next = NULL; + **lastcmdptr = new; + *lastcmdptr = &(new->next); + new->command = command; + new->seen = seen_force? seen_value : FALSE; + new->args[0].u = fmsg; + break; + + + /* Seen, unseen, and noerror are not allowed before if, which takes a + condition argument and then and else sub-commands. */ + + case if_command: + if (seen_force || noerror_force) + { + *error_pointer = string_sprintf("\"seen\", \"unseen\", or \"noerror\" " + "found before an \"if\" command near line %d", + line_number); + yield = FALSE; + } + + /* Set up the command block for if */ + + new = store_get(sizeof(filter_cmd) + 4 * sizeof(union argtypes)); + new->next = NULL; + **lastcmdptr = new; + *lastcmdptr = &(new->next); + new->command = command; + new->seen = FALSE; + new->args[0].u = NULL; + new->args[1].u = new->args[2].u = NULL; + new->args[3].u = ptr; + + /* Read the condition */ + + ptr = read_condition(ptr, &(new->args[0].c), TRUE); + if (*error_pointer != NULL) { yield = FALSE; break; } + + /* Read the commands to be obeyed if the condition is true */ + + newlastcmdptr = &(new->args[1].f); + if (!read_command_list(&ptr, &newlastcmdptr, TRUE)) yield = FALSE; + + /* If commands were successfully read, handle the various possible + terminators. There may be a number of successive "elif" sections. */ + + else + { + while (had_else_endif == had_elif) + { + filter_cmd *newnew = + store_get(sizeof(filter_cmd) + 4 * sizeof(union argtypes)); + new->args[2].f = newnew; + new = newnew; + new->next = NULL; + new->command = command; + new->seen = FALSE; + new->args[0].u = NULL; + new->args[1].u = new->args[2].u = NULL; + new->args[3].u = ptr; + + ptr = read_condition(ptr, &(new->args[0].c), TRUE); + if (*error_pointer != NULL) { yield = FALSE; break; } + newlastcmdptr = &(new->args[1].f); + if (!read_command_list(&ptr, &newlastcmdptr, TRUE)) + yield = FALSE; + } + + if (yield == FALSE) break; + + /* Handle termination by "else", possibly following one or more + "elsif" sections. */ + + if (had_else_endif == had_else) + { + newlastcmdptr = &(new->args[2].f); + if (!read_command_list(&ptr, &newlastcmdptr, TRUE)) + yield = FALSE; + else if (had_else_endif != had_endif) + { + *error_pointer = string_sprintf("\"endif\" missing near line %d of " + "filter file", line_number); + yield = FALSE; + } + } + + /* Otherwise the terminator was "endif" - this is checked by + read_command_list(). The pointer is already set to NULL. */ + } + + /* Reset the terminator flag. */ + + had_else_endif = had_neither; + break; + + + /* The mail & vacation commands have a whole slew of keyworded arguments. + The final argument values are the file expand and return message booleans, + whose offsets are defined in mailarg_index_{expand,return}. Although they + are logically booleans, because they are stored in a uschar * value, we use + NULL and not FALSE, to keep 64-bit compilers happy. */ + + case mail_command: + case vacation_command: + new = store_get(sizeof(filter_cmd) + mailargs_total * sizeof(union argtypes)); + new->next = NULL; + new->command = command; + new->seen = seen_force? seen_value : FALSE; + new->noerror = noerror_force; + for (i = 0; i < mailargs_total; i++) new->args[i].u = NULL; + + /* Read keyword/value pairs until we hit one that isn't. The data + must contain only printing chars plus tab, though the "text" value + can also contain newlines. The "file" keyword can be preceded by the + word "expand", and "return message" has no data. */ + + for (;;) + { + uschar *saveptr = ptr; + ptr = nextword(ptr, buffer, sizeof(buffer), FALSE); + if (*error_pointer != NULL) + { + yield = FALSE; + break; + } + + /* Ensure "return" is followed by "message"; that's a complete option */ + + if (Ustrcmp(buffer, "return") == 0) + { + new->args[mailarg_index_return].u = US""; /* not NULL => TRUE */ + ptr = nextword(ptr, buffer, sizeof(buffer), FALSE); + if (Ustrcmp(buffer, "message") != 0) + { + *error_pointer = string_sprintf("\"return\" not followed by \"message\" " + " near line %d of filter file", line_number); + yield = FALSE; + break; + } + continue; + } + + /* Ensure "expand" is followed by "file", then fall through to process the + file keyword. */ + + if (Ustrcmp(buffer, "expand") == 0) + { + new->args[mailarg_index_expand].u = US""; /* not NULL => TRUE */ + ptr = nextword(ptr, buffer, sizeof(buffer), FALSE); + if (Ustrcmp(buffer, "file") != 0) + { + *error_pointer = string_sprintf("\"expand\" not followed by \"file\" " + " near line %d of filter file", line_number); + yield = FALSE; + break; + } + } + + /* Scan for the keyword */ + + for (i = 0; i < MAILARGS_STRING_COUNT; i++) + if (Ustrcmp(buffer, mailargs[i]) == 0) break; + + /* Not found keyword; assume end of this command */ + + if (i >= MAILARGS_STRING_COUNT) + { + ptr = saveptr; + break; + } + + /* Found keyword, read the data item */ + + ptr = nextitem(ptr, buffer, sizeof(buffer), FALSE); + if (*error_pointer != NULL) + { + yield = FALSE; + break; + } + else new->args[i].u = string_copy(buffer); + } + + /* If this is the vacation command, apply some default settings to + some of the arguments. */ + + if (command == vacation_command) + { + if (new->args[mailarg_index_file].u == NULL) + { + new->args[mailarg_index_file].u = string_copy(US".vacation.msg"); + new->args[mailarg_index_expand].u = US""; /* not NULL => TRUE */ + } + if (new->args[mailarg_index_log].u == NULL) + new->args[mailarg_index_log].u = string_copy(US".vacation.log"); + if (new->args[mailarg_index_once].u == NULL) + new->args[mailarg_index_once].u = string_copy(US".vacation"); + if (new->args[mailarg_index_once_repeat].u == NULL) + new->args[mailarg_index_once_repeat].u = string_copy(US"7d"); + if (new->args[mailarg_index_subject].u == NULL) + new->args[mailarg_index_subject].u = string_copy(US"On vacation"); + } + + /* Join the address on to the chain of generated addresses */ + + **lastcmdptr = new; + *lastcmdptr = &(new->next); + break; + + + /* Seen and unseen just set flags */ + + case seen_command: + case unseen_command: + if (seen_force) + { + *error_pointer = string_sprintf("\"seen\" or \"unseen\" repeated " + "near line %d", line_number); + yield = FALSE; + } + seen_value = (command == seen_command); + seen_force = TRUE; + was_seen_or_unseen = TRUE; + break; + + + /* So does noerror */ + + case noerror_command: + noerror_force = TRUE; + was_noerror = TRUE; + break; + + + /* Oops */ + + default: + *error_pointer = string_sprintf("unknown filtering command \"%s\" " + "near line %d of filter file", buffer, line_number); + yield = FALSE; + break; + } + +if (!was_seen_or_unseen && !was_noerror) + { + seen_force = FALSE; + noerror_force = FALSE; + } + +*pptr = ptr; +return yield; +} + + + +/************************************************* +* Read a list of commands * +*************************************************/ + +/* If condional is TRUE, the list must be terminated +by the words "else" or "endif". + +Arguments: + pptr points to pointer to next character; the pointer is updated + lastcmdptr points to pointer to pointer to previously-read command; used + for hanging on the new command + conditional TRUE if this command is the subject of a condition + +Returns: TRUE on success +*/ + +static BOOL +read_command_list(uschar **pptr, filter_cmd ***lastcmdptr, BOOL conditional) +{ +if (conditional) expect_endif++; +had_else_endif = had_neither; +while (**pptr != 0 && had_else_endif == had_neither) + { + if (!read_command(pptr, lastcmdptr)) return FALSE; + *pptr = nextsigchar(*pptr, TRUE); + } +if (conditional) + { + expect_endif--; + if (had_else_endif == had_neither) + { + *error_pointer = US"\"endif\" missing at end of filter file"; + return FALSE; + } + } +return TRUE; +} + + + + +/************************************************* +* Test a condition * +*************************************************/ + +/* +Arguments: + c points to the condition block; c->testfor indicated whether + it's a positive or negative condition + toplevel TRUE if called from "if" directly; FALSE otherwise + +Returns: TRUE if the condition is met +*/ + +static BOOL +test_condition(condition_block *c, BOOL toplevel) +{ +BOOL yield; +const pcre *re; +uschar *exp[2], *p, *pp; +const uschar *regcomp_error = NULL; +int regcomp_error_offset; +int val[2]; +int i; + +if (c == NULL) return TRUE; /* does this ever occur? */ + +switch (c->type) + { + case cond_and: + yield = test_condition(c->left.c, FALSE) && + *error_pointer == NULL && + test_condition(c->right.c, FALSE); + break; + + case cond_or: + yield = test_condition(c->left.c, FALSE) || + (*error_pointer == NULL && + test_condition(c->right.c, FALSE)); + break; + + /* The personal test is meaningless in a system filter. The tests are now in + a separate function (so Sieve can use them). However, an Exim filter does not + scan Cc: (hence the FALSE argument). */ + + case cond_personal: + yield = system_filtering? FALSE : filter_personal(c->left.a, FALSE); + break; + + case cond_delivered: + yield = filter_delivered; + break; + + /* Only TRUE if a message is actually being processed; FALSE for address + testing and verification. */ + + case cond_errormsg: + yield = message_id[0] != 0 && + (sender_address == NULL || sender_address[0] == 0); + break; + + /* Only FALSE if a message is actually being processed; TRUE for address + and filter testing and verification. */ + + case cond_firsttime: + yield = filter_test != NULL || message_id[0] == 0 || deliver_firsttime; + break; + + /* Only TRUE if a message is actually being processed; FALSE for address + testing and verification. */ + + case cond_manualthaw: + yield = message_id[0] != 0 && deliver_manual_thaw; + break; + + /* The foranyaddress condition loops through a list of addresses */ + + case cond_foranyaddress: + p = c->left.u; + pp = expand_string(p); + if (pp == NULL) + { + *error_pointer = string_sprintf("failed to expand \"%s\" in " + "filter file: %s", p, expand_string_message); + return FALSE; + } + + yield = FALSE; + parse_allow_group = TRUE; /* Allow group syntax */ + + while (*pp != 0) + { + uschar *error; + int start, end, domain; + int saveend; + + p = parse_find_address_end(pp, FALSE); + saveend = *p; + + *p = 0; + filter_thisaddress = + parse_extract_address(pp, &error, &start, &end, &domain, FALSE); + *p = saveend; + + if (filter_thisaddress != NULL) + { + if ((filter_test != NULL && debug_selector != 0) || + (debug_selector & D_filter) != 0) + { + indent(); + debug_printf("Extracted address %s\n", filter_thisaddress); + } + yield = test_condition(c->right.c, FALSE); + } + + if (yield) break; + if (saveend == 0) break; + pp = p + 1; + } + + parse_allow_group = FALSE; /* Reset group syntax flags */ + parse_found_group = FALSE; + break; + + /* All other conditions have left and right values that need expanding; + on error, it doesn't matter what value is returned. */ + + default: + p = c->left.u; + for (i = 0; i < 2; i++) + { + exp[i] = expand_string(p); + if (exp[i] == NULL) + { + *error_pointer = string_sprintf("failed to expand \"%s\" in " + "filter file: %s", p, expand_string_message); + return FALSE; + } + p = c->right.u; + } + + /* Inner switch for the different cases */ + + switch(c->type) + { + case cond_is: + yield = strcmpic(exp[0], exp[1]) == 0; + break; + + case cond_IS: + yield = Ustrcmp(exp[0], exp[1]) == 0; + break; + + case cond_contains: + yield = strstric(exp[0], exp[1], FALSE) != NULL; + break; + + case cond_CONTAINS: + yield = Ustrstr(exp[0], exp[1]) != NULL; + break; + + case cond_begins: + yield = strncmpic(exp[0], exp[1], Ustrlen(exp[1])) == 0; + break; + + case cond_BEGINS: + yield = Ustrncmp(exp[0], exp[1], Ustrlen(exp[1])) == 0; + break; + + case cond_ends: + case cond_ENDS: + { + int len = Ustrlen(exp[1]); + uschar *s = exp[0] + Ustrlen(exp[0]) - len; + yield = (s < exp[0])? FALSE : + ((c->type == cond_ends)? strcmpic(s, exp[1]) : Ustrcmp(s, exp[1])) == 0; + } + break; + + case cond_matches: + case cond_MATCHES: + if ((filter_test != NULL && debug_selector != 0) || + (debug_selector & D_filter) != 0) + { + debug_printf("Match expanded arguments:\n"); + debug_printf(" Subject = %s\n", exp[0]); + debug_printf(" Pattern = %s\n", exp[1]); + } + + re = pcre_compile(CS exp[1], + PCRE_COPT | ((c->type == cond_matches)? PCRE_CASELESS : 0), + (const char **)®comp_error, ®comp_error_offset, NULL); + + if (re == NULL) + { + *error_pointer = string_sprintf("error while compiling " + "regular expression \"%s\": %s at offset %d", + exp[1], regcomp_error, regcomp_error_offset); + return FALSE; + } + + yield = regex_match_and_setup(re, exp[0], PCRE_EOPT, -1); + break; + + /* For above and below, convert the strings to numbers */ + + case cond_above: + case cond_below: + for (i = 0; i < 2; i++) + { + val[i] = get_number(exp[i], &yield); + if (!yield) + { + *error_pointer = string_sprintf("malformed numerical string \"%s\"", + exp[i]); + return FALSE; + } + } + yield = (c->type == cond_above)? (val[0] > val[1]) : (val[0] < val[1]); + break; + } + break; + } + +if ((filter_test != NULL && debug_selector != 0) || + (debug_selector & D_filter) != 0) + { + indent(); + debug_printf("%sondition is %s: ", + toplevel? "C" : "Sub-c", + (yield == c->testfor)? "true" : "false"); + print_condition(c, TRUE); + debug_printf("\n"); + } + +return yield == c->testfor; +} + + + +/************************************************* +* Interpret chain of commands * +*************************************************/ + +/* In testing state, just say what would be done rather than doing it. The +testprint command just expands and outputs its argument in testing state, and +does nothing otherwise. + +Arguments: + commands points to chain of commands to interpret + generated where to hang newly-generated addresses + +Returns: FF_DELIVERED success, a significant action was taken + FF_NOTDELIVERED success, no significant action + FF_DEFER defer requested + FF_FAIL fail requested + FF_FREEZE freeze requested + FF_ERROR there was a problem +*/ + +static int +interpret_commands(filter_cmd *commands, address_item **generated) +{ +uschar *s; +int mode; +address_item *addr; +BOOL condition_value; + +while (commands != NULL) + { + int ff_ret; + uschar *fmsg, *ff_name; + uschar *expargs[MAILARGS_STRING_COUNT]; + + int i, n[2]; + + /* Expand the relevant number of arguments for the command that are + not NULL. */ + + for (i = 0; i < (command_exparg_count[commands->command] & 15); i++) + { + uschar *ss = commands->args[i].u; + if (ss == NULL) + { + expargs[i] = NULL; + } + else + { + expargs[i] = expand_string(ss); + if (expargs[i] == NULL) + { + *error_pointer = string_sprintf("failed to expand \"%s\" in " + "%s command: %s", ss, command_list[commands->command], + expand_string_message); + return FF_ERROR; + } + } + } + + /* Now switch for each command, setting the "delivered" flag if any of them + have "seen" set. */ + + if (commands->seen) filter_delivered = TRUE; + + switch(commands->command) + { + case add_command: + for (i = 0; i < 2; i++) + { + uschar *ss = expargs[i]; + uschar *end; + + if (i == 1 && (*ss++ != 'n' || ss[1] != 0)) + { + *error_pointer = string_sprintf("unknown variable \"%s\" in \"add\" " + "command", expargs[i]); + return FF_ERROR; + } + + /* Allow for "--" at the start of the value (from -$n0) for example */ + if (i == 0) while (ss[0] == '-' && ss[1] == '-') ss += 2; + + n[i] = (int)Ustrtol(ss, &end, 0); + if (*end != 0) + { + *error_pointer = string_sprintf("malformed number \"%s\" in \"add\" " + "command", ss); + return FF_ERROR; + } + } + + filter_n[n[1]] += n[0]; + if (filter_test != NULL) printf("Add %d to n%d\n", n[0], n[1]); + break; + + /* A deliver command's argument must be a valid address. Its optional + second argument (system filter only) must also be a valid address. */ + + case deliver_command: + for (i = 0; i < 2; i++) + { + s = expargs[i]; + if (s != NULL) + { + int start, end, domain; + uschar *error; + uschar *ss = parse_extract_address(s, &error, &start, &end, &domain, + FALSE); + if (ss != NULL) + expargs[i] = ((filter_options & RDO_REWRITE) != 0)? + rewrite_address(ss, TRUE, FALSE, global_rewrite_rules, + rewrite_existflags) : + rewrite_address_qualify(ss, TRUE); + else + { + *error_pointer = string_sprintf("malformed address \"%s\" in " + "filter file: %s", s, error); + return FF_ERROR; + } + } + } + + /* Stick the errors address into a simple variable, as it will + be referenced a few times. Check that the caller is permitted to + specify it. */ + + s = expargs[1]; + + if (s != NULL && !system_filtering) + { + uschar *ownaddress = expand_string(US"$local_part@$domain"); + if (strcmpic(ownaddress, s) != 0) + { + *error_pointer = US"errors_to must point to the caller's address"; + return FF_ERROR; + } + } + + /* Test case: report what would happen */ + + if (filter_test != NULL) + { + indent(); + printf("%seliver message to: %s%s%s%s\n", + (commands->seen)? "D" : "Unseen d", + expargs[0], + commands->noerror? " (noerror)" : "", + (s != NULL)? " errors_to " : "", + (s != NULL)? s : US""); + } + + /* Real case. */ + + else + { + DEBUG(D_filter) debug_printf("Filter: %sdeliver message to: %s%s%s%s\n", + (commands->seen)? "" : "unseen ", + expargs[0], + commands->noerror? " (noerror)" : "", + (s != NULL)? " errors_to " : "", + (s != NULL)? s : US""); + + /* Create the new address and add it to the chain, setting the + af_ignore_error flag if necessary, and the errors address, which can be + set in a system filter and to the local address in user filters. */ + + addr = deliver_make_addr(expargs[0], TRUE); /* TRUE => copy s */ + addr->p.errors_address = (s == NULL)? + s : string_copy(s); /* Default is NULL */ + if (commands->noerror) setflag(addr, af_ignore_error); + addr->next = *generated; + *generated = addr; + } + break; + + case save_command: + s = expargs[0]; + mode = commands->args[1].i; + + /* Test case: report what would happen */ + + if (filter_test != NULL) + { + indent(); + if (mode < 0) + printf("%save message to: %s%s\n", (commands->seen)? + "S" : "Unseen s", s, commands->noerror? " (noerror)" : ""); + else + printf("%save message to: %s %04o%s\n", (commands->seen)? + "S" : "Unseen s", s, mode, commands->noerror? " (noerror)" : ""); + } + + /* Real case: Ensure save argument starts with / if there is a home + directory to prepend. */ + + else + { + DEBUG(D_filter) debug_printf("Filter: %ssave message to: %s%s\n", + (commands->seen)? "" : "unseen ", s, + commands->noerror? " (noerror)" : ""); + if (s[0] != '/' && deliver_home != NULL && deliver_home[0] != 0) + s = string_sprintf("%s/%s", deliver_home, s); + + /* Create the new address and add it to the chain, setting the + af_pfr and af_file flags, the af_ignore_error flag if necessary, and the + mode value. */ + + addr = deliver_make_addr(s, TRUE); /* TRUE => copy s */ + setflag(addr, af_pfr|af_file); + if (commands->noerror) setflag(addr, af_ignore_error); + addr->mode = mode; + addr->next = *generated; + *generated = addr; + } + break; + + case pipe_command: + s = string_copy(commands->args[0].u); + if (filter_test != NULL) + { + indent(); + printf("%sipe message to: %s%s\n", (commands->seen)? + "P" : "Unseen p", s, commands->noerror? " (noerror)" : ""); + } + else /* Ensure pipe command starts with | */ + { + DEBUG(D_filter) debug_printf("Filter: %spipe message to: %s%s\n", + (commands->seen)? "" : "unseen ", s, + commands->noerror? " (noerror)" : ""); + if (s[0] != '|') s = string_sprintf("|%s", s); + + /* Create the new address and add it to the chain, setting the + af_ignore_error flag if necessary. Set the af_expand_pipe flag so that + each command argument is expanded in the transport after the command + has been split up into separate arguments. */ + + addr = deliver_make_addr(s, TRUE); /* TRUE => copy s */ + setflag(addr, af_pfr|af_expand_pipe); + if (commands->noerror) setflag(addr, af_ignore_error); + addr->next = *generated; + *generated = addr; + + /* If there are any numeric variables in existence (e.g. after a regex + condition), or if $thisaddress is set, take a copy for use in the + expansion. Note that we can't pass NULL for filter_thisaddress, because + NULL terminates the list. */ + + if (expand_nmax >= 0 || filter_thisaddress != NULL) + { + int i; + int ecount = (expand_nmax >= 0)? expand_nmax : -1; + uschar **ss = store_get(sizeof(uschar *) * (ecount + 3)); + addr->pipe_expandn = ss; + if (filter_thisaddress == NULL) filter_thisaddress = US""; + *ss++ = string_copy(filter_thisaddress); + for (i = 0; i <= expand_nmax; i++) + *ss++ = string_copyn(expand_nstring[i], expand_nlength[i]); + *ss = NULL; + } + } + break; + + /* Set up the file name and mode, and close any previously open + file. */ + + case logfile_command: + log_mode = commands->args[1].i; + if (log_mode == -1) log_mode = 0600; + if (log_fd >= 0) + { + close(log_fd); + log_fd = -1; + } + log_filename = expargs[0]; + if (filter_test != NULL) + { + indent(); + printf("%sogfile %s\n", (commands->seen)? "Seen l" : "L", log_filename); + } + break; + + case logwrite_command: + s = expargs[0]; + + if (filter_test != NULL) + { + indent(); + printf("%sogwrite \"%s\"\n", (commands->seen)? "Seen l" : "L", + string_printing(s)); + } + + /* Attempt to write to a log file only if configured as permissible. + Logging may be forcibly skipped for verifying or testing. */ + + else if ((filter_options & RDO_LOG) != 0) /* Locked out */ + { + DEBUG(D_filter) + debug_printf("filter log command aborted: euid=%ld\n", + (long int)geteuid()); + *error_pointer = US"logwrite command forbidden"; + return FF_ERROR; + } + else if ((filter_options & RDO_REALLOG) != 0) + { + int len; + DEBUG(D_filter) debug_printf("writing filter log as euid %ld\n", + (long int)geteuid()); + if (log_fd < 0) + { + if (log_filename == NULL) + { + *error_pointer = US"attempt to obey \"logwrite\" command " + "without a previous \"logfile\""; + return FF_ERROR; + } + log_fd = Uopen(log_filename, O_CREAT|O_APPEND|O_WRONLY, log_mode); + if (log_fd < 0) + { + *error_pointer = string_open_failed(errno, "filter log file \"%s\"", + log_filename); + return FF_ERROR; + } + } + len = Ustrlen(s); + if (write(log_fd, s, len) != len) + { + *error_pointer = string_sprintf("write error on file \"%s\": %s", + log_filename, strerror(errno)); + return FF_ERROR; + } + } + else + { + DEBUG(D_filter) debug_printf("skipping logwrite (verifying or testing)\n"); + } + break; + + /* Header addition and removal is available only in the system filter. The + command is rejected at parse time otherwise. However "headers charset" is + always permitted. */ + + case headers_command: + { + int subtype = commands->args[1].i; + s = expargs[0]; + + if (filter_test != NULL) + printf("Headers %s \"%s\"\n", (subtype == TRUE)? "add" : + (subtype == FALSE)? "remove" : "charset", string_printing(s)); + + if (subtype == TRUE) + { + while (isspace(*s)) s++; + if (s[0] != 0) + { + header_add(htype_other, "%s%s", s, (s[Ustrlen(s)-1] == '\n')? + "" : "\n"); + header_last->type = header_checkname(header_last, FALSE); + if (header_last->type >= 'a') header_last->type = htype_other; + } + } + + else if (subtype == FALSE) + { + int sep = 0; + uschar *ss; + uschar *list = s; + uschar buffer[128]; + while ((ss = string_nextinlist(&list, &sep, buffer, sizeof(buffer))) + != NULL) + header_remove(0, ss); + } + + /* This setting lasts only while the filter is running; on exit, the + variable is reset to the previous value. */ + + else headers_charset = s; + } + break; + + /* Defer, freeze, and fail are available only when explicitly permitted. + These commands are rejected at parse time otherwise. The message can get + very long by the inclusion of message headers; truncate if it is, and also + ensure printing characters so as not to mess up log files. */ + + case defer_command: + ff_name = US"defer"; + ff_ret = FF_DEFER; + goto DEFERFREEZEFAIL; + + case fail_command: + ff_name = US"fail"; + ff_ret = FF_FAIL; + goto DEFERFREEZEFAIL; + + case freeze_command: + ff_name = US"freeze"; + ff_ret = FF_FREEZE; + + DEFERFREEZEFAIL: + fmsg = expargs[0]; + if (Ustrlen(fmsg) > 1024) Ustrcpy(fmsg + 1000, " ... (truncated)"); + fmsg = string_printing(fmsg); + *error_pointer = fmsg; + + if (filter_test != NULL) + { + indent(); + printf("%c%s text \"%s\"\n", toupper(ff_name[0]), ff_name+1, fmsg); + } + else DEBUG(D_filter) debug_printf("Filter: %s \"%s\"\n", ff_name, fmsg); + return ff_ret; + + case finish_command: + if (filter_test != NULL) + { + indent(); + printf("%sinish\n", (commands->seen)? "Seen f" : "F"); + } + else + { + DEBUG(D_filter) debug_printf("Filter: %sfinish\n", + (commands->seen)? " Seen " : ""); + } + finish_obeyed = TRUE; + return filter_delivered? FF_DELIVERED : FF_NOTDELIVERED; + + case if_command: + { + uschar *save_address = filter_thisaddress; + int ok = FF_DELIVERED; + condition_value = test_condition(commands->args[0].c, TRUE); + if (*error_pointer != NULL) ok = FF_ERROR; else + { + output_indent += 2; + ok = interpret_commands(commands->args[condition_value? 1:2].f, + generated); + output_indent -= 2; + } + filter_thisaddress = save_address; + if (finish_obeyed || (ok != FF_DELIVERED && ok != FF_NOTDELIVERED)) + return ok; + } + break; + + + /* To try to catch runaway loops, do not generate mail if the + return path is unset or if a non-trusted user supplied -f <> + as the return path. */ + + case mail_command: + case vacation_command: + if (return_path == NULL || return_path[0] == 0) + { + if (filter_test != NULL) + printf("%s command ignored because return_path is empty\n", + command_list[commands->command]); + else DEBUG(D_filter) debug_printf("%s command ignored because return_path " + "is empty\n", command_list[commands->command]); + break; + } + + /* Check the contents of the strings. The type of string can be deduced + from the value of i. + + . If i is equal to mailarg_index_text it's a text string for the body, + where anything goes. + + . If i is > mailarg_index_text, we are dealing with a file name, which + cannot contain non-printing characters. + + . If i is less than mailarg_index_headers we are dealing with something + that will go in a single message header line, where newlines must be + followed by white space. + + . If i is equal to mailarg_index_headers, we have a string that contains + one or more headers. Newlines that are not followed by white space must + be followed by a header name. + */ + + for (i = 0; i < MAILARGS_STRING_COUNT; i++) + { + uschar *p; + uschar *s = expargs[i]; + + if (s == NULL) continue; + + if (i != mailarg_index_text) for (p = s; *p != 0; p++) + { + int c = *p; + if (i > mailarg_index_text) + { + if (!mac_isprint(c)) + { + *error_pointer = string_sprintf("non-printing character in \"%s\" " + "in %s command", string_printing(s), + command_list[commands->command]); + return FF_ERROR; + } + } + + /* i < mailarg_index_text */ + + else if (c == '\n' && !isspace(p[1])) + { + if (i < mailarg_index_headers) + { + *error_pointer = string_sprintf("\\n not followed by space in " + "\"%.1024s\" in %s command", string_printing(s), + command_list[commands->command]); + return FF_ERROR; + } + + /* Check for the start of a new header line within the string */ + + else + { + uschar *pp; + for (pp = p + 1;; pp++) + { + c = *pp; + if (c == ':' && pp != p + 1) break; + if (c == 0 || c == ':' || isspace(*pp)) + { + *error_pointer = string_sprintf("\\n not followed by space or " + "valid header name in \"%.1024s\" in %s command", + string_printing(s), command_list[commands->command]); + return FF_ERROR; + } + pp++; + } + p = pp; + } + } + } /* Loop to scan the string */ + + /* The string is OK */ + + commands->args[i].u = s; + } + + /* Proceed with mail or vacation command */ + + if (filter_test != NULL) + { + uschar *to = commands->args[mailarg_index_to].u; + indent(); + printf("%sail to: %s%s%s\n", (commands->seen)? "Seen m" : "M", + (to == NULL)? US"" : to, + (commands->command == vacation_command)? " (vacation)" : "", + (commands->noerror)? " (noerror)" : ""); + for (i = 1; i < MAILARGS_STRING_COUNT; i++) + { + uschar *arg = commands->args[i].u; + if (arg != NULL) + { + int len = Ustrlen(mailargs[i]); + int indent = (debug_selector != 0)? output_indent : 0; + while (len++ < 7 + indent) printf(" "); + printf("%s: %s%s\n", mailargs[i], string_printing(arg), + (commands->args[mailarg_index_expand].u != NULL && + Ustrcmp(mailargs[i], "file") == 0)? " (expanded)" : ""); + } + } + if (commands->args[mailarg_index_return].u != NULL) + printf("Return original message\n"); + } + else + { + uschar *tt; + uschar *to = commands->args[mailarg_index_to].u; + if (to == NULL) to = expand_string(US"$reply_address"); + while (isspace(*to)) to++; + + for (tt = to; *tt != 0; tt++) /* Get rid of newlines so that */ + if (*tt == '\n') *tt = ' '; /* the eventual log line is OK */ + + DEBUG(D_filter) + { + debug_printf("Filter: %smail to: %s%s%s\n", + (commands->seen)? "seen " : "", + to, + (commands->command == vacation_command)? " (vacation)" : "", + (commands->noerror)? " (noerror)" : ""); + for (i = 1; i < MAILARGS_STRING_COUNT; i++) + { + uschar *arg = commands->args[i].u; + if (arg != NULL) + { + int len = Ustrlen(mailargs[i]); + while (len++ < 15) debug_printf(" "); + debug_printf("%s: %s%s\n", mailargs[i], string_printing(arg), + (commands->args[mailarg_index_expand].u != NULL && + Ustrcmp(mailargs[i], "file") == 0)? " (expanded)" : ""); + } + } + } + + /* Create the "address" for the autoreply */ + + addr = deliver_make_addr(string_sprintf(">%.256s", to), FALSE); + setflag(addr, af_pfr); + if (commands->noerror) setflag(addr, af_ignore_error); + addr->next = *generated; + *generated = addr; + addr->reply = store_get(sizeof(reply_item)); + addr->reply->from = NULL; + addr->reply->to = string_copy(to); + addr->reply->file_expand = + commands->args[mailarg_index_expand].u != NULL; + addr->reply->expand_forbid = expand_forbid; + addr->reply->return_message = + commands->args[mailarg_index_return].u != NULL; + addr->reply->once_repeat = 0; + + if (commands->args[mailarg_index_once_repeat].u != NULL) + { + addr->reply->once_repeat = + readconf_readtime(commands->args[mailarg_index_once_repeat].u, 0, + FALSE); + if (addr->reply->once_repeat < 0) + { + *error_pointer = string_sprintf("Bad time value for \"once_repeat\" " + "in mail or vacation command: %s", + commands->args[mailarg_index_once_repeat]); + return FF_ERROR; + } + } + + /* Set up all the remaining string arguments (those other than "to") */ + + for (i = 1; i < mailargs_string_passed; i++) + { + uschar *ss = commands->args[i].u; + *((uschar **)(((uschar *)(addr->reply)) + reply_offsets[i])) = + (ss == NULL)? NULL : string_copy(ss); + } + } + break; + + case testprint_command: + if (filter_test != NULL || (debug_selector & D_filter) != 0) + { + uschar *s = string_printing(expargs[0]); + if (filter_test == NULL) + debug_printf("Filter: testprint: %s\n", s); + else + printf("Testprint: %s\n", s); + } + } + + commands = commands->next; + } + +return filter_delivered? FF_DELIVERED : FF_NOTDELIVERED; +} + + + +/************************************************* +* Test for a personal message * +*************************************************/ + +/* This function is global so that it can also be called from the code that +implements Sieve filters. + +Arguments: + aliases a chain of aliases + scan_cc TRUE if Cc: and Bcc: are to be scanned (Exim filters do not) + +Returns: TRUE if the message is deemed to be personal +*/ + +BOOL +filter_personal(string_item *aliases, BOOL scan_cc) +{ +uschar *self, *self_from, *self_to; +uschar *psself = NULL, *psself_from = NULL, *psself_to = NULL; +void *reset_point = store_get(0); +BOOL yield; +header_line *h; +int to_count = 2; +int from_count = 9; + +/* If any header line in the message starts with "List-", it is not +a personal message. */ + +for (h = header_list; h != NULL; h = h->next) + { + if (h->type != htype_old && h->slen > 5 && + strncmpic(h->text, US"List-", 5) == 0) + return FALSE; + } + +/* Set up "my" address */ + +self = string_sprintf("%s@%s", deliver_localpart, deliver_domain); +self_from = rewrite_one(self, rewrite_from, NULL, FALSE, US"", + global_rewrite_rules); +self_to = rewrite_one(self, rewrite_to, NULL, FALSE, US"", + global_rewrite_rules); + + +if (self_from == NULL) self_from = self; +if (self_to == NULL) self_to = self; + +/* If there's a prefix or suffix set, we must include the prefixed/ +suffixed version of the local part in the tests. */ + +if (deliver_localpart_prefix != NULL || deliver_localpart_suffix != NULL) + { + psself = string_sprintf("%s%s%s@%s", + (deliver_localpart_prefix == NULL)? US"" : deliver_localpart_prefix, + deliver_localpart, + (deliver_localpart_suffix == NULL)? US"" : deliver_localpart_suffix, + deliver_domain); + psself_from = rewrite_one(psself, rewrite_from, NULL, FALSE, US"", + global_rewrite_rules); + psself_to = rewrite_one(psself, rewrite_to, NULL, FALSE, US"", + global_rewrite_rules); + if (psself_from == NULL) psself_from = psself; + if (psself_to == NULL) psself_to = psself; + to_count += 2; + from_count += 2; + } + +/* Do all the necessary tests; the counts are adjusted for {pre,suf}fix */ + +yield = + ( + header_match(US"to:", TRUE, TRUE, aliases, to_count, self, self_to, psself, + psself_to) || + (scan_cc && + ( + header_match(US"cc:", TRUE, TRUE, aliases, to_count, self, self_to, + psself, psself_to) + || + header_match(US"bcc:", TRUE, TRUE, aliases, to_count, self, self_to, + psself, psself_to) + ) + ) + ) && + + header_match(US"from:", TRUE, FALSE, aliases, from_count, "^server@", + "^daemon@", "^root@", "^listserv@", "^majordomo@", "^.*?-request@", + "^owner-[^@]+@", self, self_from, psself, psself_from) && + + header_match(US"auto-submitted:", FALSE, FALSE, NULL, 1, "auto-") && + header_match(US"precedence:", FALSE, FALSE, NULL, 3, "bulk","list","junk") && + + (sender_address == NULL || sender_address[0] != 0); + +store_reset(reset_point); +return yield; +} + + + +/************************************************* +* Interpret a mail filter file * +*************************************************/ + +/* +Arguments: + filter points to the entire file, read into store as a single string + options controls whether various special things are allowed, and requests + special actions + generated where to hang newly-generated addresses + error where to pass back an error text + +Returns: FF_DELIVERED success, a significant action was taken + FF_NOTDELIVERED success, no significant action + FF_DEFER defer requested + FF_FAIL fail requested + FF_FREEZE freeze requested + FF_ERROR there was a problem +*/ + +int +filter_interpret(uschar *filter, int options, address_item **generated, + uschar **error) +{ +int i; +int yield = FF_ERROR; +uschar *ptr = filter; +uschar *save_headers_charset = headers_charset; +filter_cmd *commands = NULL; +filter_cmd **lastcmdptr = &commands; + +DEBUG(D_route) debug_printf("Filter: start of processing\n"); + +/* Initialize "not in an if command", set the global flag that is always TRUE +while filtering, and zero the variables. */ + +expect_endif = 0; +output_indent = 0; +filter_running = TRUE; +for (i = 0; i < FILTER_VARIABLE_COUNT; i++) filter_n[i] = 0; + +/* To save having to pass certain values about all the time, make them static. +Also initialize the line number, for error messages, and the log file +variables. */ + +filter_options = options; +filter_delivered = FALSE; +finish_obeyed = FALSE; +error_pointer = error; +*error_pointer = NULL; +line_number = 1; +log_fd = -1; +log_mode = 0600; +log_filename = NULL; + +/* Scan filter file for syntax and build up an interpretation thereof, and +interpret the compiled commands, and if testing, say whether we ended up +delivered or not, unless something went wrong. */ + +seen_force = FALSE; +ptr = nextsigchar(ptr, TRUE); + +if (read_command_list(&ptr, &lastcmdptr, FALSE)) + yield = interpret_commands(commands, generated); + +if (filter_test != NULL || (debug_selector & D_filter) != 0) + { + uschar *s = US""; + switch(yield) + { + case FF_DEFER: + s = US"Filtering ended by \"defer\"."; + break; + + case FF_FREEZE: + s = US"Filtering ended by \"freeze\"."; + break; + + case FF_FAIL: + s = US"Filtering ended by \"fail\"."; + break; + + case FF_DELIVERED: + s = US"Filtering set up at least one significant delivery " + "or other action.\n" + "No other deliveries will occur."; + break; + + case FF_NOTDELIVERED: + s = US"Filtering did not set up a significant delivery.\n" + "Normal delivery will occur."; + break; + + case FF_ERROR: + s = string_sprintf("Filter error: %s", *error); + break; + } + + if (filter_test != NULL) printf("%s\n", CS s); + else debug_printf("%s\n", s); + } + +/* Close the log file if it was opened, and kill off any numerical variables +before returning. Reset the header decoding charset. */ + +if (log_fd >= 0) close(log_fd); +expand_nmax = -1; +filter_running = FALSE; +headers_charset = save_headers_charset; + +DEBUG(D_route) debug_printf("Filter: end of processing\n"); +return yield; +} + + +/* End of filter.c */ diff --git a/src/src/filtertest.c b/src/src/filtertest.c new file mode 100644 index 000000000..0cdee0304 --- /dev/null +++ b/src/src/filtertest.c @@ -0,0 +1,259 @@ +/* $Cambridge: exim/src/src/filtertest.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + + +/* Code for the filter test function. */ + +#include "exim.h" + + + +/************************************************* +* Test a mail filter * +*************************************************/ + +/* This is called when exim is run with the -bf option. The name +of the filter file is in filter_test, and we are running under an +unprivileged uid/gid. A test message's headers have been read into +store, and the body of the message is still accessible on the +standard input. + +Argument: + fd the standard input fd, containing the message body + is_system TRUE if testing is to be as a system filter + dot_ended TRUE if message already terminated by '.' + +Returns: TRUE if no errors +*/ + +BOOL +filter_runtest(int fd, BOOL is_system, BOOL dot_ended) +{ +int rc, body_len, body_end_len, filter_type, header_size; +register int ch; +BOOL yield; +struct stat statbuf; +address_item *generated = NULL; +uschar *error, *filebuf, *s; + +/* Read the filter file into store as will be done by the router in a real +case. */ + +if (fstat(fd, &statbuf) != 0) + { + printf("exim: failed to get size of %s: %s\n", filter_test, strerror(errno)); + return FALSE; + } + +filebuf = store_get(statbuf.st_size + 1); +rc = read(fd, filebuf, statbuf.st_size); +close(fd); + +if (rc != statbuf.st_size) + { + printf("exim: error while reading %s: %s\n", filter_test, strerror(errno)); + return FALSE; + } + +filebuf[statbuf.st_size] = 0; + +/* Check the filter type. User filters start with "# Exim filter" or "# Sieve +filter". If the filter type is not recognized, the file is treated as an +ordinary .forward file. System filters do not need the "# Exim filter" in order +to be recognized as Exim filters. */ + +filter_type = rda_is_filter(filebuf); +if (is_system && filter_type == FILTER_FORWARD) filter_type = FILTER_EXIM; + +printf("Testing %s file \"%s\"\n\n", + (filter_type == FILTER_EXIM)? "Exim filter" : + (filter_type == FILTER_SIEVE)? "Sieve filter" : + "forward file", + filter_test); + +/* Handle a plain .forward file */ + +if (filter_type == FILTER_FORWARD) + { + yield = parse_forward_list(filebuf, + RDO_REWRITE, + &generated, /* for generated addresses */ + &error, /* for errors */ + deliver_domain, /* incoming domain for \name */ + NULL, /* no check on includes */ + NULL); /* fail on syntax errors */ + + switch(yield) + { + case FF_FAIL: + printf("exim: forward file contains \":fail:\"\n"); + break; + + case FF_BLACKHOLE: + printf("exim: forwardfile contains \":blackhole:\"\n"); + break; + + case FF_ERROR: + printf("exim: error in forward file: %s\n", error); + return FALSE; + } + + if (generated == NULL) + printf("exim: no addresses generated from forward file\n"); + + else + { + printf("exim: forward file generated:\n"); + while (generated != NULL) + { + printf(" %s\n", generated->address); + generated = generated->next; + } + } + + return TRUE; + } + +/* For a filter, we have to read the remainder of the message in order to find +its size, so we might as well set up the message_body variable at the same time +(when *really* filtering this is not read unless needed). The reading code is +written out here rather than having options in read_message_data, in order to +keep that function as efficient as possible. Handling message_body_end is +somewhat more tedious. Pile it all into a circular buffer and sort out at the +end. */ + +message_body = store_malloc(message_body_visible + 1); +message_body_end = store_malloc(message_body_visible + 1); +s = message_body_end; +body_len = 0; +body_linecount = 0; +header_size = message_size; + +if (!dot_ended && !feof(stdin)) + { + if (!dot_ends) + { + while ((ch = getc(stdin)) != EOF) + { + if (ch == 0) body_zerocount++; + if (ch == '\n') body_linecount++; + if (body_len < message_body_visible) message_body[body_len++] = ch; + *s++ = ch; + if (s > message_body_end + message_body_visible) s = message_body_end; + message_size++; + } + } + else + { + int ch_state = 1; + while ((ch = getc(stdin)) != EOF) + { + if (ch == 0) body_zerocount++; + switch (ch_state) + { + case 0: /* Normal state */ + if (ch == '\n') { body_linecount++; ch_state = 1; } + break; + + case 1: /* After "\n" */ + if (ch == '.') + { + ch_state = 2; + continue; + } + if (ch != '\n') ch_state = 0; + break; + + case 2: /* After "\n." */ + if (ch == '\n') goto READ_END; + if (body_len < message_body_visible) message_body[body_len++] = '.'; + *s++ = '.'; + if (s > message_body_end + message_body_visible) + s = message_body_end; + message_size++; + ch_state = 0; + break; + } + if (body_len < message_body_visible) message_body[body_len++] = ch; + *s++ = ch; + if (s > message_body_end + message_body_visible) s = message_body_end; + message_size++; + } + READ_END: ch = ch; /* Some compilers don't like null statements */ + } + if (s == message_body_end || s[-1] != '\n') body_linecount++; + } + +message_body[body_len] = 0; +message_body_size = message_size - header_size; + +/* body_len stops at message_body_visible; it if got there, we may have +wrapped round in message_body_end. */ + +if (body_len >= message_body_visible) + { + int below = s - message_body_end; + int above = message_body_visible - below; + if (above > 0) + { + uschar *temp = store_get(below); + memcpy(temp, message_body_end, below); + memmove(message_body_end, s+1, above); + memcpy(message_body_end + above, temp, below); + s = message_body_end + message_body_visible; + } + } + +*s = 0; +body_end_len = s - message_body_end; + +/* Convert newlines and nulls in the body variables to spaces */ + +while (body_len > 0) + { + if (message_body[--body_len] == '\n' || message_body[body_len] == 0) + message_body[body_len] = ' '; + } + +while (body_end_len > 0) + { + if (message_body_end[--body_end_len] == '\n' || + message_body_end[body_end_len] == 0) + message_body_end[body_end_len] = ' '; + } + +/* Now pass the filter file to the function that interprets it. Because +filter_test is not NULL, the interpreter will output comments about what +it is doing, but an error message will have to be output here. No need to +clean up store. The last argument is 0 because Exim has given up root privilege +when running a filter test, and in any case, as it is a test, is isn't going to +try writing any files. */ + +if (is_system) + { + system_filtering = TRUE; + enable_dollar_recipients = TRUE; /* Permit $recipients in system filter */ + yield = filter_interpret + (filebuf, + RDO_DEFER|RDO_FAIL|RDO_FILTER|RDO_FREEZE|RDO_REWRITE, &generated, &error); + enable_dollar_recipients = FALSE; + system_filtering = FALSE; + } +else + { + yield = (filter_type == FILTER_SIEVE)? + sieve_interpret(filebuf, RDO_REWRITE, NULL, &generated, &error) + : + filter_interpret(filebuf, RDO_REWRITE, &generated, &error); + } + +return yield != FF_ERROR; +} + +/* End of filtertest.c */ diff --git a/src/src/functions.h b/src/src/functions.h new file mode 100644 index 000000000..dbdf798e5 --- /dev/null +++ b/src/src/functions.h @@ -0,0 +1,310 @@ +/* $Cambridge: exim/src/src/functions.h,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + + +/* Prototypes for functions that appear in various modules. Gathered together +to avoid having a lot of tiddly little headers with only a couple of lines in +them. However, some functions that are used (or not used) by utility programs +are in in fact in separate headers. */ + + +#ifdef EXIM_PERL +extern uschar *call_perl_cat(uschar *, int *, int *, uschar **, uschar *, + uschar **); +extern void cleanup_perl(void); +extern uschar *init_perl(uschar *); +#endif + + +#ifdef SUPPORT_TLS +extern int tls_client_start(int, host_item *, address_item *, uschar *, + uschar *, uschar *, uschar *, uschar *, uschar *, int); +extern void tls_close(BOOL); +extern int tls_feof(void); +extern int tls_ferror(void); +extern int tls_getc(void); +extern int tls_read(uschar *, size_t); +extern int tls_server_start(uschar *); +extern int tls_ungetc(int); +extern int tls_write(const uschar *, size_t); +#endif + + +/* Everything else... */ + +extern acl_block *acl_read(uschar *(*)(void), uschar **); +extern int acl_check(int, uschar *, uschar *, uschar **, uschar **); +extern uschar *auth_b64encode(uschar *, int); +extern int auth_b64decode(uschar *, uschar **); +extern int auth_call_pam(uschar *, uschar **); +extern int auth_call_pwcheck(uschar *, uschar **); +extern int auth_call_radius(uschar *, uschar **); +extern int auth_call_saslauthd(uschar *, uschar *, uschar *, uschar *, + uschar **); +extern int auth_get_data(uschar **, uschar *, int); +extern int auth_get_no64_data(uschar **, uschar *); +extern uschar *auth_xtextencode(uschar *, int); +extern int auth_xtextdecode(uschar *, uschar **); + +extern uschar **child_exec_exim(int, BOOL, int *, BOOL, int, ...); +extern pid_t child_open_uid(uschar **, uschar **, int, uid_t *, gid_t *, + int *, int *, uschar *, BOOL); + +extern void daemon_go(void); +extern void debug_print_argv(uschar **); +extern void debug_print_ids(uschar *); +extern void debug_print_string(uschar *); +extern void debug_print_tree(tree_node *); +extern void debug_vprintf(char *, va_list); +extern address_item *deliver_make_addr(uschar *, BOOL); +extern int deliver_message(uschar *, BOOL, BOOL); +extern void deliver_msglog(const char *, ...); +extern void deliver_set_expansions(address_item *); +extern int deliver_split_address(address_item *); +extern void deliver_succeeded(address_item *); +extern BOOL directory_make(uschar *, uschar *, int, BOOL); +extern dns_address *dns_address_from_rr(dns_answer *, dns_record *); +extern void dns_build_reverse(uschar *, uschar *); +extern void dns_init(BOOL, BOOL); +extern int dns_basic_lookup(dns_answer *, uschar *, int); +extern int dns_lookup(dns_answer *, uschar *, int, uschar **); +extern dns_record *dns_next_rr(dns_answer *, dns_scan *, int); +extern uschar *dns_text_type(int); + +extern void enq_end(uschar *); +extern BOOL enq_start(uschar *); +extern void exim_exit(int); +extern void exim_nullstd(void); +extern void exim_setugid(uid_t, gid_t, BOOL, uschar *); +extern int exim_tvcmp(struct timeval *, struct timeval *); +extern void exim_wait_tick(struct timeval *, int); +extern BOOL expand_check_condition(uschar *, uschar *, uschar *); +extern uschar *expand_string(uschar *); +extern uschar *expand_string_copy(uschar *); +extern int expand_string_integer(uschar *); + +extern int filter_interpret(uschar *, int, address_item **, uschar **); +extern BOOL filter_personal(string_item *, BOOL); +extern BOOL filter_runtest(int, BOOL, BOOL); +extern BOOL filter_system_interpret(address_item **, uschar **); + +extern void header_add(int, char *, ...); +extern int header_checkname(header_line *, BOOL); +extern BOOL header_match(uschar *, BOOL, BOOL, string_item *, int, ...); +extern uschar *host_and_ident(BOOL); +extern int host_aton(uschar *, int *); +extern void host_build_hostlist(host_item **, uschar *, BOOL); +extern ip_address_item *host_build_ifacelist(uschar *, uschar *); +extern void host_build_log_info(void); +extern void host_build_sender_fullhost(void); +extern int host_extract_port(uschar *); +extern BOOL host_find_byname(host_item *, uschar *, uschar **, BOOL); +extern int host_find_bydns(host_item *, uschar *, int, uschar *, uschar *, + uschar *,uschar **, BOOL *); +extern ip_address_item *host_find_interfaces(void); +extern BOOL host_is_in_net(uschar *, uschar *, int); +extern BOOL host_is_tls_on_connect_port(int); +extern void host_mask(int, int *, int); +extern int host_name_lookup(void); +extern int host_nmtoa(int, int *, int, uschar *); +extern uschar *host_ntoa(int, const void *, uschar *, int *); +extern int host_scan_for_local_hosts(host_item *, host_item **, BOOL *); + +extern int ip_bind(int, int, uschar *, int); +extern int ip_connect(int, int, uschar *, int, int); +extern void ip_keepalive(int, uschar *, BOOL); +extern int ip_recv(int, uschar *, int, int); +extern int ip_socket(int, int); + +extern uschar *local_part_quote(uschar *lpart); +extern void log_close_all(void); + +extern int match_address_list(uschar *, BOOL, BOOL, uschar **, + unsigned int *, int, int, uschar **); +extern int match_check_list(uschar **, int, tree_node **, unsigned int **, + int(*)(void *, uschar *, uschar **, uschar **), void *, int, + uschar *, uschar **); +extern int match_isinlist(uschar *, uschar **, int, tree_node **, + unsigned int *, int, BOOL, uschar **); +extern int match_check_string(uschar *, uschar *, int, BOOL, BOOL, BOOL, + uschar **); +extern void md5_end(md5 *, const uschar *, int, uschar *); +extern void md5_mid(md5 *, const uschar *); +extern void md5_start(md5 *); +extern void millisleep(int); +extern uschar *moan_check_errorcopy(uschar *); +extern BOOL moan_skipped_syntax_errors(uschar *, error_block *, uschar *, + BOOL, uschar *); +extern void moan_smtp_batch(uschar *, char *, ...); +extern void moan_tell_someone(uschar *, address_item *, uschar *, char *, + ...); +extern BOOL moan_to_sender(int, error_block *, header_line *, FILE *, BOOL); + +extern uschar *parse_extract_address(uschar *, uschar **, int *, int *, int *, + BOOL); +extern int parse_forward_list(uschar *, int, address_item **, uschar **, + uschar *, uschar *, error_block **); +extern uschar *parse_find_address_end(uschar *, BOOL); +extern uschar *parse_find_at(uschar *); +extern uschar *parse_fix_phrase(uschar *, int, uschar *, int); +extern uschar *parse_quote_2047(uschar *, int, uschar *, uschar *, int); + +extern BOOL queue_action(uschar *, int, uschar **, int, int); +extern void queue_check_only(void); +extern void queue_list(int, uschar **, int); +extern void queue_count(void); +extern void queue_run(uschar *, uschar *, BOOL); + +extern int random_number(int); +extern int rda_interpret(redirect_block *, int, uschar *, uschar *, ugid_block *, + address_item **, uschar **, error_block **, int *, uschar *); +extern int rda_is_filter(const uschar *); +extern BOOL readconf_depends(driver_instance *, uschar *); +extern void readconf_driver_init(uschar *, driver_instance **, + driver_info *, int, void *, int, optionlist *, int); +extern uschar *readconf_find_option(void *); +extern void readconf_main(void); +extern void readconf_print(uschar *, uschar *); +extern uschar *readconf_printtime(int); +extern uschar *readconf_readname(uschar *, int, uschar *); +extern int readconf_readtime(uschar *, int, BOOL); +extern void readconf_rest(BOOL); +extern uschar *readconf_retry_error(uschar *, uschar *, int *, int *); +extern void receive_bomb_out(uschar *); +extern BOOL receive_check_fs(int); +extern BOOL receive_check_set_sender(uschar *); +extern BOOL receive_msg(BOOL); +extern void receive_swallow_smtp(void); +extern BOOL regex_match_and_setup(const pcre *, uschar *, int, int); +extern const pcre *regex_must_compile(uschar *, BOOL, BOOL); +extern void retry_add_item(address_item *, uschar *, int); +extern BOOL retry_check_address(uschar *, host_item *, uschar *, BOOL, + uschar **, uschar **); +extern retry_config *retry_find_config(uschar *, uschar *, int, int); +extern void retry_update(address_item **, address_item **, address_item **); +extern uschar *rewrite_address(uschar *, BOOL, BOOL, rewrite_rule *, int); +extern uschar *rewrite_address_qualify(uschar *, BOOL); +extern header_line *rewrite_header(header_line *, uschar *, uschar *, + rewrite_rule *, int, BOOL); +extern uschar *rewrite_one(uschar *, int, BOOL *, BOOL, uschar *, + rewrite_rule *); +extern void rewrite_test(uschar *); +extern uschar *rfc2047_decode2(uschar *, BOOL, uschar *, int, int *, int *, + uschar **); +extern int route_address(address_item *, address_item **, address_item **, + address_item **, address_item **, int); +extern int route_check_prefix(uschar *, uschar *); +extern int route_check_suffix(uschar *, uschar *); +extern BOOL route_findgroup(uschar *, gid_t *); +extern BOOL route_finduser(uschar *, struct passwd **, uid_t *); +extern BOOL route_find_expanded_group(uschar *, uschar *, uschar *, gid_t *, + uschar **); +extern BOOL route_find_expanded_user(uschar *, uschar *, uschar *, + struct passwd **, uid_t *, uschar **); +extern void route_init(void); +extern void route_tidyup(void); + +extern uschar *search_find(void *, uschar *, uschar *, int, uschar *, int, + int, int *); +extern int search_findtype(uschar *, int); +extern int search_findtype_partial(uschar *, int *, uschar **, int *, + int *); +extern void *search_open(uschar *, int, int, uid_t *, gid_t *); +extern void search_tidyup(void); +extern void set_process_info(char *, ...); +extern void sha1_end(sha1 *, const uschar *, int, uschar *); +extern void sha1_mid(sha1 *, const uschar *); +extern void sha1_start(sha1 *); +extern int sieve_interpret(uschar *, int, uschar *, address_item **, + uschar **); +extern void sigalrm_handler(int); +extern void smtp_closedown(uschar *); +extern int smtp_connect(host_item *, int, int, uschar *, int, BOOL); +extern int smtp_feof(void); +extern int smtp_ferror(void); +extern uschar *smtp_get_connection_info(void); +extern BOOL smtp_get_interface(uschar *, int, address_item *, BOOL *, + uschar **, uschar *); +extern BOOL smtp_get_port(uschar *, address_item *, int *, uschar *); +extern int smtp_getc(void); +extern int smtp_handle_acl_fail(int, int, uschar *, uschar *); +extern BOOL smtp_read_response(smtp_inblock *, uschar *, int, int, int); +extern void smtp_respond(int, BOOL, uschar *); +extern void smtp_send_prohibition_message(int, uschar *); +extern int smtp_setup_msg(void); +extern BOOL smtp_start_session(void); +extern int smtp_ungetc(int); +extern int smtp_write_command(smtp_outblock *, BOOL, char *, ...); +extern BOOL spool_move_message(uschar *, uschar *, uschar *, uschar *); +extern BOOL spool_open_datafile(uschar *); +extern int spool_open_temp(uschar *); +extern int spool_read_header(uschar *, BOOL, BOOL); +extern int spool_write_header(uschar *, int, uschar **); +extern int stdin_getc(void); +extern int stdin_feof(void); +extern int stdin_ferror(void); +extern int stdin_ungetc(int); +extern uschar *string_append(uschar *, int *, int *, int, ...); +extern uschar *string_base62(unsigned long int); +extern uschar *string_cat(uschar *, int *, int *, const uschar *, int); +extern uschar *string_copy_dnsdomain(uschar *); +extern uschar *string_copy_malloc(uschar *); +extern uschar *string_copylc(uschar *); +extern uschar *string_copynlc(uschar *, int); +extern uschar *string_dequote(uschar **); +extern BOOL string_format(uschar *, int, char *, ...); +extern uschar *string_format_size(int, uschar *); +extern int string_interpret_escape(uschar **); +extern int string_is_ip_address(uschar *, int *); +extern uschar *string_log_address(address_item *, BOOL, BOOL); +extern uschar *string_nextinlist(uschar **, int *, uschar *, int); +extern uschar *string_open_failed(int, char *, ...); +extern uschar *string_printing2(uschar *, BOOL); +extern BOOL string_vformat(uschar *, int, char *, va_list); +extern int strcmpic(uschar *, uschar *); +extern int strncmpic(uschar *, uschar *, int); +extern uschar *strstric(uschar *, uschar *, BOOL); + +extern uschar *tod_stamp(int); +extern BOOL transport_check_waiting(uschar *, uschar *, int, uschar *, + BOOL *); +extern void transport_init(void); +extern BOOL transport_pass_socket(uschar *, uschar *, uschar *, uschar *, + int); +extern uschar *transport_rcpt_address(address_item *, BOOL); +extern BOOL transport_set_up_command(uschar ***, uschar *, BOOL, int, + address_item *, uschar *, uschar **); +extern void transport_update_waiting(host_item *, uschar *); +extern BOOL transport_write_block(int, uschar *, int); +extern BOOL transport_write_string(int, char *, ...); +extern BOOL transport_write_message(address_item *, int, int, int, uschar *, + uschar *, uschar *, uschar *, rewrite_rule *, int); +extern void tree_add_duplicate(uschar *, address_item *); +extern void tree_add_nonrecipient(uschar *); +extern void tree_add_unusable(host_item *); +extern int tree_insertnode(tree_node **, tree_node *); +extern tree_node *tree_search(tree_node *, uschar *); +extern void tree_write(tree_node *, FILE *); + +extern int verify_address(address_item *, FILE *, int, int, int, uschar *, + uschar *, BOOL *); +extern int verify_check_dnsbl(uschar **); +extern int verify_check_header_address(uschar **, uschar **, int, int, + uschar *, uschar *, int); +extern int verify_check_headers(uschar **); +extern int verify_check_host(uschar **); +extern int verify_check_this_host(uschar **, unsigned int *, uschar*, + uschar *, uschar **); +extern address_item *verify_checked_sender(uschar *); +extern void verify_get_ident(int); +extern BOOL verify_sender(int *, uschar **); +extern BOOL verify_sender_preliminary(int *, uschar **); +extern void version_init(void); + +/* End of functions.h */ diff --git a/src/src/globals.c b/src/src/globals.c new file mode 100644 index 000000000..b9cf2458f --- /dev/null +++ b/src/src/globals.c @@ -0,0 +1,1071 @@ +/* $Cambridge: exim/src/src/globals.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* All the global variables are defined together in this one module, so +that they are easy to find. */ + +#include "exim.h" + + +/* The OSF1 linker puts out a worrying warning if any sections contain no +executable code. It says + +Warning: Linking some objects which contain exception information sections + and some which do not. This may cause fatal runtime exception handling + problems. + +As this may cause people to worry needlessly, include a dummy function here +to stop the message from appearing. Make it reference itself to stop picky +compilers complaining that it is unused, and put in a dummy argument to stop +even pickier compilers complaining about infinite loops. */ + +static void dummy(int x) { dummy(x-1); } + + +/* Generic options for auths, all of which live inside auth_instance +data blocks and hence have the opt_public flag set. */ + +optionlist optionlist_auths[] = { + { "driver", opt_stringptr | opt_public, + (void *)(offsetof(auth_instance, driver_name)) }, + { "public_name", opt_stringptr | opt_public, + (void *)(offsetof(auth_instance, public_name)) }, + { "server_advertise_condition", opt_stringptr | opt_public, + (void *)(offsetof(auth_instance, advertise_condition))}, + { "server_debug_print", opt_stringptr | opt_public, + (void *)(offsetof(auth_instance, server_debug_string)) }, + { "server_mail_auth_condition", opt_stringptr | opt_public, + (void *)(offsetof(auth_instance, mail_auth_condition)) }, + { "server_set_id", opt_stringptr | opt_public, + (void *)(offsetof(auth_instance, set_id)) } +}; + +int optionlist_auths_size = sizeof(optionlist_auths)/sizeof(optionlist); + +/* An empty host aliases list. */ + +uschar *no_aliases = NULL; + + +/* For comments on these variables, see globals.h. I'm too idle to +duplicate them here... */ + +#ifdef EXIM_PERL +uschar *opt_perl_startup = NULL; +BOOL opt_perl_at_start = FALSE; +BOOL opt_perl_started = FALSE; +#endif + +#ifdef LOOKUP_IBASE +uschar *ibase_servers = NULL; +#endif + +#ifdef LOOKUP_LDAP +uschar *eldap_default_servers = NULL; +int eldap_version = -1; +#endif + +#ifdef LOOKUP_MYSQL +uschar *mysql_servers = NULL; +#endif + +#ifdef LOOKUP_ORACLE +uschar *oracle_servers = NULL; +#endif + +#ifdef LOOKUP_PGSQL +uschar *pgsql_servers = NULL; +#endif + +#ifdef SUPPORT_MOVE_FROZEN_MESSAGES +BOOL move_frozen_messages = FALSE; +#endif + +/* These variables are outside the #ifdef because it keeps the code less +cluttered in several places (e.g. during logging) if we can always refer to +them. Also, the tls_ variables are now always visible. */ + +BOOL tls_active = -1; +BOOL tls_certificate_verified = FALSE; +uschar *tls_cipher = NULL; +BOOL tls_on_connect = FALSE; +uschar *tls_on_connect_ports = NULL; +uschar *tls_peerdn = NULL; + +#ifdef SUPPORT_TLS +const pcre *regex_STARTTLS = NULL; +uschar *tls_advertise_hosts = NULL; /* This is deliberate */ +uschar *tls_certificate = NULL; +uschar *tls_crl = NULL; +uschar *tls_dhparam = NULL; +BOOL tls_offered = FALSE; +uschar *tls_privatekey = NULL; +BOOL tls_remember_esmtp = FALSE; +uschar *tls_require_ciphers = NULL; +uschar *tls_try_verify_hosts = NULL; +uschar *tls_verify_certificates= NULL; +uschar *tls_verify_hosts = NULL; +#endif + + +/* Input-reading functions for messages, so we can use special ones for +incoming TCP/IP. The defaults use stdin. We never need these for any +stand-alone tests. */ + +#ifndef STAND_ALONE +int (*receive_getc)(void) = stdin_getc; +int (*receive_ungetc)(int) = stdin_ungetc; +int (*receive_feof)(void) = stdin_feof; +int (*receive_ferror)(void) = stdin_ferror; +#endif + + +/* List of per-address expansion variables for clearing and saving/restoring +when verifying one address while routing/verifying another. We have to have +the size explicit, because it is referenced from more than one module. */ + +uschar **address_expansions[ADDRESS_EXPANSIONS_COUNT] = { + &deliver_address_data, + &deliver_domain, + &deliver_domain_data, + &deliver_domain_orig, + &deliver_domain_parent, + &deliver_localpart, + &deliver_localpart_data, + &deliver_localpart_orig, + &deliver_localpart_parent, + &deliver_localpart_prefix, + &deliver_localpart_suffix, + (uschar **)(&deliver_recipients), + &deliver_host, + &deliver_home, + &address_file, + &address_pipe, + &self_hostname, + NULL }; + +int address_expansions_count = sizeof(address_expansions)/sizeof(uschar **); + +/* General global variables */ + +tree_node *acl_anchor = NULL; +uschar *acl_not_smtp = NULL; +uschar *acl_smtp_auth = NULL; +uschar *acl_smtp_connect = NULL; +uschar *acl_smtp_data = NULL; +uschar *acl_smtp_etrn = NULL; +uschar *acl_smtp_expn = NULL; +uschar *acl_smtp_helo = NULL; +uschar *acl_smtp_mail = NULL; +uschar *acl_smtp_mailauth = NULL; +uschar *acl_smtp_predata = NULL; +uschar *acl_smtp_quit = NULL; +uschar *acl_smtp_rcpt = NULL; +uschar *acl_smtp_starttls = NULL; +uschar *acl_smtp_vrfy = NULL; +BOOL acl_temp_details = FALSE; +uschar *acl_var[ACL_C_MAX+ACL_M_MAX]; +uschar *acl_verify_message = NULL; +header_line *acl_warn_headers = NULL; +string_item *acl_warn_logged = NULL; + +/* Names of SMTP places for use in ACL error messages, and corresponding SMTP +error codes - keep in step with definitions of ACL_WHERE_xxxx in macros.h. */ + +uschar *acl_wherenames[] = { US"RCPT", + US"MAIL", + US"PREDATA", + US"DATA", + US"non-SMTP", + US"AUTH", + US"connection", + US"ETRN", + US"EXPN", + US"EHLO or HELO", + US"MAILAUTH", + US"QUIT", + US"STARTTLS", + US"VRFY" + }; + +int acl_wherecodes[] = { 550, /* RCPT */ + 550, /* MAIL */ + 550, /* PREDATA */ + 550, /* DATA */ + 0, /* not SMTP; not relevant */ + 503, /* AUTH */ + 550, /* connect */ + 458, /* ETRN */ + 550, /* EXPN */ + 550, /* HELO/EHLO */ + 0, /* MAILAUTH; not relevant */ + 0, /* QUIT; not relevant */ + 550, /* STARTTLS */ + 252 /* VRFY */ + }; + +BOOL accept_8bitmime = FALSE; +address_item *addr_duplicate = NULL; + +address_item address_defaults = { + NULL, /* next */ + NULL, /* parent */ + NULL, /* first */ + NULL, /* dupof */ + NULL, /* start_router */ + NULL, /* router */ + NULL, /* transport */ + NULL, /* host_list */ + NULL, /* host_used */ + NULL, /* fallback_hosts */ + NULL, /* reply */ + NULL, /* retries */ + NULL, /* address */ + NULL, /* unique */ + NULL, /* cc_local_part */ + NULL, /* lc_local_part */ + NULL, /* local_part */ + NULL, /* prefix */ + NULL, /* suffix */ + NULL, /* domain */ + NULL, /* address_retry_key */ + NULL, /* domain_retry_key */ + NULL, /* current_dir */ + NULL, /* home_dir */ + NULL, /* message */ + NULL, /* user_message */ + NULL, /* onetime_parent */ + NULL, /* pipe_expandn */ + NULL, /* return_filename */ + NULL, /* self_hostname */ + NULL, /* shadow_message */ + #ifdef SUPPORT_TLS + NULL, /* cipher */ + NULL, /* peerdn */ + #endif + (uid_t)(-1), /* uid */ + (gid_t)(-1), /* gid */ + 0, /* flags */ + { 0 }, /* domain_cache - any larger array should be zeroed */ + { 0 }, /* localpart_cache - ditto */ + -1, /* mode */ + 0, /* more_errno */ + ERRNO_UNKNOWNERROR, /* basic_errno */ + 0, /* child_count */ + -1, /* return_file */ + SPECIAL_NONE, /* special_action */ + DEFER, /* transport_return */ + { /* fields that are propagated to children */ + NULL, /* address_data */ + NULL, /* domain_data */ + NULL, /* localpart_data */ + NULL, /* errors_address */ + NULL, /* extra_headers */ + NULL, /* remove_headers */ + } +}; + +uschar *address_file = NULL; +uschar *address_pipe = NULL; +BOOL address_test_mode = FALSE; +tree_node *addresslist_anchor = NULL; +int addresslist_count = 0; +gid_t *admin_groups = NULL; +BOOL admin_user = FALSE; +BOOL allow_domain_literals = FALSE; +BOOL allow_mx_to_ip = FALSE; +BOOL allow_unqualified_recipient = TRUE; /* For local messages */ +BOOL allow_unqualified_sender = TRUE; /* Reset for SMTP */ +BOOL allow_utf8_domains = FALSE; +uschar *authenticated_id = NULL; +uschar *authenticated_sender = NULL; +BOOL authentication_failed = FALSE; +auth_instance *auths = NULL; +uschar *auth_advertise_hosts = US"*"; +auth_instance auth_defaults = { + NULL, /* chain pointer */ + NULL, /* name */ + NULL, /* info */ + NULL, /* private options block pointer */ + NULL, /* driver_name */ + NULL, /* advertise_condition */ + NULL, /* public_name */ + NULL, /* set_id */ + NULL, /* server_mail_auth_condition */ + NULL, /* server_debug_string */ + FALSE, /* client */ + FALSE, /* server */ + FALSE /* advertised */ +}; + +uschar *auth_defer_msg = US"reason not recorded"; +uschar *auth_defer_user_msg = US""; +int auto_thaw = 0; + +BOOL background_daemon = TRUE; +uschar *base62_chars= + US"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; +uschar *bi_command = NULL; +uschar *big_buffer = NULL; +int big_buffer_size = BIG_BUFFER_SIZE; +int body_linecount = 0; +int body_zerocount = 0; +uschar *bounce_message_file = NULL; +uschar *bounce_message_text = NULL; +uschar *bounce_recipient = NULL; +BOOL bounce_return_body = TRUE; +BOOL bounce_return_message = TRUE; +int bounce_return_size_limit = 100*1024; +uschar *bounce_sender_authentication = NULL; +int bsmtp_transaction_linecount = 0; + +int callout_cache_domain_positive_expire = 7*24*60*60; +int callout_cache_domain_negative_expire = 3*60*60; +int callout_cache_positive_expire = 24*60*60; +int callout_cache_negative_expire = 2*60*60; +uschar *callout_random_local_part = US"$primary_hostname-$tod_epoch-testing"; +uschar *check_dns_names_pattern= US"(?i)^(?>(?(1)\\.|())[^\\W_](?>[a-z0-9-]*[^\\W_])?)+$"; +int check_log_inodes = 0; +int check_log_space = 0; +int check_spool_inodes = 0; +int check_spool_space = 0; +int clmacro_count = 0; +uschar *clmacros[MAX_CLMACROS]; +BOOL config_changed = FALSE; +FILE *config_file = NULL; +uschar *config_filename = NULL; +int config_lineno = 0; +uschar *config_main_filelist = US CONFIGURE_FILE + "\0<-----------Space to patch configure_filename->"; +uschar *config_main_filename = NULL; + +#ifdef CONFIGURE_OWNER +uid_t config_uid = CONFIGURE_OWNER; +#endif + +int connection_max_messages= -1; +uschar *continue_hostname = NULL; +uschar *continue_host_address = NULL; +BOOL continue_more = FALSE; +int continue_sequence = 1; +uschar *continue_transport = NULL; + +BOOL daemon_listen = FALSE; +uschar *daemon_smtp_port = US"smtp"; +int debug_fd = -1; +FILE *debug_file = NULL; +bit_table debug_options[] = { + { US"acl", D_acl }, + { US"all", D_all }, + { US"auth", D_auth }, + { US"deliver", D_deliver }, + { US"dns", D_dns }, + { US"dnsbl", D_dnsbl }, + { US"exec", D_exec }, + { US"expand", D_expand }, + { US"filter", D_filter }, + { US"hints_lookup", D_hints_lookup }, + { US"host_lookup", D_host_lookup }, + { US"ident", D_ident }, + { US"interface", D_interface }, + { US"lists", D_lists }, + { US"load", D_load }, + { US"local_scan", D_local_scan }, + { US"lookup", D_lookup }, + { US"memory", D_memory }, + { US"pid", D_pid }, + { US"process_info", D_process_info }, + { US"queue_run", D_queue_run }, + { US"receive", D_receive }, + { US"resolver", D_resolver }, + { US"retry", D_retry }, + { US"rewrite", D_rewrite }, + { US"route", D_route }, + { US"timestamp", D_timestamp }, + { US"tls", D_tls }, + { US"transport", D_transport }, + { US"uid", D_uid }, + { US"verify", D_verify } +}; +int debug_options_count = sizeof(debug_options)/sizeof(bit_table); +unsigned int debug_selector = 0; +int delay_warning[DELAY_WARNING_SIZE] = { DELAY_WARNING_SIZE, 1, 24*60*60 }; +uschar *delay_warning_condition= US"${if match{$h_precedence:}{(?i)bulk|list|junk}{no}{yes}}"; +BOOL delivery_date_remove = TRUE; +uschar *deliver_address_data = NULL; +int deliver_datafile = -1; +uschar *deliver_domain = NULL; +uschar *deliver_domain_data = NULL; +uschar *deliver_domain_orig = NULL; +uschar *deliver_domain_parent = NULL; +BOOL deliver_drop_privilege = FALSE; +BOOL deliver_firsttime = FALSE; +BOOL deliver_force = FALSE; +BOOL deliver_freeze = FALSE; +int deliver_frozen_at = 0; +uschar *deliver_home = NULL; +uschar *deliver_host = NULL; +uschar *deliver_host_address = NULL; +uschar *deliver_in_buffer = NULL; +ino_t deliver_inode = 0; +uschar *deliver_localpart = NULL; +uschar *deliver_localpart_data = NULL; +uschar *deliver_localpart_orig = NULL; +uschar *deliver_localpart_parent = NULL; +uschar *deliver_localpart_prefix = NULL; +uschar *deliver_localpart_suffix = NULL; +BOOL deliver_force_thaw = FALSE; +BOOL deliver_manual_thaw = FALSE; +uschar *deliver_out_buffer = NULL; +int deliver_queue_load_max = -1; +address_item *deliver_recipients = NULL; +uschar *deliver_selectstring = NULL; +BOOL deliver_selectstring_regex = FALSE; +uschar *deliver_selectstring_sender = NULL; +BOOL deliver_selectstring_sender_regex = FALSE; +BOOL disable_logging = FALSE; + +uschar *dns_again_means_nonexist = NULL; +uschar *dns_ipv4_lookup = NULL; +int dns_retrans = 0; +int dns_retry = 0; +uschar *dnslist_domain = NULL; +uschar *dnslist_text = NULL; +uschar *dnslist_value = NULL; +tree_node *domainlist_anchor = NULL; +int domainlist_count = 0; +BOOL dont_deliver = FALSE; +BOOL dot_ends = TRUE; +BOOL drop_cr = FALSE; /* No longer used */ + +BOOL enable_dollar_recipients = FALSE; +BOOL envelope_to_remove = TRUE; +int errno_quota = ERRNO_QUOTA; +uschar *errors_copy = NULL; +int error_handling = ERRORS_SENDER; +uschar *errors_reply_to = NULL; +int errors_sender_rc = EXIT_FAILURE; + +gid_t exim_gid = EXIM_GID; +BOOL exim_gid_set = TRUE; /* This gid is always set */ +uschar *exim_path = US BIN_DIRECTORY "/exim" + "\0<---------------Space to patch exim_path->"; +uid_t exim_uid = EXIM_UID; +BOOL exim_uid_set = TRUE; /* This uid is always set */ +int expand_forbid = 0; +int expand_nlength[EXPAND_MAXN+1]; +int expand_nmax = -1; +uschar *expand_nstring[EXPAND_MAXN+1]; +BOOL expand_string_forcedfail = FALSE; +uschar *expand_string_message; +BOOL extract_addresses_remove_arguments = TRUE; +uschar *extra_local_interfaces = NULL; + +int filter_n[FILTER_VARIABLE_COUNT]; +BOOL filter_running = FALSE; +int filter_sn[FILTER_VARIABLE_COUNT]; +uschar *filter_test = NULL; +uschar *filter_thisaddress = NULL; +int finduser_retries = 0; +uid_t fixed_never_users[] = { FIXED_NEVER_USERS }; +uschar *freeze_tell = NULL; +uschar *fudged_queue_times = US""; + +uschar *gecos_name = NULL; +uschar *gecos_pattern = NULL; +rewrite_rule *global_rewrite_rules = NULL; + +uschar *headers_charset = US HEADERS_CHARSET; +int header_insert_maxlen = 64 * 1024; +header_line *header_last = NULL; +header_line *header_list = NULL; +int header_maxsize = HEADER_MAXSIZE; +int header_line_maxsize = 0; + +header_name header_names[] = { + { US"bcc", 3, TRUE, htype_bcc }, + { US"cc", 2, TRUE, htype_cc }, + { US"date", 4, TRUE, htype_date }, + { US"delivery-date", 13, FALSE, htype_delivery_date }, + { US"envelope-to", 11, FALSE, htype_envelope_to }, + { US"from", 4, TRUE, htype_from }, + { US"message-id", 10, TRUE, htype_id }, + { US"received", 8, FALSE, htype_received }, + { US"reply-to", 8, FALSE, htype_reply_to }, + { US"return-path", 11, FALSE, htype_return_path }, + { US"sender", 6, TRUE, htype_sender }, + { US"subject", 7, FALSE, htype_subject }, + { US"to", 2, TRUE, htype_to } +}; + +int header_names_size = sizeof(header_names)/sizeof(header_name); + +BOOL header_rewritten = FALSE; +uschar *helo_accept_junk_hosts = NULL; +uschar *helo_allow_chars = US""; +uschar *helo_lookup_domains = US"@ : @[]"; +uschar *helo_try_verify_hosts = NULL; +BOOL helo_verified = FALSE; +uschar *helo_verify_hosts = NULL; +uschar *hex_digits = US"0123456789abcdef"; +uschar *hold_domains = NULL; +BOOL host_checking = FALSE; +BOOL host_checking_callout = FALSE; +uschar *host_data = NULL; +BOOL host_find_failed_syntax= FALSE; +uschar *host_lookup = NULL; +BOOL host_lookup_failed = FALSE; +uschar *host_lookup_order = US"bydns:byaddr"; +uschar *host_lookup_msg = US""; +int host_number = 0; +uschar *host_number_string = NULL; +uschar *host_reject_connection = NULL; +tree_node *hostlist_anchor = NULL; +int hostlist_count = 0; +uschar *hosts_treat_as_local = NULL; +uschar *hosts_connection_nolog = NULL; + +int ignore_bounce_errors_after = 10*7*24*60*60; /* 10 weeks */ +BOOL ignore_fromline_local = FALSE; +uschar *ignore_fromline_hosts = NULL; +uschar *interface_address = NULL; +int interface_port = -1; +BOOL is_inetd = FALSE; + +int journal_fd = -1; + +int keep_malformed = 4*24*60*60; /* 4 days */ + +uschar *eldap_dn = NULL; +int load_average = -2; +BOOL local_error_message = FALSE; +BOOL local_from_check = TRUE; +uschar *local_from_prefix = NULL; +uschar *local_from_suffix = NULL; + +#if HAVE_IPV6 +uschar *local_interfaces = US"<; ::0 ; 0.0.0.0"; +#else +uschar *local_interfaces = US"0.0.0.0"; +#endif + +uschar *local_scan_data = NULL; +int local_scan_timeout = 5*60; +BOOL local_sender_retain = FALSE; +gid_t local_user_gid = (gid_t)(-1); +uid_t local_user_uid = (uid_t)(-1); + +tree_node *localpartlist_anchor= NULL; +int localpartlist_count = 0; +uschar *log_buffer = NULL; +unsigned int log_extra_selector = LX_default; +uschar *log_file_path = US LOG_FILE_PATH + "\0<--------------Space to patch log_file_path->"; + +/* Those log options with L_xxx identifiers have values less than 0x800000 and +are the ones that get put into log_write_selector. They can be used in calls to +log_write() to test for the bit. The options with LX_xxx identifiers have +values greater than 0x80000000 and are put int log_extra_selector (without the +top bit). They are never used in calls to log_write(), but are tested +independently. This separation became necessary when the number of log +selectors was getting close to filling a 32-bit word. */ + +bit_table log_options[] = { + { US"address_rewrite", L_address_rewrite }, + { US"all", L_all }, + { US"all_parents", L_all_parents }, + { US"arguments", LX_arguments }, + { US"connection_reject", L_connection_reject }, + { US"delay_delivery", L_delay_delivery }, + { US"deliver_time", LX_deliver_time }, + { US"delivery_size", LX_delivery_size }, + { US"dnslist_defer", L_dnslist_defer }, + { US"etrn", L_etrn }, + { US"host_lookup_failed", L_host_lookup_failed }, + { US"ident_timeout", LX_ident_timeout }, + { US"incoming_interface", LX_incoming_interface }, + { US"incoming_port", LX_incoming_port }, + { US"lost_incoming_connection", L_lost_incoming_connection }, + { US"outgoing_port", LX_outgoing_port }, + { US"queue_run", L_queue_run }, + { US"queue_time", LX_queue_time }, + { US"received_recipients", LX_received_recipients }, + { US"received_sender", LX_received_sender }, + { US"rejected_header", LX_rejected_header }, + { US"rejected_headers", LX_rejected_header }, + { US"retry_defer", L_retry_defer }, + { US"return_path_on_delivery", LX_return_path_on_delivery }, + { US"sender_on_delivery", LX_sender_on_delivery }, + { US"size_reject", L_size_reject }, + { US"skip_delivery", L_skip_delivery }, + { US"smtp_confirmation", LX_smtp_confirmation }, + { US"smtp_connection", L_smtp_connection }, + { US"smtp_incomplete_transaction", L_smtp_incomplete_transaction }, + { US"smtp_protocol_error", L_smtp_protocol_error }, + { US"smtp_syntax_error", L_smtp_syntax_error }, + { US"subject", LX_subject }, + { US"tls_certificate_verified", LX_tls_certificate_verified }, + { US"tls_cipher", LX_tls_cipher }, + { US"tls_peerdn", LX_tls_peerdn } +}; + +int log_options_count = sizeof(log_options)/sizeof(bit_table); +unsigned int log_write_selector= L_default; +uschar *log_selector_string = NULL; +FILE *log_stderr = NULL; +BOOL log_testing_mode = FALSE; +BOOL log_timezone = FALSE; +uschar *login_sender_address = NULL; +int lookup_open_max = 25; +uschar *lookup_value = NULL; + +macro_item *macros = NULL; +uschar *mailstore_basename = NULL; +int max_username_length = 0; +int message_age = 0; +uschar *message_body = NULL; +uschar *message_body_end = NULL; +int message_body_size = 0; +int message_body_visible = 500; +int message_ended = END_NOTSTARTED; +uschar *message_headers = NULL; +uschar *message_id; +uschar *message_id_domain = NULL; +uschar *message_id_text = NULL; +struct timeval message_id_tv = { 0, 0 }; +uschar message_id_option[MESSAGE_ID_LENGTH + 3]; +uschar *message_id_external; +int message_linecount = 0; +BOOL message_logs = TRUE; +int message_size = 0; +uschar *message_size_limit = US"50M"; +uschar message_subdir[2] = { 0, 0 }; +uschar *message_reference = NULL; +BOOL mua_wrapper = FALSE; + +uid_t *never_users = NULL; +BOOL no_multiline_responses = FALSE; + +uid_t original_euid; +gid_t originator_gid; +uschar *originator_login = NULL; +uschar *originator_name = NULL; +uid_t originator_uid; +uschar *override_local_interfaces = NULL; +uschar *override_pid_file_path = NULL; + +BOOL parse_allow_group = FALSE; +BOOL parse_found_group = FALSE; +uschar *percent_hack_domains = NULL; +uschar *pid_file_path = US PID_FILE_PATH + "\0<--------------Space to patch pid_file_path->"; +uschar *pipelining_advertise_hosts = US"*"; +BOOL preserve_message_logs = FALSE; +uschar *primary_hostname = NULL; +BOOL print_topbitchars = FALSE; +uschar process_info[PROCESS_INFO_SIZE]; +uschar *process_log_path = NULL; +BOOL prod_requires_admin = TRUE; + +uschar *qualify_domain_recipient = NULL; +uschar *qualify_domain_sender = NULL; +BOOL queue_2stage = FALSE; +uschar *queue_domains = NULL; +int queue_interval = -1; +BOOL queue_list_requires_admin = TRUE; +BOOL queue_only = FALSE; +uschar *queue_only_file = NULL; +int queue_only_load = -1; +BOOL queue_only_override = TRUE; +BOOL queue_only_policy = FALSE; +BOOL queue_run_first_delivery = FALSE; +BOOL queue_run_force = FALSE; +BOOL queue_run_in_order = FALSE; +BOOL queue_run_local = FALSE; +int queue_run_max = 5; +pid_t queue_run_pid = (pid_t)0; +int queue_run_pipe = -1; +BOOL queue_running = FALSE; +BOOL queue_smtp = FALSE; +uschar *queue_smtp_domains = NULL; + +unsigned int random_seed = 0; +uschar *raw_active_hostname = NULL; +uschar *raw_sender = NULL; +uschar **raw_recipients = NULL; +int raw_recipients_count = 0; + +int rcpt_count = 0; +int rcpt_fail_count = 0; +int rcpt_defer_count = 0; +gid_t real_gid; +uid_t real_uid; +BOOL really_exim = TRUE; +BOOL receive_call_bombout = FALSE; +int receive_linecount = 0; +int receive_messagecount = 0; +int receive_timeout = 0; +int received_count = 0; +uschar *received_for = NULL; + +/* This is the default text for Received headers generated by Exim. The +date will be automatically added on the end. */ + +uschar *received_header_text = US + "Received: " + "${if def:sender_rcvhost {from $sender_rcvhost\n\t}" + "{${if def:sender_ident {from $sender_ident }}" + "${if def:sender_helo_name {(helo=$sender_helo_name)\n\t}}}}" + "by $primary_hostname " + "${if def:received_protocol {with $received_protocol}} " + #ifdef SUPPORT_TLS + "${if def:tls_cipher {($tls_cipher)\n\t}}" + #endif + "(Exim $version_number)\n\t" + "id $message_id" + "${if def:received_for {\n\tfor $received_for}}" + "\0<---------------Space to patch received_header_text->"; + +int received_headers_max = 30; +uschar *received_protocol = NULL; +int received_time = 0; +uschar *recipient_data = NULL; +uschar *recipient_unqualified_hosts = NULL; +int recipients_count = 0; +BOOL recipients_discarded = FALSE; +recipient_item *recipients_list = NULL; +int recipients_list_max = 0; +int recipients_max = 0; +BOOL recipients_max_reject = FALSE; +const pcre *regex_AUTH = NULL; +const pcre *regex_check_dns_names = NULL; +const pcre *regex_From = NULL; +const pcre *regex_PIPELINING = NULL; +const pcre *regex_SIZE = NULL; +const pcre *regex_ismsgid = NULL; +int remote_delivery_count = 0; +int remote_max_parallel = 2; +uschar *remote_sort_domains = NULL; +int retry_data_expire = 7*24*60*60; +int retry_interval_max = 24*60*60; +int retry_maximum_timeout = 0; /* set from retry config */ +retry_config *retries = NULL; +uschar *return_path = NULL; +BOOL return_path_remove = TRUE; +int rewrite_existflags = 0; +uschar *rfc1413_hosts = US"*"; +int rfc1413_query_timeout = 30; +/* BOOL rfc821_domains = FALSE; <<< on the way out */ +uid_t root_uid = ROOT_UID; + +router_instance *routers = NULL; +router_instance router_defaults = { + NULL, /* chain pointer */ + NULL, /* name */ + NULL, /* info */ + NULL, /* private options block pointer */ + NULL, /* driver name */ + + NULL, /* address_data */ + NULL, /* cannot_route_message */ + NULL, /* condition */ + NULL, /* current_directory */ + NULL, /* debug_string */ + NULL, /* domains */ + NULL, /* errors_to */ + NULL, /* expand_gid */ + NULL, /* expand_uid */ + NULL, /* expand_more */ + NULL, /* expand_unseen */ + NULL, /* extra_headers */ + NULL, /* fallback_hosts */ + NULL, /* home_directory */ + NULL, /* ignore_target_hosts */ + NULL, /* local_parts */ + NULL, /* pass_router_name */ + NULL, /* prefix */ + NULL, /* redirect_router_name */ + NULL, /* remove_headers */ + NULL, /* require_files */ + NULL, /* router_home_directory */ + US"freeze", /* self */ + NULL, /* senders */ + NULL, /* suffix */ + NULL, /* translate_ip_address */ + NULL, /* transport_name */ + + TRUE, /* address_test */ + TRUE, /* expn */ + FALSE, /* caseful_local_part */ + FALSE, /* check_local_user */ + FALSE, /* disable_logging */ + FALSE, /* fail_verify_recipient */ + FALSE, /* fail_verify_sender */ + FALSE, /* gid_set */ + FALSE, /* initgroups */ + TRUE_UNSET, /* log_as_local */ + TRUE, /* more */ + FALSE, /* pass_on_timeout */ + FALSE, /* prefix_optional */ + TRUE, /* repeat_use */ + TRUE_UNSET, /* retry_use_local_part - fudge "unset" */ + FALSE, /* same_domain_copy_routing */ + FALSE, /* self_rewrite */ + FALSE, /* suffix_optional */ + FALSE, /* verify_only */ + TRUE, /* verify_recipient */ + TRUE, /* verify_sender */ + FALSE, /* uid_set */ + FALSE, /* unseen */ + + self_freeze, /* self_code */ + (uid_t)(-1), /* uid */ + (gid_t)(-1), /* gid */ + + NULL, /* fallback_hostlist */ + NULL, /* transport instance */ + NULL, /* pass_router */ + NULL /* redirect_router */ +}; + +ip_address_item *running_interfaces = NULL; +BOOL running_in_test_harness = FALSE; + +/* This is a weird one. The following string gets patched in the binary by the +script that sets up a copy of Exim for running in the test harness. It seems +that compilers are now clever, and share constant strings if they can. +Elsewhere in Exim the string "<" is used. The compiler optimization seems to +make use of the end of this string in order to save space. So the patching then +wrecks this. We default this optimization by adding some additional characters +onto the end of the string. */ + +uschar *running_status = US">>>running<<<" "\0EXTRA"; + +int runrc = 0; + +uschar *search_error_message = NULL; +BOOL search_find_defer = FALSE; +uschar *self_hostname = NULL; +uschar *sender_address = NULL; +unsigned int sender_address_cache[(MAX_NAMED_LIST * 2)/32]; +BOOL sender_address_forced = FALSE; +uschar *sender_address_unrewritten = NULL; +uschar *sender_data = NULL; +unsigned int sender_domain_cache[(MAX_NAMED_LIST * 2)/32]; +uschar *sender_fullhost = NULL; +uschar *sender_helo_name = NULL; +uschar **sender_host_aliases = &no_aliases; +uschar *sender_host_address = NULL; +uschar *sender_host_authenticated = NULL; +unsigned int sender_host_cache[(MAX_NAMED_LIST * 2)/32]; +uschar *sender_host_name = NULL; +int sender_host_port = 0; +BOOL sender_host_notsocket = FALSE; +BOOL sender_host_unknown = FALSE; +uschar *sender_ident = NULL; +BOOL sender_local = FALSE; +uschar *sender_rcvhost = NULL; +BOOL sender_set_untrusted = FALSE; +uschar *sender_unqualified_hosts = NULL; +address_item *sender_verified_list = NULL; +address_item *sender_verified_failed = NULL; +int sender_verified_rc = -1; +BOOL sender_verified_responded = FALSE; +volatile BOOL sigalrm_seen = FALSE; +uschar **sighup_argv = NULL; +int smtp_accept_count = 0; +BOOL smtp_accept_keepalive = TRUE; +int smtp_accept_max = 20; +int smtp_accept_max_nonmail= 10; +uschar *smtp_accept_max_nonmail_hosts = US"*"; +int smtp_accept_max_per_connection = 1000; +uschar *smtp_accept_max_per_host = NULL; +int smtp_accept_queue = 0; +int smtp_accept_queue_per_connection = 10; +int smtp_accept_reserve = 0; +uschar *smtp_active_hostname = NULL; +BOOL smtp_authenticated = FALSE; +uschar *smtp_banner = US"$primary_hostname ESMTP " + "Exim $version_number $tod_full" + "\0<---------------Space to patch smtp_banner->"; +BOOL smtp_batched_input = FALSE; +BOOL smtp_check_spool_space = TRUE; +uschar *smtp_command_argument = NULL; +int smtp_connect_backlog = 20; +double smtp_delay_mail = 0.0; +double smtp_delay_rcpt = 0.0; +BOOL smtp_enforce_sync = TRUE; +FILE *smtp_in = NULL; +BOOL smtp_input = FALSE; +int smtp_load_reserve = -1; +int smtp_mailcmd_count = 0; +FILE *smtp_out = NULL; +uschar *smtp_etrn_command = NULL; +BOOL smtp_etrn_serialize = TRUE; +int smtp_max_synprot_errors= 3; +int smtp_max_unknown_commands = 3; +uschar *smtp_ratelimit_hosts = NULL; +uschar *smtp_ratelimit_mail = NULL; +uschar *smtp_ratelimit_rcpt = NULL; +uschar *smtp_read_error = US""; +int smtp_receive_timeout = 5*60; +uschar *smtp_reserve_hosts = NULL; +BOOL smtp_return_error_details = FALSE; +int smtp_rlm_base = 0; +double smtp_rlm_factor = 0.0; +int smtp_rlm_limit = 0; +int smtp_rlm_threshold = INT_MAX; +int smtp_rlr_base = 0; +double smtp_rlr_factor = 0.0; +int smtp_rlr_limit = 0; +int smtp_rlr_threshold = INT_MAX; +BOOL smtp_use_pipelining = FALSE; +BOOL smtp_use_size = FALSE; +BOOL split_spool_directory = FALSE; +uschar *spool_directory = US SPOOL_DIRECTORY + "\0<--------------Space to patch spool_directory->"; +int string_datestamp_offset= -1; +BOOL strip_excess_angle_brackets = FALSE; +BOOL strip_trailing_dot = FALSE; +uschar *submission_domain = NULL; +BOOL submission_mode = FALSE; +BOOL synchronous_delivery = FALSE; +BOOL syslog_duplication = TRUE; +int syslog_facility = LOG_MAIL; +uschar *syslog_processname = US"exim"; +BOOL syslog_timestamp = TRUE; +uschar *system_filter = NULL; + +uschar *system_filter_directory_transport = NULL; +uschar *system_filter_file_transport = NULL; +uschar *system_filter_pipe_transport = NULL; +uschar *system_filter_reply_transport = NULL; + +gid_t system_filter_gid = 0; +BOOL system_filter_gid_set = FALSE; +uid_t system_filter_uid = 0; +BOOL system_filter_uid_set = FALSE; +BOOL system_filtering = FALSE; + +BOOL tcp_nodelay = TRUE; +int thismessage_size_limit = 0; +int timeout_frozen_after = 0; +BOOL timestamps_utc = FALSE; + +transport_instance *transports = NULL; + +transport_instance transport_defaults = { + NULL, /* chain pointer */ + NULL, /* name */ + NULL, /* info */ + NULL, /* private options block pointer */ + NULL, /* driver name */ + NULL, /* setup entry point */ + 1, /* batch_max */ + NULL, /* batch_id */ + NULL, /* home_dir */ + NULL, /* current_dir */ + TRUE, /* multi-domain */ + FALSE, /* overrides_hosts */ + 100, /* max_addresses */ + 500, /* connection_max_messages */ + FALSE, /* deliver_as_creator */ + FALSE, /* disable_logging */ + FALSE, /* initgroups */ + FALSE, /* uid_set */ + FALSE, /* gid_set */ + (uid_t)(-1), /* uid */ + (gid_t)(-1), /* gid */ + NULL, /* expand_uid */ + NULL, /* expand_gid */ + NULL, /* warn_message */ + NULL, /* shadow */ + NULL, /* shadow_condition */ + NULL, /* filter_command */ + NULL, /* add_headers */ + NULL, /* remove_headers */ + NULL, /* return_path */ + NULL, /* debug_string */ + NULL, /* message_size_limit */ + NULL, /* headers_rewrite */ + NULL, /* rewrite_rules */ + 0, /* rewrite_existflags */ + 300, /* filter_timeout */ + FALSE, /* body_only */ + FALSE, /* delivery_date_add */ + FALSE, /* envelope_to_add */ + FALSE, /* headers_only */ + FALSE, /* rcpt_include_affixes */ + FALSE, /* return_path_add */ + FALSE, /* return_output */ + FALSE, /* return_fail_output */ + FALSE, /* log_output */ + FALSE, /* log_fail_output */ + FALSE, /* log_defer_output */ + TRUE_UNSET /* retry_use_local_part: BOOL, but set neither + 1 nor 0 so can detect unset */ +}; + +int transport_count; +uschar **transport_filter_argv = NULL; +int transport_filter_timeout; +int transport_write_timeout= 0; + +tree_node *tree_dns_fails = NULL; +tree_node *tree_duplicates = NULL; +tree_node *tree_nonrecipients = NULL; +tree_node *tree_unusable = NULL; + +BOOL trusted_caller = FALSE; +gid_t *trusted_groups = NULL; +uid_t *trusted_users = NULL; +uschar *timezone_string = US TIMEZONE_DEFAULT; + +uschar *unknown_login = NULL; +uschar *unknown_username = NULL; +uschar *untrusted_set_sender = NULL; + +/* A regex for matching a "From_" line in an incoming message, in the form + + From ph10 Fri Jan 5 12:35 GMT 1996 + +which the "mail" commands send to the MTA (undocumented, of course), or in +the form + + From ph10 Fri, 7 Jan 97 14:00:00 GMT + +which is apparently used by some UUCPs, despite it not being in RFC 976. +Because of variations in time formats, just match up to the minutes. That +should be sufficient. Examples have been seen of time fields like 12:1:03, +so just require one digit for hours and minutes. The weekday is also absent +in some forms. */ + +uschar *uucp_from_pattern = US + "^From\\s+(\\S+)\\s+(?:[a-zA-Z]{3},?\\s+)?" /* Common start */ + "(?:" /* Non-extracting bracket */ + "[a-zA-Z]{3}\\s+\\d?\\d|" /* First form */ + "\\d?\\d\\s+[a-zA-Z]{3}\\s+\\d\\d(?:\\d\\d)?" /* Second form */ + ")" /* End alternation */ + "\\s+\\d\\d?:\\d\\d?"; /* Start of time */ + +uschar *uucp_from_sender = US"$1"; + +uschar *warn_message_file = NULL; +uschar *warnmsg_delay = NULL; +uschar *warnmsg_recipients = NULL; +BOOL write_rejectlog = TRUE; + +uschar *version_copyright = US"Copyright (c) University of Cambridge 2004"; +uschar *version_date = US"?"; +uschar *version_cnumber = US"????"; +uschar *version_string = US"?"; + +int warning_count = 0; + +/* End of globals.c */ diff --git a/src/src/globals.h b/src/src/globals.h new file mode 100644 index 000000000..17183c6f8 --- /dev/null +++ b/src/src/globals.h @@ -0,0 +1,617 @@ +/* $Cambridge: exim/src/src/globals.h,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Almost all the global variables are defined together in this one header, so +that they are easy to find. However, those that are visible during the +compilation of the local_scan() function are defined separately in the +local_scan.h header file. */ + +/* First put any specials that are required for some operating systems. */ + +#ifdef NEED_H_ERRNO +extern int h_errno; +#endif + +/* Now things that are present only when configured. */ + +#ifdef EXIM_PERL +extern uschar *opt_perl_startup; /* Startup code for Perl interpreter */ +extern BOOL opt_perl_at_start; /* Start Perl interpreter at start */ +extern BOOL opt_perl_started; /* Set once interpreter started */ +#endif + +#ifdef LOOKUP_IBASE +extern uschar *ibase_servers; +#endif + +#ifdef LOOKUP_LDAP +extern uschar *eldap_default_servers; /* List of default servers */ +extern int eldap_version; /* LDAP version */ +#endif + +#ifdef LOOKUP_MYSQL +extern uschar *mysql_servers; /* List of servers and connect info */ +#endif + +#ifdef LOOKUP_ORACLE +extern uschar *oracle_servers; /* List of servers and connect info */ +#endif + +#ifdef LOOKUP_PGSQL +extern uschar *pgsql_servers; /* List of servers and connect info */ +#endif + +#ifdef SUPPORT_MOVE_FROZEN_MESSAGES +extern BOOL move_frozen_messages; /* Get them out of the normal directory */ +#endif + +/* These variables are outside the #ifdef because it keeps the code less +cluttered in several places (e.g. during logging) if we can always refer to +them. Also, the tls_ variables are now always visible. */ + +extern int tls_active; /* fd/socket when in a TLS session */ +extern BOOL tls_certificate_verified; /* Client certificate verified */ +extern uschar *tls_cipher; /* Cipher used */ +extern BOOL tls_on_connect; /* For older MTAs that don't STARTTLS */ +extern uschar *tls_on_connect_ports; /* Ports always tls-on-connect */ +extern uschar *tls_peerdn; /* DN from peer */ + +#ifdef SUPPORT_TLS +extern const pcre *regex_STARTTLS; /* For recognizing STARTTLS settings */ +extern uschar *tls_advertise_hosts; /* host for which TLS is advertised */ +extern uschar *tls_certificate; /* Certificate file */ +extern uschar *tls_crl; /* CRL File */ +extern uschar *tls_dhparam; /* DH param file */ +extern BOOL tls_offered; /* Server offered TLS */ +extern uschar *tls_privatekey; /* Private key file */ +extern BOOL tls_remember_esmtp; /* For YAEB */ +extern uschar *tls_require_ciphers; /* So some can be avoided */ +extern uschar *tls_try_verify_hosts; /* Optional client verification */ +extern uschar *tls_verify_certificates;/* Path for certificates to check */ +extern uschar *tls_verify_hosts; /* Mandatory client verification */ +#endif + + +/* Input-reading functions for messages, so we can use special ones for +incoming TCP/IP. */ + +extern int (*receive_getc)(void); +extern int (*receive_ungetc)(int); +extern int (*receive_feof)(void); +extern int (*receive_ferror)(void); + + +/* For clearing, saving, restoring address expansion variables. We have to have +the size of this vector set explicitly, because it is referenced from more than +one module. */ + +extern uschar **address_expansions[ADDRESS_EXPANSIONS_COUNT]; + +/* General global variables */ + +extern BOOL accept_8bitmime; /* Allow *BITMIME incoming */ +extern tree_node *acl_anchor; /* Tree of named ACLs */ +extern uschar *acl_not_smtp; /* ACL run for non-SMTP messages */ +extern uschar *acl_smtp_auth; /* ACL run for AUTH */ +extern uschar *acl_smtp_connect; /* ACL run on SMTP connection */ +extern uschar *acl_smtp_data; /* ACL run after DATA received */ +extern uschar *acl_smtp_etrn; /* ACL run for ETRN */ +extern uschar *acl_smtp_expn; /* ACL run for EXPN */ +extern uschar *acl_smtp_helo; /* ACL run for HELO/EHLO */ +extern uschar *acl_smtp_mail; /* ACL run for MAIL */ +extern uschar *acl_smtp_mailauth; /* ACL run for MAIL AUTH */ +extern uschar *acl_smtp_predata; /* ACL run for DATA command */ +extern uschar *acl_smtp_quit; /* ACL run for QUIT */ +extern uschar *acl_smtp_rcpt; /* ACL run for RCPT */ +extern uschar *acl_smtp_starttls; /* ACL run for STARTTLS */ +extern uschar *acl_smtp_vrfy; /* ACL run for VRFY */ +extern BOOL acl_temp_details; /* TRUE to give details for 4xx error */ +extern uschar *acl_var[ACL_C_MAX+ACL_M_MAX]; /* User variables */ +extern uschar *acl_verify_message; /* User message for verify failure */ +extern header_line *acl_warn_headers; /* Warning headers added by ACL */ +extern string_item *acl_warn_logged; /* Logged lines */ +extern int acl_wherecodes[]; /* Response codes for ACL fails */ +extern uschar *acl_wherenames[]; /* Names for messages */ +extern address_item *addr_duplicate; /* Duplicate address list */ +extern address_item address_defaults; /* Default data for address item */ +extern uschar *address_file; /* Name of file when delivering to one */ +extern uschar *address_pipe; /* Pipe command when delivering to one */ +extern BOOL address_test_mode; /* True for -bt */ +extern tree_node *addresslist_anchor; /* Tree of defined address lists */ +extern int addresslist_count; /* Number defined */ +extern gid_t *admin_groups; /* List of admin groups */ +extern BOOL admin_user; /* True if caller can do admin */ +extern BOOL allow_domain_literals; /* As it says */ +extern BOOL allow_mx_to_ip; /* Allow MX records to -> ip address */ +extern BOOL allow_unqualified_recipient; /* As it says */ +extern BOOL allow_unqualified_sender; /* Ditto */ +extern BOOL allow_utf8_domains; /* For experimenting */ +extern uschar *authenticated_id; /* ID that was authenticated */ +extern uschar *authenticated_sender; /* From AUTH on MAIL */ +extern BOOL authentication_failed; /* TRUE if AUTH was tried and failed */ +extern uschar *auth_advertise_hosts; /* Only advertise to these */ +extern auth_info auths_available[]; /* Vector of available auth mechanisms */ +extern auth_instance *auths; /* Chain of instantiated auths */ +extern auth_instance auth_defaults; /* Default values */ +extern uschar *auth_defer_msg; /* Error message for log */ +extern uschar *auth_defer_user_msg; /* Error message for user */ +extern int auto_thaw; /* Auto-thaw interval */ + +extern BOOL background_daemon; /* Set FALSE to keep in foreground */ +extern uschar *base62_chars; /* Table of base-62 characters */ +extern uschar *bi_command; /* Command for -bi option */ +extern uschar *big_buffer; /* Used for various temp things */ +extern int big_buffer_size; /* Current size (can expand) */ +extern int body_linecount; /* Line count in body */ +extern int body_zerocount; /* Binary zero count in body */ +extern uschar *bounce_message_file; /* Template file */ +extern uschar *bounce_message_text; /* One-liner */ +extern uschar *bounce_recipient; /* When writing an errmsg */ +extern BOOL bounce_return_body; /* Include body in returned message */ +extern BOOL bounce_return_message; /* Include message in bounce */ +extern int bounce_return_size_limit; /* Max amount to return */ +extern uschar *bounce_sender_authentication; /* AUTH address for bounces */ +extern int bsmtp_transaction_linecount; /* Start of last transaction */ + +extern int callout_cache_domain_positive_expire; /* Time for positive domain callout cache records to expire */ +extern int callout_cache_domain_negative_expire; /* Time for negative domain callout cache records to expire */ +extern int callout_cache_positive_expire; /* Time for positive callout cache records to expire */ +extern int callout_cache_negative_expire; /* Time for negative callout cache records to expire */ +extern uschar *callout_random_local_part; /* Local part to be used to check if server called will accept any local part */ +extern uschar *check_dns_names_pattern;/* Regex for syntax check */ +extern int check_log_inodes; /* Minimum for message acceptance */ +extern int check_log_space; /* Minimum for message acceptance */ +extern int check_spool_inodes; /* Minimum for message acceptance */ +extern int check_spool_space; /* Minimum for message acceptance */ +extern int clmacro_count; /* Number of command line macros */ +extern uschar *clmacros[]; /* Copy of them, for re-exec */ +extern int connection_max_messages;/* Max down one SMTP connection */ +extern BOOL config_changed; /* True if -C used */ +extern FILE *config_file; /* Configuration file */ +extern uschar *config_filename; /* Configuration file name */ +extern int config_lineno; /* Line number */ +extern uschar *config_main_filelist; /* List of possible config files */ +extern uschar *config_main_filename; /* File name actually used */ +#ifdef CONFIGURE_OWNER +extern uid_t config_uid; /* Additional owner */ +#endif +extern uschar *continue_hostname; /* Host for continued delivery */ +extern uschar *continue_host_address; /* IP address for ditto */ +extern BOOL continue_more; /* Flag more addresses waiting */ +extern int continue_sequence; /* Sequence num for continued delivery */ +extern uschar *continue_transport; /* Transport for continued delivery */ + +extern BOOL daemon_listen; /* True if listening required */ +extern uschar *daemon_smtp_port; /* Can be a list of ports */ +extern int debug_fd; /* The fd for debug_file */ +extern FILE *debug_file; /* Where to write debugging info */ +extern bit_table debug_options[]; /* Table of debug options */ +extern int debug_options_count; /* Size of table */ +extern int delay_warning[]; /* Times between warnings */ +extern uschar *delay_warning_condition; /* Condition string for warnings */ +extern BOOL delivery_date_remove; /* Remove delivery-date headers */ + +extern uschar *deliver_address_data; /* Arbitrary data for an address */ +extern int deliver_datafile; /* FD for data part of message */ +extern uschar *deliver_domain; /* The local domain for delivery */ +extern uschar *deliver_domain_data; /* From domain lookup */ +extern uschar *deliver_domain_orig; /* The original local domain for delivery */ +extern uschar *deliver_domain_parent; /* The parent domain for delivery */ +extern BOOL deliver_drop_privilege; /* TRUE for unprivileged delivery */ +extern BOOL deliver_firsttime; /* True for first delivery attempt */ +extern BOOL deliver_force; /* TRUE if delivery was forced */ +extern BOOL deliver_freeze; /* TRUE if delivery is frozen */ +extern int deliver_frozen_at; /* Time of freezing */ +extern uschar *deliver_home; /* Home directory for pipes */ +extern uschar *deliver_host; /* (First) host for routed local deliveries */ + /* Remote host for filter */ +extern uschar *deliver_host_address; /* Address for remote delivery filter */ +extern uschar *deliver_in_buffer; /* Buffer for copying file */ +extern ino_t deliver_inode; /* Inode for appendfile */ +extern uschar *deliver_localpart; /* The local part for delivery */ +extern uschar *deliver_localpart_data; /* From local part lookup */ +extern uschar *deliver_localpart_orig; /* The original local part for delivery */ +extern uschar *deliver_localpart_parent; /* The parent local part for delivery */ +extern uschar *deliver_localpart_prefix; /* The stripped prefix, if any */ +extern uschar *deliver_localpart_suffix; /* The stripped suffix, if any */ +extern BOOL deliver_force_thaw; /* TRUE to force thaw in queue run */ +extern BOOL deliver_manual_thaw; /* TRUE if manually thawed */ +extern uschar *deliver_out_buffer; /* Buffer for copying file */ +extern int deliver_queue_load_max; /* Different value for queue running */ +extern address_item *deliver_recipients; /* Current set of addresses */ +extern uschar *deliver_selectstring; /* For selecting by recipient */ +extern BOOL deliver_selectstring_regex; /* String is regex */ +extern uschar *deliver_selectstring_sender; /* For selecting by sender */ +extern BOOL deliver_selectstring_sender_regex; /* String is regex */ +extern BOOL disable_logging; /* Disables log writing when TRUE */ + +extern uschar *dns_again_means_nonexist; /* Domains that are badly set up */ +extern uschar *dns_ipv4_lookup; /* For these domains, don't look for AAAA (or A6) */ +extern int dns_retrans; /* Retransmission time setting */ +extern int dns_retry; /* Number of retries */ +extern uschar *dnslist_domain; /* DNS (black) list domain */ +extern uschar *dnslist_text; /* DNS (black) list text message */ +extern uschar *dnslist_value; /* DNS (black) list IP address */ +extern tree_node *domainlist_anchor; /* Tree of defined domain lists */ +extern int domainlist_count; /* Number defined */ +extern BOOL dont_deliver; /* TRUE for -N option */ +extern BOOL dot_ends; /* TRUE if "." ends non-SMTP input */ + +/* This option is now a no-opt, retained for compatibility */ +extern BOOL drop_cr; /* For broken local MUAs */ + +extern BOOL enable_dollar_recipients; /* Make $recipients available */ +extern int envelope_to_remove; /* Remove envelope_to_headers */ +extern int errno_quota; /* Quota errno in this OS */ +extern int error_handling; /* Error handling style */ +extern uschar *errors_copy; /* For taking copies of errors */ +extern uschar *errors_reply_to; /* Reply-to for error messages */ +extern int errors_sender_rc; /* Return after message to sender*/ +extern gid_t exim_gid; /* To be used with exim_uid */ +extern BOOL exim_gid_set; /* TRUE if exim_gid set */ +extern uschar *exim_path; /* Path to exec exim */ +extern uid_t exim_uid; /* Non-root uid for exim */ +extern BOOL exim_uid_set; /* TRUE if exim_uid set */ +extern int expand_forbid; /* RDO flags for forbidding things */ +extern int expand_nlength[]; /* Lengths of numbered strings */ +extern int expand_nmax; /* Max numerical value */ +extern uschar *expand_nstring[]; /* Numbered strings */ +extern BOOL expand_string_forcedfail; /* TRUE if failure was "expected" */ +extern BOOL extract_addresses_remove_arguments; /* Controls -t behaviour */ +extern uschar *extra_local_interfaces; /* Local, non-listen interfaces */ + +extern int filter_n[FILTER_VARIABLE_COUNT]; /* filter variables */ +extern BOOL filter_running; /* TRUE while running a filter */ +extern int filter_sn[FILTER_VARIABLE_COUNT]; /* variables set by system filter */ +extern uschar *filter_test; /* Run as a filter tester on this file */ +extern uschar *filter_thisaddress; /* For address looping */ +extern int finduser_retries; /* Retry count for getpwnam() */ +extern uid_t fixed_never_users[]; /* Can't be overridden */ +extern uschar *freeze_tell; /* Message on (some) freezings */ +extern uschar *fudged_queue_times; /* For use in test harness */ + +extern uschar *gecos_name; /* To be expanded when pattern matches */ +extern uschar *gecos_pattern; /* Pattern to match */ +extern rewrite_rule *global_rewrite_rules; /* Chain of rewriting rules */ + +extern int header_insert_maxlen; /* Max for inserting headers */ +extern int header_maxsize; /* Max total length for header */ +extern int header_line_maxsize; /* Max for an individual line */ +extern header_name header_names[]; /* Table of header names */ +extern int header_names_size; /* Number of entries */ +extern BOOL header_rewritten; /* TRUE if header changed by router */ +extern uschar *helo_accept_junk_hosts; /* Allowed to use junk arg */ +extern uschar *helo_allow_chars; /* Rogue chars to allow in HELO/EHLO */ +extern uschar *helo_lookup_domains; /* If these given, lookup host name */ +extern uschar *helo_try_verify_hosts; /* Soft check HELO argument for these */ +extern BOOL helo_verified; /* True if HELO verified */ +extern uschar *helo_verify_hosts; /* Hard check HELO argument for these */ +extern uschar *hex_digits; /* Used in several places */ +extern uschar *hold_domains; /* Hold up deliveries to these */ +extern BOOL host_find_failed_syntax;/* DNS syntax check failure */ +extern BOOL host_checking_callout; /* TRUE if real callout wanted */ +extern uschar *host_data; /* Obtained from lookup in ACL */ +extern uschar *host_lookup; /* For which IP addresses are always looked up */ +extern BOOL host_lookup_failed; /* TRUE if lookup failed */ +extern uschar *host_lookup_order; /* Order of host lookup types */ +extern uschar *host_lookup_msg; /* Text for why it failed */ +extern int host_number; /* For sharing spools */ +extern uschar *host_number_string; /* For expanding */ +extern uschar *host_reject_connection; /* Reject these hosts */ +extern tree_node *hostlist_anchor; /* Tree of defined host lists */ +extern int hostlist_count; /* Number defined */ +extern uschar *hosts_connection_nolog; /* Limits the logging option */ +extern uschar *hosts_treat_as_local; /* For routing */ + +extern int ignore_bounce_errors_after; /* Keep them for this time. */ +extern BOOL ignore_fromline_local; /* Local SMTP ignore fromline */ +extern uschar *ignore_fromline_hosts; /* Hosts permitted to send "From " */ +extern BOOL is_inetd; /* True for inetd calls */ + +extern int journal_fd; /* Fd for journal file */ + +extern int keep_malformed; /* Time to keep malformed messages */ + +extern uschar *eldap_dn; /* Where LDAP DNs are left */ +extern int load_average; /* Most recently read load average */ +extern BOOL local_error_message; /* True if handling one of these */ +extern BOOL local_from_check; /* For adding Sender: */ +extern uschar *local_from_prefix; /* Permitted prefixes */ +extern uschar *local_from_suffix; /* Permitted suffixes */ +extern uschar *local_interfaces; /* For forcing specific interfaces */ +extern uschar *local_scan_data; /* Text returned by local_scan() */ +extern optionlist local_scan_options[];/* Option list for local_scan() */ +extern int local_scan_options_count; /* Size of the list */ +extern int local_scan_timeout; /* Timeout for local_scan() */ +extern BOOL local_sender_retain; /* Retain Sender: (with no From: check) */ +extern gid_t local_user_gid; /* As it says; may be set in routers */ +extern uid_t local_user_uid; /* As it says; may be set in routers */ +extern tree_node *localpartlist_anchor;/* Tree of defined localpart lists */ +extern int localpartlist_count; /* Number defined */ +extern uschar *log_buffer; /* For constructing log entries */ +extern unsigned int log_extra_selector;/* Bit map of logging options other than used by log_write() */ +extern uschar *log_file_path; /* If unset, use default */ +extern bit_table log_options[]; /* Table of options */ +extern int log_options_count; /* Size of table */ +extern unsigned int log_write_selector;/* Bit map of logging options for log_write() */ +extern uschar *log_selector_string; /* As supplied in the config */ +extern FILE *log_stderr; /* Copy of stderr for log use, or NULL */ +extern BOOL log_testing_mode; /* TRUE in various testing modes */ +extern BOOL log_timezone; /* TRUE to include the timezone in log lines */ +extern uschar *login_sender_address; /* The actual sender address */ +extern lookup_info lookup_list[]; /* Vector of available lookups */ +extern int lookup_list_count; /* Number of entries in the list */ +extern int lookup_open_max; /* Max lookup files to cache */ +extern uschar *lookup_value; /* Value looked up from file */ + +extern macro_item *macros; /* Configuration macros */ +extern uschar *mailstore_basename; /* For mailstore deliveries */ +extern int max_username_length; /* For systems with broken getpwnam() */ +extern int message_age; /* In seconds */ +extern uschar *message_body; /* Start of message body for filter */ +extern uschar *message_body_end; /* End of message body for filter */ +extern int message_body_size; /* Sic */ +extern int message_body_visible; /* Amount visible in message_body */ +extern int message_ended; /* State of message reading and how ended */ +extern uschar *message_headers; /* When built */ +extern uschar message_id_option[]; /* -E for use as option */ +extern uschar *message_id_external; /* External form of following */ +extern uschar *message_id_domain; /* Expanded to form domain-part of message_id */ +extern uschar *message_id_text; /* Expanded to form message_id */ +extern struct timeval message_id_tv; /* Time used to create last message_id */ +extern int message_linecount; /* As it says */ +extern BOOL message_logs; /* TRUE to write message logs */ +extern int message_size; /* Size of message */ +extern uschar *message_size_limit; /* As it says */ +extern uschar message_subdir[]; /* Subdirectory for messages */ +extern uschar *message_reference; /* Reference for error messages */ +extern BOOL mua_wrapper; /* TRUE when Exim is wrapping an MUA */ + +extern uid_t *never_users; /* List of uids never to be used */ +extern BOOL no_multiline_responses; /* For broken clients */ + +extern optionlist optionlist_auths[]; /* These option lists are made */ +extern int optionlist_auths_size; /* global so that readconf can */ +extern optionlist optionlist_routers[]; /* see them for printing out */ +extern int optionlist_routers_size; /* the options. */ +extern optionlist optionlist_transports[]; +extern int optionlist_transports_size; + +extern uid_t original_euid; /* Original effective uid */ +extern gid_t originator_gid; /* Gid of whoever wrote spool file */ +extern uschar *originator_login; /* Login of same */ +extern uschar *originator_name; /* Full name of same */ +extern uid_t originator_uid; /* Uid of ditto */ +extern uschar *override_local_interfaces; /* Value of -oX argument */ +extern uschar *override_pid_file_path; /* Value of -oP argument */ + +extern BOOL parse_allow_group; /* Allow group syntax */ +extern BOOL parse_found_group; /* In the middle of a group */ +extern uschar *percent_hack_domains; /* Local domains for which '% operates */ +extern uschar *pid_file_path; /* For writing daemon pids */ +extern uschar *pipelining_advertise_hosts; /* As it says */ +extern BOOL preserve_message_logs; /* Save msglog files */ +extern uschar *primary_hostname; /* Primary name of this computer */ +extern BOOL print_topbitchars; /* Topbit chars are printing chars */ +extern uschar process_info[]; /* For SIGUSR1 output */ +extern uschar *process_log_path; /* Alternate path */ +extern BOOL prod_requires_admin; /* TRUE if prodding requires admin */ + +extern uschar *qualify_domain_recipient; /* Domain to qualify recipients with */ +extern uschar *qualify_domain_sender; /* Domain to qualify senders with */ +extern BOOL queue_2stage; /* Run queue in 2-stage manner */ +extern uschar *queue_domains; /* Queue these domains */ +extern BOOL queue_list_requires_admin; /* TRUE if -bp requires admin */ +extern BOOL queue_run_first_delivery; /* If TRUE, first deliveries only */ +extern BOOL queue_run_force; /* TRUE to force during queue run */ +extern BOOL queue_run_local; /* Local deliveries only in queue run */ +extern BOOL queue_running; /* TRUE for queue running process and */ + /* immediate children */ +extern pid_t queue_run_pid; /* PID of the queue running process or 0 */ +extern int queue_run_pipe; /* Pipe for synchronizing */ +extern int queue_interval; /* Queue running interval */ +extern BOOL queue_only; /* TRUE to disable immediate delivery */ +extern int queue_only_load; /* Max load before auto-queue */ +extern uschar *queue_only_file; /* Queue if file exists/not-exists */ +extern BOOL queue_only_override; /* Allow override from command line */ +extern BOOL queue_only_policy; /* ACL or local_scan wants queue_only */ +extern BOOL queue_run_in_order; /* As opposed to random */ +extern int queue_run_max; /* Max queue runners */ +extern BOOL queue_smtp; /* Disable all immediate STMP (-odqs)*/ +extern uschar *queue_smtp_domains; /* Ditto, for these domains */ + +extern unsigned int random_seed; /* Seed for random numbers */ +extern uschar *raw_active_hostname; /* Pre-expansion */ +extern uschar *raw_sender; /* Before rewriting */ +extern uschar **raw_recipients; /* Before rewriting */ +extern int raw_recipients_count; +extern int rcpt_count; /* Count of RCPT commands in a message */ +extern int rcpt_fail_count; /* Those that got 5xx */ +extern int rcpt_defer_count; /* Those that got 4xx */ +extern gid_t real_gid; /* Real gid */ +extern uid_t real_uid; /* Real user running program */ +extern BOOL really_exim; /* FALSE in utilities */ +extern BOOL receive_call_bombout; /* Flag for crashing log */ +extern int receive_linecount; /* Mainly for BSMTP errors */ +extern int receive_messagecount; /* Mainly for BSMTP errors */ +extern int receive_timeout; /* For non-SMTP acceptance */ +extern int received_count; /* Count of Received: headers */ +extern uschar *received_for; /* For "for" field */ +extern uschar *received_header_text; /* Definition of Received: header */ +extern int received_headers_max; /* Max count of Received: headers */ +extern int received_time; /* Time the message was received */ +extern uschar *recipient_data; /* lookup data for recipients */ +extern uschar *recipient_unqualified_hosts; /* Permitted unqualified recipients */ +extern BOOL recipients_discarded; /* By an ACL */ +extern int recipients_list_max; /* Maximum number fitting in list */ +extern int recipients_max; /* Max permitted */ +extern int recipients_max_reject; /* If TRUE, reject whole message */ +extern const pcre *regex_AUTH; /* For recognizing AUTH settings */ +extern const pcre *regex_check_dns_names; /* For DNS name checking */ +extern const pcre *regex_From; /* For recognizing "From_" lines */ +extern const pcre *regex_PIPELINING; /* For recognizing PIPELINING */ +extern const pcre *regex_SIZE; /* For recognizing SIZE settings */ +extern const pcre *regex_ismsgid; /* Compiled r.e. for message it */ +extern int remote_delivery_count; /* Number of remote addresses */ +extern int remote_max_parallel; /* Maximum parallel delivery */ +extern uschar *remote_sort_domains; /* Remote domain sorting order */ +extern retry_config *retries; /* Chain of retry config information */ +extern int retry_data_expire; /* When to expire retry data */ +extern int retry_interval_max; /* Absolute maximum */ +extern int retry_maximum_timeout; /* The maximum timeout */ +extern uschar *return_path; /* Return path for a message */ +extern BOOL return_path_remove; /* Remove return-path headers */ +extern int rewrite_existflags; /* Indicate which headers have rewrites */ +extern uschar *rfc1413_hosts; /* RFC hosts */ +extern int rfc1413_query_timeout; /* Timeout on RFC 1413 calls */ +/* extern BOOL rfc821_domains; */ /* If set, syntax is 821, not 822 => being abolished */ +extern uid_t root_uid; /* The uid for root */ +extern router_info routers_available[];/* Vector of available routers */ +extern router_instance *routers; /* Chain of instantiated routers */ +extern router_instance router_defaults;/* Default values */ +extern BOOL running_in_test_harness; /*TRUE when running_status is patched */ +extern ip_address_item *running_interfaces; /* Host's running interfaces */ +extern uschar *running_status; /* Flag string for testing */ +extern int runrc; /* rc from ${run} */ + +extern uschar *search_error_message; /* Details of lookup problem */ +extern BOOL search_find_defer; /* Set TRUE if lookup deferred */ +extern uschar *self_hostname; /* Self host after routing->directors */ +extern unsigned int sender_address_cache[(MAX_NAMED_LIST * 2)/32]; /* Cache bits for sender */ +extern BOOL sender_address_forced; /* Set by -f */ +extern uschar *sender_address_unrewritten; /* Set if rewritten by verify */ +extern uschar *sender_data; /* lookup result for senders */ +extern unsigned int sender_domain_cache[(MAX_NAMED_LIST * 2)/32]; /* Cache bits for sender domain */ +extern uschar *sender_fullhost; /* Sender host name + address */ +extern uschar *sender_helo_name; /* Host name from HELO/EHLO */ +extern uschar **sender_host_aliases; /* Points to list of alias names */ +extern unsigned int sender_host_cache[(MAX_NAMED_LIST * 2)/32]; /* Cache bits for incoming host */ +extern BOOL sender_host_notsocket; /* Set for -bs and -bS */ +extern BOOL sender_host_unknown; /* TRUE for -bs and -bS except inetd */ +extern uschar *sender_ident; /* Sender identity via RFC 1413 */ +extern BOOL sender_local; /* TRUE for local senders */ +extern uschar *sender_rcvhost; /* Host data for Received: */ +extern BOOL sender_set_untrusted; /* Sender set by untrusted caller */ +extern uschar *sender_unqualified_hosts; /* Permitted unqualified senders */ +extern address_item *sender_verified_list; /* Saved chain of sender verifies */ +extern address_item *sender_verified_failed; /* The one that caused denial */ +extern volatile BOOL sigalrm_seen; /* Flag for sigalrm_handler */ +extern uschar **sighup_argv; /* Args for re-execing after SIGHUP */ +extern int smtp_accept_count; /* Count of connections */ +extern BOOL smtp_accept_keepalive; /* Set keepalive on incoming */ +extern int smtp_accept_max; /* Max SMTP connections */ +extern int smtp_accept_max_nonmail;/* Max non-mail commands in one con */ +extern uschar *smtp_accept_max_nonmail_hosts; /* Limit non-mail cmds from these hosts */ +extern int smtp_accept_max_per_connection; /* Max msgs per connection */ +extern uschar *smtp_accept_max_per_host; /* Max SMTP cons from one IP addr */ +extern int smtp_accept_queue; /* Queue after so many connections */ +extern int smtp_accept_queue_per_connection; /* Queue after so many msgs */ +extern int smtp_accept_reserve; /* Reserve these SMTP connections */ +extern uschar *smtp_active_hostname; /* Hostname for this message */ +extern BOOL smtp_authenticated; /* Sending client has authenticated */ +extern uschar *smtp_banner; /* Banner string (to be expanded) */ +extern BOOL smtp_check_spool_space; /* TRUE to check SMTP SIZE value */ +extern uschar *smtp_command_argument; /* When running certain ACLs */ +extern int smtp_connect_backlog; /* Max backlog permitted */ +extern double smtp_delay_mail; /* Current MAIL delay */ +extern double smtp_delay_rcpt; /* Current RCPT delay */ +extern BOOL smtp_enforce_sync; /* Enforce sync rules */ +extern uschar *smtp_etrn_command; /* Command to run */ +extern BOOL smtp_etrn_serialize; /* Only one at once */ +extern FILE *smtp_in; /* Incoming SMTP input file */ +extern int smtp_load_reserve; /* Only from reserved if load > this */ +extern int smtp_mailcmd_count; /* Count of MAIL commands */ +extern int smtp_max_synprot_errors;/* Max syntax/protocol errors */ +extern int smtp_max_unknown_commands; /* As it says */ +extern FILE *smtp_out; /* Incoming SMTP output file */ +extern uschar *smtp_ratelimit_hosts; /* Rate limit these hosts */ +extern uschar *smtp_ratelimit_mail; /* Parameters for MAIL limiting */ +extern uschar *smtp_ratelimit_rcpt; /* Parameters for RCPT limiting */ +extern uschar *smtp_read_error; /* Message for SMTP input error */ +extern int smtp_receive_timeout; /* Applies to each received line */ +extern uschar *smtp_reserve_hosts; /* Hosts for reserved slots */ +extern BOOL smtp_return_error_details; /* TRUE to return full info */ +extern int smtp_rlm_base; /* Base interval for MAIL rate limit */ +extern double smtp_rlm_factor; /* Factor for MAIL rate limit */ +extern int smtp_rlm_limit; /* Max delay */ +extern int smtp_rlm_threshold; /* Threshold for RCPT rate limit */ +extern int smtp_rlr_base; /* Base interval for RCPT rate limit */ +extern double smtp_rlr_factor; /* Factor for RCPT rate limit */ +extern int smtp_rlr_limit; /* Max delay */ +extern int smtp_rlr_threshold; /* Threshold for RCPT rate limit */ +extern BOOL smtp_use_pipelining; /* Global for passed connections */ +extern BOOL smtp_use_size; /* Global for passed connections */ +extern BOOL split_spool_directory; /* TRUE to use multiple subdirs */ +extern uschar *spool_directory; /* Name of spool directory */ +extern int string_datestamp_offset;/* After insertion by string_format */ +extern BOOL strip_excess_angle_brackets; /* Surrounding route-addrs */ +extern BOOL strip_trailing_dot; /* Remove dots at ends of domains */ +extern uschar *submission_domain; /* Domain for submission mode */ +extern BOOL submission_mode; /* Can be forced from ACL */ +extern BOOL synchronous_delivery; /* TRUE if -odi is set */ +extern BOOL syslog_duplication; /* FALSE => no duplicate logging */ +extern int syslog_facility; /* As defined by Syslog.h */ +extern uschar *syslog_processname; /* 'ident' param to openlog() */ +extern BOOL syslog_timestamp; /* TRUE if time on syslogs */ +extern uschar *system_filter; /* Name of system filter file */ + +extern uschar *system_filter_directory_transport; /* Transports for the */ +extern uschar *system_filter_file_transport; /* system filter */ +extern uschar *system_filter_pipe_transport; +extern uschar *system_filter_reply_transport; + +extern gid_t system_filter_gid; /* Gid for running system filter */ +extern BOOL system_filter_gid_set; /* TRUE if gid set */ +extern uid_t system_filter_uid; /* Uid for running system filter */ +extern BOOL system_filter_uid_set; /* TRUE if uid set */ +extern BOOL system_filtering; /* TRUE when running system filter */ + +extern BOOL tcp_nodelay; /* Controls TCP_NODELAY on daemon */ +extern int thismessage_size_limit; /* Limit for this message */ +extern int timeout_frozen_after; /* Max time to keep frozen messages */ +extern BOOL timestamps_utc; /* Use UTC for all times */ +extern int transport_count; /* Count of bytes transported */ +extern uschar **transport_filter_argv; /* For on-the-fly filtering */ +extern int transport_filter_timeout; /* Timeout for same */ + +extern transport_info transports_available[]; /* Vector of available transports */ +extern transport_instance *transports; /* Chain of instantiated transports */ +extern transport_instance transport_defaults; /* Default values */ + +extern int transport_write_timeout;/* Set to time out individual writes */ + +extern tree_node *tree_dns_fails; /* Tree of DNS lookup failures */ +extern tree_node *tree_duplicates; /* Tree of duplicate addresses */ +extern tree_node *tree_nonrecipients; /* Tree of nonrecipient addresses */ +extern tree_node *tree_unusable; /* Tree of unusable addresses */ + +extern BOOL trusted_caller; /* Caller is trusted */ +extern gid_t *trusted_groups; /* List of trusted groups */ +extern uid_t *trusted_users; /* List of trusted users */ +extern uschar *timezone_string; /* Required timezone setting */ + +extern uschar *unknown_login; /* To use when login id unknown */ +extern uschar *unknown_username; /* Ditto */ +extern uschar *untrusted_set_sender; /* Let untrusted users set these senders */ +extern uschar *uucp_from_pattern; /* For recognizing "From " lines */ +extern uschar *uucp_from_sender; /* For building the sender */ + +extern uschar *warn_message_file; /* Template for warning messages */ +extern uschar *warnmsg_delay; /* String form of delay time */ +extern uschar *warnmsg_recipients; /* Recipients of warning message */ +extern BOOL write_rejectlog; /* Control of reject logging */ + +extern uschar *version_copyright; /* Copyright notice */ +extern uschar *version_date; /* Date of compilation */ +extern uschar *version_cnumber; /* Compile number */ +extern uschar *version_string; /* Version string */ + +extern int warning_count; /* Delay warnings sent for this msg */ + +/* End of globals.h */ diff --git a/src/src/header.c b/src/src/header.c new file mode 100644 index 000000000..2cac551c6 --- /dev/null +++ b/src/src/header.c @@ -0,0 +1,444 @@ +/* $Cambridge: exim/src/src/header.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + + +#include "exim.h" + + +/************************************************* +* Test a header for matching name * +*************************************************/ + +/* This function tests the name of a header. It is made into a function because +it isn't just a string comparison: spaces and tabs are permitted between the +name and the colon. The h->text field should nowadays never be NULL, but check +it just in case. + +Arguments: + h points to the header + name the name to test + len the length of the name + notdel if TRUE, force FALSE for deleted headers + +Returns: TRUE or FALSE +*/ + +BOOL +header_testname(header_line *h, uschar *name, int len, BOOL notdel) +{ +uschar *tt; +if (h->type == '*' && notdel) return FALSE; +if (h->text == NULL || strncmpic(h->text, name, len) != 0) return FALSE; +tt = h->text + len; +while (*tt == ' ' || *tt == '\t') tt++; +return *tt == ':'; +} + + + +/************************************************* +* Add new header backend function * +*************************************************/ + +/* The header_last variable points to the last header during message reception +and delivery; otherwise it is NULL. We add new headers only when header_last is +not NULL. The function may get called sometimes when it is NULL (e.g. during +address verification where rewriting options exist). When called from a filter, +there may be multiple header lines in a single string. + +This is an internal static function that is the common back end to the external +functions defined below. The general interface allows the header to be inserted +before or after a given occurrence of a given header. + +(a) if "name" is NULL, the header is added at the end of all the existing + headers if "after" is true, or at the start if it is false. The "topnot" + flag is not used. + +(b) If "name" is not NULL, the first existing header with that name is sought. + If "after" is false, the new header is added before it. If "after" is true, + a check is made for adjacent headers with the same name, and the new header + is added after the last of them. If a header of the given name is not + found, the new header is added first if "topnot" is true, and at the bottom + otherwise. + +Arguments: + after TRUE for "after", FALSE for "before" + name name if adding at a specific header, else NULL + topnot TRUE to add at top if no header found + type Exim header type character (htype_something) + format sprintf format + ap va_list value for format arguments + +Returns: nothing +*/ + +static void +header_add_backend(BOOL after, uschar *name, BOOL topnot, int type, + char *format, va_list ap) +{ +header_line *h, *new; +header_line **hptr; + +uschar *p, *q; +uschar buffer[HEADER_ADD_BUFFER_SIZE]; + +if (header_last == NULL) return; + +if (!string_vformat(buffer, sizeof(buffer), format, ap)) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "string too long in header_add: " + "%.100s ...", buffer); + +/* Find where to insert this header */ + +if (name == NULL) + { + if (after) + { + hptr = &(header_last->next); + h = NULL; + } + else + { + hptr = &header_list; + h = header_list; + } + } + +else + { + int len = Ustrlen(name); + + /* Find the first non-deleted header witht the correct name. */ + + for (hptr = &header_list; (h = *hptr) != NULL; hptr = &(h->next)) + { + if (header_testname(h, name, len, TRUE)) break; + } + + /* Handle the case where no header is found. To insert at the bottom, nothing + needs to be done. */ + + if (h == NULL) + { + if (topnot) + { + hptr = &header_list; + h = header_list; + } + } + + /* Handle the case where a header is found. Check for more if "after" is + true. In this case, we want to include deleted headers in the block. */ + + else if (after) + { + for (;;) + { + if (h->next == NULL || !header_testname(h, name, len, FALSE)) break; + hptr = &(h->next); + h = h->next; + } + } + } + +/* Loop for multiple header lines, taking care about continuations. At this +point, we have hptr pointing to the link field that will point to the new +header, and h containing the following header, or NULL. */ + +for (p = q = buffer; *p != 0; ) + { + for (;;) + { + q = Ustrchr(q, '\n'); + if (q == NULL) q = p + Ustrlen(p); + if (*(++q) != ' ' && *q != '\t') break; + } + + new = store_get(sizeof(header_line)); + new->text = string_copyn(p, q - p); + new->slen = q - p; + new->type = type; + new->next = h; + + *hptr = new; + hptr = &(new->next); + + if (h == NULL) header_last = new; + p = q; + } +} + + +/************************************************* +* Add new header anywhere in the chain * +*************************************************/ + +/* This is an external interface to header_add_backend(). + +Arguments: + after TRUE for "after", FALSE for "before" + name name if adding at a specific header, else NULL + topnot TRUE to add at top if no header found + type Exim header type character (htype_something) + format sprintf format + ... format arguments + +Returns: nothing +*/ + +void +header_add_at_position(BOOL after, uschar *name, BOOL topnot, int type, + char *format, ...) +{ +va_list ap; +va_start(ap, format); +header_add_backend(after, name, topnot, type, format, ap); +va_end(ap); +} + + + +/************************************************* +* Add new header on end of chain * +*************************************************/ + +/* This is now a convenience interface to header_add_backend(). + +Arguments: + type Exim header type character + format sprintf format + ... arguments for the format + +Returns: nothing +*/ + +void +header_add(int type, char *format, ...) +{ +va_list ap; +va_start(ap, format); +header_add_backend(TRUE, NULL, FALSE, type, format, ap); +va_end(ap); +} + + + +/************************************************* +* Remove (mark as old) a header * +*************************************************/ + +/* This function is used by the filter code; it is also exported in the +local_scan() API. If no header is found, the function does nothing. + +Arguments: + occ the occurrence number for multiply-defined headers + <= 0 means "all"; deleted headers are not counted + name the header name + +Returns: nothing +*/ + +void +header_remove(int occ, uschar *name) +{ +header_line *h; +int hcount = 0; +int len = Ustrlen(name); +for (h = header_list; h != NULL; h = h->next) + { + if (header_testname(h, name, len, TRUE) && (occ <= 0 || ++hcount == occ)) + { + h->type = htype_old; + if (occ > 0) return; + } + } +} + + + +/************************************************* +* Check the name of a header * +*************************************************/ + +/* This function scans a table of header field names that Exim recognizes, and +returns the identification of a match. If "resent" is true, the header is known +to start with "resent-". In that case, the function matches only those fields +that are allowed to appear with resent- in front of them. + +Arguments: + h points to the header line + is_resent TRUE if the name starts "Resent-" + +Returns: One of the htype_ enum values, identifying the header +*/ + +int +header_checkname(header_line *h, BOOL is_resent) +{ +uschar *text = h->text; +header_name *bot = header_names; +header_name *top = header_names + header_names_size; + +if (is_resent) text += 7; + +while (bot < top) + { + header_name *mid = bot + (top - bot)/2; + int c = strncmpic(text, mid->name, mid->len); + + if (c == 0) + { + uschar *s = text + mid->len; + while (isspace(*s)) s++; + if (*s == ':') + return (!is_resent || mid->allow_resent)? mid->htype : htype_other; + c = 1; + } + + if (c > 0) bot = mid + 1; else top = mid; + } + +return htype_other; +} + + +/************************************************* +* Scan a header for certain strings * +*************************************************/ + +/* This function is used for the "personal" test. It scans a particular header +line for any one of a number of strings, matched caselessly either as plain +strings, or as regular expressions. If the header line contains a list of +addresses, each match is applied only to the operative part of each address in +the header, and non-regular expressions must be exact matches. + +The patterns can be provided either as a chain of string_item structures, or +inline in the argument list, or both. If there is more than one header of the +same name, they are all searched. + +Arguments: + name header name, including the trailing colon + has_addresses TRUE if the header contains a list of addresses + cond value to return if the header contains any of the strings + strings points to a chain of string_item blocks + count number of inline strings + ... the inline strings + +Returns: cond if the header exists and contains one of the strings; + otherwise !cond +*/ + + +/* First we have a local subroutine to handle a single pattern */ + +static BOOL +one_pattern_match(uschar *name, int slen, BOOL has_addresses, uschar *pattern) +{ +BOOL yield = FALSE; +header_line *h; +const pcre *re = NULL; + +/* If the pattern is a regex, compile it. Bomb out if compiling fails; these +patterns are all constructed internally and should be valid. */ + +if (*pattern == '^') re = regex_must_compile(pattern, TRUE, FALSE); + +/* Scan for the required header(s) and scan each one */ + +for (h = header_list; !yield && h != NULL; h = h->next) + { + if (h->type == htype_old || slen > h->slen || + strncmpic(name, h->text, slen) != 0) + continue; + + /* If the header is a list of addresses, extract each one in turn, and scan + it. A non-regex scan must be an exact match for the address. */ + + if (has_addresses) + { + uschar *s = h->text + slen; + + while (!yield && *s != 0) + { + uschar *error, *next; + uschar *e = parse_find_address_end(s, FALSE); + int terminator = *e; + int start, end, domain; + + /* Temporarily terminate the string at the address end while extracting + the operative address within. */ + + *e = 0; + next = parse_extract_address(s, &error, &start, &end, &domain, FALSE); + *e = terminator; + + /* Move on, ready for the next address */ + + s = e; + if (*s == ',') s++; + + /* If there is some kind of syntax error, just give up on this header + line. */ + + if (next == NULL) break; + + /* Otherwise, test for the pattern; a non-regex must be an exact match */ + + yield = (re == NULL)? + (strcmpic(next, pattern) == 0) + : + (pcre_exec(re, NULL, CS next, Ustrlen(next), 0, PCRE_EOPT, NULL, 0) + >= 0); + } + } + + /* For headers that are not lists of addresses, scan the entire header line, + and just require "contains" for non-regex patterns. */ + + else + { + yield = (re == NULL)? + (strstric(h->text, pattern, FALSE) != NULL) + : + (pcre_exec(re, NULL, CS h->text, h->slen, 0, PCRE_EOPT, NULL, 0) >= 0); + } + } + +return yield; +} + + +/* The externally visible interface */ + +BOOL +header_match(uschar *name, BOOL has_addresses, BOOL cond, string_item *strings, + int count, ...) +{ +va_list ap; +string_item *s; +int i; +int slen = Ustrlen(name); + +for (s = strings; s != NULL; s = s->next) + { + if (one_pattern_match(name, slen, has_addresses, s->text)) return cond; + } + +va_start(ap, count); +for (i = 0; i < count; i++) + { + if (one_pattern_match(name, slen, has_addresses, va_arg(ap, uschar *))) + return cond; + } +va_end(ap); + +return !cond; +} + +/* End of header.c */ diff --git a/src/src/host.c b/src/src/host.c new file mode 100644 index 000000000..0acafd8a8 --- /dev/null +++ b/src/src/host.c @@ -0,0 +1,3011 @@ +/* $Cambridge: exim/src/src/host.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Functions for finding hosts, either by gethostbyname(), gethostbyaddr(), or +directly via the DNS. When IPv6 is supported, getipnodebyname() and +getipnodebyaddr() may be used instead of gethostbyname() and gethostbyaddr(), +if the newer functions are available. This module also contains various other +functions concerned with hosts and addresses, and a random number function, +used for randomizing hosts with equal MXs but available for use in other parts +of Exim. */ + + +#include "exim.h" + + +/* Static variable for preserving the list of interface addresses in case it is +used more than once. */ + +static ip_address_item *local_interface_data = NULL; + + +#ifdef USE_INET_NTOA_FIX +/************************************************* +* Replacement for broken inet_ntoa() * +*************************************************/ + +/* On IRIX systems, gcc uses a different structure passing convention to the +native libraries. This causes inet_ntoa() to always yield 0.0.0.0 or +255.255.255.255. To get round this, we provide a private version of the +function here. It is used only if USE_INET_NTOA_FIX is set, which should happen +only when gcc is in use on an IRIX system. Code send to me by J.T. Breitner, +with these comments: + + code by Stuart Levy + as seen in comp.sys.sgi.admin + +Arguments: sa an in_addr structure +Returns: pointer to static text string +*/ + +char * +inet_ntoa(struct in_addr sa) +{ +static uschar addr[20]; +sprintf(addr, "%d.%d.%d.%d", + (US &sa.s_addr)[0], + (US &sa.s_addr)[1], + (US &sa.s_addr)[2], + (US &sa.s_addr)[3]); + return addr; +} +#endif + + + +/************************************************* +* Random number generator * +*************************************************/ + +/* This is a simple pseudo-random number generator. It does not have to be +very good for the uses to which it is put. When running the regression tests, +start with a fixed seed. + +Arguments: + limit: one more than the largest number required + +Returns: a pseudo-random number in the range 0 to limit-1 +*/ + +int +random_number(int limit) +{ +if (random_seed == 0) + { + if (running_in_test_harness) random_seed = 42; else + { + int p = (int)getpid(); + random_seed = (int)time(NULL) ^ ((p << 16) | p); + } + } +random_seed = 1103515245 * random_seed + 12345; +return (unsigned int)(random_seed >> 16) % limit; +} + + + +/************************************************* +* Build chain of host items from list * +*************************************************/ + +/* This function builds a chain of host items from a textual list of host +names. It does not do any lookups. If randomize is true, the chain is build in +a randomized order. There may be multiple groups of independently randomized +hosts; they are delimited by a host name consisting of just "+". + +Arguments: + anchor anchor for the chain + list text list + randomize TRUE for randomizing + +Returns: nothing +*/ + +void +host_build_hostlist(host_item **anchor, uschar *list, BOOL randomize) +{ +int sep = 0; +int fake_mx = MX_NONE; /* This value is actually -1 */ +uschar *name; +uschar buffer[1024]; + +if (list == NULL) return; +if (randomize) fake_mx--; /* Start at -2 for randomizing */ + +*anchor = NULL; + +while ((name = string_nextinlist(&list, &sep, buffer, sizeof(buffer))) != NULL) + { + host_item *h; + + if (name[0] == '+' && name[1] == 0) /* "+" delimits a randomized group */ + { /* ignore if not randomizing */ + if (randomize) fake_mx--; + continue; + } + + h = store_get(sizeof(host_item)); + h->name = string_copy(name); + h->address = NULL; + h->port = PORT_NONE; + h->mx = fake_mx; + h->sort_key = randomize? (-fake_mx)*1000 + random_number(1000) : 0; + h->status = hstatus_unknown; + h->why = hwhy_unknown; + h->last_try = 0; + + if (*anchor == NULL) + { + h->next = NULL; + *anchor = h; + } + else + { + host_item *hh = *anchor; + if (h->sort_key < hh->sort_key) + { + h->next = hh; + *anchor = h; + } + else + { + while (hh->next != NULL && h->sort_key >= (hh->next)->sort_key) + hh = hh->next; + h->next = hh->next; + hh->next = h; + } + } + } +} + + + + + +/************************************************* +* Extract port from address string * +*************************************************/ + +/* In the spool file, and in the -oMa and -oMi options, a host plus port is +given as an IP address followed by a dot and a port number. This function +decodes this. + +An alternative format for the -oMa and -oMi options is [ip address]:port which +is what Exim 4 uses for output, because it seems to becoming commonly used, +whereas the dot form confuses some programs/people. So we recognize that form +too. + +Argument: + address points to the string; if there is a port, the '.' in the string + is overwritten with zero to terminate the address; if the string + is in the [xxx]:ppp format, the address is shifted left and the + brackets are removed + +Returns: 0 if there is no port, else the port number. If there's a syntax + error, leave the incoming address alone, and return 0. +*/ + +int +host_extract_port(uschar *address) +{ +int port = 0; +uschar *endptr; + +/* Handle the "bracketed with colon on the end" format */ + +if (*address == '[') + { + uschar *rb = address + 1; + while (*rb != 0 && *rb != ']') rb++; + if (*rb++ == 0) return 0; /* Missing ]; leave invalid address */ + if (*rb == ':') + { + port = Ustrtol(rb + 1, &endptr, 10); + if (*endptr != 0) return 0; /* Invalid port; leave invalid address */ + } + else if (*rb != 0) return 0; /* Bad syntax; leave invalid address */ + memmove(address, address + 1, rb - address - 2); + rb[-2] = 0; + } + +/* Handle the "dot on the end" format */ + +else + { + int skip = -3; /* Skip 3 dots in IPv4 addresses */ + address--; + while (*(++address) != 0) + { + int ch = *address; + if (ch == ':') skip = 0; /* Skip 0 dots in IPv6 addresses */ + else if (ch == '.' && skip++ >= 0) break; + } + if (*address == 0) return 0; + port = Ustrtol(address + 1, &endptr, 10); + if (*endptr != 0) return 0; /* Invalid port; leave invalid address */ + *address = 0; + } + +return port; +} + + + +#ifndef STAND_ALONE /* Omit when standalone testing */ + +/************************************************* +* Build sender_fullhost and sender_rcvhost * +*************************************************/ + +/* This function is called when sender_host_name and/or sender_helo_name +have been set. Or might have been set - for a local message read off the spool +they won't be. In that case, do nothing. Otherwise, set up the fullhost string +as follows: + +(a) No sender_host_name or sender_helo_name: "[ip address]" +(b) Just sender_host_name: "host_name [ip address]" +(c) Just sender_helo_name: "(helo_name) [ip address]" +(d) The two are identical: "host_name [ip address]" +(e) The two are different: "host_name (helo_name) [ip address]" + +If log_incoming_port is set, the sending host's port number is added to the IP +address. + +This function also builds sender_rcvhost for use in Received: lines, whose +syntax is a bit different. This value also includes the RFC 1413 identity. +There wouldn't be two different variables if I had got all this right in the +first place. + +Because this data may survive over more than one incoming SMTP message, it has +to be in permanent store. + +Arguments: none +Returns: nothing +*/ + +void +host_build_sender_fullhost(void) +{ +uschar *address; +int old_pool = store_pool; + +if (sender_host_address == NULL) return; + +store_pool = POOL_PERM; + +/* Set up address, with or without the port. After discussion, it seems that +the only format that doesn't cause trouble is [aaaa]:pppp. However, we can't +use this directly as the first item for Received: because it ain't an RFC 2822 +domain. Sigh. */ + +address = string_sprintf("[%s]:%d", sender_host_address, sender_host_port); +if ((log_extra_selector & LX_incoming_port) == 0 || sender_host_port <= 0) + *(Ustrrchr(address, ':')) = 0; + +/* Host name is not verified */ + +if (sender_host_name == NULL) + { + uschar *portptr = Ustrstr(address, "]:"); + int size = 0; + int ptr = 0; + int adlen; /* Sun compiler doesn't like ++ in initializers */ + + adlen = (portptr == NULL)? Ustrlen(address) : (++portptr - address); + sender_fullhost = (sender_helo_name == NULL)? address : + string_sprintf("(%s) %s", sender_helo_name, address); + + sender_rcvhost = string_cat(NULL, &size, &ptr, address, adlen); + + if (sender_ident != NULL || sender_helo_name != NULL || portptr != NULL) + { + int firstptr; + sender_rcvhost = string_cat(sender_rcvhost, &size, &ptr, US" (", 2); + firstptr = ptr; + + if (portptr != NULL) + sender_rcvhost = string_append(sender_rcvhost, &size, &ptr, 2, US"port=", + portptr + 1); + + if (sender_helo_name != NULL) + sender_rcvhost = string_append(sender_rcvhost, &size, &ptr, 2, + (firstptr == ptr)? US"helo=" : US" helo=", sender_helo_name); + + if (sender_ident != NULL) + sender_rcvhost = string_append(sender_rcvhost, &size, &ptr, 2, + (firstptr == ptr)? US"ident=" : US" ident=", sender_ident); + + sender_rcvhost = string_cat(sender_rcvhost, &size, &ptr, US")", 1); + } + + sender_rcvhost[ptr] = 0; /* string_cat() always leaves room */ + + /* Release store, because string_cat allocated a minimum of 100 bytes that + are rarely completely used. */ + + store_reset(sender_rcvhost + ptr + 1); + } + +/* Host name is known and verified. */ + +else + { + int len; + if (sender_helo_name == NULL || + strcmpic(sender_host_name, sender_helo_name) == 0 || + (sender_helo_name[0] == '[' && + sender_helo_name[(len=Ustrlen(sender_helo_name))-1] == ']' && + strncmpic(sender_helo_name+1, sender_host_address, len - 2) == 0)) + { + sender_fullhost = string_sprintf("%s %s", sender_host_name, address); + sender_rcvhost = (sender_ident == NULL)? + string_sprintf("%s (%s)", sender_host_name, address) : + string_sprintf("%s (%s ident=%s)", sender_host_name, address, + sender_ident); + } + else + { + sender_fullhost = string_sprintf("%s (%s) %s", sender_host_name, + sender_helo_name, address); + sender_rcvhost = (sender_ident == NULL)? + string_sprintf("%s (%s helo=%s)", sender_host_name, + address, sender_helo_name) : + string_sprintf("%s\n\t(%s helo=%s ident=%s)", sender_host_name, + address, sender_helo_name, sender_ident); + } + } + +store_pool = old_pool; + +DEBUG(D_host_lookup) debug_printf("sender_fullhost = %s\n", sender_fullhost); +DEBUG(D_host_lookup) debug_printf("sender_rcvhost = %s\n", sender_rcvhost); +} + + + +/************************************************* +* Build host+ident message * +*************************************************/ + +/* Used when logging rejections and various ACL and SMTP incidents. The text +return depends on whether sender_fullhost and sender_ident are set or not: + + no ident, no host => U=unknown + no ident, host set => H=sender_fullhost + ident set, no host => U=ident + ident set, host set => H=sender_fullhost U=ident + +Arguments: + useflag TRUE if first item to be flagged (H= or U=); if there are two + items, the second is always flagged + +Returns: pointer to a string in big_buffer +*/ + +uschar * +host_and_ident(BOOL useflag) +{ +if (sender_fullhost == NULL) + { + (void)string_format(big_buffer, big_buffer_size, "%s%s", useflag? "U=" : "", + (sender_ident == NULL)? US"unknown" : sender_ident); + } +else + { + uschar *flag = useflag? US"H=" : US""; + uschar *iface = US""; + if ((log_extra_selector & LX_incoming_interface) != 0 && + interface_address != NULL) + iface = string_sprintf(" I=[%s]:%d", interface_address, interface_port); + if (sender_ident == NULL) + (void)string_format(big_buffer, big_buffer_size, "%s%s%s", + flag, sender_fullhost, iface); + else + (void)string_format(big_buffer, big_buffer_size, "%s%s%s U=%s", + flag, sender_fullhost, iface, sender_ident); + } +return big_buffer; +} + +#endif /* STAND_ALONE */ + + + + +/************************************************* +* Build list of local interfaces * +*************************************************/ + +/* This function interprets the contents of the local_interfaces or +extra_local_interfaces options, and creates an ip_address_item block for each +item on the list. There is no special interpretation of any IP addresses; in +particular, 0.0.0.0 and ::0 are returned without modification. If any address +includes a port, it is set in the block. Otherwise the port value is set to +zero. + +Arguments: + list the list + name the name of the option being expanded + +Returns: a chain of ip_address_items, each containing to a textual + version of an IP address, and a port number (host order) or + zero if no port was given with the address +*/ + +ip_address_item * +host_build_ifacelist(uschar *list, uschar *name) +{ +int sep = 0; +uschar *s; +uschar buffer[64]; +ip_address_item *yield = NULL; +ip_address_item *last = NULL; +ip_address_item *next; + +while ((s = string_nextinlist(&list, &sep, buffer, sizeof(buffer))) != NULL) + { + int port = host_extract_port(s); /* Leaves just the IP address */ + if (!string_is_ip_address(s, NULL)) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Malformed IP address \"%s\" in %s", + s, name); + + /* This use of strcpy() is OK because we have checked that s is a valid IP + address above. The field in the ip_address_item is large enough to hold an + IPv6 address. */ + + next = store_get(sizeof(ip_address_item)); + next->next = NULL; + Ustrcpy(next->address, s); + next->port = port; + next->v6_include_v4 = FALSE; + + if (yield == NULL) yield = last = next; else + { + last->next = next; + last = next; + } + } + +return yield; +} + + + + + +/************************************************* +* Find addresses on local interfaces * +*************************************************/ + +/* This function finds the addresses of local IP interfaces. These are used +when testing for routing to the local host. As the function may be called more +than once, the list is preserved in permanent store, pointed to by a static +variable, to save doing the work more than once per process. + +The generic list of interfaces is obtained by calling host_build_ifacelist() +for local_interfaces and extra_local_interfaces. This list scanned to remove +duplicates (which may exist with different ports - not relevant here). If +either of the wildcard IP addresses (0.0.0.0 and ::0) are encountered, they are +replaced by the appropriate (IPv4 or IPv6) list of actual local interfaces, +obtained from os_find_running_interfaces(). + +Arguments: none +Returns: a chain of ip_address_items, each containing to a textual + version of an IP address; the port numbers are not relevant +*/ + + +/* First, a local subfunction to add an interface to a list in permanent store, +but only if there isn't a previous copy of that address on the list. */ + +static ip_address_item * +add_unique_interface(ip_address_item *list, ip_address_item *ipa) +{ +ip_address_item *ipa2; +for (ipa2 = list; ipa2 != NULL; ipa2 = ipa2->next) + if (Ustrcmp(ipa2->address, ipa->address) == 0) return list; +ipa2 = store_get_perm(sizeof(ip_address_item)); +*ipa2 = *ipa; +ipa2->next = list; +return ipa2; +} + + +/* This is the globally visible function */ + +ip_address_item * +host_find_interfaces(void) +{ +ip_address_item *running_interfaces = NULL; + +if (local_interface_data == NULL) + { + void *reset_item = store_get(0); + ip_address_item *dlist = host_build_ifacelist(local_interfaces, + US"local_interfaces"); + ip_address_item *xlist = host_build_ifacelist(extra_local_interfaces, + US"extra_local_interfaces"); + ip_address_item *ipa; + + if (dlist == NULL) dlist = xlist; else + { + for (ipa = dlist; ipa->next != NULL; ipa = ipa->next); + ipa->next = xlist; + } + + for (ipa = dlist; ipa != NULL; ipa = ipa->next) + { + if (Ustrcmp(ipa->address, "0.0.0.0") == 0 || + Ustrcmp(ipa->address, "::0") == 0) + { + ip_address_item *ipa2; + BOOL ipv6 = ipa->address[0] == ':'; + if (running_interfaces == NULL) + running_interfaces = os_find_running_interfaces(); + for (ipa2 = running_interfaces; ipa2 != NULL; ipa2 = ipa2->next) + { + if ((Ustrchr(ipa2->address, ':') != NULL) == ipv6) + local_interface_data = add_unique_interface(local_interface_data, + ipa2); + } + } + else + { + local_interface_data = add_unique_interface(local_interface_data, ipa); + DEBUG(D_interface) + { + debug_printf("Configured local interface: address=%s", ipa->address); + if (ipa->port != 0) debug_printf(" port=%d", ipa->port); + debug_printf("\n"); + } + } + } + store_reset(reset_item); + } + +return local_interface_data; +} + + + + + +/************************************************* +* Convert network IP address to text * +*************************************************/ + +/* Given an IPv4 or IPv6 address in binary, convert it to a text +string and return the result in a piece of new store. The address can +either be given directly, or passed over in a sockaddr structure. Note +that this isn't the converse of host_aton() because of byte ordering +differences. See host_nmtoa() below. + +Arguments: + type if < 0 then arg points to a sockaddr, else + either AF_INET or AF_INET6 + arg points to a sockaddr if type is < 0, or + points to an IPv4 address (32 bits), or + points to an IPv6 address (128 bits), + in both cases, in network byte order + buffer if NULL, the result is returned in gotten store; + else points to a buffer to hold the answer + portptr points to where to put the port number, if non NULL; only + used when type < 0 + +Returns: pointer to character string +*/ + +uschar * +host_ntoa(int type, const void *arg, uschar *buffer, int *portptr) +{ +uschar *yield; + +/* The new world. It is annoying that we have to fish out the address from +different places in the block, depending on what kind of address it is. It +is also a pain that inet_ntop() returns a const uschar *, whereas the IPv4 +function inet_ntoa() returns just uschar *, and some picky compilers insist +on warning if one assigns a const uschar * to a uschar *. Hence the casts. */ + +#if HAVE_IPV6 +uschar addr_buffer[46]; +if (type < 0) + { + int family = ((struct sockaddr *)arg)->sa_family; + if (family == AF_INET6) + { + struct sockaddr_in6 *sk = (struct sockaddr_in6 *)arg; + yield = (uschar *)inet_ntop(family, &(sk->sin6_addr), CS addr_buffer, + sizeof(addr_buffer)); + if (portptr != NULL) *portptr = ntohs(sk->sin6_port); + } + else + { + struct sockaddr_in *sk = (struct sockaddr_in *)arg; + yield = (uschar *)inet_ntop(family, &(sk->sin_addr), CS addr_buffer, + sizeof(addr_buffer)); + if (portptr != NULL) *portptr = ntohs(sk->sin_port); + } + } +else + { + yield = (uschar *)inet_ntop(type, arg, CS addr_buffer, sizeof(addr_buffer)); + } + +/* If the result is a mapped IPv4 address, show it in V4 format. */ + +if (Ustrncmp(yield, "::ffff:", 7) == 0) yield += 7; + +#else /* HAVE_IPV6 */ + +/* The old world */ + +if (type < 0) + { + yield = US inet_ntoa(((struct sockaddr_in *)arg)->sin_addr); + if (portptr != NULL) *portptr = ntohs(((struct sockaddr_in *)arg)->sin_port); + } +else + yield = US inet_ntoa(*((struct in_addr *)arg)); +#endif + +/* If there is no buffer, put the string into some new store. */ + +if (buffer == NULL) return string_copy(yield); + +/* Callers of this function with a non-NULL buffer must ensure that it is +large enough to hold an IPv6 address, namely, at least 46 bytes. That's what +makes this use of strcpy() OK. */ + +Ustrcpy(buffer, yield); +return buffer; +} + + + + +/************************************************* +* Convert address text to binary * +*************************************************/ + +/* Given the textual form of an IP address, convert it to binary in an +array of ints. IPv4 addresses occupy one int; IPv6 addresses occupy 4 ints. +The result has the first byte in the most significant byte of the first int. In +other words, the result is not in network byte order, but in host byte order. +As a result, this is not the converse of host_ntoa(), which expects network +byte order. See host_nmtoa() below. + +Arguments: + address points to the textual address, checked for syntax + bin points to an array of 4 ints + +Returns: the number of ints used +*/ + +int +host_aton(uschar *address, int *bin) +{ +int x[4]; +int v4offset = 0; + +/* Handle IPv6 address, which may end with an IPv4 address. This code is NOT +enclosed in #if HAVE_IPV6 in order that IPv6 addresses are recognized even if +IPv6 is not supported. */ + +if (Ustrchr(address, ':') != NULL) + { + uschar *p = address; + uschar *component[8]; + BOOL ipv4_ends = FALSE; + int ci = 0; + int nulloffset = 0; + int v6count = 8; + int i; + + /* If the address starts with a colon, it will start with two colons. + Just lose the first one, which will leave a null first component. */ + + if (*p == ':') p++; + + /* Split the address into components separated by colons. */ + + while (*p != 0) + { + int len = Ustrcspn(p, ":"); + if (len == 0) nulloffset = ci; + component[ci++] = p; + p += len; + if (*p == ':') p++; + } + + /* If the final component contains a dot, it is a trailing v4 address. + As the syntax is known to be checked, just set up for a trailing + v4 address and restrict the v6 part to 6 components. */ + + if (Ustrchr(component[ci-1], '.') != NULL) + { + address = component[--ci]; + ipv4_ends = TRUE; + v4offset = 3; + v6count = 6; + } + + /* If there are fewer than 6 or 8 components, we have to insert some + more empty ones in the middle. */ + + if (ci < v6count) + { + int insert_count = v6count - ci; + for (i = v6count-1; i > nulloffset + insert_count; i--) + component[i] = component[i - insert_count]; + while (i > nulloffset) component[i--] = US""; + } + + /* Now turn the components into binary in pairs and bung them + into the vector of ints. */ + + for (i = 0; i < v6count; i += 2) + bin[i/2] = (Ustrtol(component[i], NULL, 16) << 16) + + Ustrtol(component[i+1], NULL, 16); + + /* If there was no terminating v4 component, we are done. */ + + if (!ipv4_ends) return 4; + } + +/* Handle IPv4 address */ + +sscanf(CS address, "%d.%d.%d.%d", x, x+1, x+2, x+3); +bin[v4offset] = (x[0] << 24) + (x[1] << 16) + (x[2] << 8) + x[3]; +return v4offset+1; +} + + +/************************************************* +* Apply mask to an IP address * +*************************************************/ + +/* Mask an address held in 1 or 4 ints, with the ms bit in the ms bit of the +first int, etc. + +Arguments: + count the number of ints + binary points to the ints to be masked + mask the count of ms bits to leave, or -1 if no masking + +Returns: nothing +*/ + +void +host_mask(int count, int *binary, int mask) +{ +int i; +if (mask < 0) mask = 99999; +for (i = 0; i < count; i++) + { + int wordmask; + if (mask == 0) wordmask = 0; + else if (mask < 32) + { + wordmask = (-1) << (32 - mask); + mask = 0; + } + else + { + wordmask = -1; + mask -= 32; + } + binary[i] &= wordmask; + } +} + + + + +/************************************************* +* Convert masked IP address in ints to text * +*************************************************/ + +/* We can't use host_ntoa() because it assumes the binary values are in network +byte order, and these are the result of host_aton(), which puts them in ints in +host byte order. Also, we really want IPv6 addresses to be in a canonical +format, so we output them with no abbreviation. However, we can't use the +normal colon separator in them because it terminates keys in lsearch files, so +use dot instead. + +Arguments: + count 1 or 4 (number of ints) + binary points to the ints + mask mask value; if < 0 don't add to result + buffer big enough to hold the result + +Returns: the number of characters placed in buffer, not counting + the final nul. +*/ + +int +host_nmtoa(int count, int *binary, int mask, uschar *buffer) +{ +int i, j; +uschar *tt = buffer; + +if (count == 1) + { + j = binary[0]; + for (i = 24; i >= 0; i -= 8) + { + sprintf(CS tt, "%d.", (j >> i) & 255); + while (*tt) tt++; + } + } +else + { + for (i = 0; i < 4; i++) + { + j = binary[i]; + sprintf(CS tt, "%04x.%04x.", (j >> 16) & 0xffff, j & 0xffff); + while (*tt) tt++; + } + } + +tt--; /* lose final . */ + +if (mask < 0) + *tt = 0; +else + { + sprintf(CS tt, "/%d", mask); + while (*tt) tt++; + } + +return tt - buffer; +} + + + +/************************************************* +* Check port for tls_on_connect * +*************************************************/ + +/* This function checks whether a given incoming port is configured for tls- +on-connect. It is called from the daemon and from inetd handling. If the global +option tls_on_connect is already set, all ports operate this way. Otherwise, we +check the tls_on_connect_ports option for a list of ports. + +Argument: a port number +Returns: TRUE or FALSE +*/ + +BOOL +host_is_tls_on_connect_port(int port) +{ +int sep = 0; +uschar buffer[32]; +uschar *list = tls_on_connect_ports; +uschar *s; + +if (tls_on_connect) return TRUE; + +while ((s = string_nextinlist(&list, &sep, buffer, sizeof(buffer))) != NULL) + { + uschar *end; + int lport = Ustrtol(s, &end, 10); + if (*end != 0) log_write(0, LOG_MAIN|LOG_PANIC_DIE, "tls_on_connect_ports " + "contains \"%s\", which is not a port number: exim abandoned", s); + if (lport == port) return TRUE; + } + +return FALSE; +} + + + +/************************************************* +* Check whether host is in a network * +*************************************************/ + +/* This function checks whether a given IP address matches a pattern that +represents either a single host, or a network (using CIDR notation). The caller +of this function must check the syntax of the arguments before calling it. + +Arguments: + host string representation of the ip-address to check + net string representation of the network, with optional CIDR mask + maskoffset offset to the / that introduces the mask in the key + zero if there is no mask + +Returns: + TRUE the host is inside the network + FALSE the host is NOT inside the network +*/ + +BOOL +host_is_in_net(uschar *host, uschar *net, int maskoffset) +{ +int i; +int address[4]; +int incoming[4]; +int mlen; +int size = host_aton(net, address); +int insize; + +/* No mask => all bits to be checked */ + +if (maskoffset == 0) mlen = 99999; /* Big number */ + else mlen = Uatoi(net + maskoffset + 1); + +/* Convert the incoming address to binary. */ + +insize = host_aton(host, incoming); + +/* Convert IPv4 addresses given in IPv6 compatible mode, which represent + connections from IPv4 hosts to IPv6 hosts, that is, addresses of the form + ::ffff:, to IPv4 format. */ + +if (insize == 4 && incoming[0] == 0 && incoming[1] == 0 && + incoming[2] == 0xffff) + { + insize = 1; + incoming[0] = incoming[3]; + } + +/* No match if the sizes don't agree. */ + +if (insize != size) return FALSE; + +/* Else do the masked comparison. */ + +for (i = 0; i < size; i++) + { + int mask; + if (mlen == 0) mask = 0; + else if (mlen < 32) + { + mask = (-1) << (32 - mlen); + mlen = 0; + } + else + { + mask = -1; + mlen -= 32; + } + if ((incoming[i] & mask) != (address[i] & mask)) return FALSE; + } + +return TRUE; +} + + + +/************************************************* +* Scan host list for local hosts * +*************************************************/ + +/* Scan through a chain of addresses and check whether any of them is the +address of an interface on the local machine. If so, remove that address and +any previous ones with the same MX value, and all subsequent ones (which will +have greater or equal MX values) from the chain. Note: marking them as unusable +is NOT the right thing to do because it causes the hosts not to be used for +other domains, for which they may well be correct. + +The hosts may be part of a longer chain; we only process those between the +initial pointer and the "last" pointer. + +There is also a list of "pseudo-local" host names which are checked against the +host names. Any match causes that host item to be treated the same as one which +matches a local IP address. + +If the very first host is a local host, then all MX records had a precedence +greater than or equal to that of the local host. Either there's a problem in +the DNS, or an apparently remote name turned out to be an abbreviation for the +local host. Give a specific return code, and let the caller decide what to do. +Otherwise, give a success code if at least one host address has been found. + +Arguments: + host pointer to the first host in the chain + lastptr pointer to pointer to the last host in the chain (may be updated) + removed if not NULL, set TRUE if some local addresses were removed + from the list + +Returns: + HOST_FOUND if there is at least one host with an IP address on the chain + and an MX value less than any MX value associated with the + local host + HOST_FOUND_LOCAL if a local host is among the lowest-numbered MX hosts; when + the host addresses were obtained from A records or + gethostbyname(), the MX values are set to -1. + HOST_FIND_FAILED if no valid hosts with set IP addresses were found +*/ + +int +host_scan_for_local_hosts(host_item *host, host_item **lastptr, BOOL *removed) +{ +int yield = HOST_FIND_FAILED; +host_item *last = *lastptr; +host_item *prev = NULL; +host_item *h; + +if (removed != NULL) *removed = FALSE; + +if (local_interface_data == NULL) local_interface_data = host_find_interfaces(); + +for (h = host; h != last->next; h = h->next) + { + #ifndef STAND_ALONE + if (hosts_treat_as_local != NULL) + { + int rc; + uschar *save = deliver_domain; + deliver_domain = h->name; /* set $domain */ + rc = match_isinlist(string_copylc(h->name), &hosts_treat_as_local, 0, + &domainlist_anchor, NULL, MCL_DOMAIN, TRUE, NULL); + deliver_domain = save; + if (rc == OK) goto FOUND_LOCAL; + } + #endif + + /* It seems that on many operating systems, 0.0.0.0 is treated as a synonym + for 127.0.0.1 and refers to the local host. We therefore force it always to + be treated as local. */ + + if (h->address != NULL) + { + ip_address_item *ip; + if (Ustrcmp(h->address, "0.0.0.0") == 0) goto FOUND_LOCAL; + for (ip = local_interface_data; ip != NULL; ip = ip->next) + if (Ustrcmp(h->address, ip->address) == 0) goto FOUND_LOCAL; + yield = HOST_FOUND; /* At least one remote address has been found */ + } + + /* Update prev to point to the last host item before any that have + the same MX value as the one we have just considered. */ + + if (h->next == NULL || h->next->mx != h->mx) prev = h; + } + +return yield; /* No local hosts found: return HOST_FOUND or HOST_FIND_FAILED */ + +/* A host whose IP address matches a local IP address, or whose name matches +something in hosts_treat_as_local has been found. */ + +FOUND_LOCAL: + +if (prev == NULL) + { + HDEBUG(D_host_lookup) debug_printf((h->mx >= 0)? + "local host has lowest MX\n" : + "local host found for non-MX address\n"); + return HOST_FOUND_LOCAL; + } + +HDEBUG(D_host_lookup) + { + debug_printf("local host in host list - removed hosts:\n"); + for (h = prev->next; h != last->next; h = h->next) + debug_printf(" %s %s %d\n", h->name, h->address, h->mx); + } + +if (removed != NULL) *removed = TRUE; +prev->next = last->next; +*lastptr = prev; +return yield; +} + + + + +/************************************************* +* Remove duplicate IPs in host list * +*************************************************/ + +/* You would think that administrators could set up their DNS records so that +one ended up with a list of unique IP addresses after looking up A or MX +records, but apparently duplication is common. So we scan such lists and +remove the later duplicates. Note that we may get lists in which some host +addresses are not set. + +Arguments: + host pointer to the first host in the chain + lastptr pointer to pointer to the last host in the chain (may be updated) + +Returns: nothing +*/ + +static void +host_remove_duplicates(host_item *host, host_item **lastptr) +{ +while (host != *lastptr) + { + if (host->address != NULL) + { + host_item *h = host; + while (h != *lastptr) + { + if (h->next->address != NULL && + Ustrcmp(h->next->address, host->address) == 0) + { + DEBUG(D_host_lookup) debug_printf("duplicate IP address %s (MX=%d) " + "removed\n", host->address, h->next->mx); + if (h->next == *lastptr) *lastptr = h; + h->next = h->next->next; + } + else h = h->next; + } + } + /* If the last item was removed, host may have become == *lastptr */ + if (host != *lastptr) host = host->next; + } +} + + + + +/************************************************* +* Find sender host name by gethostbyaddr() * +*************************************************/ + +/* This used to be the only way it was done, but it turns out that not all +systems give aliases for calls to gethostbyaddr() - or one of the modern +equivalents like getipnodebyaddr(). Fortunately, multiple PTR records are rare, +but they can still exist. This function is now used only when a DNS lookup of +the IP address fails, in order to give access to /etc/hosts. + +Arguments: none +Returns: OK, DEFER, FAIL +*/ + +static int +host_name_lookup_byaddr(void) +{ +int len; +uschar *s, *t; +struct hostent *hosts; +struct in_addr addr; + +/* Lookup on IPv6 system */ + +#if HAVE_IPV6 +if (Ustrchr(sender_host_address, ':') != NULL) + { + struct in6_addr addr6; + if (inet_pton(AF_INET6, CS sender_host_address, &addr6) != 1) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "unable to parse \"%s\" as an " + "IPv6 address", sender_host_address); + #if HAVE_GETIPNODEBYADDR + hosts = getipnodebyaddr(CS &addr6, sizeof(addr6), AF_INET6, &h_errno); + #else + hosts = gethostbyaddr(CS &addr6, sizeof(addr6), AF_INET6); + #endif + } +else + { + if (inet_pton(AF_INET, CS sender_host_address, &addr) != 1) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "unable to parse \"%s\" as an " + "IPv4 address", sender_host_address); + #if HAVE_GETIPNODEBYADDR + hosts = getipnodebyaddr(CS &addr, sizeof(addr), AF_INET, &h_errno); + #else + hosts = gethostbyaddr(CS &addr, sizeof(addr), AF_INET); + #endif + } + +/* Do lookup on IPv4 system */ + +#else +addr.s_addr = (S_ADDR_TYPE)inet_addr(CS sender_host_address); +hosts = gethostbyaddr(CS(&addr), sizeof(addr), AF_INET); +#endif + +/* Failed to look up the host. */ + +if (hosts == NULL) + { + HDEBUG(D_host_lookup) debug_printf("IP address lookup failed: h_errno=%d\n", + h_errno); + return (h_errno == TRY_AGAIN || h_errno == NO_RECOVERY) ? DEFER : FAIL; + } + +/* It seems there are some records in the DNS that yield an empty name. We +treat this as non-existent. In some operating systems, this is returned as an +empty string; in others as a single dot. */ + +if (hosts->h_name[0] == 0 || hosts->h_name[0] == '.') + { + HDEBUG(D_host_lookup) debug_printf("IP address lookup yielded an empty name: " + "treated as non-existent host name\n"); + return FAIL; + } + +/* Copy and lowercase the name, which is in static storage in many systems. +Put it in permanent memory. */ + +s = (uschar *)hosts->h_name; +len = Ustrlen(s) + 1; +t = sender_host_name = store_get_perm(len); +while (*s != 0) *t++ = tolower(*s++); +*t = 0; + +/* If the host has aliases, build a copy of the alias list */ + +if (hosts->h_aliases != NULL) + { + int count = 1; + uschar **aliases, **ptr; + for (aliases = USS hosts->h_aliases; *aliases != NULL; aliases++) count++; + ptr = sender_host_aliases = store_get_perm(count * sizeof(uschar *)); + for (aliases = USS hosts->h_aliases; *aliases != NULL; aliases++) + { + uschar *s = *aliases; + int len = Ustrlen(s) + 1; + uschar *t = *ptr++ = store_get_perm(len); + while (*s != 0) *t++ = tolower(*s++); + *t = 0; + } + *ptr = NULL; + } + +return OK; +} + + + +/************************************************* +* Find host name for incoming call * +*************************************************/ + +/* Put the name in permanent store, pointed to by sender_host_name. We also set +up a list of alias names, pointed to by sender_host_alias. The list is +NULL-terminated. The incoming address is in sender_host_address, either in +dotted-quad form for IPv4 or in colon-separated form for IPv6. + +This function does a thorough check that the names it finds point back to the +incoming IP address. Any that do not are discarded. Note that this is relied on +by the ACL reverse_host_lookup check. + +On some systems, get{host,ipnode}byaddr() appears to do this internally, but +this it not universally true. Also, for release 4.30, this function was changed +to do a direct DNS lookup first, by default[1], because it turns out that that +is the only guaranteed way to find all the aliases on some systems. My +experiments indicate that Solaris gethostbyaddr() gives the aliases for but +Linux does not. + +[1] The actual order is controlled by the host_lookup_order option. + +Arguments: none +Returns: OK on success, the answer being placed in the global variable + sender_host_name, with any aliases in a list hung off + sender_host_aliases + FAIL if no host name can be found + DEFER if a temporary error was encountered + +The variable host_lookup_msg is set to an empty string on sucess, or to a +reason for the failure otherwise, in a form suitable for tagging onto an error +message, and also host_lookup_failed is set TRUE if the lookup failed. Any +dynamically constructed string for host_lookup_msg must be in permanent store, +because it might be used for several incoming messages on the same SMTP +connection. */ + +int +host_name_lookup(void) +{ +int old_pool, rc; +int sep = 0; +uschar *hname, *save_hostname; +uschar **aliases; +uschar buffer[256]; +uschar *ordername; +uschar *list = host_lookup_order; +dns_record *rr; +dns_answer dnsa; +dns_scan dnss; + +HDEBUG(D_host_lookup) + debug_printf("looking up host name for %s\n", sender_host_address); + +/* For testing the case when a lookup does not complete, we have a special +reserved IP address. */ + +if (running_in_test_harness && + Ustrcmp(sender_host_address, "99.99.99.99") == 0) + { + HDEBUG(D_host_lookup) + debug_printf("Test harness: host name lookup returns DEFER\n"); + return DEFER; + } + +/* Do lookups directly in the DNS or via gethostbyaddr() (or equivalent), in +the order specified by the host_lookup_order option. */ + +while ((ordername = string_nextinlist(&list, &sep, buffer, sizeof(buffer))) + != NULL) + { + if (strcmpic(ordername, US"bydns") == 0) + { + dns_init(FALSE, FALSE); + dns_build_reverse(sender_host_address, buffer); + rc = dns_lookup(&dnsa, buffer, T_PTR, NULL); + + /* The first record we come across is used for the name; others are + considered to be aliases. We have to scan twice, in order to find out the + number of aliases. However, if all the names are empty, we will behave as + if failure. (PTR records that yield empty names have been encountered in + the DNS.) */ + + if (rc == DNS_SUCCEED) + { + uschar **aptr = NULL; + int ssize = 264; + int count = 0; + int old_pool = store_pool; + + store_pool = POOL_PERM; /* Save names in permanent storage */ + + for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS); + rr != NULL; + rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT)) + { + if (rr->type == T_PTR) count++; + } + + /* Get store for the list of aliases. For compatibility with + gethostbyaddr, we make an empty list if there are none. */ + + aptr = sender_host_aliases = store_get(count * sizeof(uschar *)); + + /* Re-scan and extract the names */ + + for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS); + rr != NULL; + rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT)) + { + uschar *s = NULL; + if (rr->type != T_PTR) continue; + s = store_get(ssize); + + /* If an overlong response was received, the data will have been + truncated and dn_expand may fail. */ + + if (dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen, + (uschar *)(rr->data), (DN_EXPAND_ARG4_TYPE)(s), ssize) < 0) + { + log_write(0, LOG_MAIN, "host name alias list truncated for %s", + sender_host_address); + break; + } + + store_reset(s + Ustrlen(s) + 1); + if (s[0] == 0) + { + HDEBUG(D_host_lookup) debug_printf("IP address lookup yielded an " + "empty name: treated as non-existent host name\n"); + continue; + } + if (sender_host_name == NULL) sender_host_name = s; + else *aptr++ = s; + while (*s != 0) { *s = tolower(*s); s++; } + } + + *aptr = NULL; /* End of alias list */ + store_pool = old_pool; /* Reset store pool */ + + /* If we've found a names, break out of the "order" loop */ + + if (sender_host_name != NULL) break; + } + + /* If the DNS lookup deferred, we must also defer. */ + + if (rc == DNS_AGAIN) + { + HDEBUG(D_host_lookup) + debug_printf("IP address PTR lookup gave temporary error\n"); + return DEFER; + } + } + + /* Do a lookup using gethostbyaddr() - or equivalent */ + + else if (strcmpic(ordername, US"byaddr") == 0) + { + HDEBUG(D_host_lookup) + debug_printf("IP address lookup using gethostbyaddr()\n"); + + rc = host_name_lookup_byaddr(); + if (rc == DEFER) return rc; /* Can't carry on */ + if (rc == OK) break; /* Found a name */ + } + } /* Loop for bydns/byaddr scanning */ + +/* If we have failed to find a name, return FAIL and log when required. +NB host_lookup_msg must be in permanent store. */ + +if (sender_host_name == NULL) + { + if (host_checking || !log_testing_mode) + log_write(L_host_lookup_failed, LOG_MAIN, "no host name found for IP " + "address %s", sender_host_address); + host_lookup_msg = US" (failed to find host name from IP address)"; + +host_lookup_failed = TRUE; + return FAIL; + } + +/* We have a host name. If we are running in the test harness, we want the host +name and its alias to appear always the same way round. There are only ever two +names in these tests. If one of them contains "alias", make sure it is second; +otherwise put them in alphabetical order. */ + +if (running_in_test_harness && *sender_host_aliases != NULL && + ( + Ustrstr(sender_host_name, "alias") != NULL || + ( + Ustrstr(*sender_host_aliases, "alias") == NULL && + Ustrcmp(sender_host_name, *sender_host_aliases) > 0 + ) + )) + { + uschar *temp = sender_host_name; + sender_host_name = *sender_host_aliases; + *sender_host_aliases = temp; + } + +/* Debug output what was found, after test harness swapping, for consistency */ + +HDEBUG(D_host_lookup) + { + uschar **aliases = sender_host_aliases; + debug_printf("IP address lookup yielded %s\n", sender_host_name); + while (*aliases != NULL) debug_printf(" alias %s\n", *aliases++); + } + +/* We need to verify that a forward lookup on the name we found does indeed +correspond to the address. This is for security: in principle a malefactor who +happened to own a reverse zone could set it to point to any names at all. + +This code was present in versions of Exim before 3.20. At that point I took it +out because I thought that gethostbyaddr() did the check anyway. It turns out +that this isn't always the case, so it's coming back in at 4.01. This version +is actually better, because it also checks aliases. + +The code was made more robust at release 4.21. Prior to that, it accepted all +the names if any of them had the correct IP address. Now the code checks all +the names, and accepts only those that have the correct IP address. */ + +save_hostname = sender_host_name; /* Save for error messages */ +aliases = sender_host_aliases; +for (hname = sender_host_name; hname != NULL; hname = *aliases++) + { + int rc; + BOOL ok = FALSE; + host_item h; + h.next = NULL; + h.name = hname; + h.mx = MX_NONE; + h.address = NULL; + + /* When called with the 5th argument FALSE, host_find_byname() won't return + HOST_FOUND_LOCAL. If the incoming address is an IPv4 address expressed in + IPv6 format, we must compare the IPv4 part to any IPv4 addresses. */ + + if ((rc = host_find_byname(&h, NULL, NULL, FALSE)) == HOST_FOUND) + { + host_item *hh; + uschar *address_ipv4 = (Ustrncmp(sender_host_address, "::ffff:", 7) == 0)? + sender_host_address + 7 : sender_host_address; + HDEBUG(D_host_lookup) debug_printf("checking addresses for %s\n", hname); + for (hh = &h; hh != NULL; hh = hh->next) + { + if ((Ustrcmp(hh->address, (Ustrchr(hh->address, ':') == NULL)? + address_ipv4 : sender_host_address)) == 0) + { + HDEBUG(D_host_lookup) debug_printf(" %s OK\n", hh->address); + ok = TRUE; + break; + } + else + { + HDEBUG(D_host_lookup) debug_printf(" %s\n", hh->address); + } + } + if (!ok) HDEBUG(D_host_lookup) + debug_printf("no IP address for %s matched %s\n", hname, + sender_host_address); + } + else if (rc == HOST_FIND_AGAIN) + { + HDEBUG(D_host_lookup) debug_printf("temporary error for host name lookup\n"); + return DEFER; + } + else + { + HDEBUG(D_host_lookup) debug_printf("no IP addresses found for %s\n", hname); + } + + /* If this name is no good, and it's the sender name, set it null pro tem; + if it's an alias, just remove it from the list. */ + + if (!ok) + { + if (hname == sender_host_name) sender_host_name = NULL; else + { + uschar **a; /* Don't amalgamate - some */ + a = --aliases; /* compilers grumble */ + while (*a != NULL) { *a = a[1]; a++; } + } + } + } + +/* If sender_host_name == NULL, it means we didn't like the name. Replace +it with the first alias, if there is one. */ + +if (sender_host_name == NULL && *sender_host_aliases != NULL) + sender_host_name = *sender_host_aliases++; + +/* If we now have a main name, all is well. */ + +if (sender_host_name != NULL) return OK; + +/* We have failed to find an address that matches. */ + +HDEBUG(D_host_lookup) + debug_printf("%s does not match any IP address for %s\n", + sender_host_address, save_hostname); + +/* This message must be in permanent store */ + +old_pool = store_pool; +store_pool = POOL_PERM; +host_lookup_msg = string_sprintf(" (%s does not match any IP address for %s)", + sender_host_address, save_hostname); +store_pool = old_pool; + +host_lookup_failed = TRUE; +return FAIL; +} + + + + +/************************************************* +* Find IP address(es) for host by name * +*************************************************/ + +/* The input is a host_item structure with the name filled in and the address +field set to NULL. We use gethostbyname(). Of course, gethostbyname() may use +the DNS, but it doesn't do MX processing. If more than one address is given, +chain on additional host items, with other relevant fields copied. + +The second argument provides a host list (usually an IP list) of hosts to +ignore. This makes it possible to ignore IPv6 link-local addresses or loopback +addresses in unreasonable places. + +The lookup may result in a change of name. For compatibility with the dns +lookup, return this via fully_qualified_name as well as updating the host item. +The lookup may also yield more than one IP address, in which case chain on +subsequent host_item structures. + +Arguments: + host a host item with the name and MX filled in; + the address is to be filled in; + multiple IP addresses cause other host items to be + chained on. + ignore_target_hosts a list of hosts to ignore + fully_qualified_name if not NULL, set to point to host name for + compatibility with host_find_bydns + local_host_check TRUE if a check for the local host is wanted + +Returns: HOST_FIND_FAILED Failed to find the host or domain + HOST_FIND_AGAIN Try again later + HOST_FOUND Host found - data filled in + HOST_FOUND_LOCAL Host found and is the local host +*/ + +int +host_find_byname(host_item *host, uschar *ignore_target_hosts, + uschar **fully_qualified_name, BOOL local_host_check) +{ +int i, yield, times; +uschar **addrlist; +host_item *last = NULL; +BOOL temp_error = FALSE; + +/* In an IPv6 world, we need to scan for both kinds of address, so go round the +loop twice. Note that we have ensured that AF_INET6 is defined even in an IPv4 +world, which makes for slightly tidier code. However, if dns_ipv4_lookup +matches the domain, we also just do IPv4 lookups here (except when testing +standalone). */ + +#if HAVE_IPV6 + int af; + + #ifndef STAND_ALONE + if (dns_ipv4_lookup != NULL && + match_isinlist(host->name, &dns_ipv4_lookup, 0, NULL, NULL, MCL_DOMAIN, + TRUE, NULL) == OK) + { af = AF_INET; times = 1; } + else + #endif /* STAND_ALONE */ + + { af = AF_INET6; times = 2; } + +/* No IPv6 support */ + +#else /* HAVE_IPV6 */ + times = 1; +#endif /* HAVE_IPV6 */ + +/* Initialize the flag that gets set for DNS syntax check errors, so that the +interface to this function can be similar to host_find_bydns. */ + +host_find_failed_syntax = FALSE; + +/* Loop to look up both kinds of address in an IPv6 world */ + +for (i = 1; i <= times; + #if HAVE_IPV6 + af = AF_INET, /* If 2 passes, IPv4 on the second */ + #endif + i++) + { + BOOL ipv4_addr; + int error_num; + struct hostent *hostdata; + + #if HAVE_IPV6 + #if HAVE_GETIPNODEBYNAME + hostdata = getipnodebyname(CS host->name, af, 0, &error_num); + #else + hostdata = gethostbyname2(CS host->name, af); + error_num = h_errno; + #endif + #else + hostdata = gethostbyname(CS host->name); + error_num = h_errno; + #endif + + if (hostdata == NULL) + { + uschar *error; + switch (error_num) + { + case HOST_NOT_FOUND: error = US"HOST_NOT_FOUND"; break; + case TRY_AGAIN: error = US"TRY_AGAIN"; break; + case NO_RECOVERY: error = US"NO_RECOVERY"; break; + case NO_DATA: error = US"NO_DATA"; break; + #if NO_DATA != NO_ADDRESS + case NO_ADDRESS: error = US"NO_ADDRESS"; break; + #endif + default: error = US"?"; break; + } + + DEBUG(D_host_lookup) debug_printf("%s returned %d (%s)\n", + #if HAVE_IPV6 + #if HAVE_GETIPNODEBYNAME + (af == AF_INET6)? "getipnodebyname(af=inet6)" : "getipnodebyname(af=inet)", + #else + (af == AF_INET6)? "gethostbyname2(af=inet6)" : "gethostbyname2(af=inet)", + #endif + #else + "gethostbyname", + #endif + error_num, error); + + if (error_num == TRY_AGAIN || error_num == NO_RECOVERY) temp_error = TRUE; + continue; + } + if ((hostdata->h_addr_list)[0] == NULL) continue; + + /* Replace the name with the fully qualified one if necessary, and fill in + the fully_qualified_name pointer. */ + + if (hostdata->h_name[0] != 0 && + Ustrcmp(host->name, hostdata->h_name) != 0) + host->name = string_copy_dnsdomain((uschar *)hostdata->h_name); + if (fully_qualified_name != NULL) *fully_qualified_name = host->name; + + /* Get the list of addresses. IPv4 and IPv6 addresses can be distinguished + by their different lengths. Scan the list, ignoring any that are to be + ignored, and build a chain from the rest. */ + + ipv4_addr = hostdata->h_length == sizeof(struct in_addr); + + for (addrlist = USS hostdata->h_addr_list; *addrlist != NULL; addrlist++) + { + uschar *text_address = + host_ntoa(ipv4_addr? AF_INET:AF_INET6, *addrlist, NULL, NULL); + + #ifndef STAND_ALONE + if (ignore_target_hosts != NULL && + verify_check_this_host(&ignore_target_hosts, NULL, host->name, + text_address, NULL) == OK) + { + DEBUG(D_host_lookup) + debug_printf("ignored host %s [%s]\n", host->name, text_address); + continue; + } + #endif + + /* If this is the first address, last == NULL and we put the data in the + original block. */ + + if (last == NULL) + { + host->address = text_address; + host->port = PORT_NONE; + host->status = hstatus_unknown; + host->why = hwhy_unknown; + last = host; + } + + /* Else add further host item blocks for any other addresses, keeping + the order. */ + + else + { + host_item *next = store_get(sizeof(host_item)); + next->name = host->name; + next->mx = host->mx; + next->address = text_address; + next->port = PORT_NONE; + next->status = hstatus_unknown; + next->why = hwhy_unknown; + next->last_try = 0; + next->next = last->next; + last->next = next; + last = next; + } + } + } + +/* If no hosts were found, the address field in the original host block will be +NULL. If temp_error is set, at least one of the lookups gave a temporary error, +so we pass that back. */ + +if (host->address == NULL) + { + uschar *msg = + #ifndef STAND_ALONE + (message_id[0] == 0 && smtp_in != NULL)? + string_sprintf("no IP address found for host %s (during %s)", host->name, + smtp_get_connection_info()) : + #endif + string_sprintf("no IP address found for host %s", host->name); + + HDEBUG(D_host_lookup) debug_printf("%s\n", msg); + if (temp_error) return HOST_FIND_AGAIN; + if (host_checking || !log_testing_mode) + log_write(L_host_lookup_failed, LOG_MAIN, "%s", msg); + return HOST_FIND_FAILED; + } + +/* Remove any duplicate IP addresses, then check to see if this is the local +host if required. */ + +host_remove_duplicates(host, &last); +yield = local_host_check? + host_scan_for_local_hosts(host, &last, NULL) : HOST_FOUND; + +/* When running in the test harness, sort into the order of addresses so as to +get repeatability. This doesn't have to be efficient. But don't interchange +IPv4 and IPv6 addresses! */ + +if (running_in_test_harness) + { + BOOL done = FALSE; + while (!done) + { + host_item *h; + done = TRUE; + for (h = host; h != last; h = h->next) + { + if ((Ustrchr(h->address, ':') == NULL) != + (Ustrchr(h->next->address, ':') == NULL)) + continue; + if (Ustrcmp(h->address, h->next->address) > 0) + { + uschar *temp = h->address; + h->address = h->next->address; + h->next->address = temp; + done = FALSE; + } + } + } + } + +HDEBUG(D_host_lookup) + { + host_item *h; + if (fully_qualified_name != NULL) + debug_printf("fully qualified name = %s\n", *fully_qualified_name); + debug_printf("%s looked up these IP addresses:\n", + #if HAVE_IPV6 + #if HAVE_GETIPNODEBYNAME + "getipnodebyname" + #else + "gethostbyname2" + #endif + #else + "gethostbyname" + #endif + ); + for (h = host; h != last->next; h = h->next) + debug_printf(" name=%s address=%s\n", h->name, + (h->address == NULL)? US"" : h->address); + } + +/* Return the found status. */ + +return yield; +} + + + +/************************************************* +* Fill in a host address from the DNS * +*************************************************/ + +/* Given a host item, with its name and mx fields set, and its address field +set to NULL, fill in its IP address from the DNS. If it is multi-homed, create +additional host items for the additional addresses, copying all the other +fields, and randomizing the order. + +On IPv6 systems, A6 records are sought first (but only if support for A6 is +configured - they may never become mainstream), then AAAA records are sought, +and finally A records are sought as well. + +The host name may be changed if the DNS returns a different name - e.g. fully +qualified or changed via CNAME. If fully_qualified_name is not NULL, dns_lookup +ensures that it points to the fully qualified name. However, this is the fully +qualified version of the original name; if a CNAME is involved, the actual +canonical host name may be different again, and so we get it directly from the +relevant RR. Note that we do NOT change the mx field of the host item in this +function as it may be called to set the addresses of hosts taken from MX +records. + +Arguments: + host points to the host item we're filling in + lastptr points to pointer to last host item in a chain of + host items (may be updated if host is last and gets + extended because multihomed) + ignore_target_hosts list of hosts to ignore + allow_ip if TRUE, recognize an IP address and return it + fully_qualified_name if not NULL, return fully qualified name here if + the contents are different (i.e. it must be preset + to something) + +Returns: HOST_FIND_FAILED couldn't find A record + HOST_FIND_AGAIN try again later + HOST_FOUND found AAAA and/or A record(s) + HOST_IGNORED found, but all IPs ignored +*/ + +static int +set_address_from_dns(host_item *host, host_item **lastptr, + uschar *ignore_target_hosts, BOOL allow_ip, uschar **fully_qualified_name) +{ +dns_record *rr; +host_item *thishostlast = NULL; /* Indicates not yet filled in anything */ +BOOL v6_find_again = FALSE; +int i; + +/* If allow_ip is set, a name which is an IP address returns that value +as its address. This is used for MX records when allow_mx_to_ip is set, for +those sites that feel they have to flaunt the RFC rules. */ + +if (allow_ip && string_is_ip_address(host->name, NULL) != 0) + { + #ifndef STAND_ALONE + if (ignore_target_hosts != NULL && + verify_check_this_host(&ignore_target_hosts, NULL, host->name, + host->name, NULL) == OK) + return HOST_IGNORED; + #endif + + host->address = host->name; + host->port = PORT_NONE; + return HOST_FOUND; + } + +/* On an IPv6 system, go round the loop up to three times, looking for A6 and +AAAA records the first two times. However, unless doing standalone testing, we +force an IPv4 lookup if the domain matches dns_ipv4_lookup is set. Since A6 +records look like being abandoned, support them only if explicitly configured +to do so. On an IPv4 system, go round the loop once only, looking only for A +records. */ + +#if HAVE_IPV6 + + #ifndef STAND_ALONE + if (dns_ipv4_lookup != NULL && + match_isinlist(host->name, &dns_ipv4_lookup, 0, NULL, NULL, MCL_DOMAIN, + TRUE, NULL) == OK) + i = 0; /* look up A records only */ + else + #endif /* STAND_ALONE */ + + #ifdef SUPPORT_A6 + i = 2; /* look up A6 and AAAA and A records */ + #else + i = 1; /* look up AAAA and A records */ + #endif /* SUPPORT_A6 */ + +/* The IPv4 world */ + +#else /* HAVE_IPV6 */ + i = 0; /* look up A records only */ +#endif /* HAVE_IPV6 */ + +for (; i >= 0; i--) + { + static int types[] = { T_A, T_AAAA, T_A6 }; + int type = types[i]; + int randoffset = (i == 0)? 500 : 0; /* Ensures v6 sorts before v4 */ + dns_answer dnsa; + dns_scan dnss; + + int rc = dns_lookup(&dnsa, host->name, type, fully_qualified_name); + + /* We want to return HOST_FIND_AGAIN if one of the A, A6, or AAAA lookups + fails or times out, but not if another one succeeds. (In the early + IPv6 days there are name servers that always fail on AAAA, but are happy + to give out an A record. We want to proceed with that A record.) */ + + if (rc != DNS_SUCCEED) + { + if (i == 0) /* Just tried for an A record, i.e. end of loop */ + { + if (host->address != NULL) return HOST_FOUND; /* A6 or AAAA was found */ + if (rc == DNS_AGAIN || rc == DNS_FAIL || v6_find_again) + return HOST_FIND_AGAIN; + return HOST_FIND_FAILED; /* DNS_NOMATCH or DNS_NODATA */ + } + + /* Tried for an A6 or AAAA record: remember if this was a temporary + error, and look for the next record type. */ + + if (rc != DNS_NOMATCH && rc != DNS_NODATA) v6_find_again = TRUE; + continue; + } + + /* Lookup succeeded: fill in the given host item with the first non-ignored + address found; create additional items for any others. A single A6 record + may generate more than one address. */ + + for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS); + rr != NULL; + rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT)) + { + if (rr->type == type) + { + /* dns_address *da = dns_address_from_rr(&dnsa, rr); */ + + dns_address *da; + da = dns_address_from_rr(&dnsa, rr); + + DEBUG(D_host_lookup) + { + if (da == NULL) + debug_printf("no addresses extracted from A6 RR for %s\n", + host->name); + } + + /* This loop runs only once for A and AAAA records, but may run + several times for an A6 record that generated multiple addresses. */ + + for (; da != NULL; da = da->next) + { + #ifndef STAND_ALONE + if (ignore_target_hosts != NULL && + verify_check_this_host(&ignore_target_hosts, NULL, + host->name, da->address, NULL) == OK) + { + DEBUG(D_host_lookup) + debug_printf("ignored host %s [%s]\n", host->name, da->address); + continue; + } + #endif + + /* If this is the first address, stick it in the given host block, + and change the name if the returned RR has a different name. */ + + if (thishostlast == NULL) + { + if (strcmpic(host->name, rr->name) != 0) + host->name = string_copy_dnsdomain(rr->name); + host->address = da->address; + host->port = PORT_NONE; + host->sort_key = host->mx * 1000 + random_number(500) + randoffset; + host->status = hstatus_unknown; + host->why = hwhy_unknown; + thishostlast = host; + } + + /* Not the first address. Check for, and ignore, duplicates. Then + insert in the chain at a random point. */ + + else + { + int new_sort_key; + host_item *next; + + /* End of our local chain is specified by "thishostlast". */ + + for (next = host;; next = next->next) + { + if (Ustrcmp(CS da->address, next->address) == 0) break; + if (next == thishostlast) { next = NULL; break; } + } + if (next != NULL) continue; /* With loop for next address */ + + /* Not a duplicate */ + + new_sort_key = host->mx * 1000 + random_number(500) + randoffset; + next = store_get(sizeof(host_item)); + + /* New address goes first: insert the new block after the first one + (so as not to disturb the original pointer) but put the new address + in the original block. */ + + if (new_sort_key < host->sort_key) + { + *next = *host; + host->next = next; + host->address = da->address; + host->port = PORT_NONE; + host->sort_key = new_sort_key; + if (thishostlast == host) thishostlast = next; /* Local last */ + if (*lastptr == host) *lastptr = next; /* Global last */ + } + + /* Otherwise scan down the addresses for this host to find the + one to insert after. */ + + else + { + host_item *h = host; + while (h != thishostlast) + { + if (new_sort_key < h->next->sort_key) break; + h = h->next; + } + *next = *h; + h->next = next; + next->address = da->address; + next->port = PORT_NONE; + next->sort_key = new_sort_key; + if (h == thishostlast) thishostlast = next; /* Local last */ + if (h == *lastptr) *lastptr = next; /* Global last */ + } + } + } + } + } + } + +/* Control gets here only if the third lookup (the A record) succeeded. +However, the address may not be filled in if it was ignored. */ + +return (host->address == NULL)? HOST_IGNORED : HOST_FOUND; +} + + + + +/************************************************* +* Find IP addresses and names for host via DNS * +*************************************************/ + +/* The input is a host_item structure with the name filled in and the address +field set to NULL. This may be in a chain of other host items. The lookup may +result in more than one IP address, in which case we must created new host +blocks for the additional addresses, and insert them into the chain. The +original name may not be fully qualified. Use the fully_qualified_name argument +to return the official name, as returned by the resolver. + +Arguments: + host point to initial host item + ignore_target_hosts a list of hosts to ignore + whichrrs flags indicating which RRs to look for: + HOST_FIND_BY_SRV => look for SRV + HOST_FIND_BY_MX => look for MX + HOST_FIND_BY_A => look for A or AAAA + also flags indicating how the lookup is done + HOST_FIND_QUALIFY_SINGLE ) passed to the + HOST_FIND_SEARCH_PARENTS ) resolver + srv_service when SRV used, the service name + srv_fail_domains DNS errors for these domains => assume nonexist + mx_fail_domains DNS errors for these domains => assume nonexist + fully_qualified_name if not NULL, return fully-qualified name + removed set TRUE if local host was removed from the list + +Returns: HOST_FIND_FAILED Failed to find the host or domain; + if there was a syntax error, + host_find_failed_syntax is set. + HOST_FIND_AGAIN Could not resolve at this time + HOST_FOUND Host found + HOST_FOUND_LOCAL The lowest MX record points to this + machine, if MX records were found, or + an A record that was found contains + an address of the local host +*/ + +int +host_find_bydns(host_item *host, uschar *ignore_target_hosts, int whichrrs, + uschar *srv_service, uschar *srv_fail_domains, uschar *mx_fail_domains, + uschar **fully_qualified_name, BOOL *removed) +{ +host_item *h, *last; +dns_record *rr; +int rc = DNS_FAIL; +int ind_type = 0; +int yield; +dns_answer dnsa; +dns_scan dnss; + +/* Set the default fully qualified name to the incoming name, initialize the +resolver if necessary, set up the relevant options, and initialize the flag +that gets set for DNS syntax check errors. */ + +if (fully_qualified_name != NULL) *fully_qualified_name = host->name; +dns_init((whichrrs & HOST_FIND_QUALIFY_SINGLE) != 0, + (whichrrs & HOST_FIND_SEARCH_PARENTS) != 0); +host_find_failed_syntax = FALSE; + +/* First, if requested, look for SRV records. The service name is given; we +assume TCP progocol. DNS domain names are constrained to a maximum of 256 +characters, so the code below should be safe. */ + +if ((whichrrs & HOST_FIND_BY_SRV) != 0) + { + uschar buffer[300]; + uschar *temp_fully_qualified_name = buffer; + int prefix_length; + + (void)sprintf(CS buffer, "_%s._tcp.%n%.256s", srv_service, &prefix_length, + host->name); + ind_type = T_SRV; + + /* Search for SRV records. If the fully qualified name is different to + the input name, pass back the new original domain, without the prepended + magic. */ + + rc = dns_lookup(&dnsa, buffer, ind_type, &temp_fully_qualified_name); + if (temp_fully_qualified_name != buffer && fully_qualified_name != NULL) + *fully_qualified_name = temp_fully_qualified_name + prefix_length; + + /* On DNS failures, we give the "try again" error unless the domain is + listed as one for which we continue. */ + + if (rc == DNS_FAIL || rc == DNS_AGAIN) + { + if (match_isinlist(host->name, &srv_fail_domains, 0, NULL, NULL, MCL_DOMAIN, + TRUE, NULL) != OK) + return HOST_FIND_AGAIN; + DEBUG(D_host_lookup) debug_printf("DNS_%s treated as DNS_NODATA " + "(domain in srv_fail_domains)\n", (rc == DNS_FAIL)? "FAIL":"AGAIN"); + } + } + +/* If we did not find any SRV records, search the DNS for MX records, if +requested to do so. If the result is DNS_NOMATCH, it means there is no such +domain, and there's no point in going on to look for address records with the +same domain. The result will be DNS_NODATA if the domain exists but has no MX +records. On DNS failures, we give the "try again" error unless the domain is +listed as one for which we continue. */ + +if (rc != DNS_SUCCEED && (whichrrs & HOST_FIND_BY_MX) != 0) + { + ind_type = T_MX; + rc = dns_lookup(&dnsa, host->name, ind_type, fully_qualified_name); + if (rc == DNS_NOMATCH) return HOST_FIND_FAILED; + if (rc == DNS_FAIL || rc == DNS_AGAIN) + { + if (match_isinlist(host->name, &mx_fail_domains, 0, NULL, NULL, MCL_DOMAIN, + TRUE, NULL) != OK) + return HOST_FIND_AGAIN; + DEBUG(D_host_lookup) debug_printf("DNS_%s treated as DNS_NODATA " + "(domain in mx_fail_domains)\n", (rc == DNS_FAIL)? "FAIL":"AGAIN"); + } + } + +/* If we haven't found anything yet, and we are requested to do so, try for an +A or AAAA record. If we find it (or them) check to see that it isn't the local +host. */ + +if (rc != DNS_SUCCEED) + { + if ((whichrrs & HOST_FIND_BY_A) == 0) + { + DEBUG(D_host_lookup) debug_printf("Address records are not being sought\n"); + return HOST_FIND_FAILED; + } + + last = host; /* End of local chainlet */ + host->mx = MX_NONE; + host->port = PORT_NONE; + rc = set_address_from_dns(host, &last, ignore_target_hosts, FALSE, + fully_qualified_name); + + /* If one or more address records have been found, check that none of them + are local. Since we know the host items all have their IP addresses + inserted, host_scan_for_local_hosts() can only return HOST_FOUND or + HOST_FOUND_LOCAL. We do not need to scan for duplicate IP addresses here, + because set_address_from_dns() removes them. */ + + if (rc == HOST_FOUND) + rc = host_scan_for_local_hosts(host, &last, removed); + else + if (rc == HOST_IGNORED) rc = HOST_FIND_FAILED; /* No special action */ + + DEBUG(D_host_lookup) + { + host_item *h; + if (host->address != NULL) + { + if (fully_qualified_name != NULL) + debug_printf("fully qualified name = %s\n", *fully_qualified_name); + for (h = host; h != last->next; h = h->next) + debug_printf("%s %s mx=%d sort=%d %s\n", h->name, + (h->address == NULL)? US"" : h->address, h->mx, h->sort_key, + (h->status >= hstatus_unusable)? US"*" : US""); + } + } + + return rc; + } + +/* We have found one or more MX or SRV records. Sort them according to +precedence. Put the data for the first one into the existing host block, and +insert new host_item blocks into the chain for the remainder. For equal +precedences one is supposed to randomize the order. To make this happen, the +sorting is actually done on the MX value * 1000 + a random number. This is put +into a host field called sort_key. + +In the case of hosts with both IPv6 and IPv4 addresses, we want to choose the +IPv6 address in preference. At this stage, we don't know what kind of address +the host has. We choose a random number < 500; if later we find an A record +first, we add 500 to the random number. Then for any other address records, we +use random numbers in the range 0-499 for AAAA records and 500-999 for A +records. + +At this point we remove any duplicates that point to the same host, retaining +only the one with the lowest precedence. We cannot yet check for precedence +greater than that of the local host, because that test cannot be properly done +until the addresses have been found - an MX record may point to a name for this +host which is not the primary hostname. */ + +last = NULL; /* Indicates that not even the first item is filled yet */ + +for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS); + rr != NULL; + rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT)) + { + int precedence; + int weight = 0; /* For SRV records */ + int port = PORT_NONE; /* For SRV records */ + uschar *s; /* MUST be unsigned for GETSHORT */ + uschar data[256]; + + if (rr->type != ind_type) continue; + s = rr->data; + GETSHORT(precedence, s); /* Pointer s is advanced */ + + /* For MX records, we use a random "weight" which causes multiple records of + the same precedence to sort randomly. */ + + if (ind_type == T_MX) + { + weight = random_number(500); + } + + /* SRV records are specified with a port and a weight. The weight is used + in a special algorithm. However, to start with, we just use it to order the + records of equal priority (precedence). */ + + else + { + GETSHORT(weight, s); + GETSHORT(port, s); + } + + /* Get the name of the host pointed to. */ + + (void)dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen, s, + (DN_EXPAND_ARG4_TYPE)data, sizeof(data)); + + /* Check that we haven't already got this host on the chain; if we have, + keep only the lower precedence. This situation shouldn't occur, but you + never know what junk might get into the DNS (and this case has been seen on + more than one occasion). */ + + if (last != NULL) /* This is not the first record */ + { + host_item *prev = NULL; + + for (h = host; h != last->next; prev = h, h = h->next) + { + if (strcmpic(h->name, data) == 0) + { + DEBUG(D_host_lookup) + debug_printf("discarded duplicate host %s (MX=%d)\n", data, + (precedence > h->mx)? precedence : h->mx); + if (precedence >= h->mx) goto NEXT_MX_RR; /* Skip greater precedence */ + if (h == host) /* Override first item */ + { + h->mx = precedence; + host->sort_key = precedence * 1000 + weight; + goto NEXT_MX_RR; + } + + /* Unwanted host item is not the first in the chain, so we can get + get rid of it by cutting it out. */ + + prev->next = h->next; + if (h == last) last = prev; + break; + } + } + } + + /* If this is the first MX or SRV record, put the data into the existing host + block. Otherwise, add a new block in the correct place; if it has to be + before the first block, copy the first block's data to a new second block. */ + + if (last == NULL) + { + host->name = string_copy_dnsdomain(data); + host->address = NULL; + host->port = port; + host->mx = precedence; + host->sort_key = precedence * 1000 + weight; + host->status = hstatus_unknown; + host->why = hwhy_unknown; + last = host; + } + + /* Make a new host item and seek the correct insertion place */ + + else + { + int sort_key = precedence * 1000 + weight; + host_item *next = store_get(sizeof(host_item)); + next->name = string_copy_dnsdomain(data); + next->address = NULL; + next->port = port; + next->mx = precedence; + next->sort_key = sort_key; + next->status = hstatus_unknown; + next->why = hwhy_unknown; + next->last_try = 0; + + /* Handle the case when we have to insert before the first item. */ + + if (sort_key < host->sort_key) + { + host_item htemp; + htemp = *host; + *host = *next; + *next = htemp; + host->next = next; + if (last == host) last = next; + } + + /* Else scan down the items we have inserted as part of this exercise; + don't go further. */ + + else + { + for (h = host; h != last; h = h->next) + { + if (sort_key < h->next->sort_key) + { + next->next = h->next; + h->next = next; + break; + } + } + + /* Join on after the last host item that's part of this + processing if we haven't stopped sooner. */ + + if (h == last) + { + next->next = last->next; + last->next = next; + last = next; + } + } + } + + NEXT_MX_RR: continue; + } + +/* If the list of hosts was obtained from SRV records, there are two things to +do. First, if there is only one host, and it's name is ".", it means there is +no SMTP service at this domain. Otherwise, we have to sort the hosts of equal +priority according to their weights, using an algorithm that is defined in RFC +2782. The hosts are currently sorted by priority and weight. For each priority +group we have to pick off one host and put it first, and then repeat for any +remaining in the same priority group. */ + +if (ind_type == T_SRV) + { + host_item **pptr; + + if (host == last && host->name[0] == 0) + { + DEBUG(D_host_lookup) debug_printf("the single SRV record is \".\"\n"); + return HOST_FIND_FAILED; + } + + DEBUG(D_host_lookup) + { + debug_printf("original ordering of hosts from SRV records:\n"); + for (h = host; h != last->next; h = h->next) + debug_printf(" %s P=%d W=%d\n", h->name, h->mx, h->sort_key % 1000); + } + + for (pptr = &host, h = host; h != last; pptr = &(h->next), h = h->next) + { + int sum = 0; + host_item *hh; + + /* Find the last following host that has the same precedence. At the same + time, compute the sum of the weights and the running totals. These can be + stored in the sort_key field. */ + + for (hh = h; hh != last; hh = hh->next) + { + int weight = hh->sort_key % 1000; /* was precedence * 1000 + weight */ + sum += weight; + hh->sort_key = sum; + if (hh->mx != hh->next->mx) break; + } + + /* If there's more than one host at this precedence (priority), we need to + pick one to go first. */ + + if (hh != h) + { + host_item *hhh; + host_item **ppptr; + int randomizer = random_number(sum + 1); + + for (ppptr = pptr, hhh = h; + hhh != hh; + ppptr = &(hhh->next), hhh = hhh->next) + { + if (hhh->sort_key >= randomizer) break; + } + + /* hhh now points to the host that should go first; ppptr points to the + place that points to it. Unfortunately, if the start of the minilist is + the start of the entire list, we can't just swap the items over, because + we must not change the value of host, since it is passed in from outside. + One day, this could perhaps be changed. + + The special case is fudged by putting the new item *second* in the chain, + and then transferring the data between the first and second items. We + can't just swap the first and the chosen item, because that would mean + that an item with zero weight might no longer be first. */ + + if (hhh != h) + { + *ppptr = hhh->next; /* Cuts it out of the chain */ + + if (h == host) + { + host_item temp = *h; + *h = *hhh; + *hhh = temp; + hhh->next = temp.next; + h->next = hhh; + } + + else + { + hhh->next = h; /* The rest of the chain follows it */ + *pptr = hhh; /* It takes the place of h */ + h = hhh; /* It's now the start of this minilist */ + } + } + } + + /* A host has been chosen to be first at this priority and h now points + to this host. There may be others at the same priority, or others at a + different priority. Before we leave this host, we need to put back a sort + key of the traditional MX kind, in case this host is multihomed, because + the sort key is used for ordering the multiple IP addresses. We do not need + to ensure that these new sort keys actually reflect the order of the hosts, + however. */ + + h->sort_key = h->mx * 1000 + random_number(500); + } /* Move on to the next host */ + } + +/* Now we have to ensure addresses exist for all the hosts. We have ensured +above that the names in the host items are all unique. The addresses may have +been returned in the additional data section of the DNS query. Because it is +more expensive to scan the returned DNS records (because you have to expand the +names) we do a single scan over them, and multiple scans of the chain of host +items (which is typically only 3 or 4 long anyway.) Add extra host items for +multi-homed hosts. */ + +for (rr = dns_next_rr(&dnsa, &dnss, RESET_ADDITIONAL); + rr != NULL; + rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT)) + { + dns_address *da; + int status = hstatus_unknown; + int why = hwhy_unknown; + int randoffset; + + if (rr->type != T_A + #if HAVE_IPV6 + && rr->type != T_AAAA + #ifdef SUPPORT_A6 + && rr->type != T_A6 + #endif + #endif + ) continue; + + /* Find the first host that matches this record's name. If there isn't + one, move on to the next RR. */ + + for (h = host; h != last->next; h = h->next) + { if (strcmpic(h->name, rr->name) == 0) break; } + if (h == last->next) continue; + + /* For IPv4 addresses, add 500 to the random part of the sort key, to ensure + they sort after IPv6 addresses. */ + + randoffset = (rr->type == T_A)? 500 : 0; + + /* Get the list of textual addresses for this RR. There may be more than one + if it is an A6 RR. Then loop to handle multiple addresses from an A6 record. + If there are none, nothing will get done - the record is ignored. */ + + for (da = dns_address_from_rr(&dnsa, rr); da != NULL; da = da->next) + { + /* Set status for an ignorable host. */ + + #ifndef STAND_ALONE + if (ignore_target_hosts != NULL && + verify_check_this_host(&ignore_target_hosts, NULL, h->name, + da->address, NULL) == OK) + { + DEBUG(D_host_lookup) + debug_printf("ignored host %s [%s]\n", h->name, da->address); + status = hstatus_unusable; + why = hwhy_ignored; + } + #endif + + /* If the address is already set for this host, it may be that + we just have a duplicate DNS record. Alternatively, this may be + a multi-homed host. Search all items with the same host name + (they will all be together) and if this address is found, skip + to the next RR. */ + + if (h->address != NULL) + { + int new_sort_key; + host_item *thishostlast; + host_item *hh = h; + + do + { + if (hh->address != NULL && Ustrcmp(CS da->address, hh->address) == 0) + goto DNS_NEXT_RR; /* Need goto to escape from inner loop */ + thishostlast = hh; + hh = hh->next; + } + while (hh != last->next && strcmpic(hh->name, rr->name) == 0); + + /* We have a multi-homed host, since we have a new address for + an existing name. Create a copy of the current item, and give it + the new address. RRs can be in arbitrary order, but one is supposed + to randomize the addresses of multi-homed hosts, so compute a new + sorting key and do that. [Latest SMTP RFC says not to randomize multi- + homed hosts, but to rely on the resolver. I'm not happy about that - + caching in the resolver will not rotate as often as the name server + does.] */ + + new_sort_key = h->mx * 1000 + random_number(500) + randoffset; + hh = store_get(sizeof(host_item)); + + /* New address goes first: insert the new block after the first one + (so as not to disturb the original pointer) but put the new address + in the original block. */ + + if (new_sort_key < h->sort_key) + { + *hh = *h; /* Note: copies the port */ + h->next = hh; + h->address = da->address; + h->sort_key = new_sort_key; + h->status = status; + h->why = why; + } + + /* Otherwise scan down the addresses for this host to find the + one to insert after. */ + + else + { + while (h != thishostlast) + { + if (new_sort_key < h->next->sort_key) break; + h = h->next; + } + *hh = *h; /* Note: copies the port */ + h->next = hh; + hh->address = da->address; + hh->sort_key = new_sort_key; + hh->status = status; + hh->why = why; + } + + if (h == last) last = hh; /* Inserted after last */ + } + + /* The existing item doesn't have its address set yet, so just set it. + Ensure that an IPv4 address gets its sort key incremented in case an IPv6 + address is found later. */ + + else + { + h->address = da->address; /* Port should be set already */ + h->status = status; + h->why = why; + h->sort_key += randoffset; + } + } /* Loop for addresses extracted from one RR */ + + /* Carry on to the next RR. It would be nice to be able to be able to stop + when every host on the list has an address, but we can't be sure there won't + be an additional address for a multi-homed host further down the list, so + we have to continue to the end. */ + + DNS_NEXT_RR: continue; + } + +/* Set the default yield to failure */ + +yield = HOST_FIND_FAILED; + +/* If we haven't found all the addresses in the additional section, we +need to search for A or AAAA records explicitly. The names shouldn't point to +CNAMES, but we use the general lookup function that handles them, just +in case. If any lookup gives a soft error, change the default yield. + +For these DNS lookups, we must disable qualify_single and search_parents; +otherwise invalid host names obtained from MX or SRV records can cause trouble +if they happen to match something local. */ + +dns_init(FALSE, FALSE); + +for (h = host; h != last->next; h = h->next) + { + if (h->address != NULL || h->status == hstatus_unusable) continue; + rc = set_address_from_dns(h, &last, ignore_target_hosts, allow_mx_to_ip, NULL); + if (rc != HOST_FOUND) + { + h->status = hstatus_unusable; + if (rc == HOST_FIND_AGAIN) + { + yield = rc; + h->why = hwhy_deferred; + } + else + h->why = (rc == HOST_IGNORED)? hwhy_ignored : hwhy_failed; + } + } + +/* Scan the list for any hosts that are marked unusable because they have +been explicitly ignored, and remove them from the list, as if they did not +exist. If we end up with just a single, ignored host, flatten its fields as if +nothing was found. */ + +if (ignore_target_hosts != NULL) + { + host_item *prev = NULL; + for (h = host; h != last->next; h = h->next) + { + REDO: + if (h->why != hwhy_ignored) /* Non ignored host, just continue */ + prev = h; + else if (prev == NULL) /* First host is ignored */ + { + if (h != last) /* First is not last */ + { + if (h->next == last) last = h; /* Overwrite it with next */ + *h = *(h->next); /* and reprocess it. */ + goto REDO; /* C should have redo, like Perl */ + } + } + else /* Ignored host is not first - */ + { /* cut it out */ + prev->next = h->next; + if (h == last) last = prev; + } + } + + if (host->why == hwhy_ignored) host->address = NULL; + } + +/* There is still one complication in the case of IPv6. Although the code above +arranges that IPv6 addresses take precedence over IPv4 addresses for multihomed +hosts, it doesn't do this for addresses that apply to different hosts with the +same MX precedence, because the sorting on MX precedence happens first. So we +have to make another pass to check for this case. We ensure that, within a +single MX preference value, IPv6 addresses come first. This can separate the +addresses of a multihomed host, but that should not matter. */ + +#if HAVE_IPV6 +if (h != last) + { + for (h = host; h != last; h = h->next) + { + host_item temp; + host_item *next = h->next; + if (h->mx != next->mx || /* If next is different MX value */ + (h->sort_key % 1000) < 500 || /* OR this one is IPv6 */ + (next->sort_key % 1000) >= 500) /* OR next is IPv4 */ + continue; /* move on to next */ + temp = *h; + temp.next = next->next; + *h = *next; + h->next = next; + *next = temp; + } + } +#endif + +/* When running in the test harness, we want the hosts always to be in the same +order so that the debugging output is the same and can be compared. Having a +fixed set of "random" numbers doesn't actually achieve this, because the RRs +come back from the resolver in a random order, so the non-random random numbers +get used in a different order. We therefore have to sort the hosts that have +the same MX values. We chose do to this by their name and then by IP address. +The fact that the sort is slow matters not - this is testing only! */ + +if (running_in_test_harness) + { + BOOL done; + do + { + done = TRUE; + for (h = host; h != last; h = h->next) + { + int c = Ustrcmp(h->name, h->next->name); + if (c == 0) c = Ustrcmp(h->address, h->next->address); + if (h->mx == h->next->mx && c > 0) + { + host_item *next = h->next; + host_item temp = *h; + temp.next = next->next; + *h = *next; + h->next = next; + *next = temp; + done = FALSE; + } + } + } + while (!done); + } + +/* Remove any duplicate IP addresses and then scan the list of hosts for any +whose IP addresses are on the local host. If any are found, all hosts with the +same or higher MX values are removed. However, if the local host has the lowest +numbered MX, then HOST_FOUND_LOCAL is returned. Otherwise, if at least one host +with an IP address is on the list, HOST_FOUND is returned. Otherwise, +HOST_FIND_FAILED is returned, but in this case do not update the yield, as it +might have been set to HOST_FIND_AGAIN just above here. If not, it will already +be HOST_FIND_FAILED. */ + +host_remove_duplicates(host, &last); +rc = host_scan_for_local_hosts(host, &last, removed); +if (rc != HOST_FIND_FAILED) yield = rc; + +DEBUG(D_host_lookup) + { + if (fully_qualified_name != NULL) + debug_printf("fully qualified name = %s\n", *fully_qualified_name); + debug_printf("host_find_bydns yield = %s (%d); returned hosts:\n", + (yield == HOST_FOUND)? "HOST_FOUND" : + (yield == HOST_FOUND_LOCAL)? "HOST_FOUND_LOCAL" : + (yield == HOST_FIND_AGAIN)? "HOST_FIND_AGAIN" : + (yield == HOST_FIND_FAILED)? "HOST_FIND_FAILED" : "?", + yield); + for (h = host; h != last->next; h = h->next) + { + debug_printf(" %s %s MX=%d ", h->name, + (h->address == NULL)? US"" : h->address, h->mx); + if (h->port != PORT_NONE) debug_printf("port=%d ", h->port); + if (h->status >= hstatus_unusable) debug_printf("*"); + debug_printf("\n"); + } + } + +return yield; +} + + + + +/************************************************* +************************************************** +* Stand-alone test program * +************************************************** +*************************************************/ + +#ifdef STAND_ALONE + +BOOL alldigits(uschar *buffer) +{ +if (!isdigit(*buffer)) return FALSE; +if (*buffer == '0' && buffer[1] == 'x') + { + buffer++; + while (isxdigit(*(++buffer))); + } +else while (isdigit(*(++buffer))); +return (*buffer == 0); +} + +int main(int argc, char **cargv) +{ +host_item h; +int whichrrs = HOST_FIND_BY_MX | HOST_FIND_BY_A; +BOOL byname = FALSE; +BOOL qualify_single = TRUE; +BOOL search_parents = FALSE; +uschar **argv = USS cargv; +uschar buffer[256]; + +primary_hostname = US""; +store_pool = POOL_MAIN; +debug_selector = D_host_lookup|D_interface; +debug_file = stdout; +debug_fd = fileno(debug_file); + +printf("Exim stand-alone host functions test\n"); + +host_find_interfaces(); +debug_selector = D_host_lookup | D_dns; + +if (argc > 1) primary_hostname = argv[1]; + +/* So that debug level changes can be done first */ + +dns_init(qualify_single, search_parents); + +printf("Testing host lookup\n"); +printf("> "); +while (Ufgets(buffer, 256, stdin) != NULL) + { + int rc; + int len = Ustrlen(buffer); + uschar *fully_qualified_name; + + while (len > 0 && isspace(buffer[len-1])) len--; + buffer[len] = 0; + + if (Ustrcmp(buffer, "q") == 0) break; + + if (Ustrcmp(buffer, "byname") == 0) byname = TRUE; + else if (Ustrcmp(buffer, "no_byname") == 0) byname = FALSE; + else if (Ustrcmp(buffer, "a_only") == 0) whichrrs = HOST_FIND_BY_A; + else if (Ustrcmp(buffer, "mx_only") == 0) whichrrs = HOST_FIND_BY_MX; + else if (Ustrcmp(buffer, "srv_only") == 0) whichrrs = HOST_FIND_BY_SRV; + else if (Ustrcmp(buffer, "srv+a") == 0) + whichrrs = HOST_FIND_BY_SRV | HOST_FIND_BY_A; + else if (Ustrcmp(buffer, "srv+mx") == 0) + whichrrs = HOST_FIND_BY_SRV | HOST_FIND_BY_MX; + else if (Ustrcmp(buffer, "srv+mx+a") == 0) + whichrrs = HOST_FIND_BY_SRV | HOST_FIND_BY_MX | HOST_FIND_BY_A; + else if (Ustrcmp(buffer, "qualify_single") == 0) qualify_single = TRUE; + else if (Ustrcmp(buffer, "no_qualify_single") == 0) qualify_single = FALSE; + else if (Ustrcmp(buffer, "search_parents") == 0) search_parents = TRUE; + else if (Ustrcmp(buffer, "no_search_parents") == 0) search_parents = FALSE; + else if (Ustrncmp(buffer, "retrans", 7) == 0) + { + sscanf(CS(buffer+8), "%d", &dns_retrans); + _res.retrans = dns_retrans; + } + else if (Ustrncmp(buffer, "retry", 5) == 0) + { + sscanf(CS(buffer+6), "%d", &dns_retry); + _res.retry = dns_retry; + } + else if (alldigits(buffer)) + { + debug_selector = Ustrtol(buffer, NULL, 0); + _res.options &= ~RES_DEBUG; + DEBUG(D_resolver) _res.options |= RES_DEBUG; + } + else + { + int flags = whichrrs; + + h.name = buffer; + h.next = NULL; + h.mx = MX_NONE; + h.port = PORT_NONE; + h.status = hstatus_unknown; + h.why = hwhy_unknown; + h.address = NULL; + + if (qualify_single) flags |= HOST_FIND_QUALIFY_SINGLE; + if (search_parents) flags |= HOST_FIND_SEARCH_PARENTS; + + rc = byname? + host_find_byname(&h, NULL, &fully_qualified_name, TRUE) + : + host_find_bydns(&h, NULL, flags, US"smtp", NULL, NULL, + &fully_qualified_name, NULL); + + if (rc == HOST_FIND_FAILED) printf("Failed\n"); + else if (rc == HOST_FIND_AGAIN) printf("Again\n"); + else if (rc == HOST_FOUND_LOCAL) printf("Local\n"); + } + + printf("\n> "); + } + +printf("Testing host_aton\n"); +printf("> "); +while (Ufgets(buffer, 256, stdin) != NULL) + { + int i; + int x[4]; + int len = Ustrlen(buffer); + + while (len > 0 && isspace(buffer[len-1])) len--; + buffer[len] = 0; + + if (Ustrcmp(buffer, "q") == 0) break; + + len = host_aton(buffer, x); + printf("length = %d ", len); + for (i = 0; i < len; i++) + { + printf("%04x ", (x[i] >> 16) & 0xffff); + printf("%04x ", x[i] & 0xffff); + } + printf("\n> "); + } + +printf("\n"); + +printf("Testing host_name_lookup\n"); +printf("> "); +while (Ufgets(buffer, 256, stdin) != NULL) + { + int len = Ustrlen(buffer); + while (len > 0 && isspace(buffer[len-1])) len--; + buffer[len] = 0; + if (Ustrcmp(buffer, "q") == 0) break; + sender_host_address = buffer; + sender_host_name = NULL; + sender_host_aliases = NULL; + host_lookup_msg = US""; + host_lookup_failed = FALSE; + if (host_name_lookup() == FAIL) /* Debug causes printing */ + printf("Lookup failed:%s\n", host_lookup_msg); + printf("\n> "); + } + +printf("\n"); + +return 0; +} +#endif /* STAND_ALONE */ + +/* End of host.c */ diff --git a/src/src/ip.c b/src/src/ip.c new file mode 100644 index 000000000..24929c84b --- /dev/null +++ b/src/src/ip.c @@ -0,0 +1,369 @@ +/* $Cambridge: exim/src/src/ip.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Functions for doing things with sockets. With the advent of IPv6 this has +got messier, so that it's worth pulling out the code into separate functions +that other parts of Exim can call, expecially as there are now several +different places in the code where sockets are used. */ + + +#include "exim.h" + + +/************************************************* +* Create a socket * +*************************************************/ + +/* Socket creation happens in a number of places so it's packaged here for +convenience. + +Arguments: + type SOCK_DGRAM or SOCK_STREAM + af AF_INET or AF_INET6 + +Returns: socket number or -1 on failure +*/ + +int +ip_socket(int type, int af) +{ +int sock = socket(af, type, 0); +if (sock < 0) + log_write(0, LOG_MAIN, "IPv%c socket creation failed: %s", + (af == AF_INET6)? '6':'4', strerror(errno)); +return sock; +} + + + + +#if HAVE_IPV6 +/************************************************* +* Convert printing address to numeric * +*************************************************/ + +/* This function converts the textual form of an IP address into a numeric form +in an appropriate structure in an IPv6 environment. The getaddrinfo() function +can (apparently) handle more complicated addresses (e.g. those containing +scopes) than inet_pton() in some environments. We use hints to tell it that the +input must be a numeric address. + +However, apparently some operating systems (or libraries) don't support +getaddrinfo(), so there is a build-time option to revert to inet_pton() (which +does not support scopes). + +Arguments: + address textual form of the address + addr where to copy back the answer + +Returns: nothing - failure provokes a panic-die +*/ + +static void +ip_addrinfo(uschar *address, struct sockaddr_in6 *saddr) +{ +#ifdef IPV6_USE_INET_PTON + + if (inet_pton(AF_INET6, CS address, &saddr->sin6_addr) != 1) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "unable to parse \"%s\" as an " + "IP address", address); + saddr->sin6_family = AF_INET6; + +#else + + int rc; + struct addrinfo hints, *res; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET6; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_NUMERICHOST; + if ((rc = getaddrinfo(CS address, NULL, &hints, &res)) != 0 || res == NULL) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "unable to parse \"%s\" as an " + "IP address: %s", address, + (rc == 0)? "NULL result returned" : gai_strerror(rc)); + memcpy(saddr, res->ai_addr, res->ai_addrlen); + freeaddrinfo(res); + +#endif +} +#endif /* HAVE_IPV6 */ + + +/************************************************* +* Bind socket to interface and port * +*************************************************/ + +/* This function binds a socket to a local interface address and port. For a +wildcard IPv6 bind, the address is ":". + +Arguments: + sock the socket + af AF_INET or AF_INET6 - the socket type + address the IP address, in text form + port the IP port (host order) + +Returns: the result of bind() +*/ + +int +ip_bind(int sock, int af, uschar *address, int port) +{ +int s_len; +union sockaddr_46 sin; +memset(&sin, 0, sizeof(sin)); + +/* Setup code when using an IPv6 socket. The wildcard address is ":", to +ensure an IPv6 socket is used. */ + +#if HAVE_IPV6 +if (af == AF_INET6) + { + if (address[0] == ':' && address[1] == 0) + { + sin.v6.sin6_family = AF_INET6; + sin.v6.sin6_addr = in6addr_any; + } + else + { + ip_addrinfo(address, &sin.v6); /* Panic-dies on error */ + } + sin.v6.sin6_port = htons(port); + s_len = sizeof(sin.v6); + } +else +#else /* HAVE_IPv6 */ +af = af; /* Avoid compiler warning */ +#endif /* HAVE_IPV6 */ + +/* Setup code when using IPv4 socket. The wildcard address is "". */ + + { + sin.v4.sin_family = AF_INET; + sin.v4.sin_port = htons(port); + s_len = sizeof(sin.v4); + if (address[0] == 0) + sin.v4.sin_addr.s_addr = (S_ADDR_TYPE)INADDR_ANY; + else + sin.v4.sin_addr.s_addr = (S_ADDR_TYPE)inet_addr(CS address); + } + +/* Now we can call the bind() function */ + +return bind(sock, (struct sockaddr *)&sin, s_len); +} + + + +/************************************************* +* Connect socket to remote host * +*************************************************/ + +/* This function connects a socket to a remote address and port. The socket may +or may not have previously been bound to a local interface. + +Arguments: + sock the socket + af AF_INET6 or AF_INET for the socket type + address the remote address, in text form + port the remote port + timeout a timeout + +Returns: 0 on success; -1 on failure, with errno set +*/ + +int +ip_connect(int sock, int af, uschar *address, int port, int timeout) +{ +struct sockaddr_in s_in4; +struct sockaddr *s_ptr; +int s_len, rc, save_errno; + +/* For an IPv6 address, use an IPv6 sockaddr structure. */ + +#if HAVE_IPV6 +struct sockaddr_in6 s_in6; +if (af == AF_INET6) + { + memset(&s_in6, 0, sizeof(s_in6)); + ip_addrinfo(address, &s_in6); /* Panic-dies on error */ + s_in6.sin6_port = htons(port); + s_ptr = (struct sockaddr *)&s_in6; + s_len = sizeof(s_in6); + } +else +#else /* HAVE_IPV6 */ +af = af; /* Avoid compiler warning */ +#endif /* HAVE_IPV6 */ + +/* For an IPv4 address, use an IPv4 sockaddr structure, even on a system with +IPv6 support. */ + + { + memset(&s_in4, 0, sizeof(s_in4)); + s_in4.sin_family = AF_INET; + s_in4.sin_port = htons(port); + s_in4.sin_addr.s_addr = (S_ADDR_TYPE)inet_addr(CS address); + s_ptr = (struct sockaddr *)&s_in4; + s_len = sizeof(s_in4); + } + +/* If no connection timeout is set, just call connect() without setting a +timer, thereby allowing the inbuilt OS timeout to operate. */ + +sigalrm_seen = FALSE; +if (timeout > 0) alarm(timeout); +rc = connect(sock, s_ptr, s_len); +save_errno = errno; +alarm(0); + +/* There is a testing facility for simulating a connection timeout, as I +can't think of any other way of doing this. It converts a connection refused +into a timeout. + +I had to add a second fudge to keep the tests working. Attempts to connect to +10.x.x.x are expected to timeout, but sometimes they now give "No route to +host". */ + +if (running_in_test_harness) + { + if ((save_errno == ECONNREFUSED && timeout == 999999) || + (save_errno == EHOSTUNREACH && timeout > 0 && + Ustrncmp(address, "10.", 3) == 0)) + { + rc = -1; + save_errno = EINTR; + sigalrm_seen = TRUE; + } + } + +/* Success */ + +if (rc >= 0) return 0; + +/* A failure whose error code is "Interrupted system call" is in fact +an externally applied timeout if the signal handler has been run. */ + +close(sock); +errno = (save_errno == EINTR && sigalrm_seen)? ETIMEDOUT : save_errno; +return -1; +} + + + +/************************************************* +* Set keepalive on a socket * +*************************************************/ + +/* Can be called for both incoming and outgoing sockets. + +Arguments: + sock the socket + address the remote host address, for failure logging + torf true for outgoing connection, false for incoming + +Returns: nothing +*/ + +void +ip_keepalive(int sock, uschar *address, BOOL torf) +{ +int fodder = 1; +if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, + (uschar *)(&fodder), sizeof(fodder)) != 0) + log_write(0, LOG_MAIN, "setsockopt(SO_KEEPALIVE) on connection %s %s " + "failed: %s", torf? "to":"from", address, strerror(errno)); +} + + + +/************************************************* +* Receive from a socket with timeout * +*************************************************/ + +/* The timeout is implemented using select(), and we loop to cover select() +getting interrupted, and the possibility of select() returning with a positive +result but no ready descriptor. Is this in fact possible? + +Arguments: + sock the socket + buffer to read into + bufsize the buffer size + timeout the timeout + +Returns: > 0 => that much data read + <= 0 on error or EOF; errno set - zero for EOF +*/ + +int +ip_recv(int sock, uschar *buffer, int buffsize, int timeout) +{ +fd_set select_inset; +struct timeval tv; +int start_recv = time(NULL); +int rc; + +/* Wait until the socket is ready */ + +for (;;) + { + FD_ZERO (&select_inset); + FD_SET (sock, &select_inset); + tv.tv_sec = timeout; + tv.tv_usec = 0; + + DEBUG(D_transport) debug_printf("waiting for data on socket\n"); + rc = select(sock + 1, (SELECT_ARG2_TYPE *)&select_inset, NULL, NULL, &tv); + + /* If some interrupt arrived, just retry. We presume this to be rare, + but it can happen (e.g. the SIGUSR1 signal sent by exiwhat causes + select() to exit). + + Aug 2004: Somebody set up a cron job that ran exiwhat every 2 minutes, making + the interrupt not at all rare. Since the timeout is typically more than 2 + minutes, the effect was to block the timeout completely. To prevent this + happening again, we do an explicit time test. */ + + if (rc < 0 && errno == EINTR) + { + DEBUG(D_transport) debug_printf("EINTR while waiting for socket data\n"); + if (time(NULL) - start_recv < timeout) continue; + DEBUG(D_transport) debug_printf("total wait time exceeds timeout\n"); + } + + /* Handle a timeout, and treat any other select error as a timeout, including + an EINTR when we have been in this loop for longer than timeout. */ + + if (rc <= 0) + { + errno = ETIMEDOUT; + return -1; + } + + /* If the socket is ready, break out of the loop. */ + + if (FD_ISSET(sock, &select_inset)) break; + } + +/* The socket is ready, read from it (via TLS if it's active). On EOF (i.e. +close down of the connection), set errno to zero; otherwise leave it alone. */ + +#ifdef SUPPORT_TLS +if (tls_active == sock) + rc = tls_read(buffer, buffsize); +else +#endif + rc = recv(sock, buffer, buffsize, 0); + +if (rc > 0) return rc; +if (rc == 0) errno = 0; +return -1; +} + + +/* End of ip.c */ diff --git a/src/src/local_scan.c b/src/src/local_scan.c new file mode 100644 index 000000000..ee5214df2 --- /dev/null +++ b/src/src/local_scan.c @@ -0,0 +1,66 @@ +/* $Cambridge: exim/src/src/local_scan.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + + +/****************************************************************************** +This file contains a template local_scan() function that just returns ACCEPT. +If you want to implement your own version, you should copy this file to, say +Local/local_scan.c, and edit the copy. To use your version instead of the +default, you must set + +LOCAL_SCAN_SOURCE=Local/local_scan.c + +in your Local/Makefile. This makes it easy to copy your version for use with +subsequent Exim releases. + +For a full description of the API to this function, see the Exim specification. +******************************************************************************/ + + +/* This is the only Exim header that you should include. The effect of +including any other Exim header is not defined, and may change from release to +release. Use only the documented interface! */ + +#include "local_scan.h" + + +/* This is a "do-nothing" version of a local_scan() function. The arguments +are: + + fd The file descriptor of the open -D file, which contains the + body of the message. The file is open for reading and + writing, but modifying it is dangerous and not recommended. + + return_text A pointer to an unsigned char* variable which you can set in + order to return a text string. It is initialized to NULL. + +The return values of this function are: + + LOCAL_SCAN_ACCEPT + The message is to be accepted. The return_text argument is + saved in $local_scan_data. + + LOCAL_SCAN_REJECT + The message is to be rejected. The returned text is used + in the rejection message. + + LOCAL_SCAN_TEMPREJECT + This specifies a temporary rejection. The returned text + is used in the rejection message. +*/ + +int +local_scan(int fd, uschar **return_text) +{ +fd = fd; /* Keep picky compilers happy */ +return_text = return_text; +return LOCAL_SCAN_ACCEPT; +} + +/* End of local_scan.c */ diff --git a/src/src/local_scan.h b/src/src/local_scan.h new file mode 100644 index 000000000..4e6f88a31 --- /dev/null +++ b/src/src/local_scan.h @@ -0,0 +1,166 @@ +/* $Cambridge: exim/src/src/local_scan.h,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* This file is the header that is the only Exim header to be included in the +source for the local_scan.c() function. It contains definitions that are made +available for use in that function, and which are documented. */ + + +/* Some basic types that make some things easier, and the store functions. */ + +#include +#include "mytypes.h" +#include "store.h" + + +/* The function and its return codes. */ + +extern int local_scan(int, uschar **); + +enum { + LOCAL_SCAN_ACCEPT, /* Accept */ + LOCAL_SCAN_ACCEPT_FREEZE, /* Accept, but freeze */ + LOCAL_SCAN_ACCEPT_QUEUE, /* Accept, but no immediate delivery */ + LOCAL_SCAN_REJECT, /* Permanent rejection */ + LOCAL_SCAN_REJECT_NOLOGHDR, /* Permanent rejection, no log header */ + LOCAL_SCAN_TEMPREJECT, /* Temporary rejection */ + LOCAL_SCAN_TEMPREJECT_NOLOGHDR /* Temporary rejection, no log header */ +}; + + +/* Return codes from the support functions lss_match_xxx(). */ + +#define OK 0 /* Successful match */ +#define DEFER 1 /* Defer - some problem */ +#define FAIL 2 /* Matching failed */ +#define ERROR 3 /* Internal or config error */ + + +/* Available logging destinations */ + +#define LOG_MAIN 1 /* Write to the main log */ +#define LOG_PANIC 2 /* Write to the panic log */ +#define LOG_REJECT 16 /* Write to the reject log, with headers */ + + +/* Accessible debugging bits */ + +#define D_v 0x00000001 +#define D_local_scan 0x00000002 + + +/* Option types that can be used for local_scan_options. The boolean ones +MUST be last so that they are contiguous with the internal boolean specials. */ + +enum { opt_stringptr, opt_int, opt_octint, opt_mkint, opt_Kint, opt_fixed, + opt_time, opt_bool }; + + +/* The length of message identification strings. This is the id used internally +by exim. The external version for use in Received: strings has a leading 'E' +added to ensure it starts with a letter. */ + +#define MESSAGE_ID_LENGTH 16 + +/* The offset to the start of the data in the data file - this allows for +the name of the data file to be present in the first line. */ + +#define SPOOL_DATA_START_OFFSET (MESSAGE_ID_LENGTH+3) + +/* Some people (Marc Merlin et al) are maintaining a patch that allows for +dynamic local_scan() libraries. This code is not yet in Exim proper, but it +helps the maintainers if we keep their ABI version numbers here. This may +mutate into more general support later. The major number is increased when the +ABI is changed in a non backward compatible way. The minor number is increased +each time a new feature is added (in a way that doesn't break backward +compatibility). */ + +#define LOCAL_SCAN_ABI_VERSION_MAJOR 1 +#define LOCAL_SCAN_ABI_VERSION_MINOR 0 + +/* Structure definitions that are documented as visible in the function. */ + +typedef struct header_line { + struct header_line *next; + int type; + int slen; + uschar *text; +} header_line; + +/* Entries in lists options are in this form. */ + +typedef struct { + char *name; + int type; + void *value; +} optionlist; + +/*Structure for holding information about an envelope address. The errors_to +field is always NULL except for one_time aliases that had errors_to on the +routers that generated them. */ + +typedef struct recipient_item { + uschar *address; /* the recipient address */ + int pno; /* parent number for "one_time" alias, or -1 */ + uschar *errors_to; /* the errors_to address or NULL */ +} recipient_item; + + +/* Global variables that are documented as visible in the function. */ + +extern unsigned int debug_selector; /* Debugging bits */ + +extern uschar *expand_string_message; /* Error info for failing expansion */ +extern uschar *headers_charset; /* Charset for RFC 2047 decoding */ +extern header_line *header_last; /* Final header */ +extern header_line *header_list; /* First header */ +extern BOOL host_checking; /* Set when checking a host */ +extern uschar *interface_address; /* Interface for incoming call */ +extern int interface_port; /* Port number for incoming call */ +extern uschar *message_id; /* Internal id of message being handled */ +extern uschar *received_protocol; /* Name of incoming protocol */ +extern int recipients_count; /* Number of recipients */ +extern recipient_item *recipients_list;/* List of recipient addresses */ +extern unsigned char *sender_address; /* Sender address */ +extern uschar *sender_host_address; /* IP address of sender, as chars */ +extern uschar *sender_host_authenticated; /* Name of authentication mechanism */ +extern uschar *sender_host_name; /* Host name from lookup */ +extern int sender_host_port; /* Port number of sender */ +extern BOOL smtp_batched_input; /* TRUE if SMTP batch (no interaction) */ +extern BOOL smtp_input; /* TRUE if input is via SMTP */ + + +/* Functions that are documented as visible in local_scan(). */ + +extern int child_close(pid_t, int); +extern pid_t child_open(uschar **, uschar **, int, int *, int *, BOOL); +extern pid_t child_open_exim(int *); +extern void debug_printf(char *, ...) PRINTF_FUNCTION; +extern uschar *expand_string(uschar *); +extern void header_add(int, char *, ...); +extern void header_add_at_position(BOOL, uschar *, BOOL, int, char *, ...); +extern void header_remove(int, uschar *); +extern BOOL header_testname(header_line *, uschar *, int, BOOL); +extern void log_write(unsigned int, int, char *format, ...); +extern int lss_b64decode(uschar *, uschar **); +extern uschar *lss_b64encode(uschar *, int); +extern int lss_match_domain(uschar *, uschar *); +extern int lss_match_local_part(uschar *, uschar *, BOOL); +extern int lss_match_address(uschar *, uschar *, BOOL); +extern int lss_match_host(uschar *, uschar *, uschar *); +extern void receive_add_recipient(uschar *, int); +extern BOOL receive_remove_recipient(uschar *); +extern uschar *rfc2047_decode(uschar *, BOOL, uschar *, int, int *, uschar **); +extern int smtp_fflush(void); +extern void smtp_printf(char *, ...) PRINTF_FUNCTION; +extern uschar *string_copy(uschar *); +extern uschar *string_copyn(uschar *, int); +extern uschar *string_sprintf(char *, ...); + +/* End of local_scan.h */ diff --git a/src/src/log.c b/src/src/log.c new file mode 100644 index 000000000..44adcc2db --- /dev/null +++ b/src/src/log.c @@ -0,0 +1,1054 @@ +/* $Cambridge: exim/src/src/log.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Functions for writing log files. The code for maintaining datestamped +log files was originally contributed by Tony Sheen. */ + + +#include "exim.h" + +#define LOG_NAME_SIZE 256 +#define MAX_SYSLOG_LEN 870 + +#define LOG_MODE_FILE 1 +#define LOG_MODE_SYSLOG 2 + +enum { lt_main, lt_reject, lt_panic, lt_process }; + +static uschar *log_names[] = { US"main", US"reject", US"panic", US"process" }; + + + +/************************************************* +* Local static variables * +*************************************************/ + +static uschar mainlog_name[LOG_NAME_SIZE]; +static uschar rejectlog_name[LOG_NAME_SIZE]; + +static uschar *mainlog_datestamp = NULL; +static uschar *rejectlog_datestamp = NULL; + +static int mainlogfd = -1; +static int rejectlogfd = -1; +static ino_t mainlog_inode = 0; +static ino_t rejectlog_inode = 0; + +static uschar *panic_save_buffer = NULL; +static BOOL panic_recurseflag = FALSE; + +static BOOL syslog_open = FALSE; +static BOOL path_inspected = FALSE; +static int logging_mode = LOG_MODE_FILE; +static uschar *file_path = US""; + + + + +/************************************************* +* Write to syslog * +*************************************************/ + +/* The given string is split into sections according to length, or at embedded +newlines, and syslogged as a numbered sequence if it is overlong or if there is +more than one line. + +Arguments: + priority syslog priority + s the string to be written + +Returns: nothing +*/ + +static void +write_syslog(int priority, uschar *s) +{ +int len, pass; +int linecount = 0; + +if (!syslog_timestamp) s += log_timezone? 26 : 20; + +len = Ustrlen(s); + +#ifndef NO_OPENLOG +if (!syslog_open) + { + #ifdef SYSLOG_LOG_PID + openlog(CS syslog_processname, LOG_PID|LOG_CONS, syslog_facility); + #else + openlog(CS syslog_processname, LOG_CONS, syslog_facility); + #endif + syslog_open = TRUE; + } +#endif + +/* First do a scan through the message in order to determine how many lines +it is going to end up as. Then rescan to output it. */ + +for (pass = 0; pass < 2; pass++) + { + int i; + int tlen; + uschar *ss = s; + for (i = 1, tlen = len; tlen > 0; i++) + { + int plen = tlen; + uschar *nlptr = Ustrchr(ss, '\n'); + if (nlptr != NULL) plen = nlptr - ss; + #ifndef SYSLOG_LONG_LINES + if (plen > MAX_SYSLOG_LEN) plen = MAX_SYSLOG_LEN; + #endif + tlen -= plen; + if (ss[plen] == '\n') tlen--; /* chars left */ + + if (pass == 0) linecount++; else + { + if (linecount == 1) + syslog(priority, "%.*s", plen, ss); + else + syslog(priority, "[%d%c%d] %.*s", i, + (ss[plen] == '\n' && tlen != 0)? '\\' : '/', + linecount, plen, ss); + } + ss += plen; + if (*ss == '\n') ss++; + } + } +} + + + +/************************************************* +* Die tidily * +*************************************************/ + +/* This is called when Exim is dying as a result of something going wrong in +the logging, or after a log call with LOG_PANIC_DIE set. Optionally write a +message to debug_file or a stderr file, if they exist. Then, if in the middle +of accepting a message, throw it away tidily; this will attempt to send an SMTP +response if appropriate. Otherwise, try to close down an outstanding SMTP call +tidily. + +Arguments: + s1 Error message to write to debug_file and/or stderr and syslog + s2 Error message for any SMTP call that is in progress +Returns: The function does not return +*/ + +static void +die(uschar *s1, uschar *s2) +{ +if (s1 != NULL) + { + write_syslog(LOG_CRIT, s1); + if (debug_file != NULL) debug_printf("%s\n", s1); + if (log_stderr != NULL && log_stderr != debug_file) + fprintf(log_stderr, "%s\n", s1); + } +if (receive_call_bombout) receive_bomb_out(s2); /* does not return */ +if (smtp_input) smtp_closedown(s2); +exim_exit(EXIT_FAILURE); +} + + + +/************************************************* +* Create a log file * +*************************************************/ + +/* This function is called to create and open a log file. It may be called in a +subprocess when the original process is root. + +Arguments: + name the file name + +The file name has been build in a working buffer, so it is permissible to +overwrite it temporarily if it is necessary to create the directory. + +Returns: a file descriptor, or < 0 on failure (errno set) +*/ + +static int +create_log(uschar *name) +{ +int fd = Uopen(name, O_CREAT|O_APPEND|O_WRONLY, LOG_MODE); + +/* If creation failed, attempt to build a log directory in case that is the +problem. */ + +if (fd < 0 && errno == ENOENT) + { + BOOL created; + uschar *lastslash = Ustrrchr(name, '/'); + *lastslash = 0; + created = directory_make(NULL, name, LOG_DIRECTORY_MODE, FALSE); + DEBUG(D_any) debug_printf("%s log directory %s\n", + created? "created" : "failed to create", name); + *lastslash = '/'; + if (created) fd = Uopen(name, O_CREAT|O_APPEND|O_WRONLY, LOG_MODE); + } + +return fd; +} + + + + +/************************************************* +* Open a log file * +*************************************************/ + +/* This function opens one of a number of logs, which all (except for the +"process log") reside in the same directory, creating the directory if it does +not exist. This may be called recursively on failure, in order to open the +panic log. + +The directory is in the static variable file_path. This is static so that it +the work of sorting out the path is done just once per Exim process. + +Exim is normally configured to avoid running as root wherever possible, the log +files must be owned by the non-privileged exim user. To ensure this, first try +an open without O_CREAT - most of the time this will succeed. If it fails, try +to create the file; if running as root, this must be done in a subprocess to +avoid races. + +Arguments: + fd where to return the resulting file descriptor + type lt_main, lt_reject, lt_panic, or lt_process + +Returns: nothing +*/ + +static void +open_log(int *fd, int type) +{ +uid_t euid; +BOOL ok; +uschar buffer[LOG_NAME_SIZE]; + +/* Sort out the file name. This depends on the type of log we are opening. The +process "log" is written in the spool directory by default, but a path name can +be specified in the configuration. */ + +if (type == lt_process) + { + if (process_log_path == NULL) + ok = string_format(buffer, sizeof(buffer), "%s/exim-process.info", + spool_directory); + else + ok = string_format(buffer, sizeof(buffer), "%s", process_log_path); + } + +/* The names of the other three logs are controlled by file_path. The panic log +is written to the same directory as the main and reject logs, but its name does +not have a datestamp. The use of datestamps is indicated by %D in file_path. +When opening the panic log, if %D is present, we remove the datestamp from the +generated name; if it is at the start, remove a following non-alphameric +character as well; otherwise, remove a preceding non-alphameric character. This +is definitely kludgy, but it sort of does what people want, I hope. */ + +else + { + ok = string_format(buffer, sizeof(buffer), CS file_path, log_names[type]); + + /* Save the name of the mainlog for rollover processing. Without a datestamp, + it gets statted to see if it has been cycled. With a datestamp, the datestamp + will be compared. The static slot for saving it is the same size as buffer, + and the text has been checked above to fit, so this use of strcpy() is OK. */ + + if (type == lt_main) + { + Ustrcpy(mainlog_name, buffer); + mainlog_datestamp = mainlog_name + string_datestamp_offset; + } + + /* Ditto for the reject log */ + + else if (type == lt_reject) + { + Ustrcpy(rejectlog_name, buffer); + rejectlog_datestamp = rejectlog_name + string_datestamp_offset; + } + + /* Remove any datestamp if this is the panic log. This is rare, so there's no + need to optimize getting the datestamp length. We remove one non-alphanumeric + char afterwards if at the start, otherwise one before. */ + + else if (string_datestamp_offset >= 0) + { + uschar *from = buffer + string_datestamp_offset; + uschar *to = from + Ustrlen(tod_stamp(tod_log_datestamp)); + if (from == buffer || from[-1] == '/') + { + if (!isalnum(*to)) to++; + } + else + { + if (!isalnum(from[-1])) from--; + } + + /* This strcpy is ok, because we know that to is a substring of from. */ + + Ustrcpy(from, to); + } + } + +/* If the file name is too long, it is an unrecoverable disaster */ + +if (!ok) + { + die(US"exim: log file path too long: aborting", + US"Logging failure; please try later"); + } + +/* We now have the file name. Try to open an existing file. After a successful +open, arrange for automatic closure on exec(), and then return. */ + +*fd = Uopen(buffer, O_APPEND|O_WRONLY, LOG_MODE); + +if (*fd >= 0) + { + fcntl(*fd, F_SETFD, fcntl(*fd, F_GETFD) | FD_CLOEXEC); + return; + } + +/* Open was not successful: try creating the file. If this is a root process, +we must do the creating in a subprocess set to exim:exim in order to ensure +that the file is created with the right ownership. Otherwise, there can be a +race if an exim process is trying to write to the log at the same time. The use +of SIGUSR1 by the exiwhat utility can provoke a lot of simultaneous writing. */ + +euid = geteuid(); + +/* If we are already running as the Exim user (even if that user is root), +we can go ahead and create in the current process. */ + +if (euid == exim_uid) *fd = create_log(buffer); + +/* Otherwise, if we are root, do the creation in an exim:exim subprocess. If we +are neither exim nor root, creation is not attempted. */ + +else if (euid == root_uid) + { + int status; + pid_t pid = fork(); + + /* In the subprocess, change uid/gid and do the creation. Return 0 from the + subprocess on success. There doesn't seem much point in testing for setgid + and setuid errors. */ + + if (pid == 0) + { + (void)setgid(exim_gid); + (void)setuid(exim_uid); + _exit((create_log(buffer) < 0)? 1 : 0); + } + + /* Wait for the subprocess. If it succeeded retry the open. */ + + while (waitpid(pid, &status, 0) != pid); + if (status == 0) *fd = Uopen(buffer, O_APPEND|O_WRONLY, LOG_MODE); + } + +/* If we now have an open file, set the close-on-exec flag and return. */ + +if (*fd >= 0) + { + fcntl(*fd, F_SETFD, fcntl(*fd, F_GETFD) | FD_CLOEXEC); + return; + } + +/* Creation failed. There are some circumstances in which we get here when +the effective uid is not root or exim, which is the problem. (For example, a +non-setuid binary with log_arguments set, called in certain ways.) Rather than +just bombing out, force the log to stderr and carry on if stderr is available. +*/ + +if (euid != root_uid && euid != exim_uid && log_stderr != NULL) + { + *fd = fileno(log_stderr); + return; + } + +/* Otherwise this is a disaster. This call is deliberately ONLY to the panic +log. If possible, save a copy of the original line that was being logged. If we +are recursing (can't open the panic log either), the pointer will already be +set. */ + +if (panic_save_buffer == NULL) + { + panic_save_buffer = (uschar *)malloc(LOG_BUFFER_SIZE); + if (panic_save_buffer != NULL) + memcpy(panic_save_buffer, log_buffer, LOG_BUFFER_SIZE); + } + +log_write(0, LOG_PANIC_DIE, "Cannot open %s log file \"%s\": %s: " + "euid=%d egid=%d", log_names[type], buffer, strerror(errno), euid, getegid()); +/* Never returns */ +} + + + +/************************************************* +* Add configuration file info to log line * +*************************************************/ + +/* This is put in a function because it's needed twice (once for debugging, +once for real). + +Arguments: + ptr pointer to the end of the line we are building + flags log flags + +Returns: updated pointer +*/ + +static uschar * +log_config_info(uschar *ptr, int flags) +{ +Ustrcpy(ptr, "Exim configuration error"); +ptr += 24; + +if ((flags & (LOG_CONFIG_FOR & ~LOG_CONFIG)) != 0) + { + Ustrcpy(ptr, " for "); + return ptr + 5; + } + +if ((flags & (LOG_CONFIG_IN & ~LOG_CONFIG)) != 0) + { + sprintf(CS ptr, " in line %d of %s", config_lineno, config_filename); + while (*ptr) ptr++; + } + +Ustrcpy(ptr, ":\n "); +return ptr + 4; +} + + +/************************************************* +* A write() operation failed * +*************************************************/ + +/* This function is called when write() fails on anything other than the panic +log, which can happen if a disk gets full or a file gets too large or whatever. +We try to save the relevant message in the panic_save buffer before crashing +out. + +Arguments: + name the name of the log being written + length the string length being written + rc the return value from write() + +Returns: does not return +*/ + +static void +log_write_failed(uschar *name, int length, int rc) +{ +int save_errno = errno; + +if (panic_save_buffer == NULL) + { + panic_save_buffer = (uschar *)malloc(LOG_BUFFER_SIZE); + if (panic_save_buffer != NULL) + memcpy(panic_save_buffer, log_buffer, LOG_BUFFER_SIZE); + } + +log_write(0, LOG_PANIC_DIE, "failed to write to %s: length=%d result=%d " + "errno=%d (%s)", name, length, rc, save_errno, + (save_errno == 0)? "write incomplete" : strerror(save_errno)); +/* Never returns */ +} + + + +/************************************************* +* Write message to log file * +*************************************************/ + +/* Exim can be configured to log to local files, or use syslog, or both. This +is controlled by the setting of log_file_path. The following cases are +recognized: + + log_file_path = "" write files in the spool/log directory + log_file_path = "xxx" write files in the xxx directory + log_file_path = "syslog" write to syslog + log_file_path = "syslog : xxx" write to syslog and to files (any order) + +The one exception to this is messages containing LOG_PROCESS. These are always +written to exim-process.info in the spool directory. They aren't really log +messages in the same sense as the others. + +The message always gets '\n' added on the end of it, since more than one +process may be writing to the log at once and we don't want intermingling to +happen in the middle of lines. To be absolutely sure of this we write the data +into a private buffer and then put it out in a single write() call. + +The flags determine which log(s) the message is written to, or for syslogging, +which priority to use, and in the case of the panic log, whether the process +should die afterwards. + +The variable really_exim is TRUE only when exim is running in privileged state +(i.e. not with a changed configuration or with testing options such as -brw). +If it is not, don't try to write to the log because permission will probably be +denied. + +Avoid actually writing to the logs when exim is called with -bv or -bt to +test an address, but take other actions, such as panicing. + +In Exim proper, the buffer for building the message is got at start-up, so that +nothing gets done if it can't be got. However, some functions that are also +used in utilities occasionally obey log_write calls in error situations, and it +is simplest to put a single malloc() here rather than put one in each utility. +Malloc is used directly because the store functions may call log_write(). + +If a message_id exists, we include it after the timestamp. + +Arguments: + selector write to main log or LOG_INFO only if this value is zero, or if + its bit is set in log_write_selector + flags each bit indicates some independent action: + LOG_SENDER add raw sender to the message + LOG_RECIPIENTS add raw recipients list to message + LOG_CONFIG add "Exim configuration error" + LOG_CONFIG_FOR add " for " instead of ":\n " + LOG_CONFIG_IN add " in line x[ of file y]" + LOG_MAIN write to main log or syslog LOG_INFO + LOG_REJECT write to reject log or syslog LOG_NOTICE + LOG_PANIC write to panic log or syslog LOG_ALERT + LOG_PANIC_DIE write to panic log or LOG_ALERT and then crash + LOG_PROCESS write to process log (always a file) + format a printf() format + ... arguments for format + +Returns: nothing +*/ + +void +log_write(unsigned int selector, int flags, char *format, ...) +{ +uschar *ptr; +int length, rc; +int paniclogfd; +va_list ap; + +/* If panic_recurseflag is set, we have failed to open the panic log. This is +the ultimate disaster. First try to write the message to a debug file and/or +stderr and also to syslog. If panic_save_buffer is not NULL, it contains the +original log line that caused the problem. Afterwards, expire. */ + +if (panic_recurseflag) + { + uschar *extra = (panic_save_buffer == NULL)? US"" : panic_save_buffer; + if (debug_file != NULL) debug_printf("%s%s", extra, log_buffer); + if (log_stderr != NULL && log_stderr != debug_file) + fprintf(log_stderr, "%s%s", extra, log_buffer); + if (*extra != 0) write_syslog(LOG_CRIT, extra); + write_syslog(LOG_CRIT, log_buffer); + die(US"exim: could not open panic log - aborting: see message(s) above", + US"Unexpected log failure, please try later"); + } + +/* Ensure we have a buffer (see comment above); this should never be obeyed +when running Exim proper, only when running utilities. */ + +if (log_buffer == NULL) + { + log_buffer = (uschar *)malloc(LOG_BUFFER_SIZE); + if (log_buffer == NULL) + { + fprintf(stderr, "exim: failed to get store for log buffer\n"); + exim_exit(EXIT_FAILURE); + } + } + +/* If we haven't already done so, inspect the setting of log_file_path to +determine whether to log to files and/or to syslog. Bits in logging_mode +control this, and for file logging, the path must end up in file_path. This +variable must be in permanent store because it may be required again later in +the process. */ + +if (!path_inspected) + { + BOOL multiple = FALSE; + int old_pool = store_pool; + + store_pool = POOL_PERM; + + /* If nothing has been set, don't waste effort... the default values for the + statics are file_path="" and logging_mode = LOG_MODE_FILE. */ + + if (log_file_path[0] != 0) + { + int sep = ':'; /* Fixed separator - outside use */ + uschar *s; + uschar *ss = log_file_path; + logging_mode = 0; + while ((s = string_nextinlist(&ss,&sep,log_buffer,LOG_BUFFER_SIZE)) != NULL) + { + if (Ustrcmp(s, "syslog") == 0) + logging_mode |= LOG_MODE_SYSLOG; + else if ((logging_mode & LOG_MODE_FILE) != 0) multiple = TRUE; + else + { + logging_mode |= LOG_MODE_FILE; + + /* If a non-empty path is given, use it */ + + if (s[0] != 0) + { + file_path = string_copy(s); + } + + /* If the path is empty, we want to use the first non-empty, non- + syslog item in LOG_FILE_PATH, if there is one, since the value of + log_file_path may have been set at runtime. If there is no such item, + use the ultimate default in the spool directory. */ + + else + { + uschar *t; + uschar *tt = US LOG_FILE_PATH; + while ((t = string_nextinlist(&tt,&sep,log_buffer,LOG_BUFFER_SIZE)) + != NULL) + { + if (Ustrcmp(t, "syslog") == 0 || t[0] == 0) continue; + file_path = string_copy(t); + break; + } + } /* Empty item in log_file_path */ + } /* First non-syslog item in log_file_path */ + } /* Scan of log_file_path */ + } + + /* If no modes have been selected, it is a major disaster */ + + if (logging_mode == 0) + die(US"Neither syslog nor file logging set in log_file_path", + US"Unexpected logging failure"); + + /* Set up the ultimate default if necessary. Then revert to the old store + pool, and record that we've sorted out the path. */ + + if ((logging_mode & LOG_MODE_FILE) != 0 && file_path[0] == 0) + file_path = string_sprintf("%s/log/%%slog", spool_directory); + store_pool = old_pool; + path_inspected = TRUE; + + /* If more than one file path was given, log a complaint. This recursive call + should work since we have now set up the routing. */ + + if (multiple) + { + log_write(0, LOG_MAIN|LOG_PANIC, + "More than one path given in log_file_path: using %s", file_path); + } + } + +/* If debugging, show all log entries, but don't show headers. Do it all +in one go so that it doesn't get split when multi-processing. */ + +DEBUG(D_any|D_v) + { + int i; + ptr = log_buffer; + + Ustrcpy(ptr, "LOG:"); + ptr += 4; + + /* Show the options that were passed into the call. These are those whose + flag values do not have the 0x80000000 bit in them. Note that this + automatically exclude the "all" setting. */ + + for (i = 0; i < log_options_count; i++) + { + unsigned int bit = log_options[i].bit; + if ((bit & 0x80000000) != 0) continue; + if ((selector & bit) != 0) + { + *ptr++ = ' '; + Ustrcpy(ptr, log_options[i].name); + while (*ptr) ptr++; + } + } + + sprintf(CS ptr, "%s%s%s%s%s\n ", + ((flags & LOG_MAIN) != 0)? " MAIN" : "", + ((flags & LOG_PANIC) != 0)? " PANIC" : "", + ((flags & LOG_PANIC_DIE) == LOG_PANIC_DIE)? " DIE" : "", + ((flags & LOG_PROCESS) != 0)? " PROCESS": "", + ((flags & LOG_REJECT) != 0)? " REJECT" : ""); + + while(*ptr) ptr++; + if ((flags & LOG_CONFIG) != 0) ptr = log_config_info(ptr, flags); + + va_start(ap, format); + if (!string_vformat(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer)-1, format, ap)) + Ustrcpy(ptr, "**** log string overflowed log buffer ****"); + va_end(ap); + + while(*ptr) ptr++; + Ustrcat(ptr, "\n"); + debug_printf("%s", log_buffer); + } + +/* If no log file is specified, we are in a mess. */ + +if ((flags & (LOG_MAIN|LOG_PANIC|LOG_REJECT|LOG_PROCESS)) == 0) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "log_write called with no log " + "flags set"); + +/* There are some weird circumstances in which logging is disabled. */ + +if (disable_logging) + { + DEBUG(D_any) debug_printf("log writing disabled\n"); + return; + } + +/* Create the main message in the log buffer, including the message +id except for the process log and when called by a utility. */ + +ptr = log_buffer; +if (really_exim && (flags & LOG_PROCESS) == 0 && message_id[0] != 0) + sprintf(CS ptr, "%s %s ", tod_stamp(tod_log), message_id); +else sprintf(CS ptr, "%s ", tod_stamp(tod_log)); + +while(*ptr) ptr++; +if ((flags & LOG_CONFIG) != 0) ptr = log_config_info(ptr, flags); + +va_start(ap, format); +if (!string_vformat(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer)-1, format, ap)) + Ustrcpy(ptr, "**** log string overflowed log buffer ****\n"); +while(*ptr) ptr++; +va_end(ap); + +/* Add the raw, unrewritten, sender to the message if required. This is done +this way because it kind of fits with LOG_RECIPIENTS. */ + +if ((flags & LOG_SENDER) != 0 && + ptr < log_buffer + LOG_BUFFER_SIZE - 8 - Ustrlen(raw_sender)) + { + sprintf(CS ptr, " from <%s>", raw_sender); + while (*ptr) ptr++; + } + +/* Add list of recipients to the message if required; the raw list, +before rewriting, was saved in raw_recipients. There may be none, if an ACL +discarded them all. */ + +if ((flags & LOG_RECIPIENTS) != 0 && ptr < log_buffer + LOG_BUFFER_SIZE - 6 && + raw_recipients_count > 0) + { + int i; + sprintf(CS ptr, " for"); + while (*ptr) ptr++; + for (i = 0; i < raw_recipients_count; i++) + { + uschar *s = raw_recipients[i]; + if (log_buffer + LOG_BUFFER_SIZE - ptr < Ustrlen(s) + 3) break; + sprintf(CS ptr, " %s", s); + while (*ptr) ptr++; + } + } + +sprintf(CS ptr, "\n"); +while(*ptr) ptr++; +length = ptr - log_buffer; + +/* Handle loggable errors when running a utility, or when address testing. +Write to log_stderr unless debugging (when it will already have been written), +or unless there is no log_stderr (expn called from daemon, for example). */ + +if (!really_exim || log_testing_mode) + { + if (debug_selector == 0 && log_stderr != NULL && + (selector == 0 || (selector & log_write_selector) != 0)) + { + if (host_checking) + fprintf(log_stderr, "LOG: %s", CS(log_buffer + 20)); /* no timestamp */ + else + fprintf(log_stderr, "%s", CS log_buffer); + } + if ((flags & LOG_PANIC_DIE) == LOG_PANIC_DIE) exim_exit(EXIT_FAILURE); + return; + } + +/* Handle the main log. We know that either syslog or file logging (or both) is +set up. A real file gets left open during reception or delivery once it has +been opened, but we don't want to keep on writing to it for too long after it +has been renamed. Therefore, do a stat() and see if the inode has changed, and +if so, re-open. */ + +if ((flags & LOG_MAIN) != 0 && + (selector == 0 || (selector & log_write_selector) != 0)) + { + if ((logging_mode & LOG_MODE_SYSLOG) != 0 && + (syslog_duplication || (flags & (LOG_REJECT|LOG_PANIC)) == 0)) + write_syslog(LOG_INFO, log_buffer); + + if ((logging_mode & LOG_MODE_FILE) != 0) + { + struct stat statbuf; + + /* Check for a change to the mainlog file name when datestamping is in + operation. This happens at midnight, at which point we want to roll over + the file. Closing it has the desired effect. */ + + if (mainlog_datestamp != NULL) + { + uschar *nowstamp = tod_stamp(tod_log_datestamp); + if (Ustrncmp (mainlog_datestamp, nowstamp, Ustrlen(nowstamp)) != 0) + { + close(mainlogfd); /* Close the file */ + mainlogfd = -1; /* Clear the file descriptor */ + mainlog_inode = 0; /* Unset the inode */ + mainlog_datestamp = NULL; /* Clear the datestamp */ + } + } + + /* Otherwise, we want to check whether the file has been renamed by a + cycling script. This could be "if else", but for safety's sake, leave it as + "if" so that renaming the log starts a new file even when datestamping is + happening. */ + + if (mainlogfd >= 0) + { + if (Ustat(mainlog_name, &statbuf) < 0 || statbuf.st_ino != mainlog_inode) + { + close(mainlogfd); + mainlogfd = -1; + mainlog_inode = 0; + } + } + + /* If the log is closed, open it. Then write the line. */ + + if (mainlogfd < 0) + { + open_log(&mainlogfd, lt_main); /* No return on error */ + if (fstat(mainlogfd, &statbuf) >= 0) mainlog_inode = statbuf.st_ino; + } + + /* Failing to write to the log is disastrous */ + + if ((rc = write(mainlogfd, log_buffer, length)) != length) + { + log_write_failed(US"main log", length, rc); + /* That function does not return */ + } + } + } + +/* Handle the log for rejected messages. This can be globally disabled. If +there are any header lines (i.e. if the rejection is happening after the DATA +phase), log the recipients and the headers. */ + +if (write_rejectlog && (flags & LOG_REJECT) != 0) + { + header_line *h; + + if (header_list != NULL && (log_extra_selector & LX_rejected_header) != 0) + { + if (recipients_count > 0) + { + int i; + + /* List the sender */ + + string_format(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer), + "Envelope-from: <%s>\n", sender_address); + while (*ptr) ptr++; + + /* List up to 5 recipients */ + + string_format(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer), + "Envelope-to: <%s>\n", recipients_list[0].address); + while (*ptr) ptr++; + + for (i = 1; i < recipients_count && i < 5; i++) + { + string_format(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer), " <%s>\n", + recipients_list[i].address); + while (*ptr) ptr++; + } + + if (i < recipients_count) + { + (void)string_format(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer), + " ...\n"); + while (*ptr) ptr++; + } + } + + /* A header with a NULL text is an unfilled in Received: header */ + + for (h = header_list; h != NULL; h = h->next) + { + BOOL fitted; + if (h->text == NULL) continue; + fitted = string_format(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer), + "%c %s", h->type, h->text); + while(*ptr) ptr++; + if (!fitted) /* Buffer is full; truncate */ + { + ptr -= 100; /* For message and separator */ + if (ptr[-1] == '\n') ptr--; + Ustrcpy(ptr, "\n*** truncated ***\n"); + while (*ptr) ptr++; + break; + } + } + + length = ptr - log_buffer; + } + + /* Write to syslog or to a log file */ + + if ((logging_mode & LOG_MODE_SYSLOG) != 0 && + (syslog_duplication || (flags & LOG_PANIC) == 0)) + write_syslog(LOG_NOTICE, log_buffer); + + /* Check for a change to the rejectlog file name when datestamping is in + operation. This happens at midnight, at which point we want to roll over + the file. Closing it has the desired effect. */ + + if ((logging_mode & LOG_MODE_FILE) != 0) + { + struct stat statbuf; + + if (rejectlog_datestamp != NULL) + { + uschar *nowstamp = tod_stamp(tod_log_datestamp); + if (Ustrncmp (rejectlog_datestamp, nowstamp, Ustrlen(nowstamp)) != 0) + { + close(rejectlogfd); /* Close the file */ + rejectlogfd = -1; /* Clear the file descriptor */ + rejectlog_inode = 0; /* Unset the inode */ + rejectlog_datestamp = NULL; /* Clear the datestamp */ + } + } + + /* Otherwise, we want to check whether the file has been renamed by a + cycling script. This could be "if else", but for safety's sake, leave it as + "if" so that renaming the log starts a new file even when datestamping is + happening. */ + + if (rejectlogfd >= 0) + { + if (Ustat(rejectlog_name, &statbuf) < 0 || + statbuf.st_ino != rejectlog_inode) + { + close(rejectlogfd); + rejectlogfd = -1; + rejectlog_inode = 0; + } + } + + /* Open the file if necessary, and write the data */ + + if (rejectlogfd < 0) + { + open_log(&rejectlogfd, lt_reject); /* No return on error */ + if (fstat(rejectlogfd, &statbuf) >= 0) rejectlog_inode = statbuf.st_ino; + } + + if ((rc = write(rejectlogfd, log_buffer, length)) != length) + { + log_write_failed(US"reject log", length, rc); + /* That function does not return */ + } + } + } + + +/* Handle the process log file, where exim processes can be made to dump +details of what they are doing by sending them a USR1 signal. Note that +a message id is not automatically added above. This information is always +written to a file - never to syslog. */ + +if ((flags & LOG_PROCESS) != 0) + { + int processlogfd; + open_log(&processlogfd, lt_process); /* No return on error */ + if ((rc = write(processlogfd, log_buffer, length)) != length) + { + log_write_failed(US"process log", length, rc); + /* That function does not return */ + } + (void)close(processlogfd); + } + + +/* Handle the panic log, which is not kept open like the others. If it fails to +open, there will be a recursive call to log_write(). We detect this above and +attempt to write to the system log as a last-ditch try at telling somebody. In +all cases, try to write to log_stderr. */ + +if ((flags & LOG_PANIC) != 0) + { + if (log_stderr != NULL && log_stderr != debug_file) + fprintf(log_stderr, "%s", CS log_buffer); + + if ((logging_mode & LOG_MODE_SYSLOG) != 0) + { + write_syslog(LOG_ALERT, log_buffer); + } + + /* If this panic logging was caused by a failure to open the main log, + the original log line is in panic_save_buffer. Make an attempt to write it. */ + + if ((logging_mode & LOG_MODE_FILE) != 0) + { + panic_recurseflag = TRUE; + open_log(&paniclogfd, lt_panic); /* Won't return on failure */ + panic_recurseflag = FALSE; + + if (panic_save_buffer != NULL) + (void) write(paniclogfd, panic_save_buffer, Ustrlen(panic_save_buffer)); + + if ((rc = write(paniclogfd, log_buffer, length)) != length) + { + int save_errno = errno; + write_syslog(LOG_CRIT, log_buffer); + sprintf(CS log_buffer, "write failed on panic log: length=%d result=%d " + "errno=%d (%s)", length, rc, save_errno, strerror(save_errno)); + write_syslog(LOG_CRIT, log_buffer); + flags |= LOG_PANIC_DIE; + } + + close(paniclogfd); + } + + /* Give up if the DIE flag is set */ + + if ((flags & LOG_PANIC_DIE) != LOG_PANIC) + die(NULL, US"Unexpected failure, please try later"); + } +} + + + +/************************************************* +* Close any open log files * +*************************************************/ + +void +log_close_all(void) +{ +if (mainlogfd >= 0) + { close(mainlogfd); mainlogfd = -1; } +if (rejectlogfd >= 0) + { close(rejectlogfd); rejectlogfd = -1; } +closelog(); +syslog_open = FALSE; +} + +/* End of log.c */ diff --git a/src/src/lss.c b/src/src/lss.c new file mode 100644 index 000000000..1d1ab8b61 --- /dev/null +++ b/src/src/lss.c @@ -0,0 +1,144 @@ +/* $Cambridge: exim/src/src/lss.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Support functions for calling from local_scan(). These are mostly just +wrappers for various internal functions. */ + + +#include "exim.h" + + +/************************************************* +* Match a domain in a list * +*************************************************/ + +/* +Arguments: + domain the domain we are testing + list the domain list + +Returns: OK/FAIL/DEFER +*/ + +int +lss_match_domain(uschar *domain, uschar *list) +{ +return match_isinlist(domain, &list, 0, &domainlist_anchor, NULL, MCL_DOMAIN, + TRUE, NULL); +} + + + +/************************************************* +* Match a local part in a list * +*************************************************/ + +/* +Arguments: + local_part the local part we are testing + list the local part list + caseless TRUE for caseless matching + +Returns: OK/FAIL/DEFER +*/ + +int +lss_match_local_part(uschar *local_part, uschar *list, BOOL caseless) +{ +return match_isinlist(local_part, &list, 0, &localpartlist_anchor, NULL, + MCL_LOCALPART, caseless, NULL); +} + + + +/************************************************* +* Match an address in a list * +*************************************************/ + +/* +Arguments: + address the address we are testing + list the address list + caseless TRUE for caseless matching + +Returns: OK/FAIL/DEFER +*/ + +int +lss_match_address(uschar *address, uschar *list, BOOL caseless) +{ +return match_address_list(address, caseless, TRUE, &list, NULL, -1, 0, NULL); +} + + + +/************************************************* +* Match a host in a list * +*************************************************/ + +/* +Arguments: + host name the name of the host we are testing, or NULL if this is the + sender host and its name hasn't yet been looked up + host address the IP address of the host, or an empty string for a local + message + list the host list + +Returns: OK/FAIL/DEFER + ERROR if failed to find host name when needed +*/ + +int +lss_match_host(uschar *host_name, uschar *host_address, uschar *list) +{ +return verify_check_this_host(&list, NULL, host_name, host_address, NULL); +} + + + +/************************************************* +* Base 64 encode/decode * +*************************************************/ + +/* These functions just give less "internal" names to the functions. + +Arguments: + clear points to the clear text bytes + len the number of bytes to encode + +Returns: a pointer to the zero-terminated base 64 string, which + is in working store +*/ + +uschar * +lss_b64encode(uschar *clear, int len) +{ +return auth_b64encode(clear, len); +} + +/* +Arguments: + code points to the coded string, zero-terminated + ptr where to put the pointer to the result, which is in + dynamic store + +Returns: the number of bytes in the result, + or -1 if the input was malformed + +A zero is added on to the end to make it easy in cases where the result is to +be interpreted as text. This is not included in the count. */ + +int +lss_b64decode(uschar *code, uschar **ptr) +{ +return auth_b64decode(code, ptr); +} + + +/* End of lss.c */ diff --git a/src/src/macros.h b/src/src/macros.h new file mode 100644 index 000000000..e21fb8b9f --- /dev/null +++ b/src/src/macros.h @@ -0,0 +1,777 @@ +/* $Cambridge: exim/src/src/macros.h,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + + +/* These two macros make it possible to obtain the result of macro-expanding +a string as a text string. This is sometimes useful for debugging output. */ + +#define mac_string(s) # s +#define mac_expanded_string(s) mac_string(s) + + +/* The address_item structure has a word full of 1-bit flags. These macros +manipulate them. */ + +#define setflag(addr,flag) addr->flags |= (flag) +#define clearflag(addr,flag) addr->flags &= ~(flag) + +#define testflag(addr,flag) ((addr->flags & (flag)) != 0) +#define testflagsall(addr,flag) ((addr->flags & (flag)) == (flag)) + +#define copyflag(addrnew,addrold,flag) \ + addrnew->flags = (addrnew->flags & ~(flag)) | (addrold->flags & (flag)) + +#define orflag(addrnew,addrold,flag) \ + addrnew->flags |= addrold->flags & (flag) + + +/* For almost all calls to convert things to printing characters, we want to +allow tabs. A macro just makes life a bit easier. */ + +#define string_printing(s) string_printing2((s), TRUE) + + +/* We need a special return code for "no recipients and failed to send an error +message". ANSI C defines only EXIT_FAILURE and EXIT_SUCCESS. On the assumption +that these are always 1 and 0 on Unix systems ... */ + +#define EXIT_NORECIPIENTS 2 + + +/* Character-handling macros. It seems that the set of standard functions in +ctype.h aren't actually all that useful. One reason for this is that email is +international, so the concept of using a locale to vary what they do is not +helpful. Another problem is that in different operating systems, the libraries +yield different results, even in the default locale. For example, Linux yields +TRUE for iscntrl() for all characters > 127, whereas many other systems yield +FALSE. For these reasons we define our own set of macros for a number of +character testing functions. Ensure that all these tests treat their arguments +as unsigned. */ + +#define mac_iscntrl(c) \ + ((uschar)(c) < 32 || (uschar)(c) == 127) + +#define mac_iscntrl_or_special(c) \ + ((uschar)(c) < 32 || strchr(" ()<>@,;:\\\".[]\177", (uschar)(c)) != NULL) + +#define mac_isgraph(c) \ + ((uschar)(c) > 32 && (uschar)(c) != 127) + +#define mac_isprint(c) \ + (((uschar)(c) >= 32 && (uschar)(c) <= 126) || c == '\t' || \ + ((uschar)(c) > 127 && print_topbitchars)) + + +/* When built with TLS support, the act of flushing SMTP output becomes +a no-op once an SSL session is in progress. */ + +#ifdef SUPPORT_TLS +#define mac_smtp_fflush() if (!tls_active) fflush(smtp_out); +#else +#define mac_smtp_fflush() fflush(smtp_out); +#endif + + +/* Define which ends of pipes are for reading and writing, as some systems +don't make the file descriptors two-way. */ + +#define pipe_read 0 +#define pipe_write 1 + +/* The RFC 1413 ident port */ + +#define IDENT_PORT 113 + +/* A macro to simplify testing bits in lookup types */ + +#define mac_islookup(a,b) ((lookup_list[a].type & (b)) != 0) + +/* Debugging control */ + +#define DEBUG(x) if ((debug_selector & (x)) != 0) +#define HDEBUG(x) if (host_checking || (debug_selector & (x)) != 0) + +/* The size of the vector for saving/restoring address expansion pointers while +verifying. This has to be explicit because it is referenced in more than one +source module. */ + +#define ADDRESS_EXPANSIONS_COUNT 18 + +/* The maximum permitted number of command-line (-D) macro definitions. We +need a limit only to make it easier to generate argument vectors for re-exec +of Exim. */ + +#define MAX_CLMACROS 10 + +/* The number of integer variables available in filter files. If this is +changed, then the tables in expand.c for accessing them must be changed too. */ + +#define FILTER_VARIABLE_COUNT 10 + +/* The size of the vector holding delay warning times */ + +#define DELAY_WARNING_SIZE 12 + +/* The size of the buffer holding the processing information string. */ + +#define PROCESS_INFO_SIZE 256 + +/* The size of buffer to get for constructing log entries. Make it big +enough to hold all the headers from a normal kind of message. */ + +#define LOG_BUFFER_SIZE 8192 + +/* The initial size of a big buffer for use in various places. It gets put +into big_buffer_size and in some circumstances increased. It should be at least +as long as the maximum path length. */ + +#if defined PATH_MAX && PATH_MAX > 1024 +#define BIG_BUFFER_SIZE PATH_MAX +#elif defined MAXPATHLEN && MAXPATHLEN > 1024 +#define BIG_BUFFER_SIZE MAXPATHLEN +#else +#define BIG_BUFFER_SIZE 1024 +#endif + +/* This limits the length of data returned by local_scan(). Because it is +written on the spool, it gets read into big_buffer. */ + +#define LOCAL_SCAN_MAX_RETURN (BIG_BUFFER_SIZE - 24) + +/* A limit to the length of an address. RFC 2821 limits the local part to 64 +and the domain to 255, so this should be adequate, taking into account quotings +etc. */ + +#define ADDRESS_MAXLENGTH 512 + +/* The length of the base names of spool files, which consist of an internal +message id with a trailing "-H" or "-D" added. */ + +#define SPOOL_NAME_LENGTH (MESSAGE_ID_LENGTH+2) + +/* The maximum number of message ids to store in a waiting database +record. */ + +#define WAIT_NAME_MAX 50 + +/* The numbers of connection and message variables for ACLs */ + +#define ACL_C_MAX 10 +#define ACL_M_MAX 10 + +/* Fixed option values for all PCRE functions */ + +#define PCRE_COPT 0 /* compile */ +#define PCRE_EOPT 0 /* exec */ + +/* Macros for trivial functions */ + +#define mac_ismsgid(s) \ + (pcre_exec(regex_ismsgid,NULL,CS s,Ustrlen(s),0,PCRE_EOPT,NULL,0) >= 0) + + +/* Options for dns_next_rr */ + +enum { RESET_NEXT, RESET_ANSWERS, RESET_ADDITIONAL }; + +/* Argument values for the time-of-day function */ + +enum { tod_log, tod_log_bare, tod_log_zone, tod_log_datestamp, + tod_zone, tod_full, tod_bsdin, tod_mbx, tod_epoch, tod_zulu }; + +/* For identifying types of driver */ + +enum { + DTYPE_NONE, + DTYPE_ROUTER, + DTYPE_TRANSPORT +}; + +/* Error numbers for generating error messages when reading a message on the +standard input. */ + +enum { + ERRMESS_BADARGADDRESS, /* Bad address via argument list */ + ERRMESS_BADADDRESS, /* Bad address read via -t */ + ERRMESS_NOADDRESS, /* Message has no addresses */ + ERRMESS_IGADDRESS, /* All -t addresses ignored */ + ERRMESS_BADNOADDRESS, /* Bad address via -t, leaving none */ + ERRMESS_IOERR, /* I/O error while reading a message */ + ERRMESS_VLONGHEADER, /* Excessively long message header */ + ERRMESS_VLONGHDRLINE, /* Excessively long single line in header */ + ERRMESS_TOOBIG, /* Message too big */ + ERRMESS_TOOMANYRECIP, /* Too many recipients */ + ERRMESS_LOCAL_SCAN, /* Rejected by local scan */ + ERRMESS_LOCAL_ACL /* Rejected by non-SMTP ACL */ +}; + +/* Error handling styles - set by option, and apply only when receiving +a local message not via SMTP. */ + +enum { + ERRORS_SENDER, /* Return to sender (default) */ + ERRORS_STDERR /* Write on stderr */ +}; + +/* Exec control values when Exim execs itself via child_exec_exim. */ + +enum { + CEE_RETURN_ARGV, /* Don't exec, just build and return argv */ + CEE_EXEC_EXIT, /* Just exit if exec fails */ + CEE_EXEC_PANIC /* Panic-die if exec fails */ +}; + +/* Returns from the routing, transport and authentication functions (not all +apply to all of them). Some other functions also use these convenient values, +and some additional values are used only by non-driver functions. + +OK, FAIL, DEFER, and ERROR are also declared in local_scan.h for use in the +local_scan() function. Do not change them unilaterally. */ + +#define OK 0 /* Successful match */ +#define DEFER 1 /* Defer - some problem */ +#define FAIL 2 /* Matching failed */ +#define ERROR 3 /* Internal or config error */ +/***********/ +#define DECLINE 4 /* Declined to handle the address, pass to next + router unless no_more is set */ +#define PASS 5 /* Pass to next driver, or to pass_router, + even if no_more is set */ +#define DISCARD 6 /* Address routed to :blackhole: or "seen finish" */ +#define SKIP 7 /* Skip this router (used in route_address only) */ +#define REROUTED 8 /* Address was changed and child created*/ +#define PANIC 9 /* Hard failed with internal error */ +#define BAD64 10 /* Bad base64 data (auth) */ +#define UNEXPECTED 11 /* Unexpected initial auth data */ +#define CANCELLED 12 /* Authentication cancelled */ +#define FAIL_SEND 13 /* send() failed in authenticator */ +#define FAIL_DROP 14 /* Fail and drop connection (used in ACL) */ + +/* Returns from the deliver_message() function */ + +#define DELIVER_ATTEMPTED_NORMAL 0 /* Tried a normal delivery */ +#define DELIVER_MUA_SUCCEEDED 1 /* Success when mua_wrapper is set */ +#define DELIVER_MUA_FAILED 2 /* Failure when mua_wrapper is set */ +#define DELIVER_NOT_ATTEMPTED 3 /* Not tried (no msg or is locked */ + +/* Returns from DNS lookup functions. */ + +enum { DNS_SUCCEED, DNS_NOMATCH, DNS_NODATA, DNS_AGAIN, DNS_FAIL }; + +/* Ending states when reading a message. The order is important. The test +for having to swallow the rest of an SMTP message is whether the value is +>= END_NOTENDED. */ + +#define END_NOTSTARTED 0 /* Message not started */ +#define END_DOT 1 /* Message ended with '.' */ +#define END_EOF 2 /* Message ended with EOF (error for SMTP) */ +#define END_NOTENDED 3 /* Message reading not yet ended */ +#define END_SIZE 4 /* Reading ended because message too big */ +#define END_WERROR 5 /* Write error while reading the message */ + +/* Options bits for debugging; D_v and D_local_scan are also in local_scan.h */ + +#define D_v 0x00000001 +#define D_local_scan 0x00000002 + +#define D_acl 0x00000004 +#define D_auth 0x00000008 +#define D_deliver 0x00000010 +#define D_dns 0x00000020 +#define D_dnsbl 0x00000040 +#define D_exec 0x00000080 +#define D_expand 0x00000100 +#define D_filter 0x00000200 +#define D_hints_lookup 0x00000400 +#define D_host_lookup 0x00000800 +#define D_ident 0x00001000 +#define D_interface 0x00002000 +#define D_lists 0x00004000 +#define D_load 0x00008000 +#define D_lookup 0x00010000 +#define D_memory 0x00020000 +#define D_pid 0x00040000 +#define D_process_info 0x00080000 +#define D_queue_run 0x00100000 +#define D_receive 0x00200000 +#define D_resolver 0x00400000 +#define D_retry 0x00800000 +#define D_rewrite 0x01000000 +#define D_route 0x02000000 +#define D_timestamp 0x04000000 +#define D_tls 0x08000000 +#define D_transport 0x10000000 +#define D_uid 0x20000000 +#define D_verify 0x40000000 + +#define D_all 0xffffffff + +#define D_any (D_all & \ + ~(D_v | \ + D_pid | \ + D_timestamp) ) + +#define D_default (D_all & \ + ~(D_expand | \ + D_filter | \ + D_interface | \ + D_load | \ + D_local_scan | \ + D_memory | \ + D_pid | \ + D_timestamp | \ + D_resolver)) + +/* Options bits for logging. Those that will end up in log_write_selector have +values < 0x80000000. They can be used in calls to log_write(). The others have +values > 0x80000000 and are put into log_extra_selector (without the top bit). +These are only ever tested independently. "All" is a magic value that is used +only in the name table to set all options in both bit maps. */ + +#define L_all 0xffffffff + +#define L_address_rewrite 0x00000001 +#define L_all_parents 0x00000002 +#define L_connection_reject 0x00000004 +#define L_delay_delivery 0x00000008 +#define L_dnslist_defer 0x00000010 +#define L_etrn 0x00000020 +#define L_host_lookup_failed 0x00000040 +#define L_lost_incoming_connection 0x00000080 +#define L_queue_run 0x00000100 +#define L_retry_defer 0x00000200 +#define L_size_reject 0x00000400 +#define L_skip_delivery 0x00000800 +#define L_smtp_connection 0x00001000 +#define L_smtp_incomplete_transaction 0x00002000 +#define L_smtp_protocol_error 0x00004000 +#define L_smtp_syntax_error 0x00008000 + +#define LX_arguments 0x80000001 +#define LX_deliver_time 0x80000002 +#define LX_delivery_size 0x80000004 +#define LX_ident_timeout 0x80000008 +#define LX_incoming_interface 0x80000010 +#define LX_incoming_port 0x80000020 +#define LX_outgoing_port 0x80000040 +#define LX_queue_time 0x80000080 +#define LX_received_sender 0x80000100 +#define LX_received_recipients 0x80000200 +#define LX_rejected_header 0x80000400 +#define LX_return_path_on_delivery 0x80000800 +#define LX_sender_on_delivery 0x80001000 +#define LX_smtp_confirmation 0x80002000 +#define LX_subject 0x80004000 +#define LX_tls_certificate_verified 0x80008000 +#define LX_tls_cipher 0x80010000 +#define LX_tls_peerdn 0x80020000 + +#define L_default (L_connection_reject | \ + L_delay_delivery | \ + L_dnslist_defer | \ + L_etrn | \ + L_host_lookup_failed | \ + L_lost_incoming_connection | \ + L_queue_run | \ + L_retry_defer | \ + L_size_reject | \ + L_skip_delivery) + +#define LX_default ((LX_rejected_header | \ + LX_tls_cipher) & 0x7fffffff) + +/* Private error numbers for delivery failures, set negative so as not +to conflict with system errno values. */ + +#define ERRNO_UNKNOWNERROR (-1) +#define ERRNO_USERSLASH (-2) +#define ERRNO_EXISTRACE (-3) +#define ERRNO_NOTREGULAR (-4) +#define ERRNO_NOTDIRECTORY (-5) +#define ERRNO_BADUGID (-6) +#define ERRNO_BADMODE (-7) +#define ERRNO_INODECHANGED (-8) +#define ERRNO_LOCKFAILED (-9) +#define ERRNO_BADADDRESS2 (-10) +#define ERRNO_FORBIDPIPE (-11) +#define ERRNO_FORBIDFILE (-12) +#define ERRNO_FORBIDREPLY (-13) +#define ERRNO_MISSINGPIPE (-14) +#define ERRNO_MISSINGFILE (-15) +#define ERRNO_MISSINGREPLY (-16) +#define ERRNO_BADREDIRECT (-17) +#define ERRNO_SMTPCLOSED (-18) +#define ERRNO_SMTPFORMAT (-19) +#define ERRNO_SPOOLFORMAT (-20) +#define ERRNO_NOTABSOLUTE (-21) +#define ERRNO_EXIMQUOTA (-22) /* Exim-imposed quota */ +#define ERRNO_HELD (-23) +#define ERRNO_FILTER_FAIL (-24) /* Delivery filter process failure */ +#define ERRNO_CHHEADER_FAIL (-25) /* Delivery add/remove header failure */ +#define ERRNO_WRITEINCOMPLETE (-26) /* Delivery write incomplete error */ +#define ERRNO_EXPANDFAIL (-27) /* Some expansion failed */ +#define ERRNO_GIDFAIL (-28) /* Failed to get gid */ +#define ERRNO_UIDFAIL (-29) /* Failed to get uid */ +#define ERRNO_BADTRANSPORT (-30) /* Unset or non-existent transport */ +#define ERRNO_MBXLENGTH (-31) /* MBX length mismatch */ +#define ERRNO_UNKNOWNHOST (-32) /* Lookup failed in smtp transport */ +#define ERRNO_FORMATUNKNOWN (-33) /* Can't match format in appendfile */ +#define ERRNO_BADCREATE (-34) /* Creation outside home in appendfile */ +#define ERRNO_LISTDEFER (-35) /* Can't check a list; lookup defer */ +#define ERRNO_DNSDEFER (-36) /* DNS lookup defer */ +#define ERRNO_TLSFAILURE (-37) /* Failed to start TLS session */ +#define ERRNO_TLSREQUIRED (-38) /* Mandatory TLS session not started */ +#define ERRNO_CHOWNFAIL (-39) /* Failed to chown a file */ +#define ERRNO_PIPEFAIL (-40) /* Failed to create a pipe */ +#define ERRNO_CALLOUTDEFER (-41) /* When verifying */ +#define ERRNO_AUTHFAIL (-42) /* When required by client */ +#define ERRNO_CONNECTTIMEOUT (-43) /* Used internally in smtp transport */ +#define ERRNO_RCPT4XX (-44) /* RCPT gave 4xx error */ + +/* These must be last, so all retry deferments can easily be identified */ + +#define ERRNO_RETRY_BASE (-51) /* Base to test against */ +#define ERRNO_RRETRY (-51) /* Not time for routing */ +#define ERRNO_LRETRY (-52) /* Not time for local delivery */ +#define ERRNO_HRETRY (-53) /* Not time for any remote host */ +#define ERRNO_LOCAL_ONLY (-54) /* Local-only delivery */ +#define ERRNO_QUEUE_DOMAIN (-55) /* Domain in queue_domains */ + +/* Special actions to take after failure or deferment. */ + +enum { + SPECIAL_NONE, /* No special action */ + SPECIAL_FREEZE, /* Freeze message */ + SPECIAL_FAIL, /* Fail the delivery */ + SPECIAL_WARN /* Send a warning message */ +}; + +/* Flags that get ORed into the more_errno field of an address to give more +information about errors for retry purposes. They are greater than 256, because +the bottom byte contains 'A' or 'M' for remote addresses, to indicate whether +the name was looked up only via an address record or whether MX records were +used, respectively. */ + +#define RTEF_CTOUT 0x0100 /* Connection timed out */ + +/* Permission and other options for parse_extract_addresses(), +filter_interpret(), and rda_interpret(), i.e. what special things are allowed +in redirection operations. Not all apply to all cases. Some of the bits allow +and some forbid, reflecting the "allow" and "forbid" options in the redirect +router, which were chosen to represent the standard situation for users' +.forward files. */ + +#define RDO_BLACKHOLE 0x00000001 /* Forbid :blackhole: */ +#define RDO_DEFER 0x00000002 /* Allow :defer: or "defer" */ +#define RDO_EACCES 0x00000004 /* Ignore EACCES */ +#define RDO_ENOTDIR 0x00000008 /* Ignore ENOTDIR */ +#define RDO_EXISTS 0x00000010 /* Forbid "exists" in expansion in filter */ +#define RDO_FAIL 0x00000020 /* Allow :fail: or "fail" */ +#define RDO_FILTER 0x00000040 /* Allow a filter script */ +#define RDO_FREEZE 0x00000080 /* Allow "freeze" */ +#define RDO_INCLUDE 0x00000100 /* Forbid :include: */ +#define RDO_LOG 0x00000200 /* Forbid "log" */ +#define RDO_LOOKUP 0x00000400 /* Forbid "lookup" in expansion in filter */ +#define RDO_PERL 0x00000800 /* Forbid "perl" in expansion in filter */ +#define RDO_READFILE 0x00001000 /* Forbid "readfile" in exp in filter */ +#define RDO_READSOCK 0x00002000 /* Forbid "readsocket" in exp in filter */ +#define RDO_RUN 0x00004000 /* Forbid "run" in expansion in filter */ +#define RDO_REALLOG 0x00008000 /* Really do log (not testing/verifying) */ +#define RDO_REWRITE 0x00010000 /* Rewrite generated addresses */ + +/* This is the set that apply to expansions in filters */ + +#define RDO_FILTER_EXPANSIONS \ + (RDO_EXISTS|RDO_LOOKUP|RDO_PERL|RDO_READFILE|RDO_READSOCK|RDO_RUN) + +/* As well as the RDO bits themselves, we need the bit numbers in order to +access (most of) the individual bits as separate options. This could be +automated, but I haven't bothered. Keep this list in step with the above! */ + +enum { RDON_BLACKHOLE, RDON_DEFER, RDON_EACCES, RDON_ENOTDIR, RDON_EXISTS, + RDON_FAIL, RDON_FILTER, RDON_FREEZE, RDON_INCLUDE, RDON_LOG, RDON_LOOKUP, + RDON_PERL, RDON_READFILE, RDON_READSOCK, RDON_RUN, RDON_REALLOG, + RDON_REWRITE }; + +/* Results of filter or forward file processing. Some are only from a filter; +some are only from a forward file. */ + +enum { + FF_DELIVERED, /* Success, took significant action */ + FF_NOTDELIVERED, /* Success, didn't take significant action */ + FF_BLACKHOLE, /* Blackholing requested */ + FF_DEFER, /* Defer requested */ + FF_FAIL, /* Fail requested */ + FF_INCLUDEFAIL, /* :include: failed */ + FF_NONEXIST, /* Forward file does not exist */ + FF_FREEZE, /* Freeze requested */ + FF_ERROR /* We have a problem */ +}; + +/* Values for identifying particular headers; printing characters are used, so +they can be read in the spool file for those headers that are permanently +marked. The lower case values don't get onto the spool; they are used only as +return values from header_checkname(). */ + +#define htype_other ' ' /* Unspecified header */ +#define htype_from 'F' +#define htype_to 'T' +#define htype_cc 'C' +#define htype_bcc 'B' +#define htype_id 'I' /* for message-id */ +#define htype_reply_to 'R' +#define htype_received 'P' /* P for Postmark */ +#define htype_sender 'S' +#define htype_old '*' /* Replaced header */ + +#define htype_date 'd' +#define htype_return_path 'p' +#define htype_delivery_date 'x' +#define htype_envelope_to 'e' +#define htype_subject 's' + +/* These values are used only when adding new headers from an ACL; they too +never get onto the spool. The type of the added header is set by reference +to the header name, by calling header_checkname(). */ + +#define htype_add_top 'a' +#define htype_add_rec 'r' +#define htype_add_bot 'z' + +/* Types of item in options lists. These are the bottom 8 bits of the "type" +field, which is an int. The opt_void value is used for entries in tables that +point to special types of value that are accessed only indirectly (e.g. the +rewrite data that is built out of a string option.) We need to have some values +visible in local_scan, so the following are declared there: + + opt_stringptr, opt_int, opt_octint, opt_mkint, opt_Kint, opt_fixed, opt_time, + opt_bool + +To make sure we don't conflict, the local_scan.h values start from zero, and +those defined here start from 32. The boolean ones must all be together so they +can be easily tested as a group. That is the only use of opt_bool_last. */ + +enum { opt_bit = 32, opt_bool_verify, opt_bool_set, opt_expand_bool, + opt_bool_last, + opt_rewrite, opt_timelist, opt_uid, opt_gid, opt_uidlist, opt_gidlist, + opt_expand_uid, opt_expand_gid, opt_void }; + +/* There's a high-ish bit which is used to flag duplicate options, kept +for compatibility, which shouldn't be output. Also used for hidden options +that are automatically maintained from others. Another high bit is used to +flag driver options that although private (so as to be settable only on some +drivers), are stored in the instance block so as to be accessible from outside. +A third high bit is set when an option is read, so as to be able to give an +error if any option is set twice. Finally, there's a bit which is set when an +option is set with the "hide" prefix, to prevent -bP from showing it to +non-admin callers. The next byte up in the int is used to keep the bit number +for booleans that are kept in one bit. */ + +#define opt_hidden 0x100 /* Private to Exim */ +#define opt_public 0x200 /* Stored in the main instance block */ +#define opt_set 0x400 /* Option is set */ +#define opt_secure 0x800 /* "hide" prefix used */ +#define opt_mask 0x0ff + +/* Verify types when directing and routing */ + +enum { v_none, v_sender, v_recipient, v_expn }; + +/* Option flags for verify_address() */ + +#define vopt_fake_sender 0x0001 /* for verify=sender=
    */ +#define vopt_is_recipient 0x0002 +#define vopt_qualify 0x0004 +#define vopt_expn 0x0008 +#define vopt_callout_postmaster 0x0010 /* during callout */ +#define vopt_callout_random 0x0020 /* during callout */ +#define vopt_callout_no_cache 0x0040 /* disable callout cache */ +#define vopt_callout_recipsender 0x0080 /* use real sender to verify recip */ +#define vopt_callout_recippmaster 0x0100 /* use postmaster to verify recip */ + +/* Values for fields in callout cache records */ + +#define ccache_unknown 0 /* test hasn't been done */ +#define ccache_accept 1 +#define ccache_reject 2 + +/* Options for lookup functions */ + +#define lookup_querystyle 1 /* query-style lookup */ +#define lookup_absfile 2 /* requires absolute file name */ + +/* Status values for host_item blocks. Require hstatus_unusable and +hstatus_unusable_expired to be last. */ + +enum { hstatus_unknown, hstatus_usable, hstatus_unusable, + hstatus_unusable_expired }; + +/* Reasons why a host is unusable (for clearer log messages) */ + +enum { hwhy_unknown, hwhy_retry, hwhy_failed, hwhy_deferred, hwhy_ignored }; + +/* Domain lookup types for routers */ + +enum { lk_default, lk_byname, lk_bydns }; + +/* Values for the self_code fields */ + +enum { self_freeze, self_defer, self_send, self_reroute, self_pass, self_fail }; + +/* Flags for rewrite rules */ + +#define rewrite_sender 0x0001 +#define rewrite_from 0x0002 +#define rewrite_to 0x0004 +#define rewrite_cc 0x0008 +#define rewrite_bcc 0x0010 +#define rewrite_replyto 0x0020 +#define rewrite_all_headers 0x003F /* all header flags */ + +#define rewrite_envfrom 0x0040 +#define rewrite_envto 0x0080 +#define rewrite_all_envelope 0x00C0 /* all envelope flags */ + +#define rewrite_all (rewrite_all_headers | rewrite_all_envelope) + +#define rewrite_smtp 0x0100 /* rewrite at SMTP time */ +#define rewrite_smtp_sender 0x0200 /* SMTP sender rewrite (allows <>) */ +#define rewrite_qualify 0x0400 /* qualify if necessary */ +#define rewrite_repeat 0x0800 /* repeat rewrite rule */ + +#define rewrite_whole 0x1000 /* option bit for headers */ +#define rewrite_quit 0x2000 /* "no more" option */ + +/* Flags for log_write(); LOG_MAIN, LOG_PANIC, and LOG_REJECT are also in +local_scan.h */ + +#define LOG_MAIN 1 /* Write to the main log */ +#define LOG_PANIC 2 /* Write to the panic log */ +#define LOG_PANIC_DIE 6 /* Write to the panic log and then die */ +#define LOG_PROCESS 8 /* Write to the process log */ +#define LOG_REJECT 16 /* Write to the reject log, with headers */ +#define LOG_SENDER 32 /* Add raw sender to the message */ +#define LOG_RECIPIENTS 64 /* Add raw recipients to the message */ +#define LOG_CONFIG 128 /* Add "Exim configuration error" */ +#define LOG_CONFIG_FOR (256+128) /* Add " for" instead of ":\n" */ +#define LOG_CONFIG_IN (512+128) /* Add " in line x[ of file y]" */ + +/* Returns from host_find_by{name,dns}() */ + +enum { + HOST_FIND_FAILED, /* failed to find the host */ + HOST_FIND_AGAIN, /* could not resolve at this time */ + HOST_FOUND, /* found host */ + HOST_FOUND_LOCAL, /* found, but MX points to local host */ + HOST_IGNORED /* found but ignored - used internally only */ +}; + +/* Flags for host_find_bydns() */ + +#define HOST_FIND_BY_SRV 0x0001 +#define HOST_FIND_BY_MX 0x0002 +#define HOST_FIND_BY_A 0x0004 +#define HOST_FIND_QUALIFY_SINGLE 0x0008 +#define HOST_FIND_SEARCH_PARENTS 0x0010 + +/* Actions applied to specific messages. */ + +enum { MSG_DELIVER, MSG_FREEZE, MSG_REMOVE, MSG_THAW, MSG_ADD_RECIPIENT, + MSG_MARK_ALL_DELIVERED, MSG_MARK_DELIVERED, MSG_EDIT_SENDER, + MSG_SHOW_BODY, MSG_SHOW_HEADER, MSG_SHOW_LOG }; + +/* Returns from the spool_read_header() function */ + +enum { + spool_read_OK, /* success */ + spool_read_notopen, /* open failed */ + spool_read_enverror, /* error in the envelope */ + spool_read_hdrerror /* error in the headers */ +}; + +/* Options for transport_write_message */ + +#define topt_add_return_path 0x001 +#define topt_add_delivery_date 0x002 +#define topt_add_envelope_to 0x004 +#define topt_use_crlf 0x008 /* Terminate lines with CRLF */ +#define topt_end_dot 0x010 /* Send terminting dot line */ +#define topt_no_headers 0x020 /* Omit headers */ +#define topt_no_body 0x040 /* Omit body */ +#define topt_escape_headers 0x080 /* Apply escape check to headers */ + +/* Codes for the host_find_failed option. */ + +#define hff_freeze 0 +#define hff_defer 1 +#define hff_pass 2 +#define hff_decline 3 +#define hff_fail 4 + +/* Router information flags */ + +#define ri_yestransport 0x0001 /* Must have a transport */ +#define ri_notransport 0x0002 /* Must not have a transport */ + +/* Codes for match types in match_check_list; to any of them, MCL_NOEXPAND may +be added */ + +#define MCL_NOEXPAND 16 + +enum { MCL_STRING, MCL_DOMAIN, MCL_HOST, MCL_ADDRESS, MCL_LOCALPART }; + +/* Codes for the places from which ACLs can be called. These are cunningly +ordered to make it easy to implement tests for certain ACLs when processing +"control" modifiers, by means of a maximum "where" value. Do not modify this +order without checking carefully! Furthermore, remember to keep these in step +with the tables of names and response codes in globals.c. */ + +enum { ACL_WHERE_RCPT, /* Some controls are for RCPT only */ + ACL_WHERE_MAIL, /* ) */ + ACL_WHERE_PREDATA, /* ) There are several tests for "in message", */ + ACL_WHERE_DATA, /* ) implemented by <= WHERE_NOTSMTP */ + ACL_WHERE_NOTSMTP, /* ) */ + + ACL_WHERE_AUTH, /* These remaining ones are not currently */ + ACL_WHERE_CONNECT, /* required to be in a special order so they */ + ACL_WHERE_ETRN, /* are just alphabetical. */ + ACL_WHERE_EXPN, + ACL_WHERE_HELO, + ACL_WHERE_MAILAUTH, + ACL_WHERE_QUIT, + ACL_WHERE_STARTTLS, + ACL_WHERE_VRFY + }; + +/* Situations for spool_write_header() */ + +enum { SW_RECEIVING, SW_DELIVERING, SW_MODIFYING }; + +/* MX fields for hosts not obtained from MX records are always negative. +MX_NONE is the default case; lesser values are used when the hosts are +randomized in batches. */ + +#define MX_NONE (-1) + +/* host_item.port defaults to PORT_NONE; the only current case where this +is changed before running the transport is when an dnslookup router sets an +explicit port number. */ + +#define PORT_NONE (-1) + +/* Flags for single-key search defaults */ + +#define SEARCH_STAR 0x01 +#define SEARCH_STARAT 0x02 + +/* Filter types */ + +enum { FILTER_UNSET, FILTER_FORWARD, FILTER_EXIM, FILTER_SIEVE }; + +/* End of macros.h */ diff --git a/src/src/match.c b/src/src/match.c new file mode 100644 index 000000000..9d2a1c0bd --- /dev/null +++ b/src/src/match.c @@ -0,0 +1,1232 @@ +/* $Cambridge: exim/src/src/match.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Functions for matching strings */ + + +#include "exim.h" + + +/* Argument block for the check_string() function. This is used for general +strings, domains, and local parts. */ + +typedef struct check_string_block { + uschar *origsubject; /* caseful; keep these two first, in */ + uschar *subject; /* step with the block below */ + int expand_setup; + BOOL use_partial; + BOOL caseless; + BOOL at_is_special; +} check_string_block; + + +/* Argument block for the check_address() function. This is used for whole +addresses. */ + +typedef struct check_address_block { + uschar *origaddress; /* caseful; keep these two first, in */ + uschar *address; /* step with the block above */ + int expand_setup; + BOOL caseless; +} check_address_block; + + + +/************************************************* +* Generalized string match * +*************************************************/ + +/* This function does a single match of a subject against a pattern, and +optionally sets up the numeric variables according to what it matched. It is +called from match_isinlist() via match_check_list() when scanning a list, and +from match_check_string() when testing just a single item. The subject and +options arguments are passed in a check_string_block so as to make it easier to +pass them through match_check_list. + +The possible types of pattern are: + + . regular expression - starts with ^ + . tail match - starts with * + . lookup - starts with search type + . if at_is_special is set in the argument block: + @ matches the primary host name + @[] matches a local IP address in brackets + @mx_any matches any domain with an MX to the local host + @mx_primary matches any domain with a primary MX to the local host + @mx_secondary matches any domain with a secondary MX to the local host + . literal - anything else + +Any of the @mx_xxx options can be followed by "/ignore=" where is +a list of IP addresses that are to be ignored (typically 127.0.0.1). + +Arguments: + arg check_string_block pointer - see below + pattern the pattern to be matched + valueptr if not NULL, and a lookup is done, return the result here + instead of discarding it; else set it to point to NULL + error for error messages (not used in this function; it never + returns ERROR) + +Contents of the argument block: + subject the subject string to be checked + expand_setup if < 0, don't set up any numeric expansion variables; + if = 0, set $0 to whole subject, and either + $1 to what matches * or + $1, $2, ... to r.e. bracketed items + if > 0, don't set $0, but do set either + $n to what matches *, or + $n, $n+1, ... to r.e. bracketed items + (where n = expand_setup) + use_partial if FALSE, override any partial- search types + caseless TRUE for caseless matching where possible + at_is_special enable special handling of items starting with @ + +Returns: OK if matched + FAIL if not matched + DEFER if lookup deferred +*/ + +static int +check_string(void *arg, uschar *pattern, uschar **valueptr, uschar **error) +{ +check_string_block *cb = (check_string_block *)arg; +int search_type, partial, affixlen, starflags; +int expand_setup = cb->expand_setup; +uschar *affix; +uschar *s = cb->subject; +uschar *filename = NULL; +uschar *keyquery, *result, *semicolon; +void *handle; + +error = error; /* Keep clever compilers from complaining */ + +if (valueptr != NULL) *valueptr = NULL; /* For non-lookup matches */ + +/* If required to set up $0, initialize the data but don't turn on by setting +expand_nmax until the match is assured. */ + +expand_nmax = -1; +if (expand_setup == 0) + { + expand_nstring[0] = s; + expand_nlength[0] = Ustrlen(s); + } +else if (expand_setup > 0) expand_setup--; + +/* Regular expression match: compile, match, and set up $ variables if +required. */ + +if (pattern[0] == '^') + { + const pcre *re = regex_must_compile(pattern, cb->caseless, FALSE); + return ((expand_setup < 0)? + pcre_exec(re, NULL, CS s, Ustrlen(s), 0, PCRE_EOPT, NULL, 0) >= 0 + : + regex_match_and_setup(re, s, 0, expand_setup) + )? + OK : FAIL; + } + +/* Tail match */ + +if (pattern[0] == '*') + { + BOOL yield; + int slen = Ustrlen(s); + int patlen; /* Sun compiler doesn't like non-constant initializer */ + + patlen = Ustrlen(++pattern); + if (patlen > slen) return FAIL; + yield = cb->caseless? + (strncmpic(s + slen - patlen, pattern, patlen) == 0) : + (Ustrncmp(s + slen - patlen, pattern, patlen) == 0); + if (yield && expand_setup >= 0) + { + expand_nstring[++expand_setup] = s; + expand_nlength[expand_setup] = slen - patlen; + expand_nmax = expand_setup; + } + return yield? OK : FAIL; + } + +/* Match a special item starting with @ if so enabled. On its own, "@" matches +the primary host name - implement this by changing the pattern. For the other +cases we have to do some more work. If we don't recognize a special pattern, +just fall through - the match will fail. */ + +if (cb->at_is_special && pattern[0] == '@') + { + if (pattern[1] == 0) + { + pattern = primary_hostname; + goto NOT_AT_SPECIAL; /* Handle as exact string match */ + } + + if (Ustrcmp(pattern, "@[]") == 0) + { + ip_address_item *ip; + int slen = Ustrlen(s); + if (s[0] != '[' && s[slen-1] != ']') return FAIL; + for (ip = host_find_interfaces(); ip != NULL; ip = ip->next) + if (Ustrncmp(ip->address, s+1, slen - 2) == 0) return OK; + return FAIL; + } + + if (strncmpic(pattern, US"@mx_", 4) == 0) + { + int rc; + host_item h; + BOOL prim = FALSE; + BOOL secy = FALSE; + BOOL removed = FALSE; + uschar *ss = pattern + 4; + uschar *ignore_target_hosts = NULL; + + if (strncmpic(ss, US"any", 3) == 0) ss += 3; + else if (strncmpic(ss, US"primary", 7) == 0) + { + ss += 7; + prim = TRUE; + } + else if (strncmpic(ss, US"secondary", 9) == 0) + { + ss += 9; + secy = TRUE; + } + else goto NOT_AT_SPECIAL; + + if (strncmpic(ss, US"/ignore=", 8) == 0) ignore_target_hosts = ss + 8; + else if (*ss != 0) goto NOT_AT_SPECIAL; + + h.next = NULL; + h.name = s; + h.address = NULL; + + rc = host_find_bydns(&h, + ignore_target_hosts, + HOST_FIND_BY_MX, /* search only for MX, not SRV or A */ + NULL, /* service name not relevant */ + NULL, /* srv_fail_domains not relevant */ + NULL, /* mx_fail_domains not relevant */ + NULL, /* no feedback FQDN */ + &removed); /* feedback if local removed */ + + if (rc == HOST_FIND_AGAIN) + { + search_error_message = string_sprintf("DNS lookup of \"%s\" deferred", s); + return DEFER; + } + + if (rc == HOST_FOUND_LOCAL && !secy) return OK; + if (prim) return FAIL; + return removed? OK : FAIL; + + /*** The above line used to be the following line, but this is incorrect, + because host_find_bydns() may return HOST_NOT_FOUND if it removed some MX + hosts, but the remaining ones were non-existent. All we are interested in + is whether or not it removed some hosts. + + return (rc == HOST_FOUND && removed)? OK : FAIL; + ***/ + } + } + +/* Escape point from code for specials that start with "@" */ + +NOT_AT_SPECIAL: + +/* This is an exact string match if there is no semicolon in the pattern. */ + +if ((semicolon = Ustrchr(pattern, ';')) == NULL) + { + BOOL yield = cb->caseless? + (strcmpic(s, pattern) == 0) : (Ustrcmp(s, pattern) == 0); + if (yield && expand_setup >= 0) expand_nmax = expand_setup; + return yield? OK : FAIL; + } + +/* Otherwise we have a lookup item. The lookup type, including partial, etc. is +the part of the string preceding the semicolon. */ + +*semicolon = 0; +search_type = search_findtype_partial(pattern, &partial, &affix, &affixlen, + &starflags); +*semicolon = ';'; +if (search_type < 0) log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s", + search_error_message); + +/* Partial matching is not appropriate for certain lookups (e.g. when looking +up user@domain for sender rejection). There's a flag to disable it. */ + +if (!cb->use_partial) partial = -1; + +/* Set the parameters for the two different kinds of lookup. */ + +keyquery = semicolon + 1; +while (isspace(*keyquery)) keyquery++; + +if (!mac_islookup(search_type, lookup_querystyle)) + { + filename = keyquery; + keyquery = s; + } + +/* Now do the actual lookup; throw away the data returned unless it was asked +for; partial matching is all handled inside search_find(). Note that there is +no search_close() because of the caching arrangements. */ + +handle = search_open(filename, search_type, 0, NULL, NULL); +if (handle == NULL) log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s", + search_error_message); +result = search_find(handle, filename, keyquery, partial, affix, affixlen, + starflags, &expand_setup); + +if (result == NULL) return search_find_defer? DEFER : FAIL; +if (valueptr != NULL) *valueptr = result; + +expand_nmax = expand_setup; +return OK; +} + + + +/************************************************* +* Public interface to check_string() * +*************************************************/ + +/* This function is called from several places where is it most convenient to +pass the arguments individually. It places them in a check_string_block +structure, and then calls check_string(). + +Arguments: + s the subject string to be checked + pattern the pattern to check it against + expand_setup expansion setup option (see check_string()) + use_partial if FALSE, override any partial- search types + caseless TRUE for caseless matching where possible + at_is_special TRUE to recognize @, @[], etc. + valueptr if not NULL, and a file lookup was done, return the result + here instead of discarding it; else set it to point to NULL + +Returns: OK if matched + FAIL if not matched + DEFER if lookup deferred +*/ + +int +match_check_string(uschar *s, uschar *pattern, int expand_setup, + BOOL use_partial, BOOL caseless, BOOL at_is_special, uschar **valueptr) +{ +check_string_block cb; +cb.origsubject = s; +cb.subject = caseless? string_copylc(s) : string_copy(s); +cb.expand_setup = expand_setup; +cb.use_partial = use_partial; +cb.caseless = caseless; +cb.at_is_special = at_is_special; +return check_string(&cb, pattern, valueptr, NULL); +} + + + +/************************************************* +* Get key string from check block * +*************************************************/ + +/* When caching the data from a lookup for a named list, we have to save the +key that was found, because other lookups of different keys on the same list +may occur. This function has knowledge of the different lookup types, and +extracts the appropriate key. + +Arguments: + arg the check block + type MCL_STRING, MCL_DOMAIN, MCL_HOST, MCL_ADDRESS, or MCL_LOCALPART +*/ + +static uschar * +get_check_key(void *arg, int type) +{ +switch(type) + { + case MCL_STRING: + case MCL_DOMAIN: + case MCL_LOCALPART: + return ((check_string_block *)arg)->subject; + + case MCL_HOST: + return ((check_host_block *)arg)->host_address; + + case MCL_ADDRESS: + return ((check_address_block *)arg)->address; + } +return US""; /* In practice, should never happen */ +} + + + +/************************************************* +* Scan list and run matching function * +*************************************************/ + +/* This function scans a list of patterns, and runs a matching function for +each item in the list. It is called from the functions that match domains, +local parts, hosts, and addresses, because its overall structure is the same in +all cases. However, the details of each particular match is different, so it +calls back to a given function do perform an actual match. + +We can't quite keep the different types anonymous here because they permit +different special cases. A pity. + +If a list item starts with !, that implies negation if the subject matches the +rest of the item (ignoring white space after the !). The result when the end of +the list is reached is FALSE unless the last item on the list is negated, in +which case it is TRUE. A file name in the list causes its lines to be +interpolated as if items in the list. An item starting with + is a named +sublist, obtained by searching the tree pointed to by anchorptr, with possible +cached match results in cache_bits. + +Arguments: + listptr pointer to the pointer to the list + sep separator character for string_nextinlist(); + normally zero for a standard list; + sometimes UCHAR_MAX+1 for single items; + anchorptr -> tree of named items, or NULL if no named items + cache_ptr pointer to pointer to cache bits for named items, or + pointer to NULL if not caching; may get set NULL if an + uncacheable named list is encountered + func function to call back to do one test + arg pointer to pass to the function; the string to be matched is + in the structure it points to + type MCL_STRING, MCL_DOMAIN, MCL_HOST, MCL_ADDRESS, or MCL_LOCALPART + these are used for some special handling + MCL_NOEXPAND (whose value is greater than any of them) may + be added to any value to suppress expansion of the list + name string to use in debugging info + valueptr where to pass back data from a lookup + +Returns: OK if matched a non-negated item + OK if hit end of list after a negated item + FAIL if expansion force-failed + FAIL if matched a negated item + FAIL if hit end of list after a non-negated item + DEFER if a lookup deferred or expansion failed +*/ + +int +match_check_list(uschar **listptr, int sep, tree_node **anchorptr, + unsigned int **cache_ptr, int (*func)(void *,uschar *,uschar **,uschar **), + void *arg, int type, uschar *name, uschar **valueptr) +{ +int yield = OK; +unsigned int *original_cache_bits = *cache_ptr; +BOOL include_unknown = FALSE; +BOOL ignore_unknown = FALSE; +uschar *list; +uschar *sss; +uschar *ot = NULL; +uschar buffer[1024]; + +/* Save time by not scanning for the option name when we don't need it. */ + +HDEBUG(D_any) + { + uschar *listname = readconf_find_option(listptr); + if (listname[0] != 0) ot = string_sprintf("%s in %s?", name, listname); + } + +/* If the list is empty, the answer is no. Skip the debugging output for +an unnamed list. */ + +if (*listptr == NULL) + { + HDEBUG(D_lists) + { + if (ot != NULL) debug_printf("%s no (option unset)\n", ot); + } + return FAIL; + } + +/* Expand the list before we scan it. A forced expansion gives the answer +"not in list"; other expansion errors cause DEFER to be returned. However, +if the type value is greater than or equal to than MCL_NOEXPAND, do not expand +the list. */ + +if (type >= MCL_NOEXPAND) + { + list = *listptr; + type -= MCL_NOEXPAND; /* Remove the "no expand" flag */ + } +else + { + list = expand_string(*listptr); + if (list == NULL) + { + if (expand_string_forcedfail) + { + HDEBUG(D_lists) debug_printf("expansion of \"%s\" forced failure: " + "assume not in this list\n", *listptr); + return FAIL; + } + log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand \"%s\" while checking " + "a list: %s", *listptr, expand_string_message); + return DEFER; + } + } + +/* For an unnamed list, use the expanded version in comments */ + +HDEBUG(D_any) + { + if (ot == NULL) ot = string_sprintf("%s in \"%s\"?", name, list); + } + +/* Now scan the list and process each item in turn, until one of them matches, +or we hit an error. */ + +while ((sss = string_nextinlist(&list, &sep, buffer, sizeof(buffer))) != NULL) + { + uschar *ss = sss; + + /* Address lists may contain +caseful, to restore caseful matching of the + local part. We have to know the layout of the control block, unfortunately. + The lower cased address is in a temporary buffer, so we just copy the local + part back to the start of it (if a local part exists). */ + + if (type == MCL_ADDRESS) + { + if (Ustrcmp(ss, "+caseful") == 0) + { + check_address_block *cb = (check_address_block *)arg; + uschar *at = Ustrrchr(cb->origaddress, '@'); + if (at != NULL) + Ustrncpy(cb->address, cb->origaddress, at - cb->origaddress); + cb->caseless = FALSE; + continue; + } + } + + /* Similar processing for local parts */ + + else if (type == MCL_LOCALPART) + { + if (Ustrcmp(ss, "+caseful") == 0) + { + check_string_block *cb = (check_string_block *)arg; + Ustrcpy(cb->subject, cb->origsubject); + cb->caseless = FALSE; + continue; + } + } + + /* If the host item is "+include_unknown", remember it in case there's a + subsequent failed reverse lookup. */ + + else if (type == MCL_HOST) + { + if (Ustrcmp(ss, "+include_unknown") == 0) + { + include_unknown = TRUE; + ignore_unknown = FALSE; + continue; + } + if (Ustrcmp(ss, "+ignore_unknown") == 0) + { + ignore_unknown = TRUE; + include_unknown = FALSE; + continue; + } + } + + /* Starting with ! specifies a negative item. It is theoretically possible + for a local part to start with !. In that case, a regex has to be used. */ + + if (*ss == '!') + { + yield = FAIL; + while (isspace((*(++ss)))); + } + else yield = OK; + + /* If the item does not begin with '/', it might be a + item for a named + list. Otherwise, it is just a single list entry that has to be matched. + We recognize '+' only when supplied with a tree of named lists. */ + + if (*ss != '/') + { + if (*ss == '+' && anchorptr != NULL) + { + int bits = 0; + int offset = 0; + int shift = 0; + unsigned int *use_cache_bits = original_cache_bits; + uschar *cached = US""; + namedlist_block *nb; + tree_node *t = tree_search(*anchorptr, ss+1); + + if (t == NULL) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "unknown named%s list \"%s\"", + (type == MCL_DOMAIN)? " domain" : + (type == MCL_HOST)? " host" : + (type == MCL_ADDRESS)? " address" : + (type == MCL_LOCALPART)? " local part" : "", + ss); + nb = t->data.ptr; + + /* If the list number is negative, it means that this list is not + cacheable because it contains expansion items. */ + + if (nb->number < 0) use_cache_bits = NULL; + + /* If we have got a cache pointer, get the bits. This is not an "else" + because the pointer may be NULL from the start if caching is not + required. */ + + if (use_cache_bits != NULL) + { + offset = (nb->number)/16; + shift = ((nb->number)%16)*2; + bits = use_cache_bits[offset] & (3 << shift); + } + + /* Not previously tested or no cache - run the full test */ + + if (bits == 0) + { + switch (match_check_list(&(nb->string), 0, anchorptr, &use_cache_bits, + func, arg, type, name, valueptr)) + { + case OK: bits = 1; break; + case FAIL: bits = 3; break; + case DEFER: goto DEFER_RETURN; + } + + /* If this list was uncacheable, or a sublist turned out to be + uncacheable, the value of use_cache_bits will now be NULL, even if it + wasn't before. Ensure that this is passed up to the next level. + Otherwise, remember the result of the search in the cache. */ + + if (use_cache_bits == NULL) + { + *cache_ptr = NULL; + } + else + { + use_cache_bits[offset] |= bits << shift; + + if (valueptr != NULL) + { + int old_pool = store_pool; + namedlist_cacheblock *p; + + /* Cached data for hosts persists over more than one message, + so we use the permanent store pool */ + + store_pool = POOL_PERM; + p = store_get(sizeof(namedlist_cacheblock)); + p->key = string_copy(get_check_key(arg, type)); + store_pool = old_pool; + + p->data = *valueptr; + p->next = nb->cache_data; + nb->cache_data = p; + if (*valueptr != NULL) + { + DEBUG(D_lists) debug_printf("data from lookup saved for " + "cache for %s: %s\n", ss, *valueptr); + } + } + } + } + + /* Previously cached; to find a lookup value, search a chain of values + and compare keys. Typically, there is only one such, but it is possible + for different keys to have matched the same named list. */ + + else + { + DEBUG(D_lists) debug_printf("cached %s match for %s\n", + ((bits & (-bits)) == bits)? "yes" : "no", ss); + cached = US" - cached"; + if (valueptr != NULL) + { + uschar *key = get_check_key(arg, type); + namedlist_cacheblock *p; + for (p = nb->cache_data; p != NULL; p = p->next) + { + if (Ustrcmp(key, p->key) == 0) + { + *valueptr = p->data; + break; + } + } + DEBUG(D_lists) debug_printf("cached lookup data = %s\n", *valueptr); + } + } + + /* Result of test is indicated by value in bits. For each test, we + have 00 => untested, 01 => tested yes, 11 => tested no. */ + + if ((bits & (-bits)) == bits) /* Only one of the two bits is set */ + { + HDEBUG(D_lists) debug_printf("%s %s (matched \"%s\"%s)\n", ot, + (yield == OK)? "yes" : "no", sss, cached); + return yield; + } + } + + /* Run the provided function to do the individual test. */ + + else + { + uschar *error; + switch ((func)(arg, ss, valueptr, &error)) + { + case OK: + HDEBUG(D_lists) debug_printf("%s %s (matched \"%s\")\n", ot, + (yield == OK)? "yes" : "no", sss); + return yield; + + case DEFER: + goto DEFER_RETURN; + + /* The ERROR return occurs only when checking hosts, when either a + forward or reverse lookup has failed. The error string gives details of + which it was. */ + + case ERROR: + if (ignore_unknown) + { + HDEBUG(D_lists) debug_printf("%s: item ignored by +ignore_unknown", + error); + } + else + { + HDEBUG(D_lists) debug_printf("%s %s (%s)\n", ot, + include_unknown? "yes":"no", error); + if (!include_unknown) return FAIL; + log_write(0, LOG_MAIN, "%s: accepted by +include_unknown", error); + return OK; + } + } + } + } + + /* If the item is a file name, we read the file and do a match attempt + on each line in the file, including possibly more negation processing. */ + + else + { + int file_yield = yield; /* In case empty file */ + uschar *filename = ss; + FILE *f = Ufopen(filename, "rb"); + uschar filebuffer[1024]; + + /* ot will be null in non-debugging cases, and anyway, we get better + wording by reworking it. */ + + if (f == NULL) + { + uschar *listname = readconf_find_option(listptr); + if (listname[0] == 0) + listname = string_sprintf("\"%s\"", *listptr); + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s", + string_open_failed(errno, "%s when checking %s", sss, listname)); + } + + /* Trailing comments are introduced by #, but in an address list or local + part list, the # must be preceded by white space or the start of the line, + because the # character is a legal character in local parts. */ + + while (Ufgets(filebuffer, sizeof(filebuffer), f) != NULL) + { + uschar *error; + uschar *sss = filebuffer; + + while ((ss = Ustrchr(sss, '#')) != NULL) + { + if ((type != MCL_ADDRESS && type != MCL_LOCALPART) || + ss == filebuffer || isspace(ss[-1])) + { + *ss = 0; + break; + } + sss = ss + 1; + } + + ss = filebuffer + Ustrlen(filebuffer); /* trailing space */ + while (ss > filebuffer && isspace(ss[-1])) ss--; + *ss = 0; + + ss = filebuffer; + while (isspace(*ss)) ss++; /* leading space */ + + if (*ss == 0) continue; /* ignore empty */ + + file_yield = yield; /* positive yield */ + sss = ss; /* for debugging */ + + if (*ss == '!') /* negation */ + { + file_yield = (file_yield == OK)? FAIL : OK; + while (isspace((*(++ss)))); + } + + switch ((func)(arg, ss, valueptr, &error)) + { + case OK: + fclose(f); + HDEBUG(D_lists) debug_printf("%s %s (matched \"%s\" in %s)\n", ot, + (yield == OK)? "yes" : "no", sss, filename); + return file_yield; + + case DEFER: + fclose(f); + goto DEFER_RETURN; + + case ERROR: /* host name lookup failed - this can only */ + if (ignore_unknown) /* be for an incoming host (not outgoing) */ + { + HDEBUG(D_lists) debug_printf("%s: item ignored by +ignore_unknown", + error); + } + else + { + HDEBUG(D_lists) debug_printf("%s %s (%s)\n", ot, + include_unknown? "yes":"no", error); + fclose(f); + if (!include_unknown) return FAIL; + log_write(0, LOG_MAIN, "%s: accepted by +include_unknown", error); + return OK; + } + } + } + + /* At the end of the file, leave the yield setting at the final setting + for the file, in case this is the last item in the list. */ + + yield = file_yield; + fclose(f); + } + } /* Loop for the next item on the top-level list */ + +/* End of list reached: if the last item was negated yield OK, else FAIL. */ + +HDEBUG(D_lists) + debug_printf("%s %s (end of list)\n", ot, (yield == OK)? "no":"yes"); +return (yield == OK)? FAIL : OK; + +/* Handle lookup defer */ + +DEFER_RETURN: +HDEBUG(D_lists) debug_printf("%s lookup deferred for %s\n", ot, sss); +return DEFER; +} + + +/************************************************* +* Match in colon-separated list * +*************************************************/ + +/* This function is used for domain lists and local part lists. It is not used +for host lists or address lists, which have additional interpretation of the +patterns. Some calls of it set sep > UCHAR_MAX in order to use its matching +facilities on single items. When this is done, it arranges to set the numerical +variables as a result of the match. + +This function is now just a short interface to match_check_list(), which does +list scanning in a general way. A good compiler will optimize the tail +recursion. + +Arguments: + s string to search for + listptr ptr to ptr to colon separated list of patterns, or NULL + sep a separator value for the list (see string_nextinlist()) + anchorptr ptr to tree for named items, or NULL if no named items + cache_bits ptr to cache_bits for ditto, or NULL if not caching + type MCL_DOMAIN when matching a domain list + MCL_LOCALPART when matching a local part list (address lists + have their own function) + MCL_STRING for others (e.g. list of ciphers) + MCL_NOEXPAND (whose value is greater than any of them) may + be added to any value to suppress expansion of the list + caseless TRUE for (mostly) caseless matching - passed directly to + match_check_string() + valueptr pointer to where any lookup data is to be passed back, + or NULL (just passed on to match_check_string) + +Returns: OK if matched a non-negated item + OK if hit end of list after a negated item + FAIL if expansion force-failed + FAIL if matched a negated item + FAIL if hit end of list after a non-negated item + DEFER if a lookup deferred +*/ + +int +match_isinlist(uschar *s, uschar **listptr, int sep, tree_node **anchorptr, + unsigned int *cache_bits, int type, BOOL caseless, uschar **valueptr) +{ +unsigned int *local_cache_bits = cache_bits; +check_string_block cb; +cb.origsubject = s; +cb.subject = caseless? string_copylc(s) : string_copy(s); +cb.expand_setup = (sep > UCHAR_MAX)? 0 : -1; +cb.use_partial = TRUE; +cb.caseless = caseless; +cb.at_is_special = (type == MCL_DOMAIN || type == MCL_DOMAIN + MCL_NOEXPAND); +if (valueptr != NULL) *valueptr = NULL; +return match_check_list(listptr, sep, anchorptr, &local_cache_bits, + check_string, &cb, type, s, valueptr); +} + + + +/************************************************* +* Match address to single address-list item * +*************************************************/ + +/* This function matches an address to an item from an address list. It is +called from match_address_list() via match_check_list(). That is why most of +its arguments are in an indirect block. + +Arguments: + arg the argument block (see below) + pattern the pattern to match + valueptr where to return a value + error for error messages (not used in this function; it never + returns ERROR) + +The argument block contains: + address the start of the subject address; when called from retry.c + it may be *@domain if the local part isn't relevant + origaddress the original, un-case-forced address (not used here, but used + in match_check_list() when +caseful is encountered) + expand_setup controls setting up of $n variables + caseless TRUE for caseless local part matching + +Returns: OK for a match + FAIL for no match + DEFER if a lookup deferred +*/ + +static int +check_address(void *arg, uschar *pattern, uschar **valueptr, uschar **error) +{ +check_address_block *cb = (check_address_block *)arg; +check_string_block csb; +int rc; +int expand_inc = 0; +unsigned int *null = NULL; +uschar *listptr; +uschar *subject = cb->address; +uschar *s, *pdomain, *sdomain; + +error = error; /* Keep clever compilers from complaining */ + +DEBUG(D_lists) debug_printf("address match: subject=%s pattern=%s\n", + subject, pattern); + +/* Handle a regular expression, which must match the entire incoming address. */ + +if (*pattern == '^') + return match_check_string(subject, pattern, cb->expand_setup, TRUE, + cb->caseless, FALSE, NULL); + +/* If the subject is the empty string, the only pattern it can match (other +than a regular expression) is the empty pattern. */ + +if (*subject == 0) return (*pattern == 0)? OK : FAIL; + +/* Find the domain in the subject */ + +sdomain = Ustrrchr(subject, '@'); + +/* Handle the case of a pattern that is just a lookup. Skip over possible +lookup names (letters, digits, hyphens). Skip over a possible * or *@ at +the end. Then we must have a semicolon for it to be a lookup. */ + +for (s = pattern; isalnum(*s) || *s == '-'; s++); +if (*s == '*') s++; +if (*s == '@') s++; + +/* If it is a straight lookup, do a lookup for the whole address. Partial +matching doesn't make sense here, so we ignore it, but write a panic log entry. +However, *@ matching will be honoured. */ + +if (*s == ';') + { + if (Ustrncmp(pattern, "partial-", 8) == 0) + log_write(0, LOG_MAIN|LOG_PANIC, "partial matching is not applicable to " + "whole-address lookups: ignored \"partial-\" in \"%s\"", pattern); + return match_check_string(subject, pattern, -1, FALSE, cb->caseless, FALSE, + valueptr); + } + +/* If the pattern starts with "@@" we have a split lookup, where the domain is +looked up to obtain a list of local parts. If the subject's local part is just +"*" (called from retry) the match always fails. */ + +if (pattern[0] == '@' && pattern[1] == '@') + { + int watchdog = 50; + uschar *list, *key, *ss; + uschar buffer[1024]; + + if (sdomain == subject + 1 && *subject == '*') return FAIL; + + /* Loop for handling chains. The last item in any list may be of the form + ">name" in order to chain on to another list. */ + + for (key = sdomain + 1; key != NULL && watchdog-- > 0; ) + { + int sep = 0; + + if ((rc = match_check_string(key, pattern + 2, -1, TRUE, FALSE, FALSE, + &list)) != OK) return rc; + + /* Check for chaining from the last item; set up the next key if one + is found. */ + + ss = Ustrrchr(list, ':'); + if (ss == NULL) ss = list; else ss++; + while (isspace(*ss)) ss++; + if (*ss == '>') + { + *ss++ = 0; + while (isspace(*ss)) ss++; + key = string_copy(ss); + } + else key = NULL; + + /* Look up the local parts provided by the list; negation is permitted. + If a local part has to begin with !, a regex can be used. */ + + while ((ss = string_nextinlist(&list, &sep, buffer, sizeof(buffer))) + != NULL) + { + int local_yield; + + if (*ss == '!') + { + local_yield = FAIL; + while (isspace((*(++ss)))); + } + else local_yield = OK; + + *sdomain = 0; + rc = match_check_string(subject, ss, -1, TRUE, cb->caseless, FALSE, + valueptr); + *sdomain = '@'; + + switch(rc) + { + case OK: + return local_yield; + + case DEFER: + return DEFER; + } + } + } + + /* End of chain loop; panic if too many times */ + + if (watchdog <= 0) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Loop detected in lookup of " + "local part of %s in %s", subject, pattern); + + /* Otherwise the local part check has failed, so the whole match + fails. */ + + return FAIL; + } + + +/* We get here if the pattern is not a lookup or a regular expression. If it +contains an @ there is both a local part and a domain. */ + +pdomain = Ustrrchr(pattern, '@'); +if (pdomain != NULL) + { + int pllen, sllen; + + /* If the domain in the pattern is empty or one of the special cases [] or + mx_{any,primary,secondary}, and the local part in the pattern ends in "@", + we have a pattern of the form @@, @@[], or + @@mx_{any,primary,secondary}. These magic "domains" are + automatically interpreted in match_check_string. We just need to arrange that + the leading @ is included in the domain. */ + + if (pdomain > pattern && pdomain[-1] == '@' && + (pdomain[1] == 0 || + Ustrcmp(pdomain+1, "[]") == 0 || + Ustrcmp(pdomain+1, "mx_any") == 0 || + Ustrcmp(pdomain+1, "mx_primary") == 0 || + Ustrcmp(pdomain+1, "mx_secondary") == 0)) + pdomain--; + + pllen = pdomain - pattern; + sllen = sdomain - subject; + + /* Compare the local parts in the subject and the pattern */ + + if (*pattern == '*') + { + int cllen = pllen - 1; + if (sllen < cllen) return FAIL; + if (cb->caseless) + { + if (strncmpic(subject+sllen-cllen, pattern + 1, cllen) != 0) + return FAIL; + } + else + { + if (Ustrncmp(subject+sllen-cllen, pattern + 1, cllen) != 0) + return FAIL; + } + if (cb->expand_setup > 0) + { + expand_nstring[cb->expand_setup] = subject; + expand_nlength[cb->expand_setup] = sllen - cllen; + expand_inc = 1; + } + } + else + { + if (sllen != pllen) return FAIL; + if (cb->caseless) + { + if (strncmpic(subject, pattern, sllen) != 0) return FAIL; + } + else + { + if (Ustrncmp(subject, pattern, sllen) != 0) return FAIL; + } + } + } + +/* If the local part matched, or was not being checked, check the domain using +the generalized function, which supports file lookups (which may defer). The +original code read as follows: + + return match_check_string(sdomain + 1, + (pdomain == NULL)? pattern : pdomain + 1, + cb->expand_setup + expand_inc, TRUE, cb->caseless, TRUE, NULL); + +This supported only literal domains and *.x.y patterns. In order to allow for +named domain lists (so that you can right, for example, "senders=+xxxx"), it +was changed to use the list scanning function. */ + +csb.origsubject = sdomain + 1; +csb.subject = (cb->caseless)? string_copylc(sdomain+1) : string_copy(sdomain+1); +csb.expand_setup = cb->expand_setup + expand_inc; +csb.use_partial = TRUE; +csb.caseless = cb->caseless; +csb.at_is_special = TRUE; + +listptr = (pdomain == NULL)? pattern : pdomain + 1; +if (valueptr != NULL) *valueptr = NULL; + +return match_check_list( + &listptr, /* list of one item */ + UCHAR_MAX+1, /* impossible separator; single item */ + &domainlist_anchor, /* it's a domain list */ + &null, /* ptr to NULL means no caching */ + check_string, /* the function to do one test */ + &csb, /* its data */ + MCL_DOMAIN + MCL_NOEXPAND, /* domain list; don't expand */ + csb.subject, /* string for messages */ + valueptr); /* where to pass back lookup data */ +} + + + + +/************************************************* +* Test whether address matches address list * +*************************************************/ + +/* This function is given an address and a list of things to match it against. +The list may contain individual addresses, regular expressions, lookup +specifications, and indirection via bare files. Negation is supported. The +address to check can consist of just a domain, which will then match only +domain items or items specified as *@domain. + +Domains are always lower cased before the match. Local parts are also lower +cased unless "caseless" is false. The work of actually scanning the list is +done by match_check_list(), with an appropriate block of arguments and a +callback to check_address(). During caseless matching, it will recognize ++caseful and revert to caseful matching. + +Arguments: + address address to test + caseless TRUE to start in caseless state + expand TRUE to allow list expansion + listptr list to check against + cache_bits points to cache bits for named address lists, or NULL + expand_setup controls setting up of $n variables - passed through + to check_address (q.v.) + sep separator character for the list; + may be 0 to get separator from the list; + may be UCHAR_MAX+1 for one-item list + valueptr where to return a lookup value, or NULL + +Returns: OK for a positive match, or end list after a negation; + FAIL for a negative match, or end list after non-negation; + DEFER if a lookup deferred +*/ + +int +match_address_list(uschar *address, BOOL caseless, BOOL expand, + uschar **listptr, unsigned int *cache_bits, int expand_setup, int sep, + uschar **valueptr) +{ +uschar *p; +check_address_block ab; +unsigned int *local_cache_bits = cache_bits; + +/* RFC 2505 recommends that for spam checking, local parts should be caselessly +compared. Therefore, Exim now forces the entire address into lower case here, +provided that "caseless" is set. (It is FALSE for calls for matching rewriting +patterns.) Otherwise just the domain is lower cases. A magic item "+caseful" in +the list can be used to restore a caseful copy of the local part from the +original address. */ + +sprintf(CS big_buffer, "%.*s", big_buffer_size - 1, address); +for (p = big_buffer + Ustrlen(big_buffer) - 1; p >= big_buffer; p--) + { + if (!caseless && *p == '@') break; + *p = tolower(*p); + } + +/* If expand_setup is zero, we need to set up $0 to the whole thing, in +case there is a match. Can't use the built-in facilities of match_check_string +(via check_address), as we may just be calling that for part of the address +(the domain). */ + +if (expand_setup == 0) + { + expand_nstring[0] = string_copy(address); + expand_nlength[0] = Ustrlen(address); + expand_setup++; + } + +/* Set up the data to be passed ultimately to check_address. */ + +ab.origaddress = address; +ab.address = big_buffer; +ab.expand_setup = expand_setup; +ab.caseless = caseless; + +return match_check_list(listptr, sep, &addresslist_anchor, &local_cache_bits, + check_address, &ab, MCL_ADDRESS + (expand? 0:MCL_NOEXPAND), address, + valueptr); +} + +/* End of match.c */ diff --git a/src/src/moan.c b/src/src/moan.c new file mode 100644 index 000000000..89a9b847d --- /dev/null +++ b/src/src/moan.c @@ -0,0 +1,701 @@ +/* $Cambridge: exim/src/src/moan.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Functions for sending messages to sender or to mailmaster. */ + + +#include "exim.h" + + + +/************************************************* +* Send error message * +*************************************************/ + +/* This function sends an error message by opening a pipe to a new process +running Exim, and writing a message to it using the "-t" option. This is not +used for delivery failures, which have their own code for handing failed +addresses. + +Arguments: + recipient addressee for the message + ident identifies the type of error + eblock chain of error_blocks containing data about the error + headers the message's headers + message_file FILE containing the body of the message + firstline contains first line of file, if it was read to check for + "From ", but it turned out not to be + +Returns: TRUE if message successfully sent +*/ + +static BOOL +moan_send_message(uschar *recipient, int ident, error_block *eblock, + header_line *headers, FILE *message_file, uschar *firstline) +{ +int written = 0; +int fd; +int status; +int count = 0; +int size_limit = bounce_return_size_limit; +FILE *f; +int pid = child_open_exim(&fd); + +/* Creation of child failed */ + +if (pid < 0) + { + DEBUG(D_any) debug_printf("Failed to create child to send message: %s\n", + strerror(errno)); + return FALSE; + } +else DEBUG(D_any) debug_printf("Child process %d for sending message\n", pid); + +/* Creation of child succeeded */ + +f = fdopen(fd, "wb"); +if (errors_reply_to != NULL) fprintf(f, "Reply-To: %s\n", errors_reply_to); +fprintf(f, "Auto_submitted: auto-generated\n"); +fprintf(f, "From: Mail Delivery System \n", + qualify_domain_sender); +fprintf(f, "To: %s\n", recipient); + +switch(ident) + { + case ERRMESS_BADARGADDRESS: + fprintf(f, + "Subject: Mail failure - malformed recipient address\n\n"); + fprintf(f, + "A message that you sent contained a recipient address that was incorrectly\n" + "constructed:\n\n"); + fprintf(f, " %s %s\n", eblock->text1, eblock->text2); + count = Ustrlen(eblock->text1); + if (count > 0 && eblock->text1[count-1] == '.') + fprintf(f, + "\nRecipient addresses must not end with a '.' character.\n"); + fprintf(f, + "\nThe message has not been delivered to any recipients.\n"); + break; + + case ERRMESS_BADNOADDRESS: + case ERRMESS_BADADDRESS: + fprintf(f, + "Subject: Mail failure - malformed recipient address\n\n"); + fprintf(f, + "A message that you sent contained one or more recipient addresses that were\n" + "incorrectly constructed:\n\n"); + + while (eblock != NULL) + { + fprintf(f, " %s: %s\n", eblock->text1, eblock->text2); + count++; + eblock = eblock->next; + } + + fprintf(f, (count == 1)? "\nThis address has been ignored. " : + "\nThese addresses have been ignored. "); + + fprintf(f, (ident == ERRMESS_BADADDRESS)? + "The other addresses in the message were\n" + "syntactically valid and have been passed on for an attempt at delivery.\n" : + + "There were no other addresses in your\n" + "message, and so no attempt at delivery was possible.\n"); + break; + + case ERRMESS_IGADDRESS: + fprintf(f, "Subject: Mail failure - no recipient addresses\n\n"); + fprintf(f, + "A message that you sent using the -t command line option contained no\n" + "addresses that were not also on the command line, and were therefore\n" + "suppressed. This left no recipient addresses, and so no delivery could\n" + "be attempted.\n"); + break; + + case ERRMESS_NOADDRESS: + fprintf(f, "Subject: Mail failure - no recipient addresses\n\n"); + fprintf(f, + "A message that you sent contained no recipient addresses, and therefore no\n" + "delivery could be attempted.\n"); + break; + + case ERRMESS_IOERR: + fprintf(f, "Subject: Mail failure - system failure\n\n"); + fprintf(f, + "A system failure was encountered while processing a message that you sent,\n" + "so it has not been possible to deliver it. The error was:\n\n%s\n", + eblock->text1); + break; + + case ERRMESS_VLONGHEADER: + fprintf(f, "Subject: Mail failure - overlong header section\n\n"); + fprintf(f, + "A message that you sent contained a header section that was excessively\n" + "long and could not be handled by the mail transmission software. The\n" + "message has not been delivered to any recipients.\n"); + break; + + case ERRMESS_VLONGHDRLINE: + fprintf(f, "Subject: Mail failure - overlong header line\n\n"); + fprintf(f, + "A message that you sent contained a header line that was excessively\n" + "long and could not be handled by the mail transmission software. The\n" + "message has not been delivered to any recipients.\n"); + break; + + case ERRMESS_TOOBIG: + fprintf(f, "Subject: Mail failure - message too big\n\n"); + fprintf(f, + "A message that you sent was longer than the maximum size allowed on this\n" + "system. It was not delivered to any recipients.\n"); + break; + + case ERRMESS_TOOMANYRECIP: + fprintf(f, "Subject: Mail failure - too many recipients\n\n"); + fprintf(f, + "A message that you sent contained more recipients than allowed on this\n" + "system. It was not delivered to any recipients.\n"); + break; + + case ERRMESS_LOCAL_SCAN: + case ERRMESS_LOCAL_ACL: + fprintf(f, "Subject: Mail failure - rejected by local scanning code\n\n"); + fprintf(f, + "A message that you sent was rejected by the local scanning code that\n" + "checks incoming messages on this system."); + if (eblock->text1 != NULL) + { + fprintf(f, + " The following error was given:\n\n %s", eblock->text1); + } + fprintf(f, "\n"); + break; + + default: + fprintf(f, "Subject: Mail failure\n\n"); + fprintf(f, + "A message that you sent has caused the error routine to be entered with\n" + "an unknown error number (%d).\n", ident); + break; + } + +/* Now copy the message - headers then the rest of the input if +available, up to the configured limit. */ + +if (size_limit == 0 || size_limit > thismessage_size_limit) + size_limit = thismessage_size_limit; + +if (size_limit > 0 && size_limit < message_size) + { + int x = size_limit; + uschar *k = US""; + if ((x & 1023) == 0) + { + k = US"K"; + x >>= 10; + } + fprintf(f, "\n" + "------ This is a copy of your message, including all the headers.\n" + "------ No more than %d%s characters of the body are included.\n\n", x, k); + } +else fprintf(f, "\n" + "------ This is a copy of your message, including all the headers. ------" + "\n\n"); + +/* If the error occurred before the Received: header was created, its text +field will still be NULL; just omit such a header line. */ + +while (headers != NULL) + { + if (headers->text != NULL) fprintf(f, "%s", CS headers->text); + headers = headers->next; + } + +if (ident != ERRMESS_VLONGHEADER && ident != ERRMESS_VLONGHDRLINE) + fputc('\n', f); + +/* After early detection of an error, the message file may be STDIN, +in which case we might have to terminate on a line containing just "." +as well as on EOF. We may already have the first line in memory. */ + +if (message_file != NULL) + { + int ch; + int state = 1; + BOOL enddot = dot_ends && message_file == stdin; + if (firstline != NULL) fprintf(f, "%s", CS firstline); + while ((ch = fgetc(message_file)) != EOF) + { + fputc(ch, f); + if (size_limit > 0 && ++written > size_limit) break; + if (enddot) + { + if (state == 0) { if (ch == '\n') state = 1; } + else if (state == 1) + { if (ch == '.') state = 2; else if (ch != '\n') state = 0; } + else + { if (ch == '\n') break; else state = 0; } + } + } + } + +/* Close the file, which should send an EOF to the child process +that is receiving the message. Wait for it to finish, without a timeout. */ + +fclose(f); +status = child_close(pid, 0); /* Waits for child to close */ +if (status != 0) + { + uschar *msg = US"Child mail process returned status"; + if (status == -257) + log_write(0, LOG_MAIN, "%s %d: errno=%d: %s", msg, status, errno, + strerror(errno)); + else + log_write(0, LOG_MAIN, "%s %d", msg, status); + return FALSE; + } + +return TRUE; +} + + + +/************************************************* +* Send message to sender * +*************************************************/ + +/* This function is called when errors are detected during the receipt of a +message. Delivery failures are handled separately in deliver.c. + +If there is a valid sender_address, and the failing message is not a local +error message, then this function calls moan_send_message to send a message to +that person. If the sender's address is null, then an error has occurred with a +message that was generated by a mailer daemon. All we can do is to write +information to log files. The same action is taken if local_error_message is +set - this can happen for non null-senders in certain configurations where exim +doesn't run setuid root. + +Arguments: + ident identifies the particular error + eblock chain of error_blocks containing data about the error + headers message's headers (chain) + message_file a FILE where the body of the message can be read + check_sender if TRUE, read the first line of the file for a possible + "From " sender (if a trusted caller) + +Returns: FALSE if there is no sender_address to send to; + else the return from moan_send_message() +*/ + +BOOL +moan_to_sender(int ident, error_block *eblock, header_line *headers, + FILE *message_file, BOOL check_sender) +{ +uschar *firstline = NULL; +uschar *msg = US"Error while reading message with no usable sender address"; + +if (message_reference != NULL) + msg = string_sprintf("%s (R=%s)", msg, message_reference); + +/* Find the sender from a From line if permitted and possible */ + +if (check_sender && message_file != NULL && trusted_caller && + Ufgets(big_buffer, BIG_BUFFER_SIZE, message_file) != NULL) + { + uschar *new_sender = NULL; + if (regex_match_and_setup(regex_From, big_buffer, 0, -1)) + new_sender = expand_string(uucp_from_sender); + if (new_sender != NULL) sender_address = new_sender; + else firstline = big_buffer; + } + +/* If viable sender address, send a message */ + +if (sender_address != NULL && sender_address[0] != 0 && !local_error_message) + return moan_send_message(sender_address, ident, eblock, headers, + message_file, firstline); + +/* Otherwise, we can only log */ + +switch(ident) + { + case ERRMESS_BADARGADDRESS: + case ERRMESS_BADNOADDRESS: + case ERRMESS_BADADDRESS: + log_write(0, LOG_MAIN, "%s: at least one malformed recipient address: " + "%s - %s", msg, eblock->text1, eblock->text2); + break; + + case ERRMESS_IGADDRESS: + case ERRMESS_NOADDRESS: + log_write(0, LOG_MAIN, "%s: no recipient addresses", msg); + break; + + /* This error has already been logged. */ + case ERRMESS_IOERR: + break; + + case ERRMESS_VLONGHEADER: + log_write(0, LOG_MAIN, "%s: excessively long message header section read " + "(more than %d characters)", msg, header_maxsize); + break; + + case ERRMESS_VLONGHDRLINE: + log_write(0, LOG_MAIN, "%s: excessively long message header line read " + "(more than %d characters)", msg, header_line_maxsize); + break; + + case ERRMESS_TOOBIG: + log_write(0, LOG_MAIN, "%s: message too big (limit set to %d)", msg, + thismessage_size_limit); + break; + + case ERRMESS_TOOMANYRECIP: + log_write(0, LOG_MAIN, "%s: too many recipients (max set to %d)", msg, + recipients_max); + break; + + case ERRMESS_LOCAL_SCAN: + log_write(0, LOG_MAIN, "%s: rejected by local_scan: %s", msg, eblock->text1); + break; + + case ERRMESS_LOCAL_ACL: + log_write(0, LOG_MAIN, "%s: rejected by non-SMTP ACL: %s", msg, eblock->text1); + break; + + default: + log_write(0, LOG_MAIN|LOG_PANIC, "%s: unknown error number %d", msg, + ident); + break; + } + +return FALSE; +} + + + +/************************************************* +* Send message to someone * +*************************************************/ + +/* This is called when exim is configured to tell someone (often the +mailmaster) about some incident. + +Arguments: + who address to send mail to + addr chain of deferred addresses whose details are to be included + subject subject text for the message + format a printf() format for the body of the message + ... arguments for the format + +Returns: nothing +*/ + +void +moan_tell_someone(uschar *who, address_item *addr, uschar *subject, + char *format, ...) +{ +FILE *f; +va_list ap; +int fd; +int pid = child_open_exim(&fd); + +if (pid < 0) + { + DEBUG(D_any) debug_printf("Failed to create child to send message: %s\n", + strerror(errno)); + return; + } + +f = fdopen(fd, "wb"); +fprintf(f, "Auto_submitted: auto-generated\n"); +fprintf(f, "From: Mail Delivery System \n", + qualify_domain_sender); +fprintf(f, "To: %s\n", who); +fprintf(f, "Subject: %s\n\n", subject); +va_start(ap, format); +vfprintf(f, format, ap); +va_end(ap); + +if (addr != NULL) + { + fprintf(f, "\nThe following address(es) have yet to be delivered:\n"); + for (; addr != NULL; addr = addr->next) + { + uschar *parent = (addr->parent == NULL)? NULL : addr->parent->address; + fprintf(f, " %s", addr->address); + if (parent != NULL) fprintf(f, " <%s>", parent); + if (addr->basic_errno > 0) fprintf(f, ": %s", strerror(addr->basic_errno)); + if (addr->message != NULL) fprintf(f, ": %s", addr->message); + fprintf(f, "\n"); + } + } + +fclose(f); +child_close(pid, 0); /* Waits for child to close; no timeout */ +} + + + +/************************************************* +* Handle SMTP batch error * +*************************************************/ + +/* This is called when something goes wrong in batched (-bS) SMTP input. +Information is written to stdout and/or stderr, and Exim exits with a non-zero +completion code. BSMTP is almost always called by some other program, so it is +up to that program to interpret the return code and do something with the error +information, and also to preserve the batch input file for human analysis. + +Formerly, Exim used to attempt to continue after some errors, but this strategy +has been abandoned as it can lead to loss of messages. + +Arguments: + cmd_buffer the command causing the error, or NULL + format a printf() format + ... arguments for the format + +Returns: does not return; exits from the program + exit code = 1 if some messages were accepted + exit code = 2 if no messages were accepted +*/ + +void +moan_smtp_batch(uschar *cmd_buffer, char *format, ...) +{ +va_list ap; +int yield = (receive_messagecount > 0)? 1 : 2; + +DEBUG(D_any) debug_printf("Handling error in batched SMTP input\n"); + +/* On stdout, write stuff that a program could parse fairly easily. */ + +va_start(ap, format); +vfprintf(stdout, format, ap); +va_end(ap); + +fprintf(stdout, "\nTransaction started in line %d\n", + bsmtp_transaction_linecount); +fprintf(stdout, "Error detected in line %d\n", receive_linecount); +if (cmd_buffer != NULL) fprintf(stdout, "%s\n", cmd_buffer); + +/* On stderr, write stuff for human consumption */ + +fprintf(stderr, + "An error was detected while processing a file of BSMTP input.\n" + "The error message was:\n\n "); + +va_start(ap, format); +vfprintf(stderr, format, ap); +va_end(ap); + +fprintf(stderr, + "\n\nThe SMTP transaction started in line %d.\n" + "The error was detected in line %d.\n", + bsmtp_transaction_linecount, receive_linecount); + +if (cmd_buffer != NULL) + { + fprintf(stderr, "The SMTP command at fault was:\n\n %s\n\n", + cmd_buffer); + } + +fprintf(stderr, "%d previous message%s successfully processed.\n", + receive_messagecount, (receive_messagecount == 1)? " was" : "s were"); + +fprintf(stderr, "The rest of the batch was abandoned.\n"); + +exim_exit(yield); +} + + + + +/************************************************* +* Check for error copies * +*************************************************/ + +/* This function is passed the recipient of an error message, and must check +the error_copies string to see whether there is an additional recipient list to +which errors for this recipient must be bcc'd. The incoming recipient is always +fully qualified. + +Argument: recipient address +Returns: additional recipient list or NULL +*/ + +uschar * +moan_check_errorcopy(uschar *recipient) +{ +uschar *item, *localpart, *domain; +uschar *listptr = errors_copy; +uschar *yield = NULL; +uschar buffer[256]; +int sep = 0; +int llen; + +if (errors_copy == NULL) return NULL; + +/* Set up pointer to the local part and domain, and compute the +length of the local part. */ + +localpart = recipient; +domain = Ustrchr(recipient, '@'); +if (domain == NULL) return NULL; /* should not occur, but avoid crash */ +llen = domain++ - recipient; + +/* Scan through the configured items */ + +while ((item = string_nextinlist(&listptr, &sep, buffer, sizeof(buffer))) + != NULL) + { + uschar *newaddress = item; + uschar *pattern = string_dequote(&newaddress); + + /* If no new address found, just skip this item. */ + + while (isspace(*newaddress)) newaddress++; + if (*newaddress == 0) continue; + + /* We now have an item to match as an address in item, and the additional + address in newaddress. If the pattern matches, expand the new address string + and return it. During expansion, make local part and domain available for + insertion. This requires a copy to be made; we can't just temporarily + terminate it, as the whole address is required for $0. */ + + if (match_address_list(recipient, TRUE, TRUE, &pattern, NULL, 0, UCHAR_MAX+1, + NULL) == OK) + { + uschar temp[256]; + Ustrncpy(temp, localpart, llen); + temp[llen] = 0; + deliver_localpart = temp; + deliver_domain = domain; + yield = expand_string_copy(newaddress); + deliver_domain = deliver_localpart = NULL; + if (yield == NULL) + log_write(0, LOG_MAIN|LOG_PANIC, "Failed to expand %s when processing " + "errors_copy: %s", newaddress, expand_string_message); + break; + } + } + +DEBUG(D_any) debug_printf("errors_copy check returned %s\n", + (yield == NULL)? US"NULL" : yield); + +expand_nmax = -1; +return yield; +} + + + +/************************************************ +* Handle skipped syntax errors * +************************************************/ + +/* This function is called by the redirect router when it has skipped over one +or more syntax errors in the list of addresses. If there is an address to mail +to, send a message, and always write the information to the log. In the case of +a filter file, a "syntax error" might actually be something else, such as the +inability to open a log file. Thus, the wording of the error message is +general. + +Arguments: + rname the router name + eblock chain of error blocks + syntax_errors_to address to send mail to, or NULL + some TRUE if some addresses were generated; FALSE if none were + custom custom message text + +Returns: FALSE if string expansion failed; TRUE otherwise +*/ + +BOOL +moan_skipped_syntax_errors(uschar *rname, error_block *eblock, + uschar *syntax_errors_to, BOOL some, uschar *custom) +{ +int pid, fd; +uschar *s, *t; +FILE *f; +error_block *e; + +for (e = eblock; e != NULL; e = e->next) + { + if (e->text2 != NULL) + log_write(0, LOG_MAIN, "%s router: skipped error: %s in \"%s\"", + rname, e->text1, e->text2); + else + log_write(0, LOG_MAIN, "%s router: skipped error: %s", rname, + e->text1); + } + +if (syntax_errors_to == NULL) return TRUE; + +s = expand_string(syntax_errors_to); +if (s == NULL) + { + log_write(0, LOG_MAIN, "%s router failed to expand %s: %s", rname, + syntax_errors_to, expand_string_message); + return FALSE; + } + +/* If we can't create a process to send the message, just forget about +it. */ + +pid = child_open_exim(&fd); + +if (pid < 0) + { + DEBUG(D_any) debug_printf("Failed to create child to send message: %s\n", + strerror(errno)); + return TRUE; + } + +f = fdopen(fd, "wb"); +fprintf(f, "Auto_submitted: auto-generated\n"); +fprintf(f, "From: Mail Delivery System \n", + qualify_domain_sender); +fprintf(f, "To: %s\n", s); +fprintf(f, "Subject: error(s) in forwarding or filtering\n\n"); + +if (custom != NULL) + { + t = expand_string(custom); + if (t == NULL) + { + log_write(0, LOG_MAIN, "%s router failed to expand %s: %s", rname, + custom, expand_string_message); + return FALSE; + } + fprintf(f, "%s\n\n", t); + } + +fprintf(f, "The %s router encountered the following error(s):\n\n", + rname); + +for (e = eblock; e != NULL; e = e->next) + { + fprintf(f, " %s", e->text1); + if (e->text2 != NULL) + fprintf(f, " in the address\n \"%s\"", e->text2); + fprintf(f, "\n\n"); + } + +if (some) + fprintf(f, "Other addresses were processed normally.\n"); +else + fprintf(f, "No valid addresses were generated.\n"); + +fclose(f); +child_close(pid, 0); /* Waits for child to close; no timeout */ + +return TRUE; +} + +/* End of moan.c */ diff --git a/src/src/mytypes.h b/src/src/mytypes.h new file mode 100644 index 000000000..18d5e8046 --- /dev/null +++ b/src/src/mytypes.h @@ -0,0 +1,102 @@ +/* $Cambridge: exim/src/src/mytypes.h,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + + +/* This header file contains type definitions and macros that I use as +"standard" in the code of Exim and its utilities. Make it idempotent because +local_scan.h includes it and exim.h includes them both (to get this earlier). */ + +#ifndef MYTYPES_H +#define MYTYPES_H + + +#define FALSE 0 +#define TRUE 1 +#define TRUE_UNSET 2 + + +/* If gcc is being used to compile Exim, we can use its facility for checking +the arguments of printf-like functions. This is done by a macro. */ + +#ifdef __GNUC__ +#define PRINTF_FUNCTION __attribute__((format(printf,1,2))) +#else +#define PRINTF_FUNCTION +#endif + + +/* Some operating systems (naughtily, imo) include a definition for "uchar" in +the standard header files, so we use "uschar". Solaris has u_char in +sys/types.h. This is just a typing convenience, of course. */ + +typedef int BOOL; +typedef unsigned char uschar; + + +/* These macros save typing for the casting that is needed to cope with the +mess that is "char" in ISO/ANSI C. Having now been bitten enough times by +systems where "char" is actually signed, I've converted Exim to use entirely +unsigned chars, except in a few special places such as arguments that are +almost always literal strings. */ + +#define CS (char *) +#define CCS (const char *) +#define CSS (char **) +#define US (unsigned char *) +#define CUS (const unsigned char *) +#define USS (unsigned char **) + +/* The C library string functions expect "char *" arguments. Use macros to +avoid having to write a cast each time. We do this for string and file +functions that are called quite often; for other calls to external libraries +(which are on the whole special-purpose) we just use individual casts. */ + +#define Uatoi(s) atoi(CCS(s)) +#define Uatol(s) atol(CCS(s)) +#define Uchdir(s) chdir(CCS(s)) +#define Uchmod(s,n) chmod(CCS(s),n) +#define Uchown(s,n,m) chown(CCS(s),n,m) +#define Ufgets(b,n,f) fgets(CS(b),n,f) +#define Ufopen(s,t) fopen(CCS(s),CCS(t)) +#define Ulink(s,t) link(CCS(s),CCS(t)) +#define Ulstat(s,t) lstat(CCS(s),t) + +#ifdef O_BINARY /* This is for Cygwin, */ +#define Uopen(s,n,m) open(CCS(s),(n)|O_BINARY,m) /* where all files must */ +#else /* be opened as binary */ +#define Uopen(s,n,m) open(CCS(s),n,m) /* to avoid problems */ +#endif /* with CRLF endings. */ +#define Uread(f,b,l) read(f,CS(b),l) +#define Urename(s,t) rename(CCS(s),CCS(t)) +#define Ustat(s,t) stat(CCS(s),t) +#define Ustrcat(s,t) strcat(CS(s),CCS(t)) +#define Ustrchr(s,n) US strchr(CCS(s),n) +#define CUstrchr(s,n) CUS strchr(CCS(s),n) +#define CUstrerror(n) CUS strerror(n) +#define Ustrcmp(s,t) strcmp(CCS(s),CCS(t)) +#define Ustrcpy(s,t) strcpy(CS(s),CCS(t)) +#define Ustrcspn(s,t) strcspn(CCS(s),CCS(t)) +#define Ustrftime(s,m,f,t) strftime(CS(s),m,f,t) +#define Ustrlen(s) (int)strlen(CCS(s)) +#define Ustrncat(s,t,n) strncat(CS(s),CCS(t),n) +#define Ustrncmp(s,t,n) strncmp(CCS(s),CCS(t),n) +#define Ustrncpy(s,t,n) strncpy(CS(s),CCS(t),n) +#define Ustrpbrk(s,t) strpbrk(CCS(s),CCS(t)) +#define Ustrrchr(s,n) US strrchr(CCS(s),n) +#define CUstrrchr(s,n) CUS strrchr(CCS(s),n) +#define Ustrspn(s,t) strspn(CCS(s),CCS(t)) +#define Ustrstr(s,t) US strstr(CCS(s),CCS(t)) +#define CUstrstr(s,t) CUS strstr(CCS(s),CCS(t)) +#define Ustrtod(s,t) strtod(CCS(s),CSS(t)) +#define Ustrtol(s,t,b) strtol(CCS(s),CSS(t),b) +#define Ustrtoul(s,t,b) strtoul(CCS(s),CSS(t),b) +#define Uunlink(s) unlink(CCS(s)) +#endif + +/* End of mytypes.h */ diff --git a/src/src/os.c b/src/src/os.c new file mode 100644 index 000000000..810baf6b6 --- /dev/null +++ b/src/src/os.c @@ -0,0 +1,810 @@ +/* $Cambridge: exim/src/src/os.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +#ifdef STAND_ALONE +#include +#include +#include +#endif + +/* This source file contains "default" system-dependent functions which +provide functionality (or lack of it) in cases where the OS-specific os.c +file has not. Some of them are tailored by macros defined in os.h files. */ + + +#ifndef OS_RESTARTING_SIGNAL +/************************************************* +* Set up restarting signal * +*************************************************/ + +/* This function has the same functionality as the ANSI C signal() function, +except that it arranges that, if the signal happens during a system call, the +system call gets restarted. (Also, it doesn't return a result.) Different +versions of Unix have different defaults, and different ways of setting up a +restarting signal handler. If the functionality is not available, the signal +should be set to be ignored. This function is used only for catching SIGUSR1. +*/ + +void +os_restarting_signal(int sig, void (*handler)(int)) +{ +/* Many systems have the SA_RESTART sigaction for specifying that a signal +should restart system calls. These include SunOS5, AIX, BSDI, IRIX, FreeBSD, +OSF1, Linux and HP-UX 10 (but *not* HP-UX 9). */ + +#ifdef SA_RESTART +struct sigaction act; +act.sa_handler = handler; +sigemptyset(&(act.sa_mask)); +act.sa_flags = SA_RESTART; +sigaction(sig, &act, NULL); + +#ifdef STAND_ALONE +printf("Used SA_RESTART\n"); +#endif + +/* SunOS4 and Ultrix default to non-interruptable signals, with SV_INTERRUPT +for making them interruptable. This seems to be a dying fashion. */ + +#elif defined SV_INTERRUPT +signal(sig, handler); + +#ifdef STAND_ALONE +printf("Used default signal()\n"); +#endif + + +/* If neither SA_RESTART nor SV_INTERRUPT is available we don't know how to +set up a restarting signal, so simply suppress the facility. */ + +#else +signal(sig, SIG_IGN); + +#ifdef STAND_ALONE +printf("Used SIG_IGN\n"); +#endif + +#endif +} + +#endif /* OS_RESTARTING_SIGNAL */ + + +#ifndef OS_NON_RESTARTING_SIGNAL +/************************************************* +* Set up non-restarting signal * +*************************************************/ + +/* This function has the same functionality as the ANSI C signal() function, +except that it arranges that, if the signal happens during a system call, the +system call gets interrupted. (Also, it doesn't return a result.) Different +versions of Unix have different defaults, and different ways of setting up a +non-restarting signal handler. For systems for which we don't know what to do, +just use the normal signal() function and hope for the best. */ + +void +os_non_restarting_signal(int sig, void (*handler)(int)) +{ +/* Many systems have the SA_RESTART sigaction for specifying that a signal +should restart system calls. These include SunOS5, AIX, BSDI, IRIX, FreeBSD, +OSF1, Linux and HP-UX 10 (but *not* HP-UX 9). */ + +#ifdef SA_RESTART +struct sigaction act; +act.sa_handler = handler; +sigemptyset(&(act.sa_mask)); +act.sa_flags = 0; +sigaction(sig, &act, NULL); + +#ifdef STAND_ALONE +printf("Used sigaction() with flags = 0\n"); +#endif + +/* SunOS4 and Ultrix default to non-interruptable signals, with SV_INTERRUPT +for making them interruptable. This seems to be a dying fashion. */ + +#elif defined SV_INTERRUPT +struct sigvec sv; +sv.sv_handler = handler; +sv.sv_flags = SV_INTERRUPT; +sv.sv_mask = -1; +sigvec(sig, &sv, NULL); + +#ifdef STAND_ALONE +printf("Used sigvec() with flags = SV_INTERRUPT\n"); +#endif + +/* If neither SA_RESTART nor SV_INTERRUPT is available we don't know how to +set up a restarting signal, so just use the standard signal() function. */ + +#else +signal(sig, handler); + +#ifdef STAND_ALONE +printf("Used default signal()\n"); +#endif + +#endif +} + +#endif /* OS_NON_RESTARTING_SIGNAL */ + + + +#ifdef STRERROR_FROM_ERRLIST +/************************************************* +* Provide strerror() for non-ANSI libraries * +*************************************************/ + +/* Some old-fashioned systems still around (e.g. SunOS4) don't have strerror() +in their libraries, but can provide the same facility by this simple +alternative function. */ + +char * +strerror(int n) +{ +if (n < 0 || n >= sys_nerr) return "unknown error number"; +return sys_errlist[n]; +} +#endif /* STRERROR_FROM_ERRLIST */ + + + +#ifndef OS_STRSIGNAL +/************************************************* +* Provide strsignal() for systems without * +*************************************************/ + +/* Some systems have strsignal() to turn signal numbers into names; others +may have other means of doing this. This function is used for those systems +that have nothing. It provides a basic translation for the common standard +signal numbers. I've been extra cautious with the ifdef's here. Probably more +than is necessary... */ + +char * +os_strsignal(int n) +{ +switch (n) + { + #ifdef SIGHUP + case SIGHUP: return "hangup"; + #endif + + #ifdef SIGINT + case SIGINT: return "interrupt"; + #endif + + #ifdef SIGQUIT + case SIGQUIT: return "quit"; + #endif + + #ifdef SIGILL + case SIGILL: return "illegal instruction"; + #endif + + #ifdef SIGTRAP + case SIGTRAP: return "trace trap"; + #endif + + #ifdef SIGABRT + case SIGABRT: return "abort"; + #endif + + #ifdef SIGEMT + case SIGEMT: return "EMT instruction"; + #endif + + #ifdef SIGFPE + case SIGFPE: return "arithmetic exception"; + #endif + + #ifdef SIGKILL + case SIGKILL: return "killed"; + #endif + + #ifdef SIGBUS + case SIGBUS: return "bus error"; + #endif + + #ifdef SIGSEGV + case SIGSEGV: return "segmentation fault"; + #endif + + #ifdef SIGSYS + case SIGSYS: return "bad system call"; + #endif + + #ifdef SIGPIPE + case SIGPIPE: return "broken pipe"; + #endif + + #ifdef SIGALRM + case SIGALRM: return "alarm"; + #endif + + #ifdef SIGTERM + case SIGTERM: return "terminated"; + #endif + + #ifdef SIGUSR1 + case SIGUSR1: return "user signal 1"; + #endif + + #ifdef SIGUSR2 + case SIGUSR2: return "user signal 2"; + #endif + + #ifdef SIGCHLD + case SIGCHLD: return "child stop or exit"; + #endif + + #ifdef SIGPWR + case SIGPWR: return "power fail/restart"; + #endif + + #ifdef SIGURG + case SIGURG: return "urgent condition on I/O channel"; + #endif + + #ifdef SIGSTOP + case SIGSTOP: return "stop"; + #endif + + #ifdef SIGTSTP + case SIGTSTP: return "stop from tty"; + #endif + + #ifdef SIGXCPU + case SIGXCPU: return "exceeded CPU limit"; + #endif + + #ifdef SIGXFSZ + case SIGXFSZ: return "exceeded file size limit"; + #endif + + default: return "unrecognized signal number"; + } +} +#endif /* OS_STRSIGNAL */ + + + +#ifndef OS_STREXIT +/************************************************* +* Provide strexit() for systems without * +*************************************************/ + +/* Actually, I don't know of any system that has a strexit() function to turn +exit codes into text, but this function is implemented this way so that if any +OS does have such a thing, it could be used instead of this build-in one. */ + +char * +os_strexit(int n) +{ +switch (n) + { + /* On systems without sysexits.h we can assume only those exit codes + that are given a default value in exim.h. */ + + #ifndef NO_SYSEXITS + case EX_USAGE: return "(could mean usage or syntax error)"; + case EX_DATAERR: return "(could mean error in input data)"; + case EX_NOINPUT: return "(could mean input data missing)"; + case EX_NOUSER: return "(could mean user nonexistent)"; + case EX_NOHOST: return "(could mean host nonexistent)"; + case EX_SOFTWARE: return "(could mean internal software error)"; + case EX_OSERR: return "(could mean internal operating system error)"; + case EX_OSFILE: return "(could mean system file missing)"; + case EX_IOERR: return "(could mean input/output error)"; + case EX_PROTOCOL: return "(could mean protocol error)"; + case EX_NOPERM: return "(could mean permission denied)"; + #endif + + case EX_EXECFAILED: return "(could mean unable to exec or command does not exist)"; + case EX_UNAVAILABLE: return "(could mean service or program unavailable)"; + case EX_CANTCREAT: return "(could mean can't create output file)"; + case EX_TEMPFAIL: return "(could mean temporary error)"; + case EX_CONFIG: return "(could mean configuration error)"; + default: return ""; + } +} +#endif /* OS_STREXIT */ + + + + +/*********************************************************** +* Load average function * +***********************************************************/ + +/* Although every Unix seems to have a different way of getting the load +average, a number of them have things in common. Some common variants are +provided below, but if an OS has unique requirements it can be handled in +a specific os.c file. What is required is a function called os_getloadavg +which takes no arguments and passes back the load average * 1000 as an int, +or -1 if no data is available. */ + + +/* ----------------------------------------------------------------------- */ +/* If the OS has got a BSD getloadavg() function, life is very easy. */ + +#if !defined(OS_LOAD_AVERAGE) && defined(HAVE_BSD_GETLOADAVG) +#define OS_LOAD_AVERAGE + +int +os_getloadavg(void) +{ +double avg; +int loads = getloadavg (&avg, 1); +if (loads != 1) return -1; +return (int)(avg * 1000.0); +} +#endif +/* ----------------------------------------------------------------------- */ + + + +/* ----------------------------------------------------------------------- */ +/* Only SunOS5 has the kstat functions as far as I know, but put the code +here as there is the -hal variant, and other systems might follow this road one +day. */ + +#if !defined(OS_LOAD_AVERAGE) && defined(HAVE_KSTAT) +#define OS_LOAD_AVERAGE + +#include + +int +os_getloadavg(void) +{ +int avg; +kstat_ctl_t *kc; +kstat_t *ksp; +kstat_named_t *kn; + +if ((kc = kstat_open()) == NULL || + (ksp = kstat_lookup(kc, LOAD_AVG_KSTAT_MODULE, 0, LOAD_AVG_KSTAT)) + == NULL || + kstat_read(kc, ksp, NULL) < 0 || + (kn = kstat_data_lookup(ksp, LOAD_AVG_SYMBOL)) == NULL) + return -1; + +avg = (int)(((double)(kn->LOAD_AVG_FIELD)/FSCALE) * 1000.0); + +kstat_close(kc); +return avg; +} + +#endif +/* ----------------------------------------------------------------------- */ + + + +/* ----------------------------------------------------------------------- */ +/* Handle OS where a kernel symbol has to be read from /dev/kmem */ + +#if !defined(OS_LOAD_AVERAGE) && defined(HAVE_DEV_KMEM) +#define OS_LOAD_AVERAGE + +#include + +static int avg_kd = -1; +static long avg_offset; + +int +os_getloadavg(void) +{ +LOAD_AVG_TYPE avg; + +if (avg_kd < 0) + { + struct nlist nl[2]; + nl[0].n_name = LOAD_AVG_SYMBOL; + nl[1].n_name = ""; + nlist (KERNEL_PATH, nl); + avg_offset = (long)nl[0].n_value; + avg_kd = open ("/dev/kmem", 0); + if (avg_kd < 0) return -1; + (void) fcntl(avg_kd, F_SETFD, FD_CLOEXEC); + } + +if (lseek (avg_kd, avg_offset, 0) == -1L + || read (avg_kd, (char *)(&avg), sizeof (avg)) != sizeof(avg)) + return -1; + +return (int)(((double)avg/FSCALE)*1000.0); +} + +#endif +/* ----------------------------------------------------------------------- */ + + + +/* ----------------------------------------------------------------------- */ +/* If nothing is known about this OS, then the load average facility is +not available. */ + +#ifndef OS_LOAD_AVERAGE + +int +os_getloadavg(void) +{ +return -1; +} + +#endif + +/* ----------------------------------------------------------------------- */ + + + +#if !defined FIND_RUNNING_INTERFACES +/************************************************* +* Find all the running network interfaces * +*************************************************/ + +/* Finding all the running interfaces is something that has os-dependent +tweaks, even in the IPv4 case, and it gets worse for IPv6, which is why this +code is now in the os-dependent source file. There is a common function which +works on most OS (except IRIX) for IPv4 interfaces, and, with some variations +controlled by macros, on at least one OS for IPv6 and IPv4 interfaces. On Linux +with IPv6, the common function is used for the IPv4 interfaces and additional +code used for IPv6. Consequently, the real function is called +os_common_find_running_interfaces() so that it can be called from the Linux +function. On non-Linux systems, the macro for os_find_running_interfaces just +calls the common function; on Linux it calls the Linux function. + +This function finds the addresses of all the running interfaces on the machine. +A chain of blocks containing the textual form of the addresses is returned. + +Problems: + + (1) Solaris 2 has the SIOGIFNUM call to get the number of interfaces, but + other OS (including Solaris 1) appear not to. So just screw in a largeish + fixed number, defined by MAX_INTERFACES. This is in the config.h file and + can be changed in Local/Makefile. Unfortunately, the www addressing scheme + means that some hosts have a very large number of virtual interfaces. Such + hosts are recommended to set local_interfaces to avoid problems with this. + + (2) If the standard code is run on IRIX, it does not return any alias + interfaces. There is special purpose code for that operating system, which + uses the sysctl() function. The code is in OS/os.c-IRIX, and this code isn't + used on that OS. + + (3) Some experimental/developing OS (e.g. GNU/Hurd) do not have any means + of finding the interfaces. If NO_FIND_INTERFACES is set, a fudge-up is used + instead. + + (4) Some operating systems set the IP address in what SIOCGIFCONF returns; + others do not, and require SIOCGIFADDR to be called to get it. For most of + the former, calling the latter does no harm, but it causes grief on Linux and + BSD systems in the case of IP aliasing, so a means of cutting it out is + provided. + +Arguments: none +Returns: a chain of ip_address_items, each pointing to a textual + version of an IP address, with the port field set to zero +*/ + + +#ifndef NO_FIND_INTERFACES + +/* If there is IPv6 support, and SIOCGLIFCONF is defined, define macros to +use these new, longer versions of the old IPv4 interfaces. Otherwise, define +the macros to use the historical versions. */ + +#if HAVE_IPV6 && defined SIOCGLIFCONF +#define V_ifconf lifconf +#define V_ifreq lifreq +#define V_GIFADDR SIOCGLIFADDR +#define V_GIFCONF SIOCGLIFCONF +#define V_GIFFLAGS SIOCGLIFFLAGS +#define V_ifc_buf lifc_buf +#define V_ifc_family lifc_family +#define V_ifc_flags lifc_flags +#define V_ifc_len lifc_len +#define V_ifr_addr lifr_addr +#define V_ifr_flags lifr_flags +#define V_ifr_name lifr_name +#define V_FAMILY_QUERY AF_UNSPEC +#define V_family ss_family +#else +#define V_ifconf ifconf +#define V_ifreq ifreq +#define V_GIFADDR SIOCGIFADDR +#define V_GIFCONF SIOCGIFCONF +#define V_GIFFLAGS SIOCGIFFLAGS +#define V_ifc_buf ifc_buf +#define V_ifc_family ifc_family +#define V_ifc_flags ifc_flags +#define V_ifc_len ifc_len +#define V_ifr_addr ifr_addr +#define V_ifr_flags ifr_flags +#define V_ifr_name ifr_name +#define V_family sa_family +#endif + +/* In all cases of IPv6 support, use an IPv6 socket. Otherwise (at least on +Solaris 8) the call to read the flags doesn't work for IPv6 interfaces. If +we find we can't actually make an IPv6 socket, the code will revert to trying +an IPv4 socket. */ + +#if HAVE_IPV6 +#define FAMILY AF_INET6 +#else +#define FAMILY AF_INET +#endif + +/* OK, after all that preliminary stuff, here's the code. */ + +ip_address_item * +os_common_find_running_interfaces(void) +{ +struct V_ifconf ifc; +struct V_ifreq ifreq; +int vs; +ip_address_item *yield = NULL; +ip_address_item *last = NULL; +ip_address_item *next; +char *cp; +char buf[MAX_INTERFACES*sizeof(struct V_ifreq)]; +struct sockaddr *addrp; +size_t len = 0; +char addrbuf[256]; + +/* We have to create a socket in order to do ioctls on it to find out +what we want to know. */ + +if ((vs = socket(FAMILY, SOCK_DGRAM, 0)) < 0) + { + #if HAVE_IPV6 + DEBUG(D_interface) + debug_printf("Unable to create IPv6 socket to find interface addresses:\n " + "error %d %s\nTrying for an IPv4 socket\n", errno, strerror(errno)); + vs = socket(AF_INET, SOCK_DGRAM, 0); + if (vs < 0) + #endif + log_write(0, LOG_PANIC_DIE, "Unable to create IPv4 socket to find interface " + "addresses: %d %s", errno, strerror(errno)); + } + +/* Get the interface configuration. Some additional data is required when the +new structures are in use. */ + +ifc.V_ifc_len = sizeof(buf); +ifc.V_ifc_buf = buf; + +#ifdef V_FAMILY_QUERY +ifc.V_ifc_family = V_FAMILY_QUERY; +ifc.V_ifc_flags = 0; +#endif + +if (ioctl(vs, V_GIFCONF, (char *)&ifc) < 0) + log_write(0, LOG_PANIC_DIE, "Unable to get interface configuration: %d %s", + errno, strerror(errno)); + +/* If the buffer is big enough, the ioctl sets the value of ifc.V_ifc_len to +the amount actually used. If the buffer isn't big enough, at least on some +operating systems, ifc.V_ifc_len still gets set to correspond to the total +number of interfaces, even though they don't all fit in the buffer. */ + +if (ifc.V_ifc_len > sizeof(buf)) + { + ifc.V_ifc_len = sizeof(buf); + DEBUG(D_interface) + debug_printf("more than %d interfaces found: remainder not used\n" + "(set MAX_INTERFACES in Local/Makefile and rebuild if you want more)\n", + MAX_INTERFACES); + } + +/* For each interface, check it is an IP interface, get its flags, and see if +it is up; if not, skip. + +BSD systems differ from others in what SIOCGIFCONF returns. Other systems +return a vector of ifreq structures whose size is as defined by the structure. +BSD systems allow sockaddrs to be longer than their sizeof, which in turn makes +the ifreq structures longer than their sizeof. The code below has its origins +in amd and ifconfig; it uses the sa_len field of each sockaddr to determine +each item's length. + +This is complicated by the fact that, at least on BSD systems, the data in the +buffer is not guaranteed to be aligned. Thus, we must first copy the basic +struct to some aligned memory before looking at the field in the fixed part to +find its length, and then recopy the correct length. */ + +for (cp = buf; cp < buf + ifc.V_ifc_len; cp += len) + { + memcpy((char *)&ifreq, cp, sizeof(ifreq)); + + #ifndef HAVE_SA_LEN + len = sizeof(struct V_ifreq); + + #else + len = ((ifreq.ifr_addr.sa_len > sizeof(ifreq.ifr_addr))? + ifreq.ifr_addr.sa_len : sizeof(ifreq.ifr_addr)) + + sizeof(ifreq.V_ifr_name); + if (len > sizeof(addrbuf)) + log_write(0, LOG_PANIC_DIE, "Address for %s interface is absurdly long", + ifreq.V_ifr_name); + + #endif + + /* If not an IP interface, skip */ + + if (ifreq.V_ifr_addr.V_family != AF_INET + #if HAVE_IPV6 + && ifreq.V_ifr_addr.V_family != AF_INET6 + #endif + ) continue; + + /* Get the interface flags, and if the interface is down, continue. Formerly, + we treated the inability to get the flags as a panic-die error. However, it + seems that on some OS (Solaris 9 being the case noted), it is possible to + have an interface in this list for which this call fails because the + interface hasn't been "plumbed" to any protocol (IPv4 or IPv6). Therefore, + we now just treat this case as "down" as well. */ + + if (ioctl(vs, V_GIFFLAGS, (char *)&ifreq) < 0) + { + continue; + /************* + log_write(0, LOG_PANIC_DIE, "Unable to get flags for %s interface: %d %s", + ifreq.V_ifr_name, errno, strerror(errno)); + *************/ + } + if ((ifreq.V_ifr_flags & IFF_UP) == 0) continue; + + /* On some operating systems we have to get the IP address of the interface + by another call. On others, it's already there, but we must reinstate the + data in ifreq, because SIOCGIFFLAGS may wreck it. */ + + #ifndef SIOCGIFCONF_GIVES_ADDR + if (ioctl(vs, V_GIFADDR, (char *)&ifreq) < 0) + log_write(0, LOG_PANIC_DIE, "Unable to get IP address for %s interface: " + "%d %s", ifreq.V_ifr_name, errno, strerror(errno)); + addrp = &ifreq.V_ifr_addr; + + #else + memcpy((char *)&ifreq, cp, sizeof(ifreq)); + memcpy(addrbuf, (char *)&(ifreq.V_ifr_addr), len - sizeof(ifreq.V_ifr_name)); + addrp = (struct sockaddr *)addrbuf; + #endif + + /* Create a data block for the address, fill in the data, and put it on the + chain. */ + + next = store_get(sizeof(ip_address_item)); + next->next = NULL; + next->port = 0; + (void)host_ntoa(-1, addrp, next->address, NULL); + + if (yield == NULL) yield = last = next; else + { + last->next = next; + last = next; + } + + DEBUG(D_interface) debug_printf("Actual local interface address is %s (%s)\n", + last->address, ifreq.V_ifr_name); + } + +/* Close the socket, and return the chain of data blocks. */ + +close(vs); +return yield; +} + +#else /* NO_FIND_INTERFACES */ + +/* Some experimental or developing OS (e.g. GNU/Hurd) do not have the ioctls, +and there is no other way to get a list of the (IP addresses of) local +interfaces. We just return the loopback address(es). */ + +ip_address_item * +os_common_find_running_interfaces(void) +{ +ip_address_item *yield = store_get(sizeof(address_item)); +yield->address = US"127.0.0.1"; +yield->port = 0; +yield->next = NULL; + +#if HAVE_IPV6 +yield->next = store_get(sizeof(address_item)); +yield->next->address = US"::1"; +yield->next->port = 0; +yield->next->next = NULL; +#endif + +DEBUG(D_interface) debug_printf("Unable to find local interface addresses " + "on this OS: returning loopback address(es)\n"); +return yield; +} + +#endif /* NO_FIND_INTERFACES */ +#endif /* FIND_RUNNING_INTERFACES */ + + + + + +/************************************************* +************************************************** +* Stand-alone test program * +************************************************** +*************************************************/ + + +#ifdef STAND_ALONE + +#ifdef CLOCKS_PER_SEC +#define REAL_CLOCK_TICK CLOCKS_PER_SEC +#else + #ifdef CLK_TCK + #define REAL_CLOCK_TICK CLK_TCK + #else + #define REAL_CLOCK_TICK 1000000 /* SunOS4 */ + #endif +#endif + + +int main(int argc, char **argv) +{ +char buffer[128]; +int fd = fileno(stdin); +int rc; + +printf("Testing restarting signal; wait for handler message, then type a line\n"); +strcpy(buffer, "*** default ***\n"); +os_restarting_signal(SIGALRM, sigalrm_handler); +alarm(2); +if ((rc = read(fd, buffer, sizeof(buffer))) < 0) + printf("No data read\n"); +else + { + buffer[rc] = 0; + printf("Read: %s", buffer); + } +alarm(0); + +printf("Testing non-restarting signal; should read no data after handler message\n"); +strcpy(buffer, "*** default ***\n"); +os_non_restarting_signal(SIGALRM, sigalrm_handler); +alarm(2); +if ((rc = read(fd, buffer, sizeof(buffer))) < 0) + printf("No data read\n"); +else + { + buffer[rc] = 0; + printf("Read: %s", buffer); + } +alarm(0); + +printf("Testing load averages (last test - ^C to kill)\n"); +for (;;) + { + int avg; + clock_t used; + clock_t before = clock(); + avg = os_getloadavg(); + used = clock() - before; + printf("cpu time = %.2f ", (double)used/REAL_CLOCK_TICK); + if (avg < 0) + { + printf("load average not available\n"); + break; + } + printf("load average = %.2f\n", (double)avg/1000.0); + sleep(2); + } +return 0; +} + +#endif + +/* End of os.c */ diff --git a/src/src/osfunctions.h b/src/src/osfunctions.h new file mode 100644 index 000000000..131f1a1fd --- /dev/null +++ b/src/src/osfunctions.h @@ -0,0 +1,23 @@ +/* $Cambridge: exim/src/src/osfunctions.h,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Prototypes for os-specific functions. For utilities, we don't need the one +that uses a type that isn't defined for them. */ + +#ifndef COMPILE_UTILITY +extern ip_address_item *os_common_find_running_interfaces(void); +#endif + +extern int os_getloadavg(void); +extern void os_restarting_signal(int, void (*)(int)); +extern void os_non_restarting_signal(int, void (*)(int)); +extern char *os_strexit(int); /* char to match os_strsignal */ +extern char *os_strsignal(int); /* char to match strsignal in some OS */ + +/* End of osfunctions.h */ diff --git a/src/src/parse.c b/src/src/parse.c new file mode 100644 index 000000000..e42e0c8a8 --- /dev/null +++ b/src/src/parse.c @@ -0,0 +1,1777 @@ +/* $Cambridge: exim/src/src/parse.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Functions for parsing addresses */ + + +#include "exim.h" + + +static uschar *last_comment_position; + + + +/* In stand-alone mode, provide a replacement for deliver_make_addr() +and rewrite_address[_qualify]() so as to avoid having to drag in too much +redundant apparatus. */ + +#ifdef STAND_ALONE + +address_item *deliver_make_addr(uschar *address, BOOL copy) +{ +address_item *addr = store_get(sizeof(address_item)); +addr->next = NULL; +addr->parent = NULL; +addr->address = address; +return addr; +} + +uschar *rewrite_address(uschar *recipient, BOOL dummy1, BOOL dummy2, rewrite_rule + *dummy3, int dummy4) +{ +return recipient; +} + +uschar *rewrite_address_qualify(uschar *recipient, BOOL dummy1) +{ +return recipient; +} + +#endif + + + + +/************************************************* +* Find the end of an address * +*************************************************/ + +/* Scan over a string looking for the termination of an address at a comma, +or end of the string. It's the source-routed addresses which cause much pain +here. Although Exim ignores source routes, it must recognize such addresses, so +we cannot get rid of this logic. + +Argument: + s pointer to the start of an address + nl_ends if TRUE, '\n' terminates an address + +Returns: pointer past the end of the address + (i.e. points to null or comma) +*/ + +uschar * +parse_find_address_end(uschar *s, BOOL nl_ends) +{ +BOOL source_routing = *s == '@'; +int no_term = source_routing? 1 : 0; + +while (*s != 0 && (*s != ',' || no_term > 0) && (*s != '\n' || !nl_ends)) + { + /* Skip single quoted characters. Strictly these should not occur outside + quoted strings in RFC 822 addresses, but they can in RFC 821 addresses. Pity + about the lack of consistency, isn't it? */ + + if (*s == '\\' && s[1] != 0) s += 2; + + /* Skip quoted items that are not inside brackets. Note that + quoted pairs are allowed inside quoted strings. */ + + else if (*s == '\"') + { + while (*(++s) != 0 && (*s != '\n' || !nl_ends)) + { + if (*s == '\\' && s[1] != 0) s++; + else if (*s == '\"') { s++; break; } + } + } + + /* Skip comments, which may include nested brackets, but quotes + are not recognized inside comments, though quoted pairs are. */ + + else if (*s == '(') + { + int level = 1; + while (*(++s) != 0 && (*s != '\n' || !nl_ends)) + { + if (*s == '\\' && s[1] != 0) s++; + else if (*s == '(') level++; + else if (*s == ')' && --level <= 0) { s++; break; } + } + } + + /* Non-special character; just advance. Passing the colon in a source + routed address means that any subsequent comma or colon may terminate unless + inside angle brackets. */ + + else + { + if (*s == '<') + { + source_routing = s[1] == '@'; + no_term = source_routing? 2 : 1; + } + else if (*s == '>') no_term--; + else if (source_routing && *s == ':') no_term--; + s++; + } + } + +return s; +} + + + +/************************************************* +* Find last @ in an address * +*************************************************/ + +/* This function is used when we have something that may not qualified. If we +know it's qualified, searching for the rightmost '@' is sufficient. Here we +have to be a bit more clever than just a plain search, in order to handle +unqualified local parts like "thing@thong" correctly. Since quotes may not +legally be part of a domain name, we can give up on hitting the first quote +when searching from the right. Now that the parsing also permits the RFC 821 +form of address, where quoted-pairs are allowed in unquoted local parts, we +must take care to handle that too. + +Argument: pointer to an address, possibly unqualified +Returns: pointer to the last @ in an address, or NULL if none +*/ + +uschar * +parse_find_at(uschar *s) +{ +uschar *t = s + Ustrlen(s); +while (--t >= s) + { + if (*t == '@') + { + int backslash_count = 0; + uschar *tt = t - 1; + while (tt > s && *tt-- == '\\') backslash_count++; + if ((backslash_count & 1) == 0) return t; + } + else if (*t == '\"') return NULL; + } +return NULL; +} + + + + +/*************************************************************************** +* In all the functions below that read a particular object type from * +* the input, return the new value of the pointer s (the first argument), * +* and put the object into the store pointed to by t (the second argument), * +* adding a terminating zero. If no object is found, t will point to zero * +* on return. * +***************************************************************************/ + + +/************************************************* +* Skip white space and comment * +*************************************************/ + +/* Algorithm: + (1) Skip spaces. + (2) If uschar not '(', return. + (3) Skip till matching ')', not counting any characters + escaped with '\'. + (4) Move past ')' and goto (1). + +The start of the last potential comment position is remembered to +make it possible to ignore comments at the end of compound items. + +Argument: current character pointer +Regurns: new character pointer +*/ + +static uschar * +skip_comment(uschar *s) +{ +last_comment_position = s; +while (*s) + { + int c, level; + while (isspace(*s)) s++; + if (*s != '(') break; + level = 1; + while((c = *(++s)) != 0) + { + if (c == '(') level++; + else if (c == ')') { if (--level <= 0) { s++; break; } } + else if (c == '\\' && s[1] != 0) s++; + } + } +return s; +} + + + +/************************************************* +* Read a domain * +*************************************************/ + +/* A domain is a sequence of subdomains, separated by dots. See comments below +for detailed syntax of the subdomains. + +If allow_domain_literals is TRUE, a "domain" may also be an IP address enclosed +in []. Make sure the output is set to the null string if there is a syntax +error as well as if there is no domain at all. + +Arguments: + s current character pointer + t where to put the domain + errorptr put error message here on failure (*t will be 0 on exit) + +Returns: new character pointer +*/ + +static uschar * +read_domain(uschar *s, uschar *t, uschar **errorptr) +{ +uschar *tt = t; +s = skip_comment(s); + +/* Handle domain literals if permitted. An RFC 822 domain literal may contain +any character except [ ] \, including linear white space, and may contain +quoted characters. However, RFC 821 restricts literals to being dot-separated +3-digit numbers, and we make the obvious extension for IPv6. Go for a sequence +of digits and dots (hex digits and colons for IPv6) here; later this will be +checked for being a syntactically valid IP address if it ever gets to a router. + +If IPv6 is supported, allow both the formal form, with IPV6: at the start, and +the informal form without it, and accept IPV4: as well, 'cause someone will use +it sooner or later. */ + +if (*s == '[') + { + *t++ = *s++; + + #if HAVE_IPV6 + if (strncmpic(s, US"IPv6:", 5) == 0 || strncmpic(s, US"IPv4:", 5) == 0) + { + memcpy(t, s, 5); + t += 5; + s += 5; + } + while (*s == '.' || *s == ':' || isxdigit(*s)) *t++ = *s++; + + #else + while (*s == '.' || isdigit(*s)) *t++ = *s++; + #endif + + if (*s == ']') *t++ = *s++; else + { + *errorptr = US"malformed domain literal"; + *tt = 0; + } + + if (!allow_domain_literals) + { + *errorptr = US"domain literals not allowed"; + *tt = 0; + } + *t = 0; + return skip_comment(s); + } + +/* Handle a proper domain, which is a sequence of dot-separated atoms. Remove +trailing dots if strip_trailing_dot is set. A subdomain is an atom. + +An atom is a sequence of any characters except specials, space, and controls. +The specials are ( ) < > @ , ; : \ " . [ and ]. This is the rule for RFC 822 +and its successor (RFC 2822). However, RFC 821 and its successor (RFC 2821) is +tighter, allowing only letters, digits, and hyphens, not starting with a +hyphen. + +There used to be a global flag that got set when checking addresses that came +in over SMTP and which should therefore should be checked according to the +stricter rule. However, it seems silly to make the distinction, because I don't +suppose anybody ever uses local domains that are 822-compliant and not +821-compliant. Furthermore, Exim now has additional data on the spool file line +after an address (after "one_time" processing), and it makes use of a # +character to delimit it. When I wrote that code, I forgot about this 822-domain +stuff, and assumed # could never appear in a domain. + +So the old code is now cut out for Release 4.11 onwards, on 09-Aug-02. In a few +years, when we are sure this isn't actually causing trouble, throw it away. + +March 2003: the story continues: There is a camp that is arguing for the use of +UTF-8 in domain names as the way to internationalization, and other MTAs +support this. Therefore, we now have a flag that permits the use of characters +with values greater than 127, encoded in UTF-8, in subdomains, so that Exim can +be used experimentally in this way. */ + +for (;;) + { + uschar *tsave = t; + +/********************* + if (rfc821_domains) + { + if (*s != '-') while (isalnum(*s) || *s == '-') *t++ = *s++; + } + else + while (!mac_iscntrl_or_special(*s)) *t++ = *s++; +*********************/ + + if (*s != '-') + { + /* Only letters, digits, and hyphens */ + + if (!allow_utf8_domains) + { + while (isalnum(*s) || *s == '-') *t++ = *s++; + } + + /* Permit legal UTF-8 characters to be included */ + + else for(;;) + { + int i, d; + if (isalnum(*s) || *s == '-') /* legal ascii characters */ + { + *t++ = *s++; + continue; + } + if ((*s & 0xc0) != 0xc0) break; /* not start of UTF-8 character */ + d = *s << 2; + for (i = 1; i < 6; i++) /* i is the number of additional bytes */ + { + if ((d & 0x80) == 0) break; + d <<= 1; + } + if (i == 6) goto BAD_UTF8; /* invalid UTF-8 */ + *t++ = *s++; /* leading UTF-8 byte */ + while (i-- > 0) /* copy and check remainder */ + { + if ((*s & 0xc0) != 0x80) + { + BAD_UTF8: + *errorptr = US"invalid UTF-8 byte sequence"; + *tt = 0; + return s; + } + *t++ = *s++; + } + } /* End of loop for UTF-8 character */ + } /* End of subdomain */ + + s = skip_comment(s); + *t = 0; + + if (t == tsave) /* empty component */ + { + if (strip_trailing_dot && t > tt && *s != '.') t[-1] = 0; else + { + *errorptr = US"domain missing or malformed"; + *tt = 0; + } + return s; + } + + if (*s != '.') break; + *t++ = *s++; + s = skip_comment(s); + } + +return s; +} + + + +/************************************************* +* Read a local-part * +*************************************************/ + +/* A local-part is a sequence of words, separated by periods. A null word +between dots is not strictly allowed but apparently many mailers permit it, +so, sigh, better be compatible. Even accept a trailing dot... + +A is either a quoted string, or an , which is a sequence +of any characters except specials, space, and controls. The specials are +( ) < > @ , ; : \ " . [ and ]. In RFC 822, a single quoted character, (a +quoted-pair) is not allowed in a word. However, in RFC 821, it is permitted in +the local part of an address. Rather than have separate parsing functions for +the different cases, take the liberal attitude always. At least one MUA is +happy to recognize this case; I don't know how many other programs do. + +Arguments: + s current character pointer + t where to put the local part + error where to point error text + allow_null TRUE if an empty local part is not an error + +Returns: new character pointer +*/ + +static uschar * +read_local_part(uschar *s, uschar *t, uschar **error, BOOL allow_null) +{ +uschar *tt = t; +*error = NULL; +for (;;) + { + int c; + uschar *tsave = t; + s = skip_comment(s); + + /* Handle a quoted string */ + + if (*s == '\"') + { + *t++ = '\"'; + while ((c = *(++s)) != 0 && c != '\"') + { + *t++ = c; + if (c == '\\' && s[1] != 0) *t++ = *(++s); + } + if (c == '\"') + { + s++; + *t++ = '\"'; + } + else + { + *error = US"unmatched doublequote in local part"; + return s; + } + } + + /* Handle an atom, but allow quoted pairs within it. */ + + else while (!mac_iscntrl_or_special(*s) || *s == '\\') + { + c = *t++ = *s++; + if (c == '\\' && *s != 0) *t++ = *s++; + } + + /* Terminate the word and skip subsequent comment */ + + *t = 0; + s = skip_comment(s); + + /* If we have read a null component at this point, give an error unless it is + terminated by a dot - an extension to RFC 822 - or if it is the first + component of the local part and an empty local part is permitted, in which + case just return normally. */ + + if (t == tsave && *s != '.') + { + if (t == tt && !allow_null) + *error = US"missing or malformed local part"; + return s; + } + + /* Anything other than a dot terminates the local part. Treat multiple dots + as a single dot, as this seems to be a common extension. */ + + if (*s != '.') break; + do { *t++ = *s++; } while (*s == '.'); + } + +return s; +} + + +/************************************************* +* Read route part of route-addr * +*************************************************/ + +/* The pointer is at the initial "@" on entry. Return it following the +terminating colon. Exim no longer supports the use of source routes, but it is +required to accept the syntax. + +Arguments: + s current character pointer + t where to put the route + errorptr where to put an error message + +Returns: new character pointer +*/ + +static uschar * +read_route(uschar *s, uschar *t, uschar **errorptr) +{ +BOOL commas = FALSE; +*errorptr = NULL; + +while (*s == '@') + { + *t++ = '@'; + s = read_domain(s+1, t, errorptr); + if (*t == 0) return s; + t += Ustrlen((const uschar *)t); + if (*s != ',') break; + *t++ = *s++; + commas = TRUE; + s = skip_comment(s); + } + +if (*s == ':') *t++ = *s++; + +/* If there is no colon, and there were no commas, the most likely error +is in fact a missing local part in the address rather than a missing colon +after the route. */ + +else *errorptr = commas? + US"colon expected after route list" : + US"no local part"; + +/* Terminate the route and return */ + +*t = 0; +return skip_comment(s); +} + + + +/************************************************* +* Read addr-spec * +*************************************************/ + +/* Addr-spec is local-part@domain. We make the domain optional - +the expected terminator for the whole thing is passed to check this. +This function is called only when we know we have a route-addr. + +Arguments: + s current character pointer + t where to put the addr-spec + term expected terminator (0 or >) + errorptr where to put an error message + domainptr set to point to the start of the domain + +Returns: new character pointer +*/ + +static uschar * +read_addr_spec(uschar *s, uschar *t, int term, uschar **errorptr, + uschar **domainptr) +{ +s = read_local_part(s, t, errorptr, FALSE); +if (*errorptr == NULL) + { + if (*s != term) + { + if (*s != '@') + *errorptr = string_sprintf("\"@\" or \".\" expected after \"%s\"", t); + else + { + t += Ustrlen((const uschar *)t); + *t++ = *s++; + *domainptr = t; + s = read_domain(s, t, errorptr); + } + } + } +return s; +} + + + +/************************************************* +* Extract operative address * +*************************************************/ + +/* This function extracts an operative address from a full RFC822 mailbox and +returns it in a piece of dynamic store. We take the easy way and get a piece +of store the same size as the input, and then copy into it whatever is +necessary. If we cannot find a valid address (syntax error), return NULL, and +point the error pointer to the reason. The arguments "start" and "end" are used +to return the offsets of the first and one past the last characters in the +original mailbox of the address that has been extracted, to aid in re-writing. +The argument "domain" is set to point to the first character after "@" in the +final part of the returned address, or zero if there is no @. + +Exim no longer supports the use of source routed addresses (those of the form +@domain,...:route_addr). It recognizes the syntax, but collapses such addresses +down to their final components. Formerly, collapse_source_routes had to be set +to achieve this effect. RFC 1123 allows collapsing with MAY, while the revision +of RFC 821 had increased this to SHOULD, so I've gone for it, because it makes +a lot of code elsewhere in Exim much simpler. + +There are some special fudges here for handling RFC 822 group address notation +which may appear in certain headers. If the flag parse_allow_group is set +TRUE and parse_found_group is FALSE when this function is called, an address +which is the start of a group (i.e. preceded by a phrase and a colon) is +recognized; the phrase is ignored and the flag parse_found_group is set. If +this flag is TRUE at the end of an address, then if an extraneous semicolon is +found, it is ignored and the flag is cleared. This logic is used only when +scanning through addresses in headers, either to fulfil the -t option or for +rewriting or checking header syntax. + +Arguments: + mailbox points to the RFC822 mailbox + errorptr where to point an error message + start set to start offset in mailbox + end set to end offset in mailbox + domain set to domain offset in result, or 0 if no domain present + allow_null allow <> if TRUE + +Returns: points to the extracted address, or NULL on error +*/ + +#define FAILED(s) { *errorptr = s; goto PARSE_FAILED; } + +uschar * +parse_extract_address(uschar *mailbox, uschar **errorptr, int *start, int *end, + int *domain, BOOL allow_null) +{ +uschar *yield = store_get(Ustrlen(mailbox) + 1); +uschar *startptr, *endptr; +uschar *s = (uschar *)mailbox; +uschar *t = (uschar *)yield; + +*domain = 0; + +/* At the start of the string we expect either an addr-spec or a phrase +preceding a . If groups are allowed, we might also find a phrase +preceding a colon and an address. If we find an initial word followed by +a dot, strict interpretation of the RFC would cause it to be taken +as the start of an addr-spec. However, many mailers break the rules +and use addresses of the form "a.n.other " and so we +allow this case. */ + +RESTART: /* Come back here after passing a group name */ + +s = skip_comment(s); +startptr = s; /* In case addr-spec */ +s = read_local_part(s, t, errorptr, TRUE); /* Dot separated words */ +if (*errorptr != NULL) goto PARSE_FAILED; + +/* If the terminator is neither < nor @ then the format of the address +must either be a bare local-part (we are now at the end), or a phrase +followed by a route-addr (more words must follow). */ + +if (*s != '@' && *s != '<') + { + if (*s == 0 || *s == ';') + { + if (*t == 0) FAILED(US"empty address"); + endptr = last_comment_position; + goto PARSE_SUCCEEDED; /* Bare local part */ + } + + /* Expect phrase route-addr, or phrase : if groups permitted, but allow + dots in the phrase; complete the loop only when '<' or ':' is encountered - + end of string will produce a null local_part and therefore fail. We don't + need to keep updating t, as the phrase isn't to be kept. */ + + while (*s != '<' && (!parse_allow_group || *s != ':')) + { + s = read_local_part(s, t, errorptr, FALSE); + if (*errorptr != NULL) + { + *errorptr = string_sprintf("%s (expected word or \"<\")", *errorptr); + goto PARSE_FAILED; + } + } + + if (*s == ':') + { + parse_found_group = TRUE; + parse_allow_group = FALSE; + s++; + goto RESTART; + } + + /* Assert *s == '<' */ + } + +/* At this point the next character is either '@' or '<'. If it is '@', only a +single local-part has previously been read. An angle bracket signifies the +start of an . Throw away anything we have saved so far before +processing it. Note that this is "if" rather than "else if" because it's also +used after reading a preceding phrase. + +There are a lot of broken sendmails out there that put additional pairs of <> +round s. If strip_excess_angle_brackets is set, allow any number of +them, as long as they match. */ + +if (*s == '<') + { + uschar *domainptr = yield; + BOOL source_routed = FALSE; + int bracket_count = 1; + + s++; + if (strip_excess_angle_brackets) + while (*s == '<') { bracket_count++; s++; } + + t = yield; + startptr = s; + s = skip_comment(s); + + /* Read an optional series of routes, each of which is a domain. They + are separated by commas and terminated by a colon. However, we totally ignore + such routes (RFC 1123 says we MAY, and the revision of RFC 821 says we + SHOULD). */ + + if (*s == '@') + { + s = read_route(s, t, errorptr); + if (*errorptr != NULL) goto PARSE_FAILED; + *t = 0; /* Ensure route is ignored - probably overkill */ + source_routed = TRUE; + } + + /* Now an addr-spec, terminated by '>'. If there is no preceding route, + we must allow an empty addr-spec if allow_null is TRUE, to permit the + address "<>" in some circumstances. A source-routed address MUST have + a domain in the final part. */ + + if (allow_null && !source_routed && *s == '>') + { + *t = 0; + *errorptr = NULL; + } + else + { + s = read_addr_spec(s, t, '>', errorptr, &domainptr); + if (*errorptr != NULL) goto PARSE_FAILED; + *domain = domainptr - yield; + if (source_routed && *domain == 0) + FAILED(US"domain missing in source-routed address"); + } + + endptr = s; + if (*errorptr != NULL) goto PARSE_FAILED; + while (bracket_count-- > 0) if (*s++ != '>') + { + *errorptr = (s[-1] == 0)? US"'>' missing at end of address" : + string_sprintf("malformed address: %.32s may not follow %.*s", + s-1, s - (uschar *)mailbox - 1, mailbox); + goto PARSE_FAILED; + } + + s = skip_comment(s); + } + +/* Hitting '@' after the first local-part means we have definitely got an +addr-spec, on a strict reading of the RFC, and the rest of the string +should be the domain. However, for flexibility we allow for a route-address +not enclosed in <> as well, which is indicated by an empty first local +part preceding '@'. The source routing is, however, ignored. */ + +else if (*t == 0) + { + uschar *domainptr = yield; + s = read_route(s, t, errorptr); + if (*errorptr != NULL) goto PARSE_FAILED; + *t = 0; /* Ensure route is ignored - probably overkill */ + s = read_addr_spec(s, t, 0, errorptr, &domainptr); + if (*errorptr != NULL) goto PARSE_FAILED; + *domain = domainptr - yield; + endptr = last_comment_position; + if (*domain == 0) FAILED(US"domain missing in source-routed address"); + } + +/* This is the strict case of local-part@domain. */ + +else + { + t += Ustrlen((const uschar *)t); + *t++ = *s++; + *domain = t - yield; + s = read_domain(s, t, errorptr); + if (*t == 0) goto PARSE_FAILED; + endptr = last_comment_position; + } + +/* Use goto to get here from the bare local part case. Arrive by falling +through for other cases. Endptr may have been moved over whitespace, so +move it back past white space if necessary. */ + +PARSE_SUCCEEDED: +if (*s != 0) + { + if (parse_found_group && *s == ';') + { + parse_found_group = FALSE; + parse_allow_group = TRUE; + } + else + { + *errorptr = string_sprintf("malformed address: %.32s may not follow %.*s", + s, s - (uschar *)mailbox, mailbox); + goto PARSE_FAILED; + } + } +*start = startptr - (uschar *)mailbox; /* Return offsets */ +while (isspace(endptr[-1])) endptr--; +*end = endptr - (uschar *)mailbox; + +/* Although this code has no limitation on the length of address extracted, +other parts of Exim may have limits, and in any case, RFC 2821 limits local +parts to 64 and domains to 255, so we do a check here, giving an error if the +address is ridiculously long. */ + +if (*end - *start > ADDRESS_MAXLENGTH) + { + *errorptr = string_sprintf("address is ridiculously long: %.64s...", yield); + return NULL; + } + +return (uschar *)yield; + +/* Use goto (via the macro FAILED) to get to here from a variety of places. +We might have an empty address in a group - the caller can choose to ignore +this. We must, however, keep the flags correct. */ + +PARSE_FAILED: +if (parse_found_group && *s == ';') + { + parse_found_group = FALSE; + parse_allow_group = TRUE; + } +return NULL; +} + +#undef FAILED + + + +/************************************************* +* Quote according to RFC 2047 * +*************************************************/ + +/* This function is used for quoting text in headers according to RFC 2047. +If the only characters that strictly need quoting are spaces, we return the +original string, unmodified. If a quoted string is too long for the buffer, it +is truncated. (This shouldn't happen: this is normally handling short strings.) + +Arguments: + string the string to quote - already checked to contain non-printing + chars + len the length of the string + charset the name of the character set; NULL => iso-8859-1 + buffer the buffer to put the answer in + buffer_size the size of the buffer + +Returns: pointer to the original string, if no quoting needed, or + pointer to buffer containing the quoted string, or + a pointer to "String too long" if the buffer can't even hold + the introduction +*/ + +uschar * +parse_quote_2047(uschar *string, int len, uschar *charset, uschar *buffer, + int buffer_size) +{ +uschar *s = string; +uschar *t; +BOOL coded = FALSE; + +if (charset == NULL) charset = US"iso-8859-1"; + +/* We don't expect this to fail! */ + +if (!string_format(buffer, buffer_size, "=?%s?Q?", charset)) + return US"String too long"; + +t = buffer + Ustrlen(buffer); +for (; len > 0; len--) + { + int ch = *s++; + if (t > buffer + buffer_size - 8) break; + if (ch < 33 || ch > 126 || + Ustrchr("?=()<>@,;:\\\".[]_", ch) != NULL) + { + if (ch == ' ') *t++ = '_'; else + { + sprintf(CS t, "=%02X", ch); + while (*t != 0) t++; + coded = TRUE; + } + } + else *t++ = ch; + } +sprintf(CS t, "?="); +return coded? buffer : string; +} + + + + +/************************************************* +* Fix up an RFC 822 "phrase" * +*************************************************/ + +/* This function is called to repair any syntactic defects in the "phrase" part +of an RFC822 address. In particular, it is applied to the user's name as read +from the passwd file when accepting a local message, and to the data from the +-F option. + +If the string contains existing quoted strings or comments containing +freestanding quotes, then we just quote those bits that need quoting - +otherwise it would get awfully messy and probably not look good. If not, we +quote the whole thing if necessary. Thus + + John Q. Smith => "John Q. Smith" + John "Jack" Smith => John "Jack" Smith + John "Jack" Q. Smith => John "Jack" "Q." Smith + John (Jack) Q. Smith => "John (Jack) Q. Smith" + John ("Jack") Q. Smith => John ("Jack") "Q." Smith +but + John (\"Jack\") Q. Smith => "John (\"Jack\") Q. Smith" + +Sheesh! This is tedious code. It is a great pity that the syntax of RFC822 is +the way it is... + +August 2000: Additional code added: + + Previously, non-printing characters were turned into question marks, which do + not need to be quoted. + + Now, a different tactic is used if there are any non-printing ASCII + characters. The encoding method from RFC 2047 is used, assuming iso-8859-1 as + the character set. + + We *could* use this for all cases, getting rid of the messy original code, + but leave it for now. It would complicate simple cases like "John Q. Smith". + +The result is passed back in the buffer; it is usually going to be added to +some other string. In order to be sure there is going to be no overflow, +restrict the length of the input to 1/4 of the buffer size - this allows for +every single character to be quoted or encoded without overflowing, and that +wouldn't happen because of amalgamation. If the phrase is too long, return a +fixed string. + +Arguments: + phrase an RFC822 phrase + len the length of the phrase + buffer a buffer to put the result in + buffer_size the size of the buffer + +Returns: the fixed RFC822 phrase +*/ + +uschar * +parse_fix_phrase(uschar *phrase, int len, uschar *buffer, int buffer_size) +{ +int ch, i; +BOOL quoted = FALSE; +uschar *s, *t, *end, *yield; + +while (len > 0 && isspace(*phrase)) { phrase++; len--; } +if (len > buffer_size/4) return US"Name too long"; + +/* See if there are any non-printing characters, and if so, use the RFC 2047 +encoding for the whole thing. */ + +for (i = 0, s = phrase; i < len; i++, s++) + if ((*s < 32 && *s != '\t') || *s > 126) break; + +if (i < len) return parse_quote_2047(phrase, len, headers_charset, buffer, + buffer_size); + +/* No non-printers; use the RFC 822 quoting rules */ + +s = phrase; +end = s + len; +yield = t = buffer + 1; + +while (s < end) + { + ch = *s++; + + /* Copy over quoted strings, remembering we encountered one */ + + if (ch == '\"') + { + *t++ = '\"'; + while (s < end && (ch = *s++) != '\"') + { + *t++ = ch; + if (ch == '\\' && s < end) *t++ = *s++; + } + *t++ = '\"'; + if (s >= end) break; + quoted = TRUE; + } + + /* Copy over comments, noting if they contain freestanding quote + characters */ + + else if (ch == '(') + { + int level = 1; + *t++ = '('; + while (s < end) + { + ch = *s++; + *t++ = ch; + if (ch == '(') level++; + else if (ch == ')') { if (--level <= 0) break; } + else if (ch == '\\' && s < end) *t++ = *s++ & 127; + else if (ch == '\"') quoted = TRUE; + } + if (ch == 0) + { + while (level--) *t++ = ')'; + break; + } + } + + /* Handle special characters that need to be quoted */ + + else if (Ustrchr(")<>@,;:\\.[]", ch) != NULL) + { + /* If hit previous quotes just make one quoted "word" */ + + if (quoted) + { + uschar *tt = t++; + while (*(--tt) != ' ' && *tt != '\"' && *tt != ')') tt[1] = *tt; + tt[1] = '\"'; + *t++ = ch; + while (s < end) + { + ch = *s++; + if (ch == ' ' || ch == '\"') { s--; break; } else *t++ = ch; + } + *t++ = '\"'; + } + + /* Else quote the whole string so far, and the rest up to any following + quotes. We must treat anything following a backslash as a literal. */ + + else + { + BOOL escaped = (ch == '\\'); + *(--yield) = '\"'; + *t++ = ch; + + /* Now look for the end or a quote */ + + while (s < end) + { + ch = *s++; + + /* Handle escaped pairs */ + + if (escaped) + { + *t++ = ch; + escaped = FALSE; + } + + else if (ch == '\\') + { + *t++ = ch; + escaped = TRUE; + } + + /* If hit subsequent quotes, insert our quote before any trailing + spaces and back up to re-handle the quote in the outer loop. */ + + else if (ch == '\"') + { + int count = 0; + while (t[-1] == ' ') { t--; count++; } + *t++ = '\"'; + while (count-- > 0) *t++ = ' '; + s--; + break; + } + + /* If hit a subsequent comment, check it for unescaped quotes, + and if so, end our quote before it. */ + + else if (ch == '(') + { + uschar *ss = s; /* uschar after '(' */ + int level = 1; + while(ss < end) + { + ch = *ss++; + if (ch == '(') level++; + else if (ch == ')') { if (--level <= 0) break; } + else if (ch == '\\' && ss+1 < end) ss++; + else if (ch == '\"') { quoted = TRUE; break; } + } + + /* Comment contains unescaped quotes; end our quote before + the start of the comment. */ + + if (quoted) + { + int count = 0; + while (t[-1] == ' ') { t--; count++; } + *t++ = '\"'; + while (count-- > 0) *t++ = ' '; + break; + } + + /* Comment does not contain unescaped quotes; include it in + our quote. */ + + else + { + if (ss >= end) ss--; + *t++ = '('; + Ustrncpy(t, s, ss-s); + t += ss-s; + s = ss; + } + } + + /* Not a comment or quote; include this character in our quotes. */ + + else *t++ = ch; + } + } + + /* Add a final quote if we hit the end of the string. */ + + if (s >= end) *t++ = '\"'; + } + + /* Non-special character; just copy it over */ + + else *t++ = ch; + } + +*t = 0; +return yield; +} + + +/************************************************* +* Extract addresses from a list * +*************************************************/ + +/* This function is called by the redirect router to scan a string containing a +list of addresses separated by commas (with optional white space) or by +newlines, and to generate a chain of address items from them. In other words, +to unpick data from an alias or .forward file. + +The SunOS5 documentation for alias files is not very clear on the syntax; it +does not say that either a comma or a newline can be used for separation. +However, that is the way Smail does it, so we follow suit. + +If a # character is encountered in a white space position, then characters from +there to the next newline are skipped. + +If an unqualified address begins with '\', just skip that character. This gives +compatibility with Sendmail's use of \ to prevent looping. Exim has its own +loop prevention scheme which handles other cases too - see the code in +route_address(). + +An "address" can be a specification of a file or a pipe; the latter may often +need to be quoted because it may contain spaces, but we don't want to retain +the quotes. Quotes may appear in normal addresses too, and should be retained. +We can distinguish between these cases, because in addresses, quotes are used +only for parts of the address, not the whole thing. Therefore, we remove quotes +from items when they entirely enclose them, but not otherwise. + +An "address" can also be of the form :include:pathname to include a list of +addresses contained in the specified file. + +Any unqualified addresses are qualified with and rewritten if necessary, via +the rewrite_address() function. + +Arguments: + s the list of addresses (typically a complete + .forward file or a list of entries in an alias file) + options option bits for permitting or denying various special cases; + not all bits are relevant here - some are for filter + files; those we use here are: + RDO_DEFER + RDO_FREEZE + RDO_FAIL + RDO_BLACKHOLE + RDO_REWRITE + RDO_INCLUDE + anchor where to hang the chain of newly-created addresses. This + should be initialized to NULL. + error where to return an error text + incoming domain domain of the incoming address; used to qualify unqualified + local parts preceded by \ + directory if NULL, no checks are done on :include: files + otherwise, included file names must start with the given + directory + syntax_errors if not NULL, it carries on after syntax errors in addresses, + building up a list of errors as error blocks chained on + here. + +Returns: FF_DELIVERED addresses extracted + FF_NOTDELIVERED no addresses extracted, but no errors + FF_BLACKHOLE :blackhole: + FF_DEFER :defer: + FF_FAIL :fail: + FF_INCLUDEFAIL some problem with :include:; *error set + FF_ERROR other problems; *error is set +*/ + +int +parse_forward_list(uschar *s, int options, address_item **anchor, + uschar **error, uschar *incoming_domain, uschar *directory, + error_block **syntax_errors) +{ +int count = 0; + +DEBUG(D_route) debug_printf("parse_forward_list: %s\n", s); + +for (;;) + { + int len; + int special = 0; + int specopt = 0; + int specbit = 0; + uschar *ss, *nexts; + address_item *addr; + BOOL inquote = FALSE; + + for (;;) + { + while (isspace(*s) || *s == ',') s++; + if (*s == '#') { while (*s != 0 && *s != '\n') s++; } else break; + } + + /* When we reach the end of the list, we return FF_DELIVERED if any child + addresses have been generated. If nothing has been generated, there are two + possibilities: either the list is really empty, or there were syntax errors + that are being skipped. (If syntax errors are not being skipped, an FF_ERROR + return is generated on hitting a syntax error and we don't get here.) For a + truly empty list we return FF_NOTDELIVERED so that the router can decline. + However, if the list is empty only because syntax errors were skipped, we + return FF_DELIVERED. */ + + if (*s == 0) + { + return (count > 0 || (syntax_errors != NULL && *syntax_errors != NULL))? + FF_DELIVERED : FF_NOTDELIVERED; + + /* This previous code returns FF_ERROR if nothing is generated but a + syntax error has been skipped. I now think it is the wrong approach, but + have left this here just in case, and for the record. */ + + #ifdef NEVER + if (count > 0) return FF_DELIVERED; /* Something was generated */ + + if (syntax_errors == NULL || /* Not skipping syntax errors, or */ + *syntax_errors == NULL) /* we didn't actually skip any */ + return FF_NOTDELIVERED; + + *error = string_sprintf("no addresses generated: syntax error in %s: %s", + (*syntax_errors)->text2, (*syntax_errors)->text1); + return FF_ERROR; + #endif + + } + + /* Find the end of the next address. Quoted strings in addresses may contain + escaped characters; I haven't found a proper specification of .forward or + alias files that mentions the quoting properties, but it seems right to do + the escaping thing in all cases, so use the function that finds the end of an + address. However, don't let a quoted string extend over the end of a line. */ + + ss = parse_find_address_end(s, TRUE); + + /* Remember where we finished, for starting the next one. */ + + nexts = ss; + + /* Remove any trailing spaces; we know there's at least one non-space. */ + + while (isspace((ss[-1]))) ss--; + + /* We now have s->start and ss->end of the next address. Remove quotes + if they completely enclose, remembering the address started with a quote + for handling pipes and files. Another round of removal of leading and + trailing spaces is then required. */ + + if (*s == '\"' && ss[-1] == '\"') + { + s++; + ss--; + inquote = TRUE; + while (s < ss && isspace(*s)) s++; + while (ss > s && isspace((ss[-1]))) ss--; + } + + /* Set up the length of the address. */ + + len = ss - s; + + DEBUG(D_route) + { + int save = s[len]; + s[len] = 0; + debug_printf("extract item: %s\n", s); + s[len] = save; + } + + /* Handle special addresses if permitted. If the address is :unknown: + ignore it - this is for backward compatibility with old alias files. You + don't need to use it nowadays - just generate an empty string. For :defer:, + :blackhole:, or :fail: we have to set up the error message and give up right + away. */ + + if (Ustrncmp(s, ":unknown:", len) == 0) + { + s = nexts; + continue; + } + + if (Ustrncmp(s, ":defer:", 7) == 0) + { special = FF_DEFER; specopt = RDO_DEFER; } /* specbit is 0 */ + else if (Ustrncmp(s, ":blackhole:", 11) == 0) + { special = FF_BLACKHOLE; specopt = specbit = RDO_BLACKHOLE; } + else if (Ustrncmp(s, ":fail:", 6) == 0) + { special = FF_FAIL; specopt = RDO_FAIL; } /* specbit is 0 */ + + if (special != 0) + { + uschar *ss = Ustrchr(s+1, ':') + 1; + if ((options & specopt) == specbit) + { + *error = string_sprintf("\"%.*s\" is not permitted", len, s); + return FF_ERROR; + } + while (*ss != 0 && isspace(*ss)) ss++; + while (s[len] != 0 && s[len] != '\n') len++; + s[len] = 0; + *error = string_copy(ss); + return special; + } + + /* If the address is of the form :include:pathname, read the file, and call + this function recursively to extract the addresses from it. If directory is + NULL, do no checks. Otherwise, insist that the file name starts with the + given directory and is a regular file. */ + + if (Ustrncmp(s, ":include:", 9) == 0) + { + uschar *filebuf; + uschar filename[256]; + uschar *t = s+9; + int flen = len - 9; + int frc; + struct stat statbuf; + address_item *last; + FILE *f; + + while (flen > 0 && isspace(*t)) { t++; flen--; } + + if (flen <= 0) + { + *error = string_sprintf("file name missing after :include:"); + return FF_ERROR; + } + + if (flen > 255) + { + *error = string_sprintf("included file name \"%s\" is too long", t); + return FF_ERROR; + } + + Ustrncpy(filename, t, flen); + filename[flen] = 0; + + /* Insist on absolute path */ + + if (filename[0]!= '/') + { + *error = string_sprintf("included file \"%s\" is not an absolute path", + filename); + return FF_ERROR; + } + + /* Check if include is permitted */ + + if ((options & RDO_INCLUDE) != 0) + { + *error = US"included files not permitted"; + return FF_ERROR; + } + + /* Check file name if required */ + + if (directory != NULL) + { + int len = Ustrlen(directory); + uschar *p = filename + len; + + if (Ustrncmp(filename, directory, len) != 0 || *p != '/') + { + *error = string_sprintf("included file %s is not in directory %s", + filename, directory); + return FF_ERROR; + } + + /* It is necessary to check that every component inside the directory + is NOT a symbolic link, in order to keep the file inside the directory. + This is mighty tedious. It is also not totally foolproof in that it + leaves the possibility of a race attack, but I don't know how to do + any better. */ + + while (*p != 0) + { + int temp; + while (*(++p) != 0 && *p != '/'); + temp = *p; + *p = 0; + if (Ulstat(filename, &statbuf) != 0) + { + *error = string_sprintf("failed to stat %s (component of included " + "file)", filename); + *p = temp; + return FF_ERROR; + } + + *p = temp; + + if ((statbuf.st_mode & S_IFMT) == S_IFLNK) + { + *error = string_sprintf("included file %s in the %s directory " + "involves a symbolic link", filename, directory); + return FF_ERROR; + } + } + } + + /* Open and stat the file */ + + if ((f = Ufopen(filename, "rb")) == NULL) + { + *error = string_open_failed(errno, "included file %s", filename); + return FF_INCLUDEFAIL; + } + + if (fstat(fileno(f), &statbuf) != 0) + { + *error = string_sprintf("failed to stat included file %s: %s", + filename, strerror(errno)); + fclose(f); + return FF_INCLUDEFAIL; + } + + /* If directory was checked, double check that we opened a regular file */ + + if (directory != NULL && (statbuf.st_mode & S_IFMT) != S_IFREG) + { + *error = string_sprintf("included file %s is not a regular file in " + "the %s directory", filename, directory); + return FF_ERROR; + } + + /* Get a buffer and read the contents */ + + if (statbuf.st_size > MAX_INCLUDE_SIZE) + { + *error = string_sprintf("included file %s is too big (max %d)", + filename, MAX_INCLUDE_SIZE); + return FF_ERROR; + } + + filebuf = store_get(statbuf.st_size + 1); + if (fread(filebuf, 1, statbuf.st_size, f) != statbuf.st_size) + { + *error = string_sprintf("error while reading included file %s: %s", + filename, strerror(errno)); + fclose(f); + return FF_ERROR; + } + filebuf[statbuf.st_size] = 0; + fclose(f); + + addr = NULL; + frc = parse_forward_list(filebuf, options, &addr, + error, incoming_domain, directory, syntax_errors); + if (frc != FF_DELIVERED && frc != FF_NOTDELIVERED) return frc; + + if (addr != NULL) + { + last = addr; + while (last->next != NULL) { count++; last = last->next; } + last->next = *anchor; + *anchor = addr; + count++; + } + } + + /* Else (not :include:) ensure address is syntactically correct and fully + qualified if not a pipe or a file, removing a leading \ if present on an + unqualified address. For pipes and files we must handle quoting. It's + not quite clear exactly what to do for partially quoted things, but the + common case of having the whole thing in quotes is straightforward. If this + was the case, inquote will have been set TRUE above and the quotes removed. + + There is a possible ambiguity over addresses whose local parts start with + a vertical bar or a slash, and the latter do in fact occur, thanks to X.400. + Consider a .forward file that contains the line + + /X=xxx/Y=xxx/OU=xxx/@some.gate.way + + Is this a file or an X.400 address? Does it make any difference if it is in + quotes? On the grounds that file names of this type are rare, Exim treats + something that parses as an RFC 822 address and has a domain as an address + rather than a file or a pipe. This is also how an address such as the above + would be treated if it came in from outside. */ + + else + { + int start, end, domain; + uschar *recipient = NULL; + int save = s[len]; + s[len] = 0; + + /* If it starts with \ and the rest of it parses as a valid mail address + without a domain, carry on with that address, but qualify it with the + incoming domain. Otherwise arrange for the address to fall through, + causing an error message on the re-parse. */ + + if (*s == '\\') + { + recipient = + parse_extract_address(s+1, error, &start, &end, &domain, FALSE); + if (recipient != NULL) + recipient = (domain != 0)? NULL : + string_sprintf("%s@%s", recipient, incoming_domain); + } + + /* Try parsing the item as an address. */ + + if (recipient == NULL) recipient = + parse_extract_address(s, error, &start, &end, &domain, FALSE); + + /* If item starts with / or | and is not a valid address, or there + is no domain, treat it as a file or pipe. If it was a quoted item, + remove the quoting occurrences of \ within it. */ + + if ((*s == '|' || *s == '/') && (recipient == NULL || domain == 0)) + { + uschar *t = store_get(Ustrlen(s) + 1); + uschar *p = t; + uschar *q = s; + while (*q != 0) + { + if (inquote) + { + *p++ = (*q == '\\')? *(++q) : *q; + q++; + } + else *p++ = *q++; + } + *p = 0; + addr = deliver_make_addr(t, TRUE); + setflag(addr, af_pfr); /* indicates pipe/file/reply */ + if (*s != '|') setflag(addr, af_file); /* indicates file */ + } + + /* Item must be an address. Complain if not, else qualify, rewrite and set + up the control block. It appears that people are in the habit of using + empty addresses but with comments as a way of putting comments into + alias and forward files. Therefore, ignore the error "empty address". + Mailing lists might want to tolerate syntax errors; there is therefore + an option to do so. */ + + else + { + if (recipient == NULL) + { + if (Ustrcmp(*error, "empty address") == 0) + { + *error = NULL; + s[len] = save; + s = nexts; + continue; + } + + if (syntax_errors != NULL) + { + error_block *e = store_get(sizeof(error_block)); + error_block *last = *syntax_errors; + if (last == NULL) *syntax_errors = e; else + { + while (last->next != NULL) last = last->next; + last->next = e; + } + e->next = NULL; + e->text1 = *error; + e->text2 = string_copy(s); + s[len] = save; + s = nexts; + continue; + } + else + { + *error = string_sprintf("%s in \"%s\"", *error, s); + s[len] = save; /* _after_ using it for *error */ + return FF_ERROR; + } + } + + /* Address was successfully parsed. Rewrite, and then make an address + block. */ + + recipient = ((options & RDO_REWRITE) != 0)? + rewrite_address(recipient, TRUE, FALSE, global_rewrite_rules, + rewrite_existflags) : + rewrite_address_qualify(recipient, TRUE); + addr = deliver_make_addr(recipient, TRUE); /* TRUE => copy recipient */ + } + + /* Restore the final character in the original data, and add to the + output chain. */ + + s[len] = save; + addr->next = *anchor; + *anchor = addr; + count++; + } + + /* Advance pointer for the next address */ + + s = nexts; + } +} + + +/************************************************* +************************************************** +* Stand-alone test program * +************************************************** +*************************************************/ + +#if defined STAND_ALONE +int main(void) +{ +int start, end, domain; +uschar buffer[1024]; +uschar outbuff[1024]; + +big_buffer = store_malloc(big_buffer_size); + +/* strip_trailing_dot = TRUE; */ +allow_domain_literals = TRUE; + +printf("Testing parse_fix_phrase\n"); + +while (Ufgets(buffer, sizeof(buffer), stdin) != NULL) + { + buffer[Ustrlen(buffer)-1] = 0; + if (buffer[0] == 0) break; + printf("%s\n", CS parse_fix_phrase(buffer, Ustrlen(buffer), outbuff, + sizeof(outbuff))); + } + +printf("Testing parse_extract_address without group syntax and without UTF-8\n"); + +while (Ufgets(buffer, sizeof(buffer), stdin) != NULL) + { + uschar *out; + uschar *errmess; + buffer[Ustrlen(buffer) - 1] = 0; + if (buffer[0] == 0) break; + out = parse_extract_address(buffer, &errmess, &start, &end, &domain, FALSE); + if (out == NULL) printf("*** bad address: %s\n", errmess); else + { + uschar extract[1024]; + Ustrncpy(extract, buffer+start, end-start); + extract[end-start] = 0; + printf("%s %d %d %d \"%s\"\n", out, start, end, domain, extract); + } + } + +printf("Testing parse_extract_address without group syntax but with UTF-8\n"); + +allow_utf8_domains = TRUE; +while (Ufgets(buffer, sizeof(buffer), stdin) != NULL) + { + uschar *out; + uschar *errmess; + buffer[Ustrlen(buffer) - 1] = 0; + if (buffer[0] == 0) break; + out = parse_extract_address(buffer, &errmess, &start, &end, &domain, FALSE); + if (out == NULL) printf("*** bad address: %s\n", errmess); else + { + uschar extract[1024]; + Ustrncpy(extract, buffer+start, end-start); + extract[end-start] = 0; + printf("%s %d %d %d \"%s\"\n", out, start, end, domain, extract); + } + } +allow_utf8_domains = FALSE; + +printf("Testing parse_extract_address with group syntax\n"); + +parse_allow_group = TRUE; +while (Ufgets(buffer, sizeof(buffer), stdin) != NULL) + { + uschar *out; + uschar *errmess; + uschar *s; + buffer[Ustrlen(buffer) - 1] = 0; + if (buffer[0] == 0) break; + s = buffer; + while (*s != 0) + { + uschar *ss = parse_find_address_end(s, FALSE); + int terminator = *ss; + *ss = 0; + out = parse_extract_address(buffer, &errmess, &start, &end, &domain, FALSE); + *ss = terminator; + + if (out == NULL) printf("*** bad address: %s\n", errmess); else + { + uschar extract[1024]; + Ustrncpy(extract, buffer+start, end-start); + extract[end-start] = 0; + printf("%s %d %d %d \"%s\"\n", out, start, end, domain, extract); + } + + s = ss + (terminator? 1:0); + while (isspace(*s)) s++; + } + } + +printf("Testing parse_find_at\n"); + +while (Ufgets(buffer, sizeof(buffer), stdin) != NULL) + { + uschar *s; + buffer[Ustrlen(buffer)-1] = 0; + if (buffer[0] == 0) break; + s = parse_find_at(buffer); + if (s == NULL) printf("no @ found\n"); + else printf("offset = %d\n", s - buffer); + } + +printf("Testing parse_extract_addresses\n"); + +while (Ufgets(buffer, sizeof(buffer), stdin) != NULL) + { + uschar *errmess; + int extracted; + address_item *anchor = NULL; + buffer[Ustrlen(buffer) - 1] = 0; + if (buffer[0] == 0) break; + if ((extracted = parse_forward_list(buffer, -1, &anchor, + &errmess, US"incoming.domain", NULL, NULL)) == FF_DELIVERED) + { + while (anchor != NULL) + { + address_item *addr = anchor; + anchor = anchor->next; + printf("%d %s\n", testflag(addr, af_pfr), addr->address); + } + } + else printf("Failed: %d %s\n", extracted, errmess); + } + +return 0; +} + +#endif + +/* End of parse.c */ diff --git a/src/src/perl.c b/src/src/perl.c new file mode 100644 index 000000000..533d2c47d --- /dev/null +++ b/src/src/perl.c @@ -0,0 +1,174 @@ +/* $Cambridge: exim/src/src/perl.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) 1998 Malcolm Beattie */ + +/* Modified by PH to get rid of the "na" usage, March 1999. + Modified further by PH for general tidying for Exim 4. + Threaded Perl support added by Stefan Traby, Nov 2002 +*/ + + +/* This Perl add-on can be distributed under the same terms as Exim itself. */ +/* See the file NOTICE for conditions of use and distribution. */ + +#include "exim.h" + +#define EXIM_TRUE TRUE +#undef TRUE + +#define EXIM_FALSE FALSE +#undef FALSE + +#define EXIM_DEBUG DEBUG +#undef DEBUG + +#include +#include +#include + +#ifndef ERRSV +#define ERRSV (GvSV(errgv)) +#endif + +/* Some people like very old perl versions, so avoid any build side-effects. */ + +#ifndef pTHX +# define pTHX +# define pTHX_ +#endif +#ifndef EXTERN_C +# define EXTERN_C extern +#endif + +EXTERN_C void boot_DynaLoader(pTHX_ CV *cv); + + +static PerlInterpreter *interp_perl = 0; + +XS(xs_expand_string) +{ + dXSARGS; + uschar *str; + STRLEN len; + + if (items != 1) + croak("Usage: Exim::expand_string(string)"); + + str = expand_string(US SvPV(ST(0), len)); + ST(0) = sv_newmortal(); + if (str != NULL) + sv_setpv(ST(0), (const char *) str); + else if (!expand_string_forcedfail) + croak("syntax error in Exim::expand_string argument: %s", + expand_string_message); +} + +XS(xs_debug_write) +{ + dXSARGS; + STRLEN len; + if (items != 1) + croak("Usage: Exim::debug_write(string)"); + debug_printf("%s", US SvPV(ST(0), len)); +} + +XS(xs_log_write) +{ + dXSARGS; + STRLEN len; + if (items != 1) + croak("Usage: Exim::log_write(string)"); + log_write(0, LOG_MAIN, "%s", US SvPV(ST(0), len)); +} + +static void xs_init(pTHX) +{ + char *file = __FILE__; + newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file); + newXS("Exim::expand_string", xs_expand_string, file); + newXS("Exim::debug_write", xs_debug_write, file); + newXS("Exim::log_write", xs_log_write, file); +} + +uschar * +init_perl(uschar *startup_code) +{ + static int argc = 2; + static char *argv[3] = { "exim-perl", "/dev/null", 0 }; + SV *sv; + STRLEN len; + + if (interp_perl) return 0; + interp_perl = perl_alloc(); + perl_construct(interp_perl); + perl_parse(interp_perl, xs_init, argc, argv, 0); + perl_run(interp_perl); + { + dSP; + sv = newSVpv(CS startup_code, 0); + PUSHMARK(SP); + perl_eval_sv(sv, G_SCALAR|G_DISCARD|G_KEEPERR); + SvREFCNT_dec(sv); + if (SvTRUE(ERRSV)) return US SvPV(ERRSV, len); + return NULL; + } +} + +void +cleanup_perl(void) +{ + if (!interp_perl) + return; + perl_destruct(interp_perl); + perl_free(interp_perl); + interp_perl = 0; +} + +uschar * +call_perl_cat(uschar *yield, int *sizep, int *ptrp, uschar **errstrp, + uschar *name, uschar **arg) +{ + dSP; + SV *sv; + STRLEN len; + uschar *str; + int items; + + if (!interp_perl) + { + *errstrp = US"the Perl interpreter has not been started"; + return 0; + } + + ENTER; + SAVETMPS; + PUSHMARK(SP); + while (*arg != NULL) XPUSHs(newSVpv(CS (*arg++), 0)); + PUTBACK; + items = perl_call_pv(CS name, G_SCALAR|G_EVAL); + SPAGAIN; + sv = POPs; + PUTBACK; + if (SvTRUE(ERRSV)) + { + *errstrp = US SvPV(ERRSV, len); + return NULL; + } + if (!SvOK(sv)) + { + *errstrp = 0; + return NULL; + } + str = US SvPV(sv, len); + yield = string_cat(yield, sizep, ptrp, str, (int)len); + FREETMPS; + LEAVE; + + return yield; +} + +/* End of perl.c */ diff --git a/src/src/queue.c b/src/src/queue.c new file mode 100644 index 000000000..cfcb0440a --- /dev/null +++ b/src/src/queue.c @@ -0,0 +1,1366 @@ +/* $Cambridge: exim/src/src/queue.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Functions that operate on the input queue. */ + + +#include "exim.h" + + + +/* The number of nodes to use for the bottom-up merge sort when a list of queue +items is to be ordered. The code for this sort was contributed as a patch by +Michael Haardt. */ + +#define LOG2_MAXNODES 32 + + + +/************************************************* +* Helper sort function for queue_get_spool_list * +*************************************************/ + +/* This function is used when sorting the queue list in the function +queue_get_spool_list() below. + +Arguments: + a points to an ordered list of queue_filename items + b points to another ordered list + +Returns: a pointer to a merged ordered list +*/ + +static queue_filename * +merge_queue_lists(queue_filename *a, queue_filename *b) +{ +queue_filename *first = NULL; +queue_filename **append = &first; + +while (a != NULL && b != NULL) + { + if (Ustrcmp(a->text, b->text) < 0) + { + *append = a; + append= &a->next; + a = a->next; + } + else + { + *append = b; + append= &b->next; + b = b->next; + } + } + +*append=((a != NULL)? a : b); +return first; +} + + + + + +/************************************************* +* Get list of spool files * +*************************************************/ + +/* Scan the spool directory and return a list of the relevant file names +therein. Single-character sub-directories are handled as follows: + + If the first argument is > 0, a sub-directory is scanned; the letter is + taken from the nth entry in subdirs. + + If the first argument is 0, sub-directories are not scanned. However, a + list of them is returned. + + If the first argument is < 0, sub-directories are scanned for messages, + and a single, unified list is created. The returned data blocks contain the + identifying character of the subdirectory, if any. The subdirs vector is + still required as an argument. + +If the randomize argument is TRUE, messages are returned in "randomized" order. +Actually, the order is anything but random, but the algorithm is cheap, and the +point is simply to ensure that the same order doesn't occur every time, in case +a particular message is causing a remote MTA to barf - we would like to try +other messages to that MTA first. + +If the randomize argument is FALSE, sort the list according to the file name. +This should give the order in which the messages arrived. It is normally used +only for presentation to humans, in which case the (possibly expensive) sort +that it does is not part of the normal operational code. However, if +queue_run_in_order is set, sorting has to take place for queue runs as well. +When randomize is FALSE, the first argument is normally -1, so all messages are +included. + +Arguments: + subdiroffset sub-directory character offset, or 0 or -1 (see above) + subdirs vector to store list of subdirchars + subcount pointer to int in which to store count of subdirs + randomize TRUE if the order of the list is to be unpredictable + +Returns: pointer to a chain of queue name items +*/ + +static queue_filename * +queue_get_spool_list(int subdiroffset, uschar *subdirs, int *subcount, + BOOL randomize) +{ +int i; +int flags = 0; +int resetflags = -1; +int subptr; +queue_filename *yield = NULL; +queue_filename *last = NULL; +struct dirent *ent; +DIR *dd; +uschar buffer[256]; +queue_filename *root[LOG2_MAXNODES]; + +/* When randomizing, the file names are added to the start or end of the list +according to the bits of the flags variable. Get a collection of bits from the +current time. Use the bottom 16 and just keep re-using them if necessary. When +not randomizing, initialize the sublists for the bottom-up merge sort. */ + +if (randomize) resetflags = time(NULL) & 0xFFFF; + else for (i = 0; i < LOG2_MAXNODES; i++) root[i] = NULL; + +/* If processing the full queue, or just the top-level, start at the base +directory, and initialize the first subdirectory name (as none). Otherwise, +start at the sub-directory offset. */ + +if (subdiroffset <= 0) + { + i = 0; + subdirs[0] = 0; + *subcount = 0; + } +else i = subdiroffset; + +/* Set up prototype for the directory name. */ + +sprintf(CS buffer, "%s/input", spool_directory); +subptr = Ustrlen(buffer); +buffer[subptr+2] = 0; /* terminator for lengthened name */ + +/* This loop runs at least once, for the main or given directory, and then as +many times as necessary to scan any subdirectories encountered in the main +directory, if they are to be scanned at this time. */ + +for (; i <= *subcount; i++) + { + int count = 0; + int subdirchar = subdirs[i]; /* 0 for main directory */ + + if (subdirchar != 0) + { + buffer[subptr] = '/'; + buffer[subptr+1] = subdirchar; + } + + dd = opendir(CS buffer); + if (dd == NULL) continue; + + /* Now scan the directory. */ + + while ((ent = readdir(dd)) != NULL) + { + uschar *name = US ent->d_name; + int len = Ustrlen(name); + + /* Count entries */ + + count++; + + /* If we find a single alphameric sub-directory in the base directory, + add it to the list for subsequent scans. */ + + if (i == 0 && len == 1 && isalnum(*name)) + { + *subcount = *subcount + 1; + subdirs[*subcount] = *name; + continue; + } + + /* Otherwise, if it is a header spool file, add it to the list */ + + if (len == SPOOL_NAME_LENGTH && + Ustrcmp(name + SPOOL_NAME_LENGTH - 2, "-H") == 0) + { + queue_filename *next = + store_get(sizeof(queue_filename) + Ustrlen(name)); + Ustrcpy(next->text, name); + next->dir_uschar = subdirchar; + + /* Handle the creation of a randomized list. The first item becomes both + the top and bottom of the list. Subsequent items are inserted either at + the top or the bottom, randomly. This is, I argue, faster than doing a + sort by allocating a random number to each item, and it also saves having + to store the number with each item. */ + + if (randomize) + { + if (yield == NULL) + { + next->next = NULL; + yield = last = next; + } + else + { + if (flags == 0) flags = resetflags; + if ((flags & 1) == 0) + { + next->next = yield; + yield = next; + } + else + { + next->next = NULL; + last->next = next; + last = next; + } + flags = flags >> 1; + } + } + + /* Otherwise do a bottom-up merge sort based on the name. */ + + else + { + int j; + next->next = NULL; + for (j = 0; j < LOG2_MAXNODES; j++) + { + if (root[j] != NULL) + { + next = merge_queue_lists(next, root[j]); + root[j] = (j == LOG2_MAXNODES - 1)? next : NULL; + } + else + { + root[j] = next; + break; + } + } + } + } + } + + /* Finished with this directory */ + + closedir(dd); + + /* If we have just scanned a sub-directory, and it was empty (count == 2 + implies just "." and ".." entries), and Exim is no longer configured to + use sub-directories, attempt to get rid of it. At the same time, try to + get rid of any corresponding msglog subdirectory. These are just cosmetic + tidying actions, so just ignore failures. If we are scanning just a single + sub-directory, break the loop. */ + + if (i != 0) + { + if (!split_spool_directory && count <= 2) + { + rmdir(CS buffer); + sprintf(CS big_buffer, "%s/msglog/%c", spool_directory, subdirchar); + rmdir(CS big_buffer); + } + if (subdiroffset > 0) break; /* Single sub-directory */ + } + + /* If we have just scanned the base directory, and subdiroffset is 0, + we do not want to continue scanning the sub-directories. */ + + else + { + if (subdiroffset == 0) break; + } + } /* Loop for multiple subdirectories */ + +/* When using a bottom-up merge sort, do the final merging of the sublists. +Then pass back the final list of file items. */ + +if (!randomize) + for (i = 0; i < LOG2_MAXNODES; ++i) + yield = merge_queue_lists(yield, root[i]); + +return yield; +} + + + + +/************************************************* +* Perform a queue run * +*************************************************/ + +/* The arguments give the messages to start and stop at; NULL means start at +the beginning or stop at the end. If the given start message doesn't exist, we +start at the next lexically greater one, and likewise we stop at the after the +previous lexically lesser one if the given stop message doesn't exist. Because +a queue run can take some time, stat each file before forking, in case it has +been delivered in the meantime by some other means. + +The global variables queue_run_force and queue_run_local may be set to cause +forced deliveries or local-only deliveries, respectively. + +If deliver_selectstring[_sender] is not NULL, skip messages whose recipients do +not contain the string. As this option is typically used when a machine comes +back online, we want to ensure that at least one delivery attempt takes place, +so force the first one. The selecting string can optionally be a regex, or +refer to the sender instead of recipients. + +If queue_2stage is set, the queue is scanned twice. The first time, queue_smtp +is set so that routing is done for all messages. Thus in the second run those +that are routed to the same host should go down the same SMTP connection. + +Arguments: + start_id message id to start at, or NULL for all + stop_id message id to end at, or NULL for all + recurse TRUE if recursing for 2-stage run + +Returns: nothing +*/ + +void +queue_run(uschar *start_id, uschar *stop_id, BOOL recurse) +{ +BOOL force_delivery = queue_run_force || deliver_selectstring != NULL || + deliver_selectstring_sender != NULL; +const pcre *selectstring_regex = NULL; +const pcre *selectstring_regex_sender = NULL; +uschar *log_detail = NULL; +int subcount = 0; +int i; +uschar subdirs[64]; + +/* Cancel any specific queue domains. Turn off the flag that causes SMTP +deliveries not to happen, unless doing a 2-stage queue run, when the SMTP flag +gets set. Save the queue_runner's pid and the flag that indicates any +deliveries run directly from this process. Deliveries that are run by handing +on TCP/IP channels have queue_run_pid set, but not queue_running. */ + +queue_domains = NULL; +queue_smtp_domains = NULL; +queue_smtp = queue_2stage; + +queue_run_pid = getpid(); +queue_running = TRUE; + +/* Log the true start of a queue run, and fancy options */ + +if (!recurse) + { + uschar extras[8]; + uschar *p = extras; + + if (queue_2stage) *p++ = 'q'; + if (queue_run_first_delivery) *p++ = 'i'; + if (queue_run_force) *p++ = 'f'; + if (deliver_force_thaw) *p++ = 'f'; + if (queue_run_local) *p++ = 'l'; + *p = 0; + + p = big_buffer; + sprintf(CS p, "pid=%d", (int)queue_run_pid); + while (*p != 0) p++; + + if (extras[0] != 0) + { + sprintf(CS p, " -q%s", extras); + while (*p != 0) p++; + } + + if (deliver_selectstring != NULL) + { + sprintf(CS p, " -R%s %s", deliver_selectstring_regex? "r" : "", + deliver_selectstring); + while (*p != 0) p++; + } + + if (deliver_selectstring_sender != NULL) + { + sprintf(CS p, " -S%s %s", deliver_selectstring_sender_regex? "r" : "", + deliver_selectstring_sender); + while (*p != 0) p++; + } + + log_detail = string_copy(big_buffer); + log_write(L_queue_run, LOG_MAIN, "Start queue run: %s", log_detail); + } + +/* If deliver_selectstring is a regex, compile it. */ + +if (deliver_selectstring != NULL && deliver_selectstring_regex) + selectstring_regex = regex_must_compile(deliver_selectstring, TRUE, FALSE); + +if (deliver_selectstring_sender != NULL && deliver_selectstring_sender_regex) + selectstring_regex_sender = + regex_must_compile(deliver_selectstring_sender, TRUE, FALSE); + +/* If the spool is split into subdirectories, we want to process it one +directory at a time, so as to spread out the directory scanning and the +delivering when there are lots of messages involved, except when +queue_run_in_order is set. + +In the random order case, this loop runs once for the main directory (handling +any messages therein), and then repeats for any subdirectories that were found. +When the first argument of queue_get_spool_list() is 0, it scans the top +directory, fills in subdirs, and sets subcount. The order of the directories is +then randomized after the first time through, before they are scanned in +subsqeuent iterations. + +When the first argument of queue_get_spool_list() is -1 (for queue_run_in_ +order), it scans all directories and makes a single message list. */ + +for (i = (queue_run_in_order? -1 : 0); + i <= (queue_run_in_order? -1 : subcount); + i++) + { + queue_filename *f; + void *reset_point1 = store_get(0); + + DEBUG(D_queue_run) + { + if (i == 0) + debug_printf("queue running main directory\n"); + else if (i == -1) + debug_printf("queue running combined directories\n"); + else + debug_printf("queue running subdirectory '%c'\n", subdirs[i]); + } + + for (f = queue_get_spool_list(i, subdirs, &subcount, !queue_run_in_order); + f != NULL; + f = f->next) + { + pid_t pid; + int status; + int pfd[2]; + struct stat statbuf; + uschar buffer[256]; + + /* Unless deliveries are forced, if deliver_queue_load_max is non-negative, + check that the load average is low enough to permit deliveries. */ + + if (!queue_run_force && deliver_queue_load_max >= 0) + { + load_average = os_getloadavg(); + if (load_average > deliver_queue_load_max) + { + log_write(0, LOG_MAIN, "abandon queue run (load %.2f, max %.2f)", + (double)load_average/1000.0, + (double)deliver_queue_load_max/1000.0); + i = subcount; /* Don't process other directories */ + break; + } + else + { + DEBUG(D_load) debug_printf("load average = %.2f max = %.2f\n", + (double)load_average/1000.0, + (double)deliver_queue_load_max/1000.0); + } + } + + /* Skip this message unless it's within the ID limits */ + + if (stop_id != NULL && Ustrncmp(f->text, stop_id, MESSAGE_ID_LENGTH) > 0) + continue; + if (start_id != NULL && Ustrncmp(f->text, start_id, MESSAGE_ID_LENGTH) < 0) + continue; + + /* Check that the message still exists */ + + message_subdir[0] = f->dir_uschar; + sprintf(CS buffer, "%s/input/%s/%s", spool_directory, message_subdir, + f->text); + if (Ustat(buffer, &statbuf) < 0) continue; + + /* There are some tests that require the reading of the header file. Ensure + the store used is scavenged afterwards so that this process doesn't keep + growing its store. We have to read the header file again when actually + delivering, but it's cheaper than forking a delivery process for each + message when many are not going to be delivered. */ + + if (deliver_selectstring != NULL || deliver_selectstring_sender != NULL || + queue_run_first_delivery) + { + BOOL wanted = TRUE; + BOOL orig_dont_deliver = dont_deliver; + void *reset_point2 = store_get(0); + + /* Restore the original setting of dont_deliver after reading the header, + so that a setting for a particular message doesn't force it for any that + follow. If the message is chosen for delivery, the header is read again + in the deliver_message() function, in a subprocess. */ + + if (spool_read_header(f->text, FALSE, TRUE) != spool_read_OK) continue; + dont_deliver = orig_dont_deliver; + + /* Now decide if we want to deliver this message. As we have read the + header file, we might as well do the freeze test now, and save forking + another process. */ + + if (deliver_freeze && !deliver_force_thaw) + { + log_write(L_skip_delivery, LOG_MAIN, "Message is frozen"); + wanted = FALSE; + } + + /* Check first_delivery in the case when there are no message logs. */ + + else if (queue_run_first_delivery && !deliver_firsttime) + { + DEBUG(D_queue_run) debug_printf("%s: not first delivery\n", f->text); + wanted = FALSE; + } + + /* Check for a matching address if deliver_selectstring[_sender} is set. + If so, we do a fully delivery - don't want to omit other addresses since + their routing might trigger re-writing etc. */ + + /* Sender matching */ + + else if (deliver_selectstring_sender != NULL && + !(deliver_selectstring_sender_regex? + (pcre_exec(selectstring_regex_sender, NULL, CS sender_address, + Ustrlen(sender_address), 0, PCRE_EOPT, NULL, 0) >= 0) + : + (strstric(sender_address, deliver_selectstring_sender, FALSE) + != NULL))) + { + DEBUG(D_queue_run) debug_printf("%s: sender address did not match %s\n", + f->text, deliver_selectstring_sender); + wanted = FALSE; + } + + /* Recipient matching */ + + else if (deliver_selectstring != NULL) + { + int i; + for (i = 0; i < recipients_count; i++) + { + uschar *address = recipients_list[i].address; + if ((deliver_selectstring_regex? + (pcre_exec(selectstring_regex, NULL, CS address, + Ustrlen(address), 0, PCRE_EOPT, NULL, 0) >= 0) + : + (strstric(address, deliver_selectstring, FALSE) != NULL)) + && + tree_search(tree_nonrecipients, address) == NULL) + break; + } + + if (i >= recipients_count) + { + DEBUG(D_queue_run) + debug_printf("%s: no recipient address matched %s\n", + f->text, deliver_selectstring); + wanted = FALSE; + } + } + + /* Recover store used when reading the header */ + + store_reset(reset_point2); + if (!wanted) continue; /* With next message */ + } + + /* OK, got a message we want to deliver. Create a pipe which will + serve as a means of detecting when all the processes created by the + delivery process are finished. This is relevant when the delivery + process passes one or more SMTP channels on to its own children. The + pipe gets passed down; by reading on it here we detect when the last + descendent dies by the unblocking of the read. It's a pity that for + most of the time the pipe isn't used, but creating a pipe should be + pretty cheap. */ + + if (pipe(pfd) < 0) + { + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to create pipe in queue " + "runner process %d: %s", queue_run_pid, strerror(errno)); + } + queue_run_pipe = pfd[pipe_write]; /* To ensure it gets passed on. */ + + /* Make sure it isn't stdin. This seems unlikely, but just to be on the + safe side... */ + + if (queue_run_pipe == 0) + { + queue_run_pipe = dup(queue_run_pipe); + close(0); + } + + /* Before forking to deliver the message, ensure any open and cached + lookup files or databases are closed. Otherwise, closing in the subprocess + can make the next subprocess have problems. There won't often be anything + open here, but it is possible (e.g. if spool_directory is an expanded + string). A single call before this loop would probably suffice, but just in + case expansions get inserted at some point, I've taken the heavy-handed + approach. When nothing is open, the call should be cheap. */ + + search_tidyup(); + + /* Now deliver the message; get the id by cutting the -H off the file + name. The return of the process is zero if a delivery was attempted. */ + + set_process_info("running queue: %s", f->text); + f->text[SPOOL_NAME_LENGTH-2] = 0; + if ((pid = fork()) == 0) + { + int rc; + (void)close(pfd[pipe_read]); + rc = deliver_message(f->text, force_delivery, FALSE); + _exit(rc == DELIVER_NOT_ATTEMPTED); + } + if (pid < 0) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "fork of delivery process from " + "queue runner %d failed\n", queue_run_pid); + + /* Close the writing end of the synchronizing pipe in this process, + then wait for the first level process to terminate. */ + + close(pfd[pipe_write]); + set_process_info("running queue: waiting for %s (%d)", f->text, pid); + while (wait(&status) != pid); + + /* A zero return means a delivery was attempted; turn off the force flag + for any subsequent calls unless queue_force is set. */ + + if ((status & 0xffff) == 0) force_delivery = queue_run_force; + + /* If the process crashed, tell somebody */ + + else if ((status & 0x00ff) != 0) + { + log_write(0, LOG_MAIN|LOG_PANIC, + "queue run: process %d crashed with signal %d while delivering %s", + (int)pid, status & 0x00ff, f->text); + } + + /* Before continuing, wait till the pipe gets closed at the far end. This + tells us that any children created by the delivery to re-use any SMTP + channels have all finished. Since no process actually writes to the pipe, + the mere fact that read() unblocks is enough. */ + + set_process_info("running queue: waiting for children of %d", pid); + (void)read(pfd[pipe_read], buffer, sizeof(buffer)); + (void)close(pfd[pipe_read]); + set_process_info("running queue"); + + /* If we are in the test harness, and this is not the first of a 2-stage + queue run, update fudged queue times. */ + + if (running_in_test_harness && !queue_2stage) + { + uschar *fqtnext = Ustrchr(fudged_queue_times, '/'); + if (fqtnext != NULL) fudged_queue_times = fqtnext + 1; + } + } /* End loop for list of messages */ + + store_reset(reset_point1); /* Scavenge list of messages */ + + /* If this was the first time through for random order processing, and + sub-directories have been found, randomize their order if necessary. */ + + if (i == 0 && subcount > 1 && !queue_run_in_order) + { + int j; + for (j = 1; j <= subcount; j++) + { + int r = random_number(100); + if (r >= 50) + { + int k = (r % subcount) + 1; + int x = subdirs[j]; + subdirs[j] = subdirs[k]; + subdirs[k] = x; + } + } + } + } /* End loop for multiple directories */ + +/* If queue_2stage is true, we do it all again, with the 2stage flag +turned off. */ + +if (queue_2stage) + { + queue_2stage = FALSE; + queue_run(start_id, stop_id, TRUE); + } + +/* At top level, log the end of the run. */ + +if (!recurse) log_write(L_queue_run, LOG_MAIN, "End queue run: %s", log_detail); +} + + + + +/************************************************ +* Count messages on the queue * +************************************************/ + +/* Called as a result of -bpc + +Arguments: none +Returns: nothing +*/ + +void +queue_count(void) +{ +int subcount; +int count = 0; +queue_filename *f = NULL; +uschar subdirs[64]; +f = queue_get_spool_list( + -1, /* entire queue */ + subdirs, /* for holding sub list */ + &subcount, /* for subcount */ + FALSE); /* not random */ +for (; f != NULL; f = f->next) count++; +fprintf(stdout, "%d\n", count); +} + + + +/************************************************ +* List extra deliveries * +************************************************/ + +/* This is called from queue_list below to print out all addresses that +have received a message but which were not primary addresses. That is, all +the addresses in the tree of non-recipients that are not primary addresses. +The tree has been scanned and the data field filled in for those that are +primary addresses. + +Argument: points to the tree node +Returns: nothing +*/ + +static void queue_list_extras(tree_node *p) +{ +if (p->left != NULL) queue_list_extras(p->left); +if (!p->data.val) printf(" +D %s\n", p->name); +if (p->right != NULL) queue_list_extras(p->right); +} + + + +/************************************************ +* List messages on the queue * +************************************************/ + +/* Or a given list of messages. In the "all" case, we get a list of file names +as quickly as possible, then scan each one for information to output. If any +disappear while we are processing, just leave them out, but give an error if an +explicit list was given. This function is a top-level function that is obeyed +as a result of the -bp argument. As there may be a lot of messages on the +queue, we must tidy up the store after reading the headers for each one. + +Arguments: + option 0 => list top-level recipients, with "D" for those delivered + 1 => list only undelivered top-level recipients + 2 => as 0, plus any generated delivered recipients + If 8 is added to any of these values, the queue is listed in + random order. + list => first of any message ids to list + count count of message ids; 0 => all + +Returns: nothing +*/ + +void +queue_list(int option, uschar **list, int count) +{ +int i; +int subcount; +int now = (int)time(NULL); +void *reset_point; +queue_filename *f = NULL; +uschar subdirs[64]; + +/* If given a list of messages, build a chain containing their ids. */ + +if (count > 0) + { + queue_filename *last = NULL; + for (i = 0; i < count; i++) + { + queue_filename *next = + store_get(sizeof(queue_filename) + Ustrlen(list[i]) + 2); + sprintf(CS next->text, "%s-H", list[i]); + next->dir_uschar = '*'; + next->next = NULL; + if (i == 0) f = next; else last->next = next; + last = next; + } + } + +/* Otherwise get a list of the entire queue, in order if necessary. */ + +else + f = queue_get_spool_list( + -1, /* entire queue */ + subdirs, /* for holding sub list */ + &subcount, /* for subcount */ + option >= 8); /* randomize if required */ + +if (option >= 8) option -= 8; + +/* Now scan the chain and print information, resetting store used +each time. */ + +reset_point = store_get(0); + +for (; f != NULL; f = f->next) + { + int rc, save_errno; + int size = 0; + BOOL env_read; + + store_reset(reset_point); + message_size = 0; + message_subdir[0] = f->dir_uschar; + rc = spool_read_header(f->text, FALSE, count <= 0); + if (rc == spool_read_notopen && errno == ENOENT && count <= 0) continue; + save_errno = errno; + + env_read = (rc == spool_read_OK || rc == spool_read_hdrerror); + + if (env_read) + { + int ptr; + FILE *jread; + struct stat statbuf; + + sprintf(CS big_buffer, "%s/input/%s/%s", spool_directory, message_subdir, + f->text); + ptr = Ustrlen(big_buffer)-1; + big_buffer[ptr] = 'D'; + + /* Add the data size to the header size; don't count the file name + at the start of the data file, but add one for the notional blank line + that precedes the data. */ + + if (Ustat(big_buffer, &statbuf) == 0) + size = message_size + statbuf.st_size - SPOOL_DATA_START_OFFSET + 1; + i = (now - received_time)/60; /* minutes on queue */ + if (i > 90) + { + i = (i + 30)/60; + if (i > 72) printf("%2dd ", (i + 12)/24); else printf("%2dh ", i); + } + else printf("%2dm ", i); + + /* Collect delivered addresses from any J file */ + + big_buffer[ptr] = 'J'; + jread = Ufopen(big_buffer, "rb"); + if (jread != NULL) + { + while (Ufgets(big_buffer, big_buffer_size, jread) != NULL) + { + int n = Ustrlen(big_buffer); + big_buffer[n-1] = 0; + tree_add_nonrecipient(big_buffer); + } + fclose(jread); + } + } + + fprintf(stdout, "%s ", string_format_size(size, big_buffer)); + for (i = 0; i < 16; i++) fputc(f->text[i], stdout); + + if (env_read && sender_address != NULL) + { + printf(" <%s>", sender_address); + if (sender_set_untrusted) printf(" (%s)", originator_login); + } + + if (rc != spool_read_OK) + { + printf("\n "); + if (save_errno == ERRNO_SPOOLFORMAT) + { + struct stat statbuf; + sprintf(CS big_buffer, "%s/input/%s/%s", spool_directory, message_subdir, + f->text); + if (Ustat(big_buffer, &statbuf) == 0) + { + int size = statbuf.st_size; /* Because might be a long */ + printf("*** spool format error: size=%d ***", size); + } + else printf("*** spool format error ***"); + } + else printf("*** spool read error: %s ***", strerror(save_errno)); + if (rc != spool_read_hdrerror) + { + printf("\n\n"); + continue; + } + } + + if (deliver_freeze) printf(" *** frozen ***"); + + printf("\n"); + + if (recipients_list != NULL) + { + for (i = 0; i < recipients_count; i++) + { + tree_node *delivered = + tree_search(tree_nonrecipients, recipients_list[i].address); + if (!delivered || option != 1) + printf(" %s %s\n", (delivered != NULL)? "D":" ", + recipients_list[i].address); + if (delivered != NULL) delivered->data.val = TRUE; + } + if (option == 2 && tree_nonrecipients != NULL) + queue_list_extras(tree_nonrecipients); + printf("\n"); + } + } +} + + + +/************************************************* +* Act on a specific message * +*************************************************/ + +/* Actions that require a list of addresses make use of +argv/argc/recipients_arg. Other actions do not. This function does its +own authority checking. + +Arguments: + id id of the message to work on + action which action is required (MSG_xxx) + argv the original argv for Exim + argc the original argc for Exim + recipients_arg offset to the list of recipients in argv + +Returns: FALSE if there was any problem +*/ + +BOOL +queue_action(uschar *id, int action, uschar **argv, int argc, int recipients_arg) +{ +int i, j; +BOOL yield = TRUE; +BOOL removed = FALSE; +struct passwd *pw; +uschar *doing = NULL; +uschar *username; +uschar *errmsg; +uschar spoolname[256]; + +/* Set the global message_id variable, used when re-writing spool files. This +also causes message ids to be added to log messages. */ + +Ustrcpy(message_id, id); + +/* The "actions" that just list the files do not require any locking to be +done. Only admin users may read the spool files. */ + +if (action >= MSG_SHOW_BODY) + { + int fd, i, rc; + uschar *subdirectory, *suffix; + + if (!admin_user) + { + printf("Permission denied\n"); + return FALSE; + } + + if (recipients_arg < argc) + { + printf("*** Only one message can be listed at once\n"); + return FALSE; + } + + if (action == MSG_SHOW_BODY) + { + subdirectory = US"input"; + suffix = US"-D"; + } + else if (action == MSG_SHOW_HEADER) + { + subdirectory = US"input"; + suffix = US"-H"; + } + else + { + subdirectory = US"msglog"; + suffix = US""; + } + + for (i = 0; i < 2; i++) + { + message_subdir[0] = (split_spool_directory == (i == 0))? id[5] : 0; + sprintf(CS spoolname, "%s/%s/%s/%s%s", spool_directory, subdirectory, + message_subdir, id, suffix); + fd = Uopen(spoolname, O_RDONLY, 0); + if (fd >= 0) break; + if (i == 0) continue; + printf("Failed to open %s file for %s%s: %s\n", subdirectory, id, suffix, + strerror(errno)); + if (action == MSG_SHOW_LOG && !message_logs) + printf("(No message logs are being created because the message_logs " + "option is false.)\n"); + return FALSE; + } + + while((rc = read(fd, big_buffer, big_buffer_size)) > 0) + write(fileno(stdout), big_buffer, rc); + + close(fd); + return TRUE; + } + +/* For actions that actually act, open and lock the data file to ensure that no +other process is working on this message. If the file does not exist, continue +only if the action is remove and the user is an admin user, to allow for +tidying up broken states. */ + +if (!spool_open_datafile(id)) + { + if (errno == ENOENT) + { + yield = FALSE; + printf("Spool data file for %s does not exist\n", id); + if (action != MSG_REMOVE || !admin_user) return FALSE; + printf("Continuing, to ensure all files removed\n"); + } + else + { + if (errno == 0) printf("Message %s is locked\n", id); + else printf("Couldn't open spool file for %s: %s\n", id, + strerror(errno)); + return FALSE; + } + } + +/* Read the spool header file for the message. Again, continue after an +error only in the case of deleting by an administrator. Setting the third +argument false causes it to look both in the main spool directory and in +the appropriate subdirectory, and set message_subdir according to where it +found the message. */ + +sprintf(CS spoolname, "%s-H", id); +if (spool_read_header(spoolname, TRUE, FALSE) != spool_read_OK) + { + yield = FALSE; + if (errno != ERRNO_SPOOLFORMAT) + printf("Spool read error for %s: %s\n", spoolname, strerror(errno)); + else + printf("Spool format error for %s\n", spoolname); + if (action != MSG_REMOVE || !admin_user) + { + close(deliver_datafile); + deliver_datafile = -1; + return FALSE; + } + printf("Continuing to ensure all files removed\n"); + } + +/* Check that the user running this process is entitled to operate on this +message. Only admin users may freeze/thaw, add/cancel recipients, or otherwise +mess about, but the original sender is permitted to remove a message. That's +why we leave this check until after the headers are read. */ + +if (!admin_user && (action != MSG_REMOVE || real_uid != originator_uid)) + { + printf("Permission denied\n"); + close(deliver_datafile); + deliver_datafile = -1; + return FALSE; + } + +/* Set up the user name for logging. */ + +pw = getpwuid(real_uid); +username = (pw != NULL)? + US pw->pw_name : string_sprintf("uid %ld", (long int)real_uid); + +/* Take the necessary action. */ + +printf("Message %s ", id); + +switch(action) + { + case MSG_FREEZE: + if (deliver_freeze) + { + yield = FALSE; + printf("is already frozen\n"); + } + else + { + deliver_freeze = TRUE; + deliver_manual_thaw = FALSE; + deliver_frozen_at = time(NULL); + if (spool_write_header(id, SW_MODIFYING, &errmsg) >= 0) + { + printf("is now frozen\n"); + log_write(0, LOG_MAIN, "frozen by %s", username); + } + else + { + yield = FALSE; + printf("could not be frozen: %s\n", errmsg); + } + } + break; + + + case MSG_THAW: + if (!deliver_freeze) + { + yield = FALSE; + printf("is not frozen\n"); + } + else + { + deliver_freeze = FALSE; + deliver_manual_thaw = TRUE; + if (spool_write_header(id, SW_MODIFYING, &errmsg) >= 0) + { + printf("is no longer frozen\n"); + log_write(0, LOG_MAIN, "unfrozen by %s", username); + } + else + { + yield = FALSE; + printf("could not be unfrozen: %s\n", errmsg); + } + } + break; + + + /* We must ensure all files are removed from both the input directory + and the appropriate subdirectory, to clean up cases when there are odd + files left lying around in odd places. In the normal case message_subdir + will have been set correctly by spool_read_header, but as this is a rare + operation, just run everything twice. */ + + case MSG_REMOVE: + message_subdir[0] = id[5]; + for (j = 0; j < 2; message_subdir[0] = 0, j++) + { + sprintf(CS spoolname, "%s/msglog/%s/%s", spool_directory, message_subdir, id); + if (Uunlink(spoolname) < 0) + { + if (errno != ENOENT) + { + yield = FALSE; + printf("Error while removing %s: %s\n", spoolname, + strerror(errno)); + } + } + else removed = TRUE; + + for (i = 0; i < 3; i++) + { + sprintf(CS spoolname, "%s/input/%s/%s-%c", spool_directory, message_subdir, + id, "DHJ"[i]); + if (Uunlink(spoolname) < 0) + { + if (errno != ENOENT) + { + yield = FALSE; + printf("Error while removing %s: %s\n", spoolname, + strerror(errno)); + } + } + else removed = TRUE; + } + } + + /* In the common case, the datafile is open (and locked), so give the + obvious message. Otherwise be more specific. */ + + if (deliver_datafile >= 0) printf("has been removed\n"); + else printf("has been removed or did not exist\n"); + if (removed) log_write(0, LOG_MAIN, "removed by %s", username); + break; + + + case MSG_MARK_ALL_DELIVERED: + for (i = 0; i < recipients_count; i++) + { + tree_add_nonrecipient(recipients_list[i].address); + } + if (spool_write_header(id, SW_MODIFYING, &errmsg) >= 0) + { + printf("has been modified\n"); + for (i = 0; i < recipients_count; i++) + log_write(0, LOG_MAIN, "address <%s> marked delivered by %s", + recipients_list[i].address, username); + } + else + { + yield = FALSE; + printf("- could not mark all delivered: %s\n", errmsg); + } + break; + + + case MSG_EDIT_SENDER: + if (recipients_arg < argc - 1) + { + yield = FALSE; + printf("- only one sender address can be specified\n"); + break; + } + doing = US"editing sender"; + /* Fall through */ + + case MSG_ADD_RECIPIENT: + if (doing == NULL) doing = US"adding recipient"; + /* Fall through */ + + case MSG_MARK_DELIVERED: + if (doing == NULL) doing = US"marking as delivered"; + + /* Common code for EDIT_SENDER, ADD_RECIPIENT, & MARK_DELIVERED */ + + if (recipients_arg >= argc) + { + yield = FALSE; + printf("- error while %s: no address given\n", doing); + break; + } + + for (; recipients_arg < argc; recipients_arg++) + { + int start, end, domain; + uschar *errmess; + uschar *recipient = + parse_extract_address(argv[recipients_arg], &errmess, &start, &end, + &domain, (action == MSG_EDIT_SENDER)); + + if (recipient == NULL) + { + yield = FALSE; + printf("- error while %s:\n bad address %s: %s\n", + doing, argv[recipients_arg], errmess); + } + else if (recipient[0] != 0 && domain == 0) + { + yield = FALSE; + printf("- error while %s:\n bad address %s: " + "domain missing\n", doing, argv[recipients_arg]); + } + else + { + if (action == MSG_ADD_RECIPIENT) + { + receive_add_recipient(recipient, -1); + log_write(0, LOG_MAIN, "recipient <%s> added by %s", + recipient, username); + } + else if (action == MSG_MARK_DELIVERED) + { + for (i = 0; i < recipients_count; i++) + if (Ustrcmp(recipients_list[i].address, recipient) == 0) break; + if (i >= recipients_count) + { + printf("- error while %s:\n %s is not a recipient:" + " message not updated\n", doing, recipient); + yield = FALSE; + } + else + { + tree_add_nonrecipient(recipients_list[i].address); + log_write(0, LOG_MAIN, "address <%s> marked delivered by %s", + recipient, username); + } + } + else /* MSG_EDIT_SENDER */ + { + sender_address = recipient; + log_write(0, LOG_MAIN, "sender address changed to <%s> by %s", + recipient, username); + } + } + } + + if (yield) + { + if (spool_write_header(id, SW_MODIFYING, &errmsg) >= 0) + printf("has been modified\n"); + else + { + yield = FALSE; + printf("- while %s: %s\n", doing, errmsg); + } + } + + break; + } + +/* Closing the datafile releases the lock and permits other processes +to operate on the message (if it still exists). */ + +close(deliver_datafile); +deliver_datafile = -1; +return yield; +} + + + +/************************************************* +* Check the queue_only_file condition * +*************************************************/ + +/* The queue_only_file option forces certain kinds of queueing if a given file +exists. + +Arguments: none +Returns: nothing +*/ + +void +queue_check_only(void) +{ +BOOL *set; +int sep = 0; +struct stat statbuf; +uschar *s, *ss, *name; +uschar buffer[1024]; + +if (queue_only_file == NULL) return; + +s = queue_only_file; +while ((ss = string_nextinlist(&s, &sep, buffer, sizeof(buffer))) != NULL) + { + if (Ustrncmp(ss, "smtp", 4) == 0) + { + name = US"queue_smtp"; + set = &queue_smtp; + ss += 4; + } + else + { + name = US"queue_only"; + set = &queue_only; + } + + if (Ustat(ss, &statbuf) == 0) + { + *set = TRUE; + DEBUG(D_receive) debug_printf("%s set because %s exists\n", name, ss); + } + } +} + +/* End of queue.c */ diff --git a/src/src/rda.c b/src/src/rda.c new file mode 100644 index 000000000..66fd8745d --- /dev/null +++ b/src/src/rda.c @@ -0,0 +1,945 @@ +/* $Cambridge: exim/src/src/rda.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* This module contains code for extracting addresses from a forwarding list +(from an alias or forward file) or by running the filter interpreter. It may do +this in a sub-process if a uid/gid are supplied. */ + + +#include "exim.h" + +enum { FILE_EXIST, FILE_NOT_EXIST, FILE_EXIST_UNCLEAR }; + +#define REPLY_EXISTS 0x01 +#define REPLY_EXPAND 0x02 +#define REPLY_RETURN 0x04 + + +/************************************************* +* Check string for filter program * +*************************************************/ + +/* This function checks whether a string is actually a filter program. The rule +is that it must start with "# Exim filter ..." (any capitalization, spaces +optional). It is envisaged that in future, other kinds of filter may be +implemented. That's why it is implemented the way it is. The function is global +because it is also called from filter.c when checking filters. + +Argument: the string + +Returns: FILTER_EXIM if it starts with "# Exim filter" + FILTER_SIEVE if it starts with "# Sieve filter" + FILTER_FORWARD otherwise +*/ + +/* This is an auxiliary function for matching a tag. */ + +static BOOL +match_tag(const uschar *s, const uschar *tag) +{ +for (; *tag != 0; s++, tag++) + { + if (*tag == ' ') + { + while (*s == ' ' || *s == '\t') s++; + s--; + } + else if (tolower(*s) != tolower(*tag)) break; + } +return (*tag == 0); +} + +/* This is the real function. It should be easy to add checking different +tags for other types of filter. */ + +int +rda_is_filter(const uschar *s) +{ +while (isspace(*s)) s++; /* Skips initial blank lines */ +if (match_tag(s, CUS"# exim filter")) return FILTER_EXIM; + else if (match_tag(s, CUS"# sieve filter")) return FILTER_SIEVE; + else return FILTER_FORWARD; +} + + + + +/************************************************* +* Check for existence of file * +*************************************************/ + +/* First of all, we stat the file. If this fails, we try to stat the enclosing +directory, because a file in an unmounted NFS directory will look the same as a +non-existent file. It seems that in Solaris 2.6, statting an entry in an +indirect map that is currently unmounted does not cause the mount to happen. +Instead, dummy data is returned, which defeats the whole point of this test. +However, if a stat() is done on some object inside the directory, such as the +"." back reference to itself, then the mount does occur. If an NFS host is +taken offline, it is possible for the stat() to get stuck until it comes back. +To guard against this, stick a timer round it. If we can't access the "." +inside the directory, try the plain directory, just in case that helps. + +Argument: + filename the file name + error for message on error + +Returns: FILE_EXIST the file exists + FILE_NOT_EXIST the file does not exist + FILE_EXIST_UNCLEAR cannot determine existence +*/ + +static int +rda_exists(uschar *filename, uschar **error) +{ +int rc, saved_errno; +uschar *slash; +struct stat statbuf; + +if ((rc = Ustat(filename, &statbuf)) >= 0) return FILE_EXIST; +saved_errno = errno; + +Ustrncpy(big_buffer, filename, big_buffer_size - 3); +sigalrm_seen = FALSE; + +if (saved_errno == ENOENT) + { + slash = Ustrrchr(big_buffer, '/'); + Ustrcpy(slash+1, "."); + + alarm(30); + rc = Ustat(big_buffer, &statbuf); + if (rc != 0 && errno == EACCES && !sigalrm_seen) + { + *slash = 0; + rc = Ustat(big_buffer, &statbuf); + } + saved_errno = errno; + alarm(0); + + DEBUG(D_route) debug_printf("stat(%s)=%d\n", big_buffer, rc); + } + +if (sigalrm_seen || rc != 0) + { + *error = string_sprintf("failed to stat %s (%s)", big_buffer, + sigalrm_seen? "timeout" : strerror(saved_errno)); + return FILE_EXIST_UNCLEAR; + } + +*error = string_sprintf("%s does not exist", filename); +DEBUG(D_route) debug_printf("%s\n", *error); +return FILE_NOT_EXIST; +} + + + +/************************************************* +* Get forwarding list from a file * +*************************************************/ + +/* Open a file and read its entire contents into a block of memory. Certain +opening errors are optionally treated the same as "file does not exist". + +ENOTDIR means that something along the line is not a directory: there are +installations that set home directories to be /dev/null for non-login accounts +but in normal circumstances this indicates some kind of configuration error. + +EACCES means there's a permissions failure. Some users turn off read permission +on a .forward file to suspend forwarding, but this is probably an error in any +kind of mailing list processing. + +The redirect block that contains the file name also contains constraints such +as who may own the file, and mode bits that must not be set. This function is + +Arguments: + rdata rdirect block, containing file name and constraints + options for the RDO_ENOTDIR and RDO_EACCES options + error where to put an error message + yield what to return from rda_interpret on error + +Returns: pointer to string in store; NULL on error +*/ + +static uschar * +rda_get_file_contents(redirect_block *rdata, int options, uschar **error, + int *yield) +{ +FILE *fwd; +uschar *filebuf; +uschar *filename = rdata->string; +BOOL uid_ok = !rdata->check_owner; +BOOL gid_ok = !rdata->check_group; +struct stat statbuf; + +/* Attempt to open the file. If it appears not to exist, check up on the +containing directory by statting it. If the directory does not exist, we treat +this situation as an error (which will cause delivery to defer); otherwise we +pass back FF_NONEXIST, which causes the redirect router to decline. + +However, if the ignore_enotdir option is set (to ignore "something on the +path is not a directory" errors), the right behaviour seems to be not to do the +directory test. */ + +fwd = Ufopen(filename, "rb"); +if (fwd == NULL) + { + switch(errno) + { + case ENOENT: /* File does not exist */ + DEBUG(D_route) debug_printf("%s does not exist\n%schecking parent directory\n", + filename, + ((options & RDO_ENOTDIR) != 0)? "ignore_enotdir set => skip " : ""); + *yield = (((options & RDO_ENOTDIR) != 0) || + rda_exists(filename, error) == FILE_NOT_EXIST)? + FF_NONEXIST : FF_ERROR; + return NULL; + + case ENOTDIR: /* Something on the path isn't a directory */ + if ((options & RDO_ENOTDIR) == 0) goto DEFAULT_ERROR; + DEBUG(D_route) debug_printf("non-directory on path %s: file assumed not to " + "exist\n", filename); + *yield = FF_NONEXIST; + return NULL; + + case EACCES: /* Permission denied */ + if ((options & RDO_EACCES) == 0) goto DEFAULT_ERROR; + DEBUG(D_route) debug_printf("permission denied for %s: file assumed not to " + "exist\n", filename); + *yield = FF_NONEXIST; + return NULL; + + DEFAULT_ERROR: + default: + *error = string_open_failed(errno, "%s", filename); + *yield = FF_ERROR; + return NULL; + } + } + +/* Check that we have a regular file. */ + +if (fstat(fileno(fwd), &statbuf) != 0) + { + *error = string_sprintf("failed to stat %s: %s", filename, strerror(errno)); + goto ERROR_RETURN; + } + +if ((statbuf.st_mode & S_IFMT) != S_IFREG) + { + *error = string_sprintf("%s is not a regular file", filename); + goto ERROR_RETURN; + } + +/* Check for unwanted mode bits */ + +if ((statbuf.st_mode & rdata->modemask) != 0) + { + *error = string_sprintf("bad mode (0%o) for %s: 0%o bit(s) unexpected", + statbuf.st_mode, filename, statbuf.st_mode & rdata->modemask); + goto ERROR_RETURN; + } + +/* Check the file owner and file group if required to do so. */ + +if (!uid_ok) + { + if (rdata->pw != NULL && statbuf.st_uid == rdata->pw->pw_uid) + uid_ok = TRUE; + else if (rdata->owners != NULL) + { + int i; + for (i = 1; i <= (int)(rdata->owners[0]); i++) + if (rdata->owners[i] == statbuf.st_uid) { uid_ok = TRUE; break; } + } + } + +if (!gid_ok) + { + if (rdata->pw != NULL && statbuf.st_gid == rdata->pw->pw_gid) + gid_ok = TRUE; + else if (rdata->owngroups != NULL) + { + int i; + for (i = 1; i <= (int)(rdata->owngroups[0]); i++) + if (rdata->owngroups[i] == statbuf.st_gid) { gid_ok = TRUE; break; } + } + } + +if (!uid_ok || !gid_ok) + { + *error = string_sprintf("bad %s for %s", uid_ok? "group" : "owner", filename); + goto ERROR_RETURN; + } + +/* Put an upper limit on the size of the file, just to stop silly people +feeding in ridiculously large files, which can easily be created by making +files that have holes in them. */ + +if (statbuf.st_size > MAX_FILTER_SIZE) + { + *error = string_sprintf("%s is too big (max %d)", filename, MAX_FILTER_SIZE); + goto ERROR_RETURN; + } + +/* Read the file in one go in order to minimize the time we have it open. */ + +filebuf = store_get(statbuf.st_size + 1); + +if (fread(filebuf, 1, statbuf.st_size, fwd) != statbuf.st_size) + { + *error = string_sprintf("error while reading %s: %s", + filename, strerror(errno)); + goto ERROR_RETURN; + } +filebuf[statbuf.st_size] = 0; + +/* Don't pass statbuf.st_size directly to debug_printf. On some systems it +is a long, which may not be the same as an int. */ + +DEBUG(D_route) + { + int size = (int)statbuf.st_size; + debug_printf("%d bytes read from %s\n", size, filename); + } + +fclose(fwd); +return filebuf; + +/* Return an error: the string is already set up. */ + +ERROR_RETURN: +*yield = FF_ERROR; +fclose(fwd); +return NULL; +} + + + +/************************************************* +* Extract info from list or filter * +*************************************************/ + +/* This function calls the appropriate function to extract addresses from a +forwarding list, or to run a filter file and get addresses from there. + +Arguments: + rdata the redirection block + options the options bits + include_directory restrain to this directory + sieve_vacation_directory passed to sieve_interpret + generated where to hang generated addresses + error for error messages + eblockp for details of skipped syntax errors + (NULL => no skip) + filtertype set to the filter type: + FILTER_FORWARD => a traditional .forward file + FILTER_EXIM => an Exim filter file + FILTER_SIEVE => a Sieve filter file + a system filter is always forced to be FILTER_EXIM + +Returns: a suitable return for rda_interpret() +*/ + +static int +rda_extract(redirect_block *rdata, int options, uschar *include_directory, + uschar *sieve_vacation_directory, address_item **generated, uschar **error, + error_block **eblockp, int *filtertype) +{ +uschar *data; + +if (rdata->isfile) + { + int yield; + data = rda_get_file_contents(rdata, options, error, &yield); + if (data == NULL) return yield; + } +else data = rdata->string; + +*filtertype = system_filtering? FILTER_EXIM : rda_is_filter(data); + +/* Filter interpretation is done by a general function that is also called from +the filter testing option (-bf). There are two versions: one for Exim filtering +and one for Sieve filtering. Several features of string expansion may be locked +out at sites that don't trust users. This is done by setting flags in +expand_forbid that the expander inspects. */ + +if (*filtertype != FILTER_FORWARD) + { + int frc; + int old_expand_forbid = expand_forbid; + + if ((options & RDO_FILTER) == 0) + { + *error = US"filtering not enabled"; + return FF_ERROR; + } + + DEBUG(D_route) debug_printf("data is %s filter program\n", + (*filtertype == FILTER_EXIM)? "an Exim" : "a Sieve"); + + expand_forbid = + (expand_forbid & ~RDO_FILTER_EXPANSIONS) | + (options & RDO_FILTER_EXPANSIONS); + + frc = (*filtertype == FILTER_EXIM)? + filter_interpret(data, options, generated, error) + : + sieve_interpret(data, options, sieve_vacation_directory, generated, error); + + expand_forbid = old_expand_forbid; + return frc; + } + +/* Not a filter script */ + +DEBUG(D_route) debug_printf("file is not a filter file\n"); + +return parse_forward_list(data, + options, /* specials that are allowed */ + generated, /* where to hang them */ + error, /* for errors */ + deliver_domain, /* to qualify \name */ + include_directory, /* restrain to directory */ + eblockp); /* for skipped syntax errors */ +} + + + + +/************************************************* +* Write string down pipe * +*************************************************/ + +/* This function is used for tranferring a string down a pipe between +processes. If the pointer is NULL, a length of zero is written. + +Arguments: + fd the pipe + s the string + +Returns: nothing +*/ + +static void +rda_write_string(int fd, uschar *s) +{ +int len = (s == NULL)? 0 : Ustrlen(s) + 1; +write(fd, &len, sizeof(int)); +if (s != NULL) write(fd, s, len); +} + + + +/************************************************* +* Read string from pipe * +*************************************************/ + +/* This function is used for receiving a string from a pipe. + +Arguments: + fd the pipe + sp where to put the string + +Returns: FALSE if data missing +*/ + +static BOOL +rda_read_string(int fd, uschar **sp) +{ +int len; + +if (read(fd, &len, sizeof(int)) != sizeof(int)) return FALSE; +if (len == 0) *sp = NULL; else + { + *sp = store_get(len); + if (read(fd, *sp, len) != len) return FALSE; + } +return TRUE; +} + + + +/************************************************* +* Interpret forward list or filter * +*************************************************/ + +/* This function is passed a forward list string (unexpanded) or the name of a +file (unexpanded) whose contents are the forwarding list. The list may in fact +be a filter program if it starts with "#Exim filter" or "#Sieve filter". Other +types of filter, with different inital tag strings, may be introduced in due +course. + +The job of the function is to process the forwarding list or filter. It is +pulled out into this separate function, because it is used for system filter +files as well as from the redirect router. + +If the function is given a uid/gid, it runs a subprocess that passes the +results back via a pipe. This provides security for things like :include:s in +users' .forward files, and "logwrite" calls in users' filter files. A +sub-process is NOT used when: + + . No uid/gid is provided + . The input is a string which is not a filter string, and does not contain + :include: + . The input is a file whose non-existence can be detected in the main + process (which is usually running as root). + +Arguments: + rdata redirect data (file + constraints, or data string) + options options to pass to the extraction functions, + plus ENOTDIR and EACCES handling bits + include_directory restrain :include: to this directory + sieve_vacation_directory directory passed to sieve_interpret() + ugid uid/gid to run under - if NULL, no change + generated where to hang generated addresses, initially NULL + error pointer for error message + eblockp for skipped syntax errors; NULL if no skipping + filtertype set to the type of file: + FILTER_FORWARD => traditional .forward file + FILTER_EXIM => an Exim filter file + FILTER_SIEVE => a Sieve filter file + a system filter is always forced to be FILTER_EXIM + rname router name for error messages in the format + "xxx router" or "system filter" + +Returns: values from extraction function, or FF_NONEXIST: + FF_DELIVERED success, a significant action was taken + FF_NOTDELIVERED success, no significant action + FF_BLACKHOLE :blackhole: + FF_DEFER defer requested + FF_FAIL fail requested + FF_INCLUDEFAIL some problem with :include: + FF_FREEZE freeze requested + FF_ERROR there was a problem + FF_NONEXIST the file does not exist +*/ + +int +rda_interpret(redirect_block *rdata, int options, uschar *include_directory, + uschar *sieve_vacation_directory, ugid_block *ugid, address_item **generated, + uschar **error, error_block **eblockp, int *filtertype, uschar *rname) +{ +int fd, rc, pfd[2]; +int yield, status; +BOOL had_disaster = FALSE; +pid_t pid; +uschar *data; +uschar *readerror = US""; +void (*oldsignal)(int); + +DEBUG(D_route) debug_printf("rda_interpret (%s): %s\n", + (rdata->isfile)? "file" : "string", rdata->string); + +/* Do the expansions of the file name or data first, while still privileged. */ + +data = expand_string(rdata->string); +if (data == NULL) + { + if (expand_string_forcedfail) return FF_NOTDELIVERED; + *error = string_sprintf("failed to expand \"%s\": %s", rdata->string, + expand_string_message); + return FF_ERROR; + } +rdata->string = data; + +DEBUG(D_route) debug_printf("expanded: %s\n", data); + +if (rdata->isfile && data[0] != '/') + { + *error = string_sprintf("\"%s\" is not an absolute path", data); + return FF_ERROR; + } + +/* If no uid/gid are supplied, or if we have a data string which does not start +with #Exim filter or #Sieve filter, and does not contain :include:, do all the +work in this process. Note that for a system filter, we always have a file, so +the work is done in this process only if no user is supplied. */ + +if (!ugid->uid_set || /* Either there's no uid, or */ + (!rdata->isfile && /* We've got the data, and */ + rda_is_filter(data) == FILTER_FORWARD && /* It's not a filter script, */ + Ustrstr(data, ":include:") == NULL)) /* and there's no :include: */ + { + return rda_extract(rdata, options, include_directory, + sieve_vacation_directory, generated, error, eblockp, filtertype); + } + +/* We need to run the processing code in a sub-process. However, if we can +determine the non-existence of a file first, we can decline without having to +create the sub-process. */ + +if (rdata->isfile && rda_exists(data, error) == FILE_NOT_EXIST) + return FF_NONEXIST; + +/* If the file does exist, or we can't tell (non-root mounted NFS directory) +we have to create the subprocess to do everything as the given user. The +results of processing are passed back via a pipe. */ + +if (pipe(pfd) != 0) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "creation of pipe for filter or " + ":include: failed for %s: %s", rname, strerror(errno)); + +/* Ensure that SIGCHLD is set to SIG_DFL before forking, so that the child +process can be waited for. We sometimes get here with it set otherwise. Save +the old state for resetting on the wait. */ + +oldsignal = signal(SIGCHLD, SIG_DFL); +if ((pid = fork()) == 0) + { + header_line *waslast = header_last; /* Save last header */ + + fd = pfd[pipe_write]; + close(pfd[pipe_read]); + exim_setugid(ugid->uid, ugid->gid, FALSE, rname); + + /* Addresses can get rewritten in filters; if we are not root or the exim + user (and we probably are not), turn off rewrite logging, because we cannot + write to the log now. */ + + if (ugid->uid != root_uid && ugid->uid != exim_uid) + { + DEBUG(D_rewrite) debug_printf("turned off address rewrite logging (not " + "root or exim in this process)\n"); + log_write_selector &= ~L_address_rewrite; + } + + /* Now do the business */ + + yield = rda_extract(rdata, options, include_directory, + sieve_vacation_directory, generated, error, eblockp, filtertype); + + /* Pass back whether it was a filter, and the return code and any overall + error text via the pipe. */ + + write(fd, filtertype, sizeof(int)); + write(fd, &yield, sizeof(int)); + rda_write_string(fd, *error); + + /* Pass back the contents of any syntax error blocks if we have a pointer */ + + if (eblockp != NULL) + { + error_block *ep; + for (ep = *eblockp; ep != NULL; ep = ep->next) + { + rda_write_string(fd, ep->text1); + rda_write_string(fd, ep->text2); + } + rda_write_string(fd, NULL); /* Indicates end of eblocks */ + } + + /* If this is a system filter, we have to pass back the numbers of any + original header lines that were removed, and then any header lines that were + added but not subsequently removed. */ + + if (system_filtering) + { + int i = 0; + header_line *h; + for (h = header_list; h != waslast->next; i++, h = h->next) + { + if (h->type == htype_old) write(fd, &i, sizeof(i)); + } + i = -1; + write(fd, &i, sizeof(i)); + + while (waslast != header_last) + { + waslast = waslast->next; + if (waslast->type != htype_old) + { + rda_write_string(fd, waslast->text); + write(fd, &(waslast->type), sizeof(waslast->type)); + } + } + rda_write_string(fd, NULL); /* Indicates end of added headers */ + } + + /* Write the contents of the $n variables */ + + write(fd, filter_n, sizeof(filter_n)); + + /* If the result was DELIVERED or NOTDELIVERED, we pass back the generated + addresses, and their associated information, through the pipe. This is + just tedious, but it seems to be the only safe way. We do this also for + FAIL and FREEZE, because a filter is allowed to set up deliveries that + are honoured before freezing or failing. */ + + if (yield == FF_DELIVERED || yield == FF_NOTDELIVERED || + yield == FF_FAIL || yield == FF_FREEZE) + { + address_item *addr; + for (addr = *generated; addr != NULL; addr = addr->next) + { + int reply_options = 0; + + rda_write_string(fd, addr->address); + write(fd, &(addr->mode), sizeof(addr->mode)); + write(fd, &(addr->flags), sizeof(addr->flags)); + rda_write_string(fd, addr->p.errors_address); + + if (addr->pipe_expandn != NULL) + { + uschar **pp; + for (pp = addr->pipe_expandn; *pp != NULL; pp++) + rda_write_string(fd, *pp); + } + rda_write_string(fd, NULL); + + if (addr->reply == NULL) + write(fd, &reply_options, sizeof(int)); /* 0 means no reply */ + else + { + reply_options |= REPLY_EXISTS; + if (addr->reply->file_expand) reply_options |= REPLY_EXPAND; + if (addr->reply->return_message) reply_options |= REPLY_RETURN; + write(fd, &reply_options, sizeof(int)); + write(fd, &(addr->reply->expand_forbid), sizeof(int)); + write(fd, &(addr->reply->once_repeat), sizeof(time_t)); + rda_write_string(fd, addr->reply->to); + rda_write_string(fd, addr->reply->cc); + rda_write_string(fd, addr->reply->bcc); + rda_write_string(fd, addr->reply->from); + rda_write_string(fd, addr->reply->reply_to); + rda_write_string(fd, addr->reply->subject); + rda_write_string(fd, addr->reply->headers); + rda_write_string(fd, addr->reply->text); + rda_write_string(fd, addr->reply->file); + rda_write_string(fd, addr->reply->logfile); + rda_write_string(fd, addr->reply->oncelog); + } + } + + rda_write_string(fd, NULL); /* Marks end of addresses */ + } + + /* OK, this process is now done. Must use _exit() and not exit() !! */ + + close(fd); + _exit(0); + } + +/* Back in the main process: panic if the fork did not succeed. */ + +if (pid < 0) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "fork failed for %s", rname); + +/* Read the pipe to get the data from the filter/forward. Our copy of the +writing end must be closed first, as otherwise read() won't return zero on an +empty pipe. Afterwards, close the reading end. */ + +close(pfd[pipe_write]); + +/* Read initial data, including yield and contents of *error */ + +fd = pfd[pipe_read]; +if (read(fd, filtertype, sizeof(int)) != sizeof(int) || + read(fd, &yield, sizeof(int)) != sizeof(int) || + !rda_read_string(fd, error)) goto DISASTER; + +DEBUG(D_route) + debug_printf("rda_interpret: subprocess yield=%d error=%s\n", yield, *error); + +/* Read the contents of any syntax error blocks if we have a pointer */ + +if (eblockp != NULL) + { + uschar *s; + error_block *e; + error_block **p = eblockp; + for (;;) + { + if (!rda_read_string(fd, &s)) goto DISASTER; + if (s == NULL) break; + e = store_get(sizeof(error_block)); + e->next = NULL; + e->text1 = s; + if (!rda_read_string(fd, &s)) goto DISASTER; + e->text2 = s; + *p = e; + p = &(e->next); + } + } + +/* If this is a system filter, read the identify of any original header lines +that were removed, and then read data for any new ones that were added. */ + +if (system_filtering) + { + int hn = 0; + header_line *h = header_list; + + for (;;) + { + int n; + if (read(fd, &n, sizeof(int)) != sizeof(int)) goto DISASTER; + if (n < 0) break; + while (hn < n) + { + hn++; + h = h->next; + if (h == NULL) goto DISASTER_NO_HEADER; + } + h->type = htype_old; + } + + for (;;) + { + uschar *s; + int type; + if (!rda_read_string(fd, &s)) goto DISASTER; + if (s == NULL) break; + if (read(fd, &type, sizeof(type)) != sizeof(type)) goto DISASTER; + header_add(type, "%s", s); + } + } + +/* Read the values of the $n variables */ + +if (read(fd, filter_n, sizeof(filter_n)) != sizeof(filter_n)) goto DISASTER; + +/* If the yield is DELIVERED, NOTDELIVERED, FAIL, or FREEZE there may follow +addresses and data to go with them. Keep them in the same order in the +generated chain. */ + +if (yield == FF_DELIVERED || yield == FF_NOTDELIVERED || + yield == FF_FAIL || yield == FF_FREEZE) + { + address_item **nextp = generated; + + for (;;) + { + int i, reply_options; + address_item *addr; + uschar *recipient; + uschar *expandn[EXPAND_MAXN + 2]; + + /* First string is the address; NULL => end of addresses */ + + if (!rda_read_string(fd, &recipient)) goto DISASTER; + if (recipient == NULL) break; + + /* Hang on the end of the chain */ + + addr = deliver_make_addr(recipient, FALSE); + *nextp = addr; + nextp = &(addr->next); + + /* Next comes the mode and the flags fields */ + + if (read(fd, &(addr->mode), sizeof(addr->mode)) != sizeof(addr->mode) || + read(fd, &(addr->flags), sizeof(addr->flags)) != sizeof(addr->flags) || + !rda_read_string(fd, &(addr->p.errors_address))) goto DISASTER; + + /* Next comes a possible setting for $thisaddress and any numerical + variables for pipe expansion, terminated by a NULL string. The maximum + number of numericals is EXPAND_MAXN. Note that we put filter_thisaddress + into the zeroth item in the vector - this is sorted out inside the pipe + transport. */ + + for (i = 0; i < EXPAND_MAXN + 1; i++) + { + uschar *temp; + if (!rda_read_string(fd, &temp)) goto DISASTER; + if (i == 0) filter_thisaddress = temp; /* Just in case */ + expandn[i] = temp; + if (temp == NULL) break; + } + + if (i > 0) + { + addr->pipe_expandn = store_get((i+1) * sizeof(uschar **)); + addr->pipe_expandn[i] = NULL; + while (--i >= 0) addr->pipe_expandn[i] = expandn[i]; + } + + /* Then an int containing reply options; zero => no reply data. */ + + if (read(fd, &reply_options, sizeof(int)) != sizeof(int)) goto DISASTER; + if ((reply_options & REPLY_EXISTS) != 0) + { + addr->reply = store_get(sizeof(reply_item)); + + addr->reply->file_expand = (reply_options & REPLY_EXPAND) != 0; + addr->reply->return_message = (reply_options & REPLY_RETURN) != 0; + + if (read(fd,&(addr->reply->expand_forbid),sizeof(int)) != + sizeof(int) || + read(fd,&(addr->reply->once_repeat),sizeof(time_t)) != + sizeof(time_t) || + !rda_read_string(fd, &(addr->reply->to)) || + !rda_read_string(fd, &(addr->reply->cc)) || + !rda_read_string(fd, &(addr->reply->bcc)) || + !rda_read_string(fd, &(addr->reply->from)) || + !rda_read_string(fd, &(addr->reply->reply_to)) || + !rda_read_string(fd, &(addr->reply->subject)) || + !rda_read_string(fd, &(addr->reply->headers)) || + !rda_read_string(fd, &(addr->reply->text)) || + !rda_read_string(fd, &(addr->reply->file)) || + !rda_read_string(fd, &(addr->reply->logfile)) || + !rda_read_string(fd, &(addr->reply->oncelog))) + goto DISASTER; + } + } + } + +/* All data has been transferred from the sub-process. Reap it, close the +reading end of the pipe, and we are done. */ + +WAIT_EXIT: +while ((rc = wait(&status)) != pid) + { + if (rc < 0 && errno == ECHILD) /* Process has vanished */ + { + log_write(0, LOG_MAIN, "redirection process %d vanished unexpectedly", pid); + goto FINAL_EXIT; + } + } + +if (had_disaster) + { + *error = string_sprintf("internal problem in %s: failure to transfer " + "data from subprocess: status=%04x%s%s%s", rname, + status, readerror, + (*error == NULL)? US"" : US": error=", + (*error == NULL)? US"" : *error); + log_write(0, LOG_MAIN|LOG_PANIC, "%s", *error); + } +else if (status != 0) + { + log_write(0, LOG_MAIN|LOG_PANIC, "internal problem in %s: unexpected status " + "%04x from redirect subprocess (but data correctly received)", rname, + status); + } + +FINAL_EXIT: +close(fd); +signal(SIGCHLD, oldsignal); /* restore */ +return yield; + + +/* Come here if the data indicates removal of a header that we can't find */ + +DISASTER_NO_HEADER: +readerror = US" readerror=bad header identifier"; +had_disaster = TRUE; +yield = FF_ERROR; +goto WAIT_EXIT; + +/* Come here is there's a shambles in transferring the data over the pipe. The +value of errno should still be set. */ + +DISASTER: +readerror = string_sprintf(" readerror='%s'", strerror(errno)); +had_disaster = TRUE; +yield = FF_ERROR; +goto WAIT_EXIT; +} + +/* End of rda.c */ diff --git a/src/src/readconf.c b/src/src/readconf.c new file mode 100644 index 000000000..95470763d --- /dev/null +++ b/src/src/readconf.c @@ -0,0 +1,3702 @@ +/* $Cambridge: exim/src/src/readconf.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Functions for reading the configuration file, and for displaying +overall configuration values. Thanks to Brian Candler for the original +implementation of the conditional .ifdef etc. */ + +#include "exim.h" + +#define CSTATE_STACK_SIZE 10 + + +/* Structure for chain (stack) of .included files */ + +typedef struct config_file_item { + struct config_file_item *next; + uschar *filename; + FILE *file; + int lineno; +} config_file_item; + +/* Structure of table of conditional words and their state transitions */ + +typedef struct cond_item { + uschar *name; + int namelen; + int action1; + int action2; + int pushpop; +} cond_item; + +/* Structure of table of syslog facility names and values */ + +typedef struct syslog_fac_item { + uschar *name; + int value; +} syslog_fac_item; + + +/* Static variables */ + +static config_file_item *config_file_stack = NULL; /* For includes */ + +static uschar *syslog_facility_str = NULL; +static uschar next_section[24]; +static uschar time_buffer[24]; + +/* State variables for conditional loading (.ifdef / .else / .endif) */ + +static int cstate = 0; +static int cstate_stack_ptr = -1; +static int cstate_stack[CSTATE_STACK_SIZE]; + +/* Table of state transitions for handling conditional inclusions. There are +four possible state transitions: + + .ifdef true + .ifdef false + .elifdef true (or .else) + .elifdef false + +.endif just causes the previous cstate to be popped off the stack */ + +static int next_cstate[3][4] = + { + /* State 0: reading from file, or reading until next .else or .endif */ + { 0, 1, 2, 2 }, + /* State 1: condition failed, skipping until next .else or .endif */ + { 2, 2, 0, 1 }, + /* State 2: skipping until .endif */ + { 2, 2, 2, 2 }, + }; + +/* Table of conditionals and the states to set. For each name, there are four +values: the length of the name (to save computing it each time), the state to +set if a macro was found in the line, the state to set if a macro was not found +in the line, and a stack manipulation setting which is: + + -1 pull state value off the stack + 0 don't alter the stack + +1 push value onto stack, before setting new state +*/ + +static cond_item cond_list[] = { + { US"ifdef", 5, 0, 1, 1 }, + { US"ifndef", 6, 1, 0, 1 }, + { US"elifdef", 7, 2, 3, 0 }, + { US"elifndef", 8, 3, 2, 0 }, + { US"else", 4, 2, 2, 0 }, + { US"endif", 5, 0, 0, -1 } +}; + +static int cond_list_size = sizeof(cond_list)/sizeof(cond_item); + +/* Table of syslog facility names and their values */ + +static syslog_fac_item syslog_list[] = { + { US"mail", LOG_MAIL }, + { US"user", LOG_USER }, + { US"news", LOG_NEWS }, + { US"uucp", LOG_UUCP }, + { US"local0", LOG_LOCAL0 }, + { US"local1", LOG_LOCAL1 }, + { US"local2", LOG_LOCAL2 }, + { US"local3", LOG_LOCAL3 }, + { US"local4", LOG_LOCAL4 }, + { US"local5", LOG_LOCAL5 }, + { US"local6", LOG_LOCAL6 }, + { US"local7", LOG_LOCAL7 }, + { US"daemon", LOG_DAEMON } +}; + +static int syslog_list_size = sizeof(syslog_list)/sizeof(syslog_fac_item); + + + + +/************************************************* +* Main configuration options * +*************************************************/ + +/* The list of options that can be set in the main configuration file. This +must be in alphabetic order because it is searched by binary chop. */ + +static optionlist optionlist_config[] = { + { "*set_exim_group", opt_bool|opt_hidden, &exim_gid_set }, + { "*set_exim_user", opt_bool|opt_hidden, &exim_uid_set }, + { "*set_system_filter_group", opt_bool|opt_hidden, &system_filter_gid_set }, + { "*set_system_filter_user", opt_bool|opt_hidden, &system_filter_uid_set }, + { "accept_8bitmime", opt_bool, &accept_8bitmime }, + { "acl_not_smtp", opt_stringptr, &acl_not_smtp }, + { "acl_smtp_auth", opt_stringptr, &acl_smtp_auth }, + { "acl_smtp_connect", opt_stringptr, &acl_smtp_connect }, + { "acl_smtp_data", opt_stringptr, &acl_smtp_data }, + { "acl_smtp_etrn", opt_stringptr, &acl_smtp_etrn }, + { "acl_smtp_expn", opt_stringptr, &acl_smtp_expn }, + { "acl_smtp_helo", opt_stringptr, &acl_smtp_helo }, + { "acl_smtp_mail", opt_stringptr, &acl_smtp_mail }, + { "acl_smtp_mailauth", opt_stringptr, &acl_smtp_mailauth }, + { "acl_smtp_predata", opt_stringptr, &acl_smtp_predata }, + { "acl_smtp_quit", opt_stringptr, &acl_smtp_quit }, + { "acl_smtp_rcpt", opt_stringptr, &acl_smtp_rcpt }, +#ifdef SUPPORT_TLS + { "acl_smtp_starttls", opt_stringptr, &acl_smtp_starttls }, +#endif + { "acl_smtp_vrfy", opt_stringptr, &acl_smtp_vrfy }, + { "admin_groups", opt_gidlist, &admin_groups }, + { "allow_domain_literals", opt_bool, &allow_domain_literals }, + { "allow_mx_to_ip", opt_bool, &allow_mx_to_ip }, + { "allow_utf8_domains", opt_bool, &allow_utf8_domains }, + { "auth_advertise_hosts", opt_stringptr, &auth_advertise_hosts }, + { "auto_thaw", opt_time, &auto_thaw }, + { "bi_command", opt_stringptr, &bi_command }, + { "bounce_message_file", opt_stringptr, &bounce_message_file }, + { "bounce_message_text", opt_stringptr, &bounce_message_text }, + { "bounce_return_body", opt_bool, &bounce_return_body }, + { "bounce_return_message", opt_bool, &bounce_return_message }, + { "bounce_return_size_limit", opt_mkint, &bounce_return_size_limit }, + { "bounce_sender_authentication",opt_stringptr,&bounce_sender_authentication }, + { "callout_domain_negative_expire", opt_time, &callout_cache_domain_negative_expire }, + { "callout_domain_positive_expire", opt_time, &callout_cache_domain_positive_expire }, + { "callout_negative_expire", opt_time, &callout_cache_negative_expire }, + { "callout_positive_expire", opt_time, &callout_cache_positive_expire }, + { "callout_random_local_part",opt_stringptr, &callout_random_local_part }, + { "check_log_inodes", opt_int, &check_log_inodes }, + { "check_log_space", opt_Kint, &check_log_space }, + { "check_spool_inodes", opt_int, &check_spool_inodes }, + { "check_spool_space", opt_Kint, &check_spool_space }, + { "daemon_smtp_port", opt_stringptr|opt_hidden, &daemon_smtp_port }, + { "daemon_smtp_ports", opt_stringptr, &daemon_smtp_port }, + { "delay_warning", opt_timelist, &delay_warning }, + { "delay_warning_condition", opt_stringptr, &delay_warning_condition }, + { "deliver_drop_privilege", opt_bool, &deliver_drop_privilege }, + { "deliver_queue_load_max", opt_fixed, &deliver_queue_load_max }, + { "delivery_date_remove", opt_bool, &delivery_date_remove }, + { "dns_again_means_nonexist", opt_stringptr, &dns_again_means_nonexist }, + { "dns_check_names_pattern", opt_stringptr, &check_dns_names_pattern }, + { "dns_ipv4_lookup", opt_stringptr, &dns_ipv4_lookup }, + { "dns_retrans", opt_time, &dns_retrans }, + { "dns_retry", opt_int, &dns_retry }, + /* This option is now a no-op, retained for compability */ + { "drop_cr", opt_bool, &drop_cr }, +/*********************************************************/ + { "envelope_to_remove", opt_bool, &envelope_to_remove }, + { "errors_copy", opt_stringptr, &errors_copy }, + { "errors_reply_to", opt_stringptr, &errors_reply_to }, + { "exim_group", opt_gid, &exim_gid }, + { "exim_path", opt_stringptr, &exim_path }, + { "exim_user", opt_uid, &exim_uid }, + { "extra_local_interfaces", opt_stringptr, &extra_local_interfaces }, + { "extract_addresses_remove_arguments", opt_bool, &extract_addresses_remove_arguments }, + { "finduser_retries", opt_int, &finduser_retries }, + { "freeze_tell", opt_stringptr, &freeze_tell }, + { "gecos_name", opt_stringptr, &gecos_name }, + { "gecos_pattern", opt_stringptr, &gecos_pattern }, + { "header_line_maxsize", opt_int, &header_line_maxsize }, + { "header_maxsize", opt_int, &header_maxsize }, + { "headers_charset", opt_stringptr, &headers_charset }, + { "helo_accept_junk_hosts", opt_stringptr, &helo_accept_junk_hosts }, + { "helo_allow_chars", opt_stringptr, &helo_allow_chars }, + { "helo_lookup_domains", opt_stringptr, &helo_lookup_domains }, + { "helo_try_verify_hosts", opt_stringptr, &helo_try_verify_hosts }, + { "helo_verify_hosts", opt_stringptr, &helo_verify_hosts }, + { "hold_domains", opt_stringptr, &hold_domains }, + { "host_lookup", opt_stringptr, &host_lookup }, + { "host_lookup_order", opt_stringptr, &host_lookup_order }, + { "host_reject_connection", opt_stringptr, &host_reject_connection }, + { "hosts_connection_nolog", opt_stringptr, &hosts_connection_nolog }, + { "hosts_treat_as_local", opt_stringptr, &hosts_treat_as_local }, +#ifdef LOOKUP_IBASE + { "ibase_servers", opt_stringptr, &ibase_servers }, +#endif + { "ignore_bounce_errors_after", opt_time, &ignore_bounce_errors_after }, + { "ignore_fromline_hosts", opt_stringptr, &ignore_fromline_hosts }, + { "ignore_fromline_local", opt_bool, &ignore_fromline_local }, + { "keep_malformed", opt_time, &keep_malformed }, +#ifdef LOOKUP_LDAP + { "ldap_default_servers", opt_stringptr, &eldap_default_servers }, + { "ldap_version", opt_int, &eldap_version }, +#endif + { "local_from_check", opt_bool, &local_from_check }, + { "local_from_prefix", opt_stringptr, &local_from_prefix }, + { "local_from_suffix", opt_stringptr, &local_from_suffix }, + { "local_interfaces", opt_stringptr, &local_interfaces }, + { "local_scan_timeout", opt_time, &local_scan_timeout }, + { "local_sender_retain", opt_bool, &local_sender_retain }, + { "localhost_number", opt_stringptr, &host_number_string }, + { "log_file_path", opt_stringptr, &log_file_path }, + { "log_selector", opt_stringptr, &log_selector_string }, + { "log_timezone", opt_bool, &log_timezone }, + { "lookup_open_max", opt_int, &lookup_open_max }, + { "max_username_length", opt_int, &max_username_length }, + { "message_body_visible", opt_mkint, &message_body_visible }, + { "message_id_header_domain", opt_stringptr, &message_id_domain }, + { "message_id_header_text", opt_stringptr, &message_id_text }, + { "message_logs", opt_bool, &message_logs }, + { "message_size_limit", opt_stringptr, &message_size_limit }, +#ifdef SUPPORT_MOVE_FROZEN_MESSAGES + { "move_frozen_messages", opt_bool, &move_frozen_messages }, +#endif + { "mua_wrapper", opt_bool, &mua_wrapper }, +#ifdef LOOKUP_MYSQL + { "mysql_servers", opt_stringptr, &mysql_servers }, +#endif + { "never_users", opt_uidlist, &never_users }, +#ifdef LOOKUP_ORACLE + { "oracle_servers", opt_stringptr, &oracle_servers }, +#endif + { "percent_hack_domains", opt_stringptr, &percent_hack_domains }, +#ifdef EXIM_PERL + { "perl_at_start", opt_bool, &opt_perl_at_start }, + { "perl_startup", opt_stringptr, &opt_perl_startup }, +#endif +#ifdef LOOKUP_PGSQL + { "pgsql_servers", opt_stringptr, &pgsql_servers }, +#endif + { "pid_file_path", opt_stringptr, &pid_file_path }, + { "pipelining_advertise_hosts", opt_stringptr, &pipelining_advertise_hosts }, + { "preserve_message_logs", opt_bool, &preserve_message_logs }, + { "primary_hostname", opt_stringptr, &primary_hostname }, + { "print_topbitchars", opt_bool, &print_topbitchars }, + { "process_log_path", opt_stringptr, &process_log_path }, + { "prod_requires_admin", opt_bool, &prod_requires_admin }, + { "qualify_domain", opt_stringptr, &qualify_domain_sender }, + { "qualify_recipient", opt_stringptr, &qualify_domain_recipient }, + { "queue_domains", opt_stringptr, &queue_domains }, + { "queue_list_requires_admin",opt_bool, &queue_list_requires_admin }, + { "queue_only", opt_bool, &queue_only }, + { "queue_only_file", opt_stringptr, &queue_only_file }, + { "queue_only_load", opt_fixed, &queue_only_load }, + { "queue_only_override", opt_bool, &queue_only_override }, + { "queue_run_in_order", opt_bool, &queue_run_in_order }, + { "queue_run_max", opt_int, &queue_run_max }, + { "queue_smtp_domains", opt_stringptr, &queue_smtp_domains }, + { "receive_timeout", opt_time, &receive_timeout }, + { "received_header_text", opt_stringptr, &received_header_text }, + { "received_headers_max", opt_int, &received_headers_max }, + { "recipient_unqualified_hosts", opt_stringptr, &recipient_unqualified_hosts }, + { "recipients_max", opt_int, &recipients_max }, + { "recipients_max_reject", opt_bool, &recipients_max_reject }, + { "remote_max_parallel", opt_int, &remote_max_parallel }, + { "remote_sort_domains", opt_stringptr, &remote_sort_domains }, + { "retry_data_expire", opt_time, &retry_data_expire }, + { "retry_interval_max", opt_time, &retry_interval_max }, + { "return_path_remove", opt_bool, &return_path_remove }, + { "return_size_limit", opt_mkint|opt_hidden, &bounce_return_size_limit }, + { "rfc1413_hosts", opt_stringptr, &rfc1413_hosts }, + { "rfc1413_query_timeout", opt_time, &rfc1413_query_timeout }, + { "sender_unqualified_hosts", opt_stringptr, &sender_unqualified_hosts }, + { "smtp_accept_keepalive", opt_bool, &smtp_accept_keepalive }, + { "smtp_accept_max", opt_int, &smtp_accept_max }, + { "smtp_accept_max_nonmail", opt_int, &smtp_accept_max_nonmail }, + { "smtp_accept_max_nonmail_hosts", opt_stringptr, &smtp_accept_max_nonmail_hosts }, + { "smtp_accept_max_per_connection", opt_int, &smtp_accept_max_per_connection }, + { "smtp_accept_max_per_host", opt_stringptr, &smtp_accept_max_per_host }, + { "smtp_accept_queue", opt_int, &smtp_accept_queue }, + { "smtp_accept_queue_per_connection", opt_int, &smtp_accept_queue_per_connection }, + { "smtp_accept_reserve", opt_int, &smtp_accept_reserve }, + { "smtp_active_hostname", opt_stringptr, &raw_active_hostname }, + { "smtp_banner", opt_stringptr, &smtp_banner }, + { "smtp_check_spool_space", opt_bool, &smtp_check_spool_space }, + { "smtp_connect_backlog", opt_int, &smtp_connect_backlog }, + { "smtp_enforce_sync", opt_bool, &smtp_enforce_sync }, + { "smtp_etrn_command", opt_stringptr, &smtp_etrn_command }, + { "smtp_etrn_serialize", opt_bool, &smtp_etrn_serialize }, + { "smtp_load_reserve", opt_fixed, &smtp_load_reserve }, + { "smtp_max_synprot_errors", opt_int, &smtp_max_synprot_errors }, + { "smtp_max_unknown_commands",opt_int, &smtp_max_unknown_commands }, + { "smtp_ratelimit_hosts", opt_stringptr, &smtp_ratelimit_hosts }, + { "smtp_ratelimit_mail", opt_stringptr, &smtp_ratelimit_mail }, + { "smtp_ratelimit_rcpt", opt_stringptr, &smtp_ratelimit_rcpt }, + { "smtp_receive_timeout", opt_time, &smtp_receive_timeout }, + { "smtp_reserve_hosts", opt_stringptr, &smtp_reserve_hosts }, + { "smtp_return_error_details",opt_bool, &smtp_return_error_details }, + { "split_spool_directory", opt_bool, &split_spool_directory }, + { "spool_directory", opt_stringptr, &spool_directory }, + { "strip_excess_angle_brackets", opt_bool, &strip_excess_angle_brackets }, + { "strip_trailing_dot", opt_bool, &strip_trailing_dot }, + { "syslog_duplication", opt_bool, &syslog_duplication }, + { "syslog_facility", opt_stringptr, &syslog_facility_str }, + { "syslog_processname", opt_stringptr, &syslog_processname }, + { "syslog_timestamp", opt_bool, &syslog_timestamp }, + { "system_filter", opt_stringptr, &system_filter }, + { "system_filter_directory_transport", opt_stringptr,&system_filter_directory_transport }, + { "system_filter_file_transport",opt_stringptr,&system_filter_file_transport }, + { "system_filter_group", opt_gid, &system_filter_gid }, + { "system_filter_pipe_transport",opt_stringptr,&system_filter_pipe_transport }, + { "system_filter_reply_transport",opt_stringptr,&system_filter_reply_transport }, + { "system_filter_user", opt_uid, &system_filter_uid }, + { "tcp_nodelay", opt_bool, &tcp_nodelay }, + { "timeout_frozen_after", opt_time, &timeout_frozen_after }, + { "timezone", opt_stringptr, &timezone_string }, +#ifdef SUPPORT_TLS + { "tls_advertise_hosts", opt_stringptr, &tls_advertise_hosts }, + { "tls_certificate", opt_stringptr, &tls_certificate }, + { "tls_crl", opt_stringptr, &tls_crl }, + { "tls_dhparam", opt_stringptr, &tls_dhparam }, + { "tls_on_connect_ports", opt_stringptr, &tls_on_connect_ports }, + { "tls_privatekey", opt_stringptr, &tls_privatekey }, + { "tls_remember_esmtp", opt_bool, &tls_remember_esmtp }, + { "tls_require_ciphers", opt_stringptr, &tls_require_ciphers }, + { "tls_try_verify_hosts", opt_stringptr, &tls_try_verify_hosts }, + { "tls_verify_certificates", opt_stringptr, &tls_verify_certificates }, + { "tls_verify_hosts", opt_stringptr, &tls_verify_hosts }, +#endif + { "trusted_groups", opt_gidlist, &trusted_groups }, + { "trusted_users", opt_uidlist, &trusted_users }, + { "unknown_login", opt_stringptr, &unknown_login }, + { "unknown_username", opt_stringptr, &unknown_username }, + { "untrusted_set_sender", opt_stringptr, &untrusted_set_sender }, + { "uucp_from_pattern", opt_stringptr, &uucp_from_pattern }, + { "uucp_from_sender", opt_stringptr, &uucp_from_sender }, + { "warn_message_file", opt_stringptr, &warn_message_file }, + { "write_rejectlog", opt_bool, &write_rejectlog } +}; + +static int optionlist_config_size = + sizeof(optionlist_config)/sizeof(optionlist); + + + +/************************************************* +* Find the name of an option * +*************************************************/ + +/* This function is to aid debugging. Various functions take arguments that are +pointer variables in the options table or in option tables for various drivers. +For debugging output, it is useful to be able to find the name of the option +which is currently being processed. This function finds it, if it exists, by +searching the table(s). + +Arguments: a value that is presumed to be in the table above +Returns: the option name, or an empty string +*/ + +uschar * +readconf_find_option(void *p) +{ +int i; +router_instance *r; +transport_instance *t; + +for (i = 0; i < optionlist_config_size; i++) + if (p == optionlist_config[i].value) return US optionlist_config[i].name; + +for (r = routers; r != NULL; r = r->next) + { + router_info *ri = r->info; + for (i = 0; i < ri->options_count[0]; i++) + { + if ((ri->options[i].type & opt_mask) != opt_stringptr) continue; + if (p == (char *)(r->options_block) + (long int)(ri->options[i].value)) + return US ri->options[i].name; + } + } + +for (t = transports; t != NULL; t = t->next) + { + transport_info *ti = t->info; + for (i = 0; i < ti->options_count[0]; i++) + { + if ((ti->options[i].type & opt_mask) != opt_stringptr) continue; + if (p == (char *)(t->options_block) + (long int)(ti->options[i].value)) + return US ti->options[i].name; + } + } + +return US""; +} + + + + + +/************************************************* +* Read configuration line * +*************************************************/ + +/* A logical line of text is read from the configuration file into the big +buffer, taking account of macros, .includes, and continuations. The size of +big_buffer is increased if necessary. The count of configuration lines is +maintained. Physical input lines starting with # (ignoring leading white space, +and after macro replacement) and empty logical lines are always ignored. +Leading and trailing spaces are removed. + +If we hit a line of the form "begin xxxx", the xxxx is placed in the +next_section vector, and the function returns NULL, indicating the end of a +configuration section. On end-of-file, NULL is returned with next_section +empty. + +Arguments: none + +Returns: a pointer to the first non-blank in the line, + or NULL if eof or end of section is reached +*/ + +static uschar * +get_config_line(void) +{ +int startoffset = 0; /* To first non-blank char in logical line */ +int len = 0; /* Of logical line so far */ +int newlen; +uschar *s, *ss; +macro_item *m; +BOOL macro_found; + +/* Loop for handling continuation lines, skipping comments, and dealing with +.include files. */ + +for (;;) + { + if (Ufgets(big_buffer+len, big_buffer_size-len, config_file) == NULL) + { + if (config_file_stack != NULL) /* EOF inside .include */ + { + fclose(config_file); + config_file = config_file_stack->file; + config_filename = config_file_stack->filename; + config_lineno = config_file_stack->lineno; + config_file_stack = config_file_stack->next; + continue; + } + + /* EOF at top level */ + + if (cstate_stack_ptr >= 0) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "Unexpected end of configuration file: .endif missing"); + + if (len != 0) break; /* EOF after continuation */ + next_section[0] = 0; /* EOF at start of logical line */ + return NULL; + } + + config_lineno++; + newlen = len + Ustrlen(big_buffer + len); + + /* Handle pathologically long physical lines - yes, it did happen - by + extending big_buffer at this point. The code also copes with very long + logical lines. */ + + while (newlen == big_buffer_size - 1 && big_buffer[newlen - 1] != '\n') + { + uschar *newbuffer; + big_buffer_size += BIG_BUFFER_SIZE; + newbuffer = store_malloc(big_buffer_size); + + /* This use of strcpy is OK because we know that the string in the old + buffer is shorter than the new buffer. */ + + Ustrcpy(newbuffer, big_buffer); + store_free(big_buffer); + big_buffer = newbuffer; + if (Ufgets(big_buffer+newlen, big_buffer_size-newlen, config_file) == NULL) + break; + newlen += Ustrlen(big_buffer + newlen); + } + + /* Find the true start of the physical line - leading spaces are always + ignored. */ + + ss = big_buffer + len; + while (isspace(*ss)) ss++; + + /* Process the physical line for macros. If this is the start of the logical + line, skip over initial text at the start of the line if it starts with an + upper case character followed by a sequence of name characters and an equals + sign, because that is the definition of a new macro, and we don't do + replacement therein. */ + + s = ss; + if (len == 0 && isupper(*s)) + { + while (isalnum(*s) || *s == '_') s++; + while (isspace(*s)) s++; + if (*s != '=') s = ss; /* Not a macro definition */ + } + + /* For each defined macro, scan the line (from after XXX= if present), + replacing all occurrences of the macro. */ + + macro_found = FALSE; + for (m = macros; m != NULL; m = m->next) + { + uschar *p, *pp; + uschar *t = s; + + while ((p = Ustrstr(t, m->name)) != NULL) + { + int moveby; + int namelen = Ustrlen(m->name); + int replen = Ustrlen(m->replacement); + + /* Expand the buffer if necessary */ + + while (newlen - namelen + replen + 1 > big_buffer_size) + { + int newsize = big_buffer_size + BIG_BUFFER_SIZE; + uschar *newbuffer = store_malloc(newsize); + memcpy(newbuffer, big_buffer, newlen + 1); + p = newbuffer + (p - big_buffer); + s = newbuffer + (s - big_buffer); + ss = newbuffer + (ss - big_buffer); + t = newbuffer + (t - big_buffer); + big_buffer_size = newsize; + store_free(big_buffer); + big_buffer = newbuffer; + } + + /* Shuffle the remaining characters up or down in the buffer before + copying in the replacement text. Don't rescan the replacement for this + same macro. */ + + pp = p + namelen; + moveby = replen - namelen; + if (moveby != 0) + { + memmove(p + replen, pp, (big_buffer + newlen) - pp + 1); + newlen += moveby; + } + Ustrncpy(p, m->replacement, replen); + t = p + replen; + macro_found = TRUE; + } + } + + /* An empty macro replacement at the start of a line could mean that ss no + longer points to the first non-blank character. */ + + while (isspace(*ss)) ss++; + + /* Check for comment lines - these are physical lines. */ + + if (*ss == '#') continue; + + /* Handle conditionals, which are also applied to physical lines. Conditions + are of the form ".ifdef ANYTEXT" and are treated as true if any macro + expansion occured on the rest of the line. A preliminary test for the leading + '.' saves effort on most lines. */ + + if (*ss == '.') + { + int i; + + /* Search the list of conditional directives */ + + for (i = 0; i < cond_list_size; i++) + { + int n; + cond_item *c = cond_list+i; + if (Ustrncmp(ss+1, c->name, c->namelen) != 0) continue; + + /* The following character must be white space or end of string */ + + n = ss[1 + c->namelen]; + if (n != ' ' && n != 't' && n != '\n' && n != 0) break; + + /* .ifdef and .ifndef push the current state onto the stack, then set + a new one from the table. Stack overflow is an error */ + + if (c->pushpop > 0) + { + if (cstate_stack_ptr >= CSTATE_STACK_SIZE - 1) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + ".%s nested too deeply", c->name); + cstate_stack[++cstate_stack_ptr] = cstate; + cstate = next_cstate[cstate][macro_found? c->action1 : c->action2]; + } + + /* For any of the others, stack underflow is an error. The next state + comes either from the stack (.endif) or from the table. */ + + else + { + if (cstate_stack_ptr < 0) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + ".%s without matching .ifdef", c->name); + cstate = (c->pushpop < 0)? cstate_stack[cstate_stack_ptr--] : + next_cstate[cstate][macro_found? c->action1 : c->action2]; + } + + /* Having dealt with a directive, break the loop */ + + break; + } + + /* If we have handled a conditional directive, continue with the next + physical line. Otherwise, fall through. */ + + if (i < cond_list_size) continue; + } + + /* If the conditional state is not 0 (actively using these lines), ignore + this input line. */ + + if (cstate != 0) continue; /* Conditional skip */ + + /* Handle .include lines - these are also physical lines. */ + + if (Ustrncmp(ss, ".include", 8) == 0 && + (isspace(ss[8]) || + (Ustrncmp(ss+8, "_if_exists", 10) == 0 && isspace(ss[18])))) + { + uschar *t; + int include_if_exists = isspace(ss[8])? 0 : 10; + config_file_item *save; + struct stat statbuf; + + ss += 9 + include_if_exists; + while (isspace(*ss)) ss++; + t = ss + Ustrlen(ss); + while (t > ss && isspace(t[-1])) t--; + if (*ss == '\"' && t[-1] == '\"') + { + ss++; + t--; + } + *t = 0; + + if (include_if_exists != 0 && (Ustat(ss, &statbuf) != 0)) continue; + + save = store_get(sizeof(config_file_item)); + save->next = config_file_stack; + config_file_stack = save; + save->file = config_file; + save->filename = config_filename; + save->lineno = config_lineno; + + config_file = Ufopen(ss, "rb"); + if (config_file == NULL) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "failed to open included " + "configuration file %s", ss); + config_filename = string_copy(ss); + config_lineno = 0; + continue; + } + + /* If this is the start of the logical line, remember where the non-blank + data starts. Otherwise shuffle down continuation lines to remove leading + white space. */ + + if (len == 0) + startoffset = ss - big_buffer; + else + { + s = big_buffer + len; + if (ss > s) + { + memmove(s, ss, (newlen - len) - (ss - s) + 1); + newlen -= ss - s; + } + } + + /* Accept the new addition to the line. Remove trailing white space. */ + + len = newlen; + while (len > 0 && isspace(big_buffer[len-1])) len--; + big_buffer[len] = 0; + + /* We are done if the line does not end in backslash and contains some data. + Empty logical lines are ignored. For continuations, remove the backslash and + go round the loop to read the continuation line. */ + + if (len > 0) + { + if (big_buffer[len-1] != '\\') break; /* End of logical line */ + big_buffer[--len] = 0; /* Remove backslash */ + } + } /* Loop for reading multiple physical lines */ + +/* We now have a logical line. Test for the end of a configuration section (or, +more accurately, for the start of the next section). Place the name of the next +section in next_section, and return NULL. If the name given is longer than +next_section, truncate it. It will be unrecognized later, because all the known +section names do fit. Leave space for pluralizing. */ + +s = big_buffer + startoffset; /* First non-space character */ +if (strncmpic(s, US"begin ", 6) == 0) + { + s += 6; + while (isspace(*s)) s++; + if (big_buffer + len - s > sizeof(next_section) - 2) + s[sizeof(next_section) - 2] = 0; + Ustrcpy(next_section, s); + return NULL; + } + +/* Return the first non-blank character. */ + +return s; +} + + + +/************************************************* +* Read a name * +*************************************************/ + +/* The yield is the pointer to the next uschar. Names longer than the +output space are silently truncated. This function is also used from acl.c when +parsing ACLs. + +Arguments: + name where to put the name + len length of name + s input pointer + +Returns: new input pointer +*/ + +uschar * +readconf_readname(uschar *name, int len, uschar *s) +{ +int p = 0; +while (isspace(*s)) s++; +if (isalpha(*s)) + { + while (isalnum(*s) || *s == '_') + { + if (p < len-1) name[p++] = *s; + s++; + } + } +name[p] = 0; +while (isspace(*s)) s++; +return s; +} + + + + +/************************************************* +* Read a time value * +*************************************************/ + +/* This function is also called from outside, to read argument +time values. The format of a time value is: + + [w][d][h][m][s] + +as long as at least one is present. If a format error is encountered, +return a negative value. The value must be terminated by the given +terminator. + +Arguments: + s input pointer + terminator required terminating character + return_msec if TRUE, allow fractional seconds and return milliseconds + +Returns: the time value, or -1 on syntax error + value is seconds if return_msec is FALSE + value is milliseconds if return_msec is TRUE +*/ + +int +readconf_readtime(uschar *s, int terminator, BOOL return_msec) +{ +int yield = 0; +for (;;) + { + int value, count; + double fraction; + + if (!isdigit(*s)) return -1; + (void)sscanf(CS s, "%d%n", &value, &count); + s += count; + + switch (*s) + { + case 'w': value *= 7; + case 'd': value *= 24; + case 'h': value *= 60; + case 'm': value *= 60; + case 's': s++; + break; + + case '.': + if (!return_msec) return -1; + (void)sscanf(CS s, "%lf%n", &fraction, &count); + s += count; + if (*s++ != 's') return -1; + yield += (int)(fraction * 1000.0); + break; + + default: return -1; + } + + if (return_msec) value *= 1000; + yield += value; + if (*s == terminator) return yield; + } +/* Control never reaches here. */ +} + + + +/************************************************* +* Read a fixed point value * +*************************************************/ + +/* The value is returned *1000 + +Arguments: + s input pointer + terminator required terminator + +Returns: the value, or -1 on error +*/ + +static int +readconf_readfixed(uschar *s, int terminator) +{ +int yield = 0; +int value, count; +if (!isdigit(*s)) return -1; +(void)sscanf(CS s, "%d%n", &value, &count); +s += count; +yield = value * 1000; +if (*s == '.') + { + int m = 100; + while (isdigit((*(++s)))) + { + yield += (*s - '0') * m; + m /= 10; + } + } + +return (*s == terminator)? yield : (-1); +} + + + +/************************************************* +* Find option in list * +*************************************************/ + +/* The lists are always in order, so binary chop can be used. + +Arguments: + name the option name to search for + ol the first entry in the option list + last one more than the offset of the last entry in the option list + +Returns: pointer to an option entry, or NULL if not found +*/ + +static optionlist * +find_option(uschar *name, optionlist *ol, int last) +{ +int first = 0; +while (last > first) + { + int middle = (first + last)/2; + int c = Ustrcmp(name, ol[middle].name); + if (c == 0) return ol + middle; + else if (c > 0) first = middle + 1; + else last = middle; + } +return NULL; +} + + + +/************************************************* +* Find a set flag in option list * +*************************************************/ + +/* Because some versions of Unix make no restrictions on the values of uids and +gids (even negative ones), we cannot represent "unset" by a special value. +There is therefore a separate boolean variable for each one indicating whether +a value is set or not. This function returns a pointer to the boolean, given +the original option name. It is a major disaster if the flag cannot be found. + +Arguments: + name the name of the uid or gid option + oltop points to the start of the relevant option list + last one more than the offset of the last item in the option list + data_block NULL when reading main options => data values in the option + list are absolute addresses; otherwise they are byte offsets + in data_block (used for driver options) + +Returns: a pointer to the boolean flag. +*/ + +static BOOL * +get_set_flag(uschar *name, optionlist *oltop, int last, void *data_block) +{ +optionlist *ol; +uschar name2[64]; +sprintf(CS name2, "*set_%.50s", name); +ol = find_option(name2, oltop, last); +if (ol == NULL) log_write(0, LOG_MAIN|LOG_PANIC_DIE, + "Exim internal error: missing set flag for %s", name); +return (data_block == NULL)? (BOOL *)(ol->value) : + (BOOL *)((uschar *)data_block + (long int)(ol->value)); +} + + + + +/************************************************* +* Output extra characters message and die * +*************************************************/ + +/* Called when an option line has junk on the end. Sometimes this is because +the sysadmin thinks comments are permitted. + +Arguments: + s points to the extra characters + t1..t3 strings to insert in the log message + +Returns: doesn't return; dies +*/ + +static void +extra_chars_error(uschar *s, uschar *t1, uschar *t2, uschar *t3) +{ +uschar *comment = US""; +if (*s == '#') comment = US" (# is comment only at line start)"; +log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "extra characters follow %s%s%s%s", t1, t2, t3, comment); +} + + + +/************************************************* +* Read rewrite information * +*************************************************/ + +/* Each line of rewrite information contains: + +. A complete address in the form user@domain, possibly with + leading * for each part; or alternatively, a regex. + +. A replacement string (which will be expanded). + +. An optional sequence of one-letter flags, indicating which + headers etc. to apply this rule to. + +All this is decoded and placed into a control block. The OR of the flags is +maintained in a common word. + +Arguments: + p points to the string that makes up the rule + existflags points to the overall flag word + isglobal TRUE if reading global rewrite rules + +Returns: the control block for the parsed rule. +*/ + +static rewrite_rule * +readconf_one_rewrite(uschar *p, int *existflags, BOOL isglobal) +{ +rewrite_rule *next = store_get(sizeof(rewrite_rule)); + +next->next = NULL; +next->key = string_dequote(&p); + +while (isspace(*p)) p++; +if (*p == 0) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "missing rewrite replacement string"); + +next->flags = 0; +next->replacement = string_dequote(&p); + +while (*p != 0) switch (*p++) + { + case ' ': case '\t': break; + + case 'q': next->flags |= rewrite_quit; break; + case 'w': next->flags |= rewrite_whole; break; + + case 'h': next->flags |= rewrite_all_headers; break; + case 's': next->flags |= rewrite_sender; break; + case 'f': next->flags |= rewrite_from; break; + case 't': next->flags |= rewrite_to; break; + case 'c': next->flags |= rewrite_cc; break; + case 'b': next->flags |= rewrite_bcc; break; + case 'r': next->flags |= rewrite_replyto; break; + + case 'E': next->flags |= rewrite_all_envelope; break; + case 'F': next->flags |= rewrite_envfrom; break; + case 'T': next->flags |= rewrite_envto; break; + + case 'Q': next->flags |= rewrite_qualify; break; + case 'R': next->flags |= rewrite_repeat; break; + + case 'S': + next->flags |= rewrite_smtp; + if (next->key[0] != '^' && Ustrncmp(next->key, "\\N^", 3) != 0) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "rewrite rule has the S flag but is not a regular expression"); + break; + + default: + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "unknown rewrite flag character '%c' " + "(could be missing quotes round replacement item)", p[-1]); + break; + } + +/* If no action flags are set, set all the "normal" rewrites. */ + +if ((next->flags & (rewrite_all | rewrite_smtp)) == 0) + next->flags |= isglobal? rewrite_all : rewrite_all_headers; + +/* Remember which exist, for optimization, and return the rule */ + +*existflags |= next->flags; +return next; +} + + + + +/************************************************* +* Read global rewrite information * +*************************************************/ + +/* Each line is a single rewrite rule; it is parsed into a control block +by readconf_one_rewrite(), and its flags are ORed into the global flag +word rewrite_existflags. */ + +void +readconf_rewrites(void) +{ +rewrite_rule **chain = &global_rewrite_rules; +uschar *p; + +while ((p = get_config_line()) != NULL) + { + rewrite_rule *next = readconf_one_rewrite(p, &rewrite_existflags, TRUE); + *chain = next; + chain = &(next->next); + } +} + + + +/************************************************* +* Read a string * +*************************************************/ + +/* Strings are read into the normal store pool. As long we aren't too +near the end of the current block, the string will just use what is necessary +on the top of the stacking pool, because string_cat() uses the extension +mechanism. + +Argument: + s the rest of the input line + name the option name (for errors) + +Returns: pointer to the string +*/ + +static uschar * +read_string(uschar *s, uschar *name) +{ +uschar *yield; +uschar *ss; + +if (*s != '\"') return string_copy(s); + +ss = s; +yield = string_dequote(&s); + +if (s == ss+1 || s[-1] != '\"') + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "missing quote at end of string value for %s", name); + +if (*s != 0) extra_chars_error(s, US"string value for ", name, US""); + +return yield; +} + + +/************************************************* +* Handle option line * +*************************************************/ + +/* This function is called from several places to process a line containing the +setting of an option. The first argument is the line to be decoded; it has been +checked not to be empty and not to start with '#'. Trailing newlines and white +space have been removed. The second argument is a pointer to the list of +variable names that are to be recognized, together with their types and +locations, and the third argument gives the number of entries in the list. + +The fourth argument is a pointer to a data block. If it is NULL, then the data +values in the options list are absolute addresses. Otherwise, they are byte +offsets in the data block. + +String option data may continue onto several lines; this function reads further +data from config_file if necessary. + +The yield of this function is normally zero. If a string continues onto +multiple lines, then the data value is permitted to be followed by a comma +or a semicolon (for use in drivers) and the yield is that character. + +Arguments: + buffer contains the configuration line to be handled + oltop points to the start of the relevant option list + last one more than the offset of the last item in the option list + data_block NULL when reading main options => data values in the option + list are absolute addresses; otherwise they are byte offsets + in data_block when they have opt_public set; otherwise + they are byte offsets in data_block->options_block. + unknown_txt format string to use in panic message for unknown option; + must contain %s for option name + if given as NULL, don't panic on unknown option + +Returns: TRUE if an option was read successfully, + FALSE false for an unknown option if unknown_txt == NULL, + otherwise panic and die on an unknown option +*/ + +static BOOL +readconf_handle_option(uschar *buffer, optionlist *oltop, int last, + void *data_block, uschar *unknown_txt) +{ +int ptr = 0; +int offset = 0; +int n, count, type, value; +int issecure = 0; +uid_t uid; +gid_t gid; +BOOL boolvalue = TRUE; +BOOL freesptr = TRUE; +optionlist *ol, *ol2; +struct passwd *pw; +void *reset_point; +int intbase = 0; +uschar *inttype = US""; +uschar *sptr; +uschar *s = buffer; +uschar name[64]; +uschar name2[64]; + +/* There may be leading spaces; thereafter, we expect an option name starting +with a letter. */ + +while (isspace(*s)) s++; +if (!isalpha(*s)) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "option setting expected: %s", s); + +/* Read the name of the option, and skip any subsequent white space. If +it turns out that what we read was "hide", set the flag indicating that +this is a secure option, and loop to read the next word. */ + +for (n = 0; n < 2; n++) + { + while (isalnum(*s) || *s == '_') + { + if (ptr < sizeof(name)-1) name[ptr++] = *s; + s++; + } + name[ptr] = 0; + while (isspace(*s)) s++; + if (Ustrcmp(name, "hide") != 0) break; + issecure = opt_secure; + ptr = 0; + } + +/* Deal with "no_" or "not_" here for booleans */ + +if (Ustrncmp(name, "no_", 3) == 0) + { + boolvalue = FALSE; + offset = 3; + } + +if (Ustrncmp(name, "not_", 4) == 0) + { + boolvalue = FALSE; + offset = 4; + } + +/* Search the list for the given name. A non-existent name, or an option that +is set twice, is a disaster. */ + +ol = find_option(name + offset, oltop, last); + +if (ol == NULL) + { + if (unknown_txt == NULL) return FALSE; + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, CS unknown_txt, name); + } + +if ((ol->type & opt_set) != 0) + { + uschar *mname = name; + if (Ustrncmp(mname, "no_", 3) == 0) mname += 3; + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "\"%s\" option set for the second time", mname); + } + +ol->type |= opt_set | issecure; +type = ol->type & opt_mask; + +/* Types with data values must be followed by '='; the "no[t]_" prefix +applies only to boolean values. */ + +if (type < opt_bool || type > opt_bool_last) + { + if (offset != 0) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "negation prefix applied to a non-boolean option"); + if (*s == 0) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "unexpected end of line (data missing) after %s", name); + if (*s != '=') + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "missing \"=\" after %s", name); + } + +/* If a boolean wasn't preceded by "no[t]_" it can be followed by = and +true/false/yes/no, or, in the case of opt_expanded_bool, a general string that +ultimately expands to one of those values. */ + +else if (*s != 0 && (offset != 0 || *s != '=')) + extra_chars_error(s, US"boolean option ", name, US""); + +/* Skip white space after = */ + +if (*s == '=') while (isspace((*(++s)))); + +/* If there is a data block and the opt_public flag is not set, change +the data block pointer to the private options block. */ + +if (data_block != NULL && (ol->type & opt_public) == 0) + data_block = (void *)(((driver_instance *)data_block)->options_block); + +/* Now get the data according to the type. */ + +switch (type) + { + /* If a string value is not enclosed in quotes, it consists of + the rest of the current line, verbatim. Otherwise, string escapes + are processed. + + A transport is specified as a string, which is then looked up in the + list of transports. A search type is specified as one of a number of + known strings. + + A set or rewrite rules for a driver is specified as a string, which is + then parsed into a suitable chain of control blocks. + + Uids and gids are specified as strings which are then looked up in the + passwd file. Lists of uids and gids are similarly specified as colon- + separated strings. */ + + case opt_stringptr: + case opt_uid: + case opt_gid: + case opt_expand_uid: + case opt_expand_gid: + case opt_uidlist: + case opt_gidlist: + case opt_rewrite: + + reset_point = sptr = read_string(s, name); + + /* Having read a string, we now have several different ways of using it, + depending on the data type, so do another switch. If keeping the actual + string is not required (because it is interpreted), freesptr is set TRUE, + and at the end we reset the pool. */ + + switch (type) + { + /* If this was a string, set the variable to point to the new string, + and set the flag so its store isn't reclaimed. If it was a list of rewrite + rules, we still keep the string (for printing), and parse the rules into a + control block and flags word. */ + + case opt_stringptr: + case opt_rewrite: + if (data_block == NULL) + *((uschar **)(ol->value)) = sptr; + else + *((uschar **)((uschar *)data_block + (long int)(ol->value))) = sptr; + freesptr = FALSE; + if (type == opt_rewrite) + { + int sep = 0; + int *flagptr; + uschar *p = sptr; + rewrite_rule **chain; + optionlist *ol3; + + sprintf(CS name2, "*%.50s_rules", name); + ol2 = find_option(name2, oltop, last); + sprintf(CS name2, "*%.50s_flags", name); + ol3 = find_option(name2, oltop, last); + + if (ol2 == NULL || ol3 == NULL) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "rewrite rules not available for driver"); + + if (data_block == NULL) + { + chain = (rewrite_rule **)(ol2->value); + flagptr = (int *)(ol3->value); + } + else + { + chain = (rewrite_rule **)((uschar *)data_block + (long int)(ol2->value)); + flagptr = (int *)((uschar *)data_block + (long int)(ol3->value)); + } + + while ((p = string_nextinlist(&sptr, &sep, big_buffer, BIG_BUFFER_SIZE)) + != NULL) + { + rewrite_rule *next = readconf_one_rewrite(p, flagptr, FALSE); + *chain = next; + chain = &(next->next); + } + + if ((*flagptr & (rewrite_all_envelope | rewrite_smtp)) != 0) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "rewrite rule specifies a " + "non-header rewrite - not allowed at transport time -"); + } + break; + + /* If it was an expanded uid, see if there is any expansion to be + done by checking for the presence of a $ character. If there is, save it + in the corresponding *expand_user option field. Otherwise, fall through + to treat it as a fixed uid. Ensure mutual exclusivity of the two kinds + of data. */ + + case opt_expand_uid: + sprintf(CS name2, "*expand_%.50s", name); + ol2 = find_option(name2, oltop, last); + if (ol2 != NULL) + { + uschar *ss = (Ustrchr(sptr, '$') != NULL)? sptr : NULL; + + if (data_block == NULL) + *((uschar **)(ol2->value)) = ss; + else + *((uschar **)((uschar *)data_block + (long int)(ol2->value))) = ss; + + if (ss != NULL) + { + *(get_set_flag(name, oltop, last, data_block)) = FALSE; + freesptr = FALSE; + break; + } + } + + /* Look up a fixed uid, and also make use of the corresponding gid + if a passwd entry is returned and the gid has not been set. */ + + case opt_uid: + if (!route_finduser(sptr, &pw, &uid)) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "user %s was not found", sptr); + if (data_block == NULL) + *((uid_t *)(ol->value)) = uid; + else + *((uid_t *)((uschar *)data_block + (long int)(ol->value))) = uid; + + /* Set the flag indicating a fixed value is set */ + + *(get_set_flag(name, oltop, last, data_block)) = TRUE; + + /* Handle matching gid if we have a passwd entry: done by finding the + same name with terminating "user" changed to "group"; if not found, + ignore. Also ignore if the value is already set. */ + + if (pw == NULL) break; + Ustrcpy(name+Ustrlen(name)-4, "group"); + ol2 = find_option(name, oltop, last); + if (ol2 != NULL && ((ol2->type & opt_mask) == opt_gid || + (ol2->type & opt_mask) == opt_expand_gid)) + { + BOOL *set_flag = get_set_flag(name, oltop, last, data_block); + if (! *set_flag) + { + if (data_block == NULL) + *((gid_t *)(ol2->value)) = pw->pw_gid; + else + *((gid_t *)((uschar *)data_block + (long int)(ol2->value))) = pw->pw_gid; + *set_flag = TRUE; + } + } + break; + + /* If it was an expanded gid, see if there is any expansion to be + done by checking for the presence of a $ character. If there is, save it + in the corresponding *expand_user option field. Otherwise, fall through + to treat it as a fixed gid. Ensure mutual exclusivity of the two kinds + of data. */ + + case opt_expand_gid: + sprintf(CS name2, "*expand_%.50s", name); + ol2 = find_option(name2, oltop, last); + if (ol2 != NULL) + { + uschar *ss = (Ustrchr(sptr, '$') != NULL)? sptr : NULL; + + if (data_block == NULL) + *((uschar **)(ol2->value)) = ss; + else + *((uschar **)((uschar *)data_block + (long int)(ol2->value))) = ss; + + if (ss != NULL) + { + *(get_set_flag(name, oltop, last, data_block)) = FALSE; + freesptr = FALSE; + break; + } + } + + /* Handle freestanding gid */ + + case opt_gid: + if (!route_findgroup(sptr, &gid)) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "group %s was not found", sptr); + if (data_block == NULL) + *((gid_t *)(ol->value)) = gid; + else + *((gid_t *)((uschar *)data_block + (long int)(ol->value))) = gid; + *(get_set_flag(name, oltop, last, data_block)) = TRUE; + break; + + /* If it was a uid list, look up each individual entry, and build + a vector of uids, with a count in the first element. Put the vector + in malloc store so we can free the string. (We are reading into + permanent store already.) */ + + case opt_uidlist: + { + int count = 1; + uid_t *list; + int ptr = 0; + uschar *p = sptr; + + if (*p != 0) count++; + while (*p != 0) if (*p++ == ':') count++; + list = store_malloc(count*sizeof(uid_t)); + list[ptr++] = (uid_t)(count - 1); + + if (data_block == NULL) + *((uid_t **)(ol->value)) = list; + else + *((uid_t **)((uschar *)data_block + (long int)(ol->value))) = list; + + p = sptr; + while (count-- > 1) + { + int sep = 0; + (void)string_nextinlist(&p, &sep, big_buffer, BIG_BUFFER_SIZE); + if (!route_finduser(big_buffer, NULL, &uid)) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "user %s was not found", + big_buffer); + list[ptr++] = uid; + } + } + break; + + /* If it was a gid list, look up each individual entry, and build + a vector of gids, with a count in the first element. Put the vector + in malloc store so we can free the string. (We are reading into permanent + store already.) */ + + case opt_gidlist: + { + int count = 1; + gid_t *list; + int ptr = 0; + uschar *p = sptr; + + if (*p != 0) count++; + while (*p != 0) if (*p++ == ':') count++; + list = store_malloc(count*sizeof(gid_t)); + list[ptr++] = (gid_t)(count - 1); + + if (data_block == NULL) + *((gid_t **)(ol->value)) = list; + else + *((gid_t **)((uschar *)data_block + (long int)(ol->value))) = list; + + p = sptr; + while (count-- > 1) + { + int sep = 0; + (void)string_nextinlist(&p, &sep, big_buffer, BIG_BUFFER_SIZE); + if (!route_findgroup(big_buffer, &gid)) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "group %s was not found", + big_buffer); + list[ptr++] = gid; + } + } + break; + } + + /* Release store if the value of the string doesn't need to be kept. */ + + if (freesptr) store_reset(reset_point); + break; + + /* Expanded boolean: if no characters follow, or if there are no dollar + characters, this is a fixed-valued boolean, and we fall through. Otherwise, + save the string for later expansion in the alternate place. */ + + case opt_expand_bool: + if (*s != 0 && Ustrchr(s, '$') != 0) + { + sprintf(CS name2, "*expand_%.50s", name); + ol2 = find_option(name2, oltop, last); + if (ol2 != NULL) + { + reset_point = sptr = read_string(s, name); + if (data_block == NULL) + *((uschar **)(ol2->value)) = sptr; + else + *((uschar **)((uschar *)data_block + (long int)(ol2->value))) = sptr; + freesptr = FALSE; + break; + } + } + /* Fall through */ + + /* Boolean: if no characters follow, the value is boolvalue. Otherwise + look for yes/not/true/false. Some booleans are stored in a single bit in + a single int. There's a special fudge for verify settings; without a suffix + they set both xx_sender and xx_recipient. The table points to the sender + value; search subsequently for the recipient. There's another special case: + opt_bool_set also notes when a boolean has been set. */ + + case opt_bool: + case opt_bit: + case opt_bool_verify: + case opt_bool_set: + if (*s != 0) + { + s = readconf_readname(name2, 64, s); + if (strcmpic(name2, US"true") == 0 || strcmpic(name2, US"yes") == 0) + boolvalue = TRUE; + else if (strcmpic(name2, US"false") == 0 || strcmpic(name2, US"no") == 0) + boolvalue = FALSE; + else log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "\"%s\" is not a valid value for the \"%s\" option", name2, name); + if (*s != 0) extra_chars_error(s, string_sprintf("\"%s\" ", name2), + US"for boolean option ", name); + } + + /* Handle single-bit type. */ + + if (type == opt_bit) + { + int bit = 1 << ((ol->type >> 16) & 31); + int *ptr = (data_block == NULL)? + (int *)(ol->value) : + (int *)((uschar *)data_block + (long int)ol->value); + if (boolvalue) *ptr |= bit; else *ptr &= ~bit; + break; + } + + /* Handle full BOOL types */ + + if (data_block == NULL) + *((BOOL *)(ol->value)) = boolvalue; + else + *((BOOL *)((uschar *)data_block + (long int)(ol->value))) = boolvalue; + + /* Verify fudge */ + + if (type == opt_bool_verify) + { + sprintf(CS name2, "%.50s_recipient", name + offset); + ol2 = find_option(name2, oltop, last); + if (ol2 != NULL) + { + if (data_block == NULL) + *((BOOL *)(ol2->value)) = boolvalue; + else + *((BOOL *)((uschar *)data_block + (long int)(ol2->value))) = boolvalue; + } + } + + /* Note that opt_bool_set type is set, if there is somewhere to do so */ + + else if (type == opt_bool_set) + { + sprintf(CS name2, "*set_%.50s", name + offset); + ol2 = find_option(name2, oltop, last); + if (ol2 != NULL) + { + if (data_block == NULL) + *((BOOL *)(ol2->value)) = TRUE; + else + *((BOOL *)((uschar *)data_block + (long int)(ol2->value))) = TRUE; + } + } + break; + + /* Octal integer */ + + case opt_octint: + intbase = 8; + inttype = US"octal "; + + /* Integer: a simple(ish) case; allow octal and hex formats, and + suffixes K and M. The different types affect output, not input. */ + + case opt_mkint: + case opt_int: + { + uschar *endptr; + errno = 0; + value = strtol(CS s, CSS &endptr, intbase); + + if (endptr == s) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "%sinteger expected for %s", + inttype, name); + + if (errno != ERANGE) + { + if (tolower(*endptr) == 'k') + { + if (value > INT_MAX/1024 || value < INT_MIN/1024) errno = ERANGE; + else value *= 1024; + endptr++; + } + else if (tolower(*endptr) == 'm') + { + if (value > INT_MAX/(1024*1024) || value < INT_MIN/(1024*1024)) + errno = ERANGE; + else value *= 1024*1024; + endptr++; + } + } + + if (errno == ERANGE) log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "absolute value of integer \"%s\" is too large (overflow)", s); + + while (isspace(*endptr)) endptr++; + if (*endptr != 0) + extra_chars_error(endptr, inttype, US"integer value for ", name); + } + + if (data_block == NULL) + *((int *)(ol->value)) = value; + else + *((int *)((uschar *)data_block + (long int)(ol->value))) = value; + break; + + /* Integer held in K: again, allow octal and hex formats, and suffixes K and + M. */ + + case opt_Kint: + { + uschar *endptr; + errno = 0; + value = strtol(CS s, CSS &endptr, intbase); + + if (endptr == s) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "%sinteger expected for %s", + inttype, name); + + if (errno != ERANGE) + { + if (tolower(*endptr) == 'm') + { + if (value > INT_MAX/1024 || value < INT_MIN/1024) errno = ERANGE; + else value *= 1024; + endptr++; + } + else if (tolower(*endptr) == 'k') + { + endptr++; + } + else + { + value = (value + 512)/1024; + } + } + + if (errno == ERANGE) log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "absolute value of integer \"%s\" is too large (overflow)", s); + + while (isspace(*endptr)) endptr++; + if (*endptr != 0) + extra_chars_error(endptr, inttype, US"integer value for ", name); + } + + if (data_block == NULL) + *((int *)(ol->value)) = value; + else + *((int *)((uschar *)data_block + (long int)(ol->value))) = value; + break; + + /* Fixed-point number: held to 3 decimal places. */ + + case opt_fixed: + if (sscanf(CS s, "%d%n", &value, &count) != 1) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "fixed-point number expected for %s", name); + + if (value < 0) log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "integer \"%s\" is too large (overflow)", s); + + value *= 1000; + + if (value < 0) log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "integer \"%s\" is too large (overflow)", s); + + if (s[count] == '.') + { + int d = 100; + while (isdigit(s[++count])) + { + value += (s[count] - '0') * d; + d /= 10; + } + } + + while (isspace(s[count])) count++; + + if (s[count] != 0) + extra_chars_error(s+count, US"fixed-point value for ", name, US""); + + if (data_block == NULL) + *((int *)(ol->value)) = value; + else + *((int *)((uschar *)data_block + (long int)(ol->value))) = value; + break; + + /* There's a special routine to read time values. */ + + case opt_time: + value = readconf_readtime(s, 0, FALSE); + if (value < 0) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "invalid time value for %s", + name); + if (data_block == NULL) + *((int *)(ol->value)) = value; + else + *((int *)((uschar *)data_block + (long int)(ol->value))) = value; + break; + + /* A time list is a list of colon-separated times, with the first + element holding the size of the list and the second the number of + entries used. */ + + case opt_timelist: + { + int count = 0; + int *list = (data_block == NULL)? + (int *)(ol->value) : + (int *)((uschar *)data_block + (long int)(ol->value)); + + if (*s != 0) for (count = 1; count <= list[0] - 2; count++) + { + int terminator = 0; + uschar *snext = Ustrchr(s, ':'); + if (snext != NULL) + { + uschar *ss = snext; + while (ss > s && isspace(ss[-1])) ss--; + terminator = *ss; + } + value = readconf_readtime(s, terminator, FALSE); + if (value < 0) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "invalid time value for %s", + name); + if (count > 1 && value <= list[count]) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "time value out of order for %s", name); + list[count+1] = value; + if (snext == NULL) break; + s = snext + 1; + while (isspace(*s)) s++; + } + + if (count > list[0] - 2) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "too many time values for %s", + name); + if (count > 0 && list[2] == 0) count = 0; + list[1] = count; + } + + break; + } + +return TRUE; +} + + + +/************************************************* +* Print a time value * +*************************************************/ + +/* +Argument: a time value in seconds +Returns: pointer to a fixed buffer containing the time as a string, + in readconf_readtime() format +*/ + +uschar * +readconf_printtime(int t) +{ +int s, m, h, d, w; +uschar *p = time_buffer; + +s = t % 60; +t /= 60; +m = t % 60; +t /= 60; +h = t % 24; +t /= 24; +d = t % 7; +w = t/7; + +if (w > 0) { sprintf(CS p, "%dw", w); while (*p) p++; } +if (d > 0) { sprintf(CS p, "%dd", d); while (*p) p++; } +if (h > 0) { sprintf(CS p, "%dh", h); while (*p) p++; } +if (m > 0) { sprintf(CS p, "%dm", m); while (*p) p++; } +if (s > 0 || p == time_buffer) sprintf(CS p, "%ds", s); + +return time_buffer; +} + + + +/************************************************* +* Print an individual option value * +*************************************************/ + +/* This is used by the -bP option, so prints to the standard output. +The entire options list is passed in as an argument, because some options come +in pairs - typically uid/gid settings, which can either be explicit numerical +values, or strings to be expanded later. If the numerical value is unset, +search for "*expand_" to see if there is a string equivalent. + +Arguments: + ol option entry, or NULL for an unknown option + name option name + options_block NULL for main configuration options; otherwise points to + a driver block; if the option doesn't have opt_public + set, then options_block->options_block is where the item + resides. + oltop points to the option list in which ol exists + last one more than the offset of the last entry in optop + +Returns: nothing +*/ + +static void +print_ol(optionlist *ol, uschar *name, void *options_block, + optionlist *oltop, int last) +{ +struct passwd *pw; +struct group *gr; +optionlist *ol2; +void *value; +uid_t *uidlist; +gid_t *gidlist; +uschar *s; +uschar name2[64]; + +if (ol == NULL) + { + printf("%s is not a known option\n", name); + return; + } + +/* Non-admin callers cannot see options that have been flagged secure by the +"hide" prefix. */ + +if (!admin_user && (ol->type & opt_secure) != 0) + { + printf("%s = \n", name); + return; + } + +/* Else show the value of the option */ + +value = ol->value; +if (options_block != NULL) + { + if ((ol->type & opt_public) == 0) + options_block = (void *)(((driver_instance *)options_block)->options_block); + value = (void *)((uschar *)options_block + (long int)value); + } + +switch(ol->type & opt_mask) + { + case opt_stringptr: + case opt_rewrite: /* Show the text value */ + s = *((uschar **)value); + printf("%s = %s\n", name, (s == NULL)? US"" : string_printing2(s, FALSE)); + break; + + case opt_int: + printf("%s = %d\n", name, *((int *)value)); + break; + + case opt_mkint: + { + int x = *((int *)value); + if (x != 0 && (x & 1023) == 0) + { + int c = 'K'; + x >>= 10; + if ((x & 1023) == 0) + { + c = 'M'; + x >>= 10; + } + printf("%s = %d%c\n", name, x, c); + } + else printf("%s = %d\n", name, x); + } + break; + + case opt_Kint: + { + int x = *((int *)value); + if (x == 0) printf("%s = 0\n", name); + else if ((x & 1023) == 0) printf("%s = %dM\n", name, x >> 10); + else printf("%s = %dK\n", name, x); + } + break; + + case opt_octint: + printf("%s = %#o\n", name, *((int *)value)); + break; + + /* Can be negative only when "unset", in which case integer */ + + case opt_fixed: + { + int x = *((int *)value); + int f = x % 1000; + int d = 100; + if (x < 0) printf("%s =\n", name); else + { + printf("%s = %d.", name, x/1000); + do + { + printf("%d", f/d); + f %= d; + d /= 10; + } + while (f != 0); + printf("\n"); + } + } + break; + + /* If the numerical value is unset, try for the string value */ + + case opt_expand_uid: + if (! *get_set_flag(name, oltop, last, options_block)) + { + sprintf(CS name2, "*expand_%.50s", name); + ol2 = find_option(name2, oltop, last); + if (ol2 != NULL) + { + void *value2 = ol2->value; + if (options_block != NULL) + value2 = (void *)((uschar *)options_block + (long int)value2); + s = *((uschar **)value2); + printf("%s = %s\n", name, (s == NULL)? US"" : string_printing(s)); + break; + } + } + + /* Else fall through */ + + case opt_uid: + if (! *get_set_flag(name, oltop, last, options_block)) + printf("%s =\n", name); + else + { + pw = getpwuid(*((uid_t *)value)); + if (pw == NULL) + printf("%s = %ld\n", name, (long int)(*((uid_t *)value))); + else printf("%s = %s\n", name, pw->pw_name); + } + break; + + /* If the numerical value is unset, try for the string value */ + + case opt_expand_gid: + if (! *get_set_flag(name, oltop, last, options_block)) + { + sprintf(CS name2, "*expand_%.50s", name); + ol2 = find_option(name2, oltop, last); + if (ol2 != NULL && (ol2->type & opt_mask) == opt_stringptr) + { + void *value2 = ol2->value; + if (options_block != NULL) + value2 = (void *)((uschar *)options_block + (long int)value2); + s = *((uschar **)value2); + printf("%s = %s\n", name, (s == NULL)? US"" : string_printing(s)); + break; + } + } + + /* Else fall through */ + + case opt_gid: + if (! *get_set_flag(name, oltop, last, options_block)) + printf("%s =\n", name); + else + { + gr = getgrgid(*((int *)value)); + if (gr == NULL) + printf("%s = %ld\n", name, (long int)(*((int *)value))); + else printf("%s = %s\n", name, gr->gr_name); + } + break; + + case opt_uidlist: + uidlist = *((uid_t **)value); + printf("%s =", name); + if (uidlist != NULL) + { + int i; + uschar sep = ' '; + for (i = 1; i <= (int)(uidlist[0]); i++) + { + uschar *name = NULL; + pw = getpwuid(uidlist[i]); + if (pw != NULL) name = US pw->pw_name; + if (name != NULL) printf("%c%s", sep, name); + else printf("%c%ld", sep, (long int)(uidlist[i])); + sep = ':'; + } + } + printf("\n"); + break; + + case opt_gidlist: + gidlist = *((gid_t **)value); + printf("%s =", name); + if (gidlist != NULL) + { + int i; + uschar sep = ' '; + for (i = 1; i <= (int)(gidlist[0]); i++) + { + uschar *name = NULL; + gr = getgrgid(gidlist[i]); + if (gr != NULL) name = US gr->gr_name; + if (name != NULL) printf("%c%s", sep, name); + else printf("%c%ld", sep, (long int)(gidlist[i])); + sep = ':'; + } + } + printf("\n"); + break; + + case opt_time: + printf("%s = %s\n", name, readconf_printtime(*((int *)value))); + break; + + case opt_timelist: + { + int i; + int *list = (int *)value; + printf("%s = ", name); + for (i = 0; i < list[1]; i++) + printf("%s%s", (i == 0)? "" : ":", readconf_printtime(list[i+2])); + printf("\n"); + } + break; + + case opt_bit: + printf("%s%s\n", ((*((int *)value)) & (1 << ((ol->type >> 16) & 31)))? + "" : "no_", name); + break; + + case opt_expand_bool: + sprintf(CS name2, "*expand_%.50s", name); + ol2 = find_option(name2, oltop, last); + if (ol2 != NULL && ol2->value != NULL) + { + void *value2 = ol2->value; + if (options_block != NULL) + value2 = (void *)((uschar *)options_block + (long int)value2); + s = *((uschar **)value2); + if (s != NULL) + { + printf("%s = %s\n", name, string_printing(s)); + break; + } + /* s == NULL => string not set; fall through */ + } + + /* Fall through */ + + case opt_bool: + case opt_bool_verify: + case opt_bool_set: + printf("%s%s\n", (*((BOOL *)value))? "" : "no_", name); + break; + } +} + + + +/************************************************* +* Print value from main configuration * +*************************************************/ + +/* This function, called as a result of encountering the -bP option, +causes the value of any main configuration variable to be output if the +second argument is NULL. There are some special values: + + all print all main configuration options + configure_file print the name of the configuration file + routers print the routers' configurations + transports print the transports' configuration + authenticators print the authenticators' configuration + router_list print a list of router names + transport_list print a list of transport names + authenticator_list print a list of authentication mechanism names + +name print a named list item + local_scan print the local_scan options + +If the second argument is not NULL, it must be one of "router", "transport", or +"authenticator" in which case the first argument identifies the driver whose +options are to be printed. + +Arguments: + name option name if type == NULL; else driver name + type NULL or driver type name, as described above + +Returns: nothing +*/ + +void +readconf_print(uschar *name, uschar *type) +{ +BOOL names_only = FALSE; +optionlist *ol; +optionlist *ol2 = NULL; +driver_instance *d = NULL; +int size = 0; + +if (type == NULL) + { + if (*name == '+') + { + int i; + tree_node *t; + BOOL found = FALSE; + static uschar *types[] = { US"address", US"domain", US"host", + US"localpart" }; + static tree_node **anchors[] = { &addresslist_anchor, &domainlist_anchor, + &hostlist_anchor, &localpartlist_anchor }; + + for (i = 0; i < 4; i++) + { + t = tree_search(*(anchors[i]), name+1); + if (t != NULL) + { + found = TRUE; + printf("%slist %s = %s\n", types[i], name+1, + ((namedlist_block *)(t->data.ptr))->string); + } + } + + if (!found) + printf("no address, domain, host, or local part list called \"%s\" " + "exists\n", name+1); + + return; + } + + if (Ustrcmp(name, "configure_file") == 0) + { + printf("%s\n", CS config_main_filename); + return; + } + + if (Ustrcmp(name, "all") == 0) + { + for (ol = optionlist_config; + ol < optionlist_config + optionlist_config_size; ol++) + { + if ((ol->type & opt_hidden) == 0) + print_ol(ol, US ol->name, NULL, optionlist_config, optionlist_config_size); + } + return; + } + + if (Ustrcmp(name, "local_scan") == 0) + { + #ifndef LOCAL_SCAN_HAS_OPTIONS + printf("local_scan() options are not supported\n"); + #else + for (ol = local_scan_options; + ol < local_scan_options + local_scan_options_count; ol++) + { + print_ol(ol, US ol->name, NULL, local_scan_options, + local_scan_options_count); + } + #endif + return; + } + + if (Ustrcmp(name, "routers") == 0) + { + type = US"router"; + name = NULL; + } + else if (Ustrcmp(name, "transports") == 0) + { + type = US"transport"; + name = NULL; + } + + else if (Ustrcmp(name, "authenticators") == 0) + { + type = US"authenticator"; + name = NULL; + } + + else if (Ustrcmp(name, "authenticator_list") == 0) + { + type = US"authenticator"; + name = NULL; + names_only = TRUE; + } + + else if (Ustrcmp(name, "router_list") == 0) + { + type = US"router"; + name = NULL; + names_only = TRUE; + } + else if (Ustrcmp(name, "transport_list") == 0) + { + type = US"transport"; + name = NULL; + names_only = TRUE; + } + else + { + print_ol(find_option(name, optionlist_config, optionlist_config_size), + name, NULL, optionlist_config, optionlist_config_size); + return; + } + } + +/* Handle the options for a router or transport. Skip options that are flagged +as hidden. Some of these are options with names starting with '*', used for +internal alternative representations of other options (which the printing +function will sort out). Others are synonyms kept for backward compatibility. +*/ + +if (Ustrcmp(type, "router") == 0) + { + d = (driver_instance *)routers; + ol2 = optionlist_routers; + size = optionlist_routers_size; + } +else if (Ustrcmp(type, "transport") == 0) + { + d = (driver_instance *)transports; + ol2 = optionlist_transports; + size = optionlist_transports_size; + } +else if (Ustrcmp(type, "authenticator") == 0) + { + d = (driver_instance *)auths; + ol2 = optionlist_auths; + size = optionlist_auths_size; + } + +if (names_only) + { + for (; d != NULL; d = d->next) printf("%s\n", CS d->name); + return; + } + +/* Either search for a given driver, or print all of them */ + +for (; d != NULL; d = d->next) + { + if (name == NULL) + printf("\n%s %s:\n", d->name, type); + else if (Ustrcmp(d->name, name) != 0) continue; + + for (ol = ol2; ol < ol2 + size; ol++) + { + if ((ol->type & opt_hidden) == 0) + print_ol(ol, US ol->name, d, ol2, size); + } + + for (ol = d->info->options; + ol < d->info->options + *(d->info->options_count); ol++) + { + if ((ol->type & opt_hidden) == 0) + print_ol(ol, US ol->name, d, d->info->options, *(d->info->options_count)); + } + if (name != NULL) return; + } +if (name != NULL) printf("%s %s not found\n", type, name); +} + + + +/************************************************* +* Read a named list item * +*************************************************/ + +/* This function reads a name and a list (i.e. string). The name is used to +save the list in a tree, sorted by its name. Each entry also has a number, +which can be used for caching tests, but if the string contains any expansion +items other than $key, the number is set negative to inhibit caching. This +mechanism is used for domain, host, and address lists that are referenced by +the "+name" syntax. + +Arguments: + anchorp points to the tree anchor + numberp points to the current number for this tree + max the maximum number permitted + s the text of the option line, starting immediately after the name + of the list type + tname the name of the list type, for messages + +Returns: nothing +*/ + +static void +read_named_list(tree_node **anchorp, int *numberp, int max, uschar *s, + uschar *tname) +{ +BOOL forcecache = FALSE; +uschar *ss; +tree_node *t; +namedlist_block *nb = store_get(sizeof(namedlist_block)); + +if (Ustrncmp(s, "_cache", 6) == 0) + { + forcecache = TRUE; + s += 6; + } + +if (!isspace(*s)) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "unrecognized configuration line"); + +if (*numberp >= max) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "too many named %ss (max is %d)\n", + tname, max); + +while (isspace(*s)) s++; +ss = s; +while (isalnum(*s) || *s == '_') s++; +t = store_get(sizeof(tree_node) + s-ss); +Ustrncpy(t->name, ss, s-ss); +t->name[s-ss] = 0; +while (isspace(*s)) s++; + +if (!tree_insertnode(anchorp, t)) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "duplicate name \"%s\" for a named %s", t->name, tname); + +t->data.ptr = nb; +nb->number = *numberp; +*numberp += 1; + +if (*s++ != '=') log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "missing '=' after \"%s\"", t->name); +while (isspace(*s)) s++; +nb->string = read_string(s, t->name); +nb->cache_data = NULL; + +/* Check the string for any expansions; if any are found, mark this list +uncacheable unless the user has explicited forced caching. */ + +if (!forcecache && Ustrchr(nb->string, '$') != NULL) nb->number = -1; +} + + + + +/************************************************* +* Unpick data for a rate limit * +*************************************************/ + +/* This function is called to unpick smtp_ratelimit_{mail,rcpt} into four +separate values. + +Arguments: + s string, in the form t,b,f,l + where t is the threshold (integer) + b is the initial delay (time) + f is the multiplicative factor (fixed point) + k is the maximum time (time) + threshold where to store threshold + base where to store base in milliseconds + factor where to store factor in milliseconds + limit where to store limit + +Returns: nothing (panics on error) +*/ + +static void +unpick_ratelimit(uschar *s, int *threshold, int *base, double *factor, + int *limit) +{ +uschar bstring[16], lstring[16]; + +if (sscanf(CS s, "%d, %15[0123456789smhdw.], %lf, %15s", threshold, bstring, + factor, lstring) == 4) + { + *base = readconf_readtime(bstring, 0, TRUE); + *limit = readconf_readtime(lstring, 0, TRUE); + if (*base >= 0 && *limit >= 0) return; + } +log_write(0, LOG_MAIN|LOG_PANIC_DIE, "malformed ratelimit data: %s", s); +} + + + + +/************************************************* +* Read main configuration options * +*************************************************/ + +/* This function is the first to be called for configuration reading. It +opens the configuration file and reads general configuration settings until +it reaches the end of the configuration section. The file is then left open so +that the remaining configuration data can subsequently be read if needed for +this run of Exim. + +The configuration file must be owned either by root or exim, and be writeable +only by root or uid/gid exim. The values for Exim's uid and gid can be changed +in the config file, so the test is done on the compiled in values. A slight +anomaly, to be carefully documented. + +The name of the configuration file is taken from a list that is included in the +binary of Exim. It can be altered from the command line, but if that is done, +root privilege is immediately withdrawn unless the caller is root or exim. +The first file on the list that exists is used. + +For use on multiple systems that share file systems, first look for a +configuration file whose name has the current node name on the end. If that is +not found, try the generic name. For really contorted configurations, that run +multiple Exims with different uid settings, first try adding the effective uid +before the node name. These complications are going to waste resources on most +systems. Therefore they are available only when requested by compile-time +options. */ + +void +readconf_main(void) +{ +int sep = 0; +struct stat statbuf; +uschar *s, *filename; +uschar *list = config_main_filelist; + +/* Loop through the possible file names */ + +while((filename = string_nextinlist(&list, &sep, big_buffer, big_buffer_size)) + != NULL) + { + /* Cut out all the fancy processing unless specifically wanted */ + + #if defined(CONFIGURE_FILE_USE_NODE) || defined(CONFIGURE_FILE_USE_EUID) + uschar *suffix = filename + Ustrlen(filename); + + /* Try for the node-specific file if a node name exists */ + + #ifdef CONFIGURE_FILE_USE_NODE + struct utsname uts; + if (uname(&uts) >= 0) + { + #ifdef CONFIGURE_FILE_USE_EUID + sprintf(CS suffix, ".%ld.%.256s", (long int)original_euid, uts.nodename); + config_file = Ufopen(filename, "rb"); + if (config_file == NULL) + #endif /* CONFIGURE_FILE_USE_EUID */ + { + sprintf(CS suffix, ".%.256s", uts.nodename); + config_file = Ufopen(filename, "rb"); + } + } + #endif /* CONFIGURE_FILE_USE_NODE */ + + /* Otherwise, try the generic name, possibly with the euid added */ + + #ifdef CONFIGURE_FILE_USE_EUID + if (config_file == NULL) + { + sprintf(CS suffix, ".%ld", (long int)original_euid); + config_file = Ufopen(filename, "rb"); + } + #endif /* CONFIGURE_FILE_USE_EUID */ + + /* Finally, try the unadorned name */ + + if (config_file == NULL) + { + *suffix = 0; + config_file = Ufopen(filename, "rb"); + } + #else /* if neither defined */ + + /* This is the common case when the fancy processing is not included. */ + + config_file = Ufopen(filename, "rb"); + #endif + + /* If the file does not exist, continue to try any others. For any other + error, break out (and die). */ + + if (config_file != NULL || errno != ENOENT) break; + } + +/* On success, save the name for verification; config_filename is used when +logging configuration errors (it changes for .included files) whereas +config_main_filename is the name shown by -bP. Failure to open a configuration +file is a serious disaster. */ + +if (config_file != NULL) + { + config_filename = config_main_filename = string_copy(filename); + } +else + { + if (filename == NULL) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "non-existent configuration file(s): " + "%s", config_main_filelist); + else + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s", string_open_failed(errno, + "configuration file %s", filename)); + } + +/* Check the status of the file we have opened, unless it was specified on +the command line, in which case privilege was given away at the start. */ + +if (!config_changed) + { + if (fstat(fileno(config_file), &statbuf) != 0) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to stat configuration file %s", + big_buffer); + + if ((statbuf.st_uid != root_uid && /* owner not root */ + statbuf.st_uid != exim_uid /* owner not exim */ + #ifdef CONFIGURE_OWNER + && statbuf.st_uid != config_uid /* owner not the special one */ + #endif + ) || /* or */ + (statbuf.st_gid != exim_gid && /* group not exim & */ + (statbuf.st_mode & 020) != 0) || /* group writeable */ + /* or */ + ((statbuf.st_mode & 2) != 0)) /* world writeable */ + + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Exim configuration file %s has the " + "wrong owner, group, or mode", big_buffer); + } + +/* Process the main configuration settings. They all begin with a lower case +letter. If we see something starting with an upper case letter, it is taken as +a macro definition. */ + +while ((s = get_config_line()) != NULL) + { + if (isupper(s[0])) + { + macro_item *m; + macro_item *mlast = NULL; + uschar name[64]; + int namelen = 0; + + while (isalnum(*s) || *s == '_') + { + if (namelen >= sizeof(name) - 1) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "macro name too long (maximum is %d characters)", sizeof(name) - 1); + name[namelen++] = *s++; + } + name[namelen] = 0; + while (isspace(*s)) s++; + if (*s++ != '=') log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "malformed macro definition"); + while (isspace(*s)) s++; + + /* If an existing macro of the same name was defined on the command line, + we just skip this definition. Otherwise it's an error to attempt to + redefine a macro. It is also an error to define a macro whose name begins + with the name of a previously-defined macro. */ + + for (m = macros; m != NULL; m = m->next) + { + int len = Ustrlen(m->name); + + if (Ustrcmp(m->name, name) == 0) + { + if (m->command_line) break; + log_write(0, LOG_CONFIG|LOG_PANIC_DIE, "macro \"%s\" is already " + "defined", name); + } + + if (len < namelen && Ustrstr(name, m->name) != NULL) + log_write(0, LOG_CONFIG|LOG_PANIC_DIE, "\"%s\" cannot be defined as " + "a macro because previously defined macro \"%s\" is a substring", + name, m->name); + + mlast = m; + } + if (m != NULL) continue; /* Found an overriding command-line definition */ + + m = store_get(sizeof(macro_item) + namelen); + m->next = NULL; + m->command_line = FALSE; + if (mlast == NULL) macros = m; else mlast->next = m; + + /* This use of strcpy() is OK because we have obtained a block of store + whose size is based on the length of "name". The definition of the + macro_item structure includes a final vector called "name" which is one + byte long. Thus, adding "namelen" gives us enough room to store the "name" + string. */ + + Ustrcpy(m->name, name); + m->replacement = string_copy(s); + } + + else if (Ustrncmp(s, "domainlist", 10) == 0) + read_named_list(&domainlist_anchor, &domainlist_count, + MAX_NAMED_LIST, s+10, US"domain list"); + + else if (Ustrncmp(s, "hostlist", 8) == 0) + read_named_list(&hostlist_anchor, &hostlist_count, + MAX_NAMED_LIST, s+8, US"host list"); + + else if (Ustrncmp(s, US"addresslist", 11) == 0) + read_named_list(&addresslist_anchor, &addresslist_count, + MAX_NAMED_LIST, s+11, US"address list"); + + else if (Ustrncmp(s, US"localpartlist", 13) == 0) + read_named_list(&localpartlist_anchor, &localpartlist_count, + MAX_NAMED_LIST, s+13, US"local part list"); + + else + (void) readconf_handle_option(s, optionlist_config, optionlist_config_size, + NULL, US"main option \"%s\" unknown"); + } + + +/* If local_sender_retain is set, local_from_check must be unset. */ + +if (local_sender_retain && local_from_check) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "both local_from_check and " + "local_sender_retain are set; this combination is not allowed"); + +/* If the timezone string is empty, set it to NULL, implying no TZ variable +wanted. */ + +if (timezone_string != NULL && *timezone_string == 0) timezone_string = NULL; + +/* remote_max_parallel must be > 0 */ + +if (remote_max_parallel <= 0) remote_max_parallel = 1; + +/* The primary host name may be required for expansion of spool_directory +and log_file_path, so make sure it is set asap. It is obtained from uname(), +but if that yields an unqualified value, make a FQDN by using gethostbyname to +canonize it. Some people like upper case letters in their host names, so we +don't force the case. */ + +if (primary_hostname == NULL) + { + uschar *hostname; + struct utsname uts; + if (uname(&uts) < 0) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "uname() failed to yield host name"); + hostname = US uts.nodename; + + if (Ustrchr(hostname, '.') == NULL) + { + int af = AF_INET; + struct hostent *hostdata; + + #if HAVE_IPV6 + if (dns_ipv4_lookup == NULL || + match_isinlist(hostname, &dns_ipv4_lookup, 0, NULL, NULL, MCL_DOMAIN, + TRUE, NULL) != OK) + af = AF_INET6; + #else + af = AF_INET; + #endif + + for (;;) + { + #if HAVE_IPV6 + #if HAVE_GETIPNODEBYNAME + int error_num; + hostdata = getipnodebyname(CS hostname, af, 0, &error_num); + #else + hostdata = gethostbyname2(CS hostname, af); + #endif + #else + hostdata = gethostbyname(CS hostname); + #endif + + if (hostdata != NULL) + { + hostname = US hostdata->h_name; + break; + } + + if (af == AF_INET) break; + af = AF_INET; + } + } + + primary_hostname = string_copy(hostname); + } + +/* Set up default value for smtp_active_hostname */ + +smtp_active_hostname = primary_hostname; + +/* If spool_directory wasn't set in the build-time configuration, it must have +got set above. Of course, writing to the log may not work if log_file_path is +not set, but it will at least get to syslog or somewhere, with any luck. */ + +if (*spool_directory == 0) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "spool_directory undefined: cannot " + "proceed"); + +/* Expand the spool directory name; it may, for example, contain the primary +host name. Same comment about failure. */ + +s = expand_string(spool_directory); +if (s == NULL) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to expand spool_directory " + "\"%s\": %s", spool_directory, expand_string_message); +spool_directory = s; + +/* Expand log_file_path, which must contain "%s" in any component that isn't +the null string or "syslog". It is also allowed to contain one instance of %D. +However, it must NOT contain % followed by anything else. */ + +if (*log_file_path != 0) + { + uschar *ss, *sss; + int sep = ':'; /* Fixed for log file path */ + s = expand_string(log_file_path); + if (s == NULL) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to expand log_file_path " + "\"%s\": %s", log_file_path, expand_string_message); + + ss = s; + while ((sss = string_nextinlist(&ss,&sep,big_buffer,big_buffer_size)) != NULL) + { + uschar *t; + if (sss[0] == 0 || Ustrcmp(sss, "syslog") == 0) continue; + t = Ustrstr(sss, "%s"); + if (t == NULL) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "log_file_path \"%s\" does not " + "contain \"%%s\"", sss); + *t = 'X'; + t = Ustrchr(sss, '%'); + if (t != NULL) + { + if (t[1] != 'D' || Ustrchr(t+2, '%') != NULL) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "log_file_path \"%s\" contains " + "unexpected \"%%\" character", s); + } + } + + log_file_path = s; + } + +/* Interpret syslog_facility into an integer argument for 'ident' param to +openlog(). Default is LOG_MAIL set in globals.c. Allow the user to omit the +leading "log_". */ + +if (syslog_facility_str != NULL) + { + int i; + uschar *s = syslog_facility_str; + + if ((Ustrlen(syslog_facility_str) >= 4) && + (strncmpic(syslog_facility_str, US"log_", 4) == 0)) + s += 4; + + for (i = 0; i < syslog_list_size; i++) + { + if (strcmpic(s, syslog_list[i].name) == 0) + { + syslog_facility = syslog_list[i].value; + break; + } + } + + if (i >= syslog_list_size) + { + log_write(0, LOG_PANIC_DIE|LOG_CONFIG, + "failed to interpret syslog_facility \"%s\"", syslog_facility_str); + } + } + +/* Expand pid_file_path */ + +if (*pid_file_path != 0) + { + s = expand_string(pid_file_path); + if (s == NULL) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to expand pid_file_path " + "\"%s\": %s", pid_file_path, expand_string_message); + pid_file_path = s; + } + +/* Compile the regex for matching a UUCP-style "From_" line in an incoming +message. */ + +regex_From = regex_must_compile(uucp_from_pattern, FALSE, TRUE); + +/* Unpick the SMTP rate limiting options, if set */ + +if (smtp_ratelimit_mail != NULL) + { + unpick_ratelimit(smtp_ratelimit_mail, &smtp_rlm_threshold, + &smtp_rlm_base, &smtp_rlm_factor, &smtp_rlm_limit); + } + +if (smtp_ratelimit_rcpt != NULL) + { + unpick_ratelimit(smtp_ratelimit_rcpt, &smtp_rlr_threshold, + &smtp_rlr_base, &smtp_rlr_factor, &smtp_rlr_limit); + } + +/* The qualify domains default to the primary host name */ + +if (qualify_domain_sender == NULL) + qualify_domain_sender = primary_hostname; +if (qualify_domain_recipient == NULL) + qualify_domain_recipient = qualify_domain_sender; + +/* Setting system_filter_user in the configuration sets the gid as well if a +name is given, but a numerical value does not. */ + +if (system_filter_uid_set && !system_filter_gid_set) + { + struct passwd *pw = getpwuid(system_filter_uid); + if (pw == NULL) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Failed to look up uid %ld", + (long int)system_filter_uid); + system_filter_gid = pw->pw_gid; + system_filter_gid_set = TRUE; + } + +/* If the errors_reply_to field is set, check that it is syntactically valid +and ensure it contains a domain. */ + +if (errors_reply_to != NULL) + { + uschar *errmess; + int start, end, domain; + uschar *recipient = parse_extract_address(errors_reply_to, &errmess, + &start, &end, &domain, FALSE); + + if (recipient == NULL) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG, + "error in errors_reply_to (%s): %s", errors_reply_to, errmess); + + if (domain == 0) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG, + "errors_reply_to (%s) does not contain a domain", errors_reply_to); + } + +/* If smtp_accept_queue or smtp_accept_max_per_host is set, then +smtp_accept_max must also be set. */ + +if (smtp_accept_max == 0 && + (smtp_accept_queue > 0 || smtp_accept_max_per_host != NULL)) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG, + "smtp_accept_max must be set if smtp_accept_queue or " + "smtp_accept_max_per_host is set"); + +/* Set up the host number if anything is specified. It is an expanded string +so that it can be computed from the host name, for example. We do this last +so as to ensure that everything else is set up before the expansion. */ + +if (host_number_string != NULL) + { + uschar *end; + uschar *s = expand_string(host_number_string); + long int n = Ustrtol(s, &end, 0); + while (isspace(*end)) end++; + if (*end != 0) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG, + "localhost_number value is not a number: %s", s); + if (n > LOCALHOST_MAX) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG, + "localhost_number is greater than the maximum allowed value (%d)", + LOCALHOST_MAX); + host_number = n; + } + +#ifdef SUPPORT_TLS +/* If tls_verify_hosts is set, tls_verify_certificates must also be set */ + +if ((tls_verify_hosts != NULL || tls_try_verify_hosts != NULL) && + tls_verify_certificates == NULL) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG, + "tls_%sverify_hosts is set, but tls_verify_certificates is not set", + (tls_verify_hosts != NULL)? "" : "try_"); +#endif +} + + + +/************************************************* +* Initialize one driver * +*************************************************/ + +/* This is called once the driver's generic options, if any, have been read. +We can now find the driver, set up defaults for the private options, and +unset any "set" bits in the private options table (which might have been +set by another incarnation of the same driver). + +Arguments: + d pointer to driver instance block, with generic + options filled in + drivers_available vector of available drivers + size_of_info size of each block in drivers_available + class class of driver, for error message + +Returns: pointer to the driver info block +*/ + +static driver_info * +init_driver(driver_instance *d, driver_info *drivers_available, + int size_of_info, uschar *class) +{ +driver_info *dd; + +for (dd = drivers_available; dd->driver_name[0] != 0; + dd = (driver_info *)(((uschar *)dd) + size_of_info)) + { + if (Ustrcmp(d->driver_name, dd->driver_name) == 0) + { + int i; + int len = dd->options_len; + d->info = dd; + d->options_block = store_get(len); + memcpy(d->options_block, dd->options_block, len); + for (i = 0; i < *(dd->options_count); i++) + dd->options[i].type &= ~opt_set; + return dd; + } + } + +log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "%s %s: cannot find %s driver \"%s\"", class, d->name, class, d->driver_name); + +return NULL; /* never obeyed */ +} + + + + +/************************************************* +* Initialize driver list * +*************************************************/ + +/* This function is called for routers, transports, and authentication +mechanisms. It reads the data from the current point in the configuration file +up to the end of the section, and sets up a chain of instance blocks according +to the file's contents. The file will already have been opened by a call to +readconf_main, and must be left open for subsequent reading of further data. + +Any errors cause a panic crash. Note that the blocks with names driver_info and +driver_instance must map the first portions of all the _info and _instance +blocks for this shared code to work. + +Arguments: + class "router", "transport", or "authenticator" + anchor &routers, &transports, &auths + drivers_available available drivers + size_of_info size of each info block + instance_default points to default data for an instance + instance_size size of instance block + driver_optionlist generic option list + driver_optionlist_count count of generic option list + +Returns: nothing +*/ + +void +readconf_driver_init( + uschar *class, + driver_instance **anchor, + driver_info *drivers_available, + int size_of_info, + void *instance_default, + int instance_size, + optionlist *driver_optionlist, + int driver_optionlist_count) +{ +driver_instance **p = anchor; +driver_instance *d = NULL; +uschar *buffer; + +/* Now process the configuration lines */ + +while ((buffer = get_config_line()) != NULL) + { + uschar name[64]; + + /* Read the first name on the line and test for the start of a new driver. + If this isn't the start of a new driver, the line will be re-read. */ + + uschar *s = readconf_readname(name, sizeof(name), buffer); + + /* If the line starts with a name terminated by a colon, we are at the + start of the definition of a new driver. The rest of the line must be + blank. */ + + if (*s++ == ':') + { + int i; + + /* Finish off initializing the previous driver. */ + + if (d != NULL) + { + if (d->driver_name == NULL) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG, + "no driver defined for %s \"%s\"", class, d->name); + (d->info->init)(d); + } + + /* Check that we haven't already got a driver of this name */ + + for (d = *anchor; d != NULL; d = d->next) + if (Ustrcmp(name, d->name) == 0) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG, + "there are two %ss called \"%s\"", class, name); + + /* Set up a new driver instance data block on the chain, with + its default values installed. */ + + d = store_get(instance_size); + memcpy(d, instance_default, instance_size); + *p = d; + p = &(d->next); + d->name = string_copy(name); + + /* Clear out the "set" bits in the generic options */ + + for (i = 0; i < driver_optionlist_count; i++) + driver_optionlist[i].type &= ~opt_set; + + /* Check nothing more on this line, then do the next loop iteration. */ + + while (isspace(*s)) s++; + if (*s != 0) extra_chars_error(s, US"driver name ", name, US""); + continue; + } + + /* Give an error if we have not set up a current driver yet. */ + + if (d == NULL) log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "%s name missing", class); + + /* First look to see if this is a generic option; if it is "driver", + initialize the driver. If is it not a generic option, we can look for a + private option provided that the driver has been previously set up. */ + + if (readconf_handle_option(buffer, driver_optionlist, + driver_optionlist_count, d, NULL)) + { + if (d->info == NULL && d->driver_name != NULL) + init_driver(d, drivers_available, size_of_info, class); + } + + /* Handle private options - pass the generic block because some may + live therein. A flag with each option indicates if it is in the public + block. */ + + else if (d->info != NULL) + { + readconf_handle_option(buffer, d->info->options, + *(d->info->options_count), d, US"option \"%s\" unknown"); + } + + /* The option is not generic and the driver name has not yet been given. */ + + else log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "option \"%s\" unknown " + "(\"driver\" must be specified before any private options)", name); + } + +/* Run the initialization function for the final driver. */ + +if (d != NULL) + { + if (d->driver_name == NULL) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG, + "no driver defined for %s \"%s\"", class, d->name); + (d->info->init)(d); + } +} + + + +/************************************************* +* Check driver dependency * +*************************************************/ + +/* This function is passed a driver instance and a string. It checks whether +any of the string options for the driver contains the given string as an +expansion variable. + +Arguments: + d points to a driver instance block + s the string to search for + +Returns: TRUE if a dependency is found +*/ + +BOOL +readconf_depends(driver_instance *d, uschar *s) +{ +int count = *(d->info->options_count); +optionlist *ol; +uschar *ss; + +for (ol = d->info->options; ol < d->info->options + count; ol++) + { + void *options_block; + uschar *value; + int type = ol->type & opt_mask; + if (type != opt_stringptr) continue; + options_block = ((ol->type & opt_public) == 0)? d->options_block : (void *)d; + value = *(uschar **)((uschar *)options_block + (long int)(ol->value)); + if (value != NULL && (ss = Ustrstr(value, s)) != NULL) + { + if (ss <= value || (ss[-1] != '$' && ss[-1] != '{') || + isalnum(ss[Ustrlen(s)])) continue; + DEBUG(D_transport) debug_printf("driver %s: \"%s\" option depends on %s\n", + d->name, ol->name, s); + return TRUE; + } + } + +DEBUG(D_transport) debug_printf("driver %s does not depend on %s\n", d->name, s); +return FALSE; +} + + + + +/************************************************* +* Decode an error type for retries * +*************************************************/ + +/* This function is global because it is also called from the main +program when testing retry information. It decodes strings such as "quota_7d" +into numerical error codes. + +Arguments: + pp points to start of text + p points past end of text + basic_errno points to an int to receive the main error number + more_errno points to an int to receive the secondary error data + +Returns: NULL if decoded correctly; else points to error text +*/ + +uschar * +readconf_retry_error(uschar *pp, uschar *p, int *basic_errno, int *more_errno) +{ +int len; +uschar *q = pp; +while (q < p && *q != '_') q++; +len = q - pp; + +if (len == 5 && strncmpic(pp, US"quota", len) == 0) + { + *basic_errno = ERRNO_EXIMQUOTA; + if (q != p && (*more_errno = readconf_readtime(q+1, *p, FALSE)) < 0) + return US"bad time value"; + } + +else if (len == 7 && strncmpic(pp, US"refused", len) == 0) + { + *basic_errno = ECONNREFUSED; + if (q != p) + { + if (strncmpic(q+1, US"MX", p-q-1) == 0) *more_errno = 'M'; + else if (strncmpic(q+1, US"A", p-q-1) == 0) *more_errno = 'A'; + else return US"A or MX expected after \"refused\""; + } + } + +else if (len == 7 && strncmpic(pp, US"timeout", len) == 0) + { + *basic_errno = ETIMEDOUT; + if (q != p) + { + int i; + int xlen = p - q - 1; + uschar *x = q + 1; + + static uschar *extras[] = + { US"A", US"MX", US"connect", US"connect_A", US"connect_MX" }; + static int values[] = + { 'A', 'M', RTEF_CTOUT, RTEF_CTOUT|'A', RTEF_CTOUT|'M' }; + + for (i = 0; i < sizeof(extras)/sizeof(uschar *); i++) + { + if (strncmpic(x, extras[i], xlen) == 0) + { + *more_errno = values[i]; + break; + } + } + + if (i >= sizeof(extras)/sizeof(uschar *)) + { + if (strncmpic(x, US"DNS", xlen) == 0) + { + log_write(0, LOG_MAIN|LOG_PANIC, "\"timeout_dns\" is no longer " + "available in retry rules (it has never worked) - treated as " + "\"timeout\""); + } + else return US"\"A\", \"MX\", or \"connect\" expected after \"timeout\""; + } + } + } + +else if (strncmpic(pp, US"rcpt_4", 6) == 0) + { + BOOL bad = FALSE; + int x = 255; /* means "any 4xx code" */ + if (p != pp + 8) bad = TRUE; else + { + int a = pp[6], b = pp[7]; + if (isdigit(a)) + { + x = (a - '0') * 10; + if (isdigit(b)) x += b - '0'; + else if (b == 'x') x += 100; + else bad = TRUE; + } + else if (a != 'x' || b != 'x') bad = TRUE; + } + + if (bad) return US"rcpt_4 must be followed by xx, dx, or dd, where " + "x is literal and d is any digit"; + + *basic_errno = ERRNO_RCPT4XX; + *more_errno = x << 8; + } + +else if (len == 4 && strncmpic(pp, US"auth", len) == 0 && + strncmpic(q+1, US"failed", p-q-1) == 0) + { + *basic_errno = ERRNO_AUTHFAIL; + } + +else if (len != 1 || Ustrncmp(pp, "*", 1) != 0) + return string_sprintf("unknown or malformed retry error \"%.*s\"", p-pp, pp); + +return NULL; +} + + + + +/************************************************* +* Read retry information * +*************************************************/ + +/* Each line of retry information contains: + +. A domain name pattern or an address pattern; + +. An error name, possibly with additional data, or *; + +. An optional sequence of retry items, each consisting of an identifying + letter, a cutoff time, and optional parameters. + +All this is decoded and placed into a control block. */ + + +/* Subroutine to read an argument, preceded by a comma and terminated +by comma, semicolon, whitespace, or newline. The types are: 0 = time value, +1 = fixed point number (returned *1000). + +Arguments: + paddr pointer to pointer to current character; updated + type 0 => read a time; 1 => read a fixed point number + +Returns: time in seconds or fixed point number * 1000 +*/ + +static int +retry_arg(uschar **paddr, int type) +{ +uschar *p = *paddr; +uschar *pp; + +if (*p++ != ',') log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "comma expected"); + +while (isspace(*p)) p++; +pp = p; +while (isalnum(*p) || (type == 1 && *p == '.')) p++; + +if (*p != 0 && !isspace(*p) && *p != ',' && *p != ';') + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "comma or semicolon expected"); + +*paddr = p; +switch (type) + { + case 0: + return readconf_readtime(pp, *p, FALSE); + case 1: + return readconf_readfixed(pp, *p); + } +return 0; /* Keep picky compilers happy */ +} + +/* The function proper */ + +void +readconf_retries(void) +{ +retry_config **chain = &retries; +retry_config *next; +uschar *p; + +while ((p = get_config_line()) != NULL) + { + retry_rule **rchain; + uschar *pp, *error; + + next = store_get(sizeof(retry_config)); + next->next = NULL; + *chain = next; + chain = &(next->next); + next->basic_errno = next->more_errno = 0; + next->senders = NULL; + next->rules = NULL; + rchain = &(next->rules); + + next->pattern = string_dequote(&p); + while (isspace(*p)) p++; + pp = p; + while (mac_isgraph(*p)) p++; + if (p - pp <= 0) log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "missing error type"); + + /* Test error names for things we understand. */ + + if ((error = readconf_retry_error(pp, p, &(next->basic_errno), + &(next->more_errno))) != NULL) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "%s", error); + + /* There may be an optional address list of senders to be used as another + constraint on the rule. This was added later, so the syntax is a bit of a + fudge. Anything that is not a retry rule starting "F," or "G," is treated as + an address list. */ + + while (isspace(*p)) p++; + if (Ustrncmp(p, "senders", 7) == 0) + { + p += 7; + while (isspace(*p)) p++; + if (*p++ != '=') log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "\"=\" expected after \"senders\" in retry rule"); + while (isspace(*p)) p++; + next->senders = string_dequote(&p); + } + + /* Now the retry rules. Keep the maximum timeout encountered. */ + + while (isspace(*p)) p++; + + while (*p != 0) + { + retry_rule *rule = store_get(sizeof(retry_rule)); + *rchain = rule; + rchain = &(rule->next); + rule->next = NULL; + rule->rule = toupper(*p++); + rule->timeout = retry_arg(&p, 0); + if (rule->timeout > retry_maximum_timeout) + retry_maximum_timeout = rule->timeout; + + switch (rule->rule) + { + case 'F': /* Fixed interval */ + rule->p1 = retry_arg(&p, 0); + break; + + case 'G': /* Geometrically increasing intervals */ + rule->p1 = retry_arg(&p, 0); + rule->p2 = retry_arg(&p, 1); + break; + + default: + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "unknown retry rule letter"); + break; + } + + if (rule->timeout <= 0 || rule->p1 <= 0 || + (rule->rule == 'G' && rule->p2 < 1000)) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "bad parameters for retry rule"); + + while (isspace(*p)) p++; + if (*p == ';') + { + p++; + while (isspace(*p)) p++; + } + else if (*p != 0) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "semicolon expected"); + } + } +} + + + +/************************************************* +* Initialize authenticators * +*************************************************/ + +/* Read the authenticators section of the configuration file. + +Arguments: none +Returns: nothing +*/ + +static void +auths_init(void) +{ +auth_instance *au, *bu; +readconf_driver_init(US"authenticator", + (driver_instance **)(&auths), /* chain anchor */ + (driver_info *)auths_available, /* available drivers */ + sizeof(auth_info), /* size of info block */ + &auth_defaults, /* default values for generic options */ + sizeof(auth_instance), /* size of instance block */ + optionlist_auths, /* generic options */ + optionlist_auths_size); + +for (au = auths; au != NULL; au = au->next) + { + if (au->public_name == NULL) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG, "no public name specified for " + "the %s authenticator", au->name); + for (bu = au->next; bu != NULL; bu = bu->next) + { + if (strcmpic(au->public_name, bu->public_name) == 0) + { + if ((au->client && bu->client) || (au->server && bu->server)) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG, "two %s authenticators " + "(%s and %s) have the same public name (%s)", + (au->client)? US"client" : US"server", au->name, bu->name, + au->public_name); + } + } + } +} + + + + +/************************************************* +* Read ACL information * +*************************************************/ + +/* If this run of Exim is not doing something that involves receiving a +message, we can just skip over the ACL information. No need to parse it. + +First, we have a function for acl_read() to call back to get the next line. We +need to remember the line we passed, because at the end it will contain the +name of the next ACL. */ + +static uschar *acl_line; + +static uschar * +acl_callback(void) +{ +acl_line = get_config_line(); +return acl_line; +} + + +/* Now the main function: + +Arguments: + skip TRUE when this Exim process is doing something that will + not need the ACL data + +Returns: nothing +*/ + +static void +readconf_acl(BOOL skip) +{ +uschar *p; + +/* Not receiving messages, don't need to parse the ACL data */ + +if (skip) + { + DEBUG(D_acl) debug_printf("skipping ACL configuration - not needed\n"); + while ((p = get_config_line()) != NULL); + return; + } + +/* Read each ACL and add it into the tree */ + +acl_line = get_config_line(); + +while(acl_line != NULL) + { + uschar name[64]; + tree_node *node; + uschar *error; + + p = readconf_readname(name, sizeof(name), acl_line); + if (*p != ':') + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "missing ACL name"); + + node = store_get(sizeof(tree_node) + Ustrlen(name)); + Ustrcpy(node->name, name); + if (!tree_insertnode(&acl_anchor, node)) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "there are two ACLs called \"%s\"", name); + + node->data.ptr = acl_read(acl_callback, &error); + + if (node->data.ptr == NULL && error != NULL) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "error in ACL: %s", error); + } +} + + + +/************************************************* +* Read configuration for local_scan() * +*************************************************/ + +/* This function is called after "begin local_scan" is encountered in the +configuration file. If the local_scan() function allows for configuration +options, we can process them. Otherwise, we expire in a panic. + +Arguments: none +Returns: nothing +*/ + +static void +local_scan_init(void) +{ +#ifndef LOCAL_SCAN_HAS_OPTIONS +log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, "local_scan() options not supported: " + "(LOCAL_SCAN_HAS_OPTIONS not defined in Local/Makefile)"); +#else + +uschar *p; +while ((p = get_config_line()) != NULL) + { + (void) readconf_handle_option(p, local_scan_options, local_scan_options_count, + NULL, US"local_scan option \"%s\" unknown"); + } +#endif +} + + + +/************************************************* +* Read rest of configuration (after main) * +*************************************************/ + +/* This function reads the rest of the runtime configuration, after the main +configuration. It is called only when actually needed. Each subsequent section +of the configuration starts with a line of the form + + begin name + +where the name is "routers", "transports", etc. A section is terminated by +hitting the next "begin" line, and the next name is left in next_section. +Because it may confuse people as to whether the names are singular or plural, +we add "s" if it's missing. There is always enough room in next_section for +this. This function is basically just a switch. + +Arguments: + skip_acl TRUE if ACL information is not needed + +Returns: nothing +*/ + +static uschar *section_list[] = { + US"acls", + US"authenticators", + US"local_scans", + US"retrys", + US"rewrites", + US"routers", + US"transports"}; + +void +readconf_rest(BOOL skip_acl) +{ +int had = 0; + +while(next_section[0] != 0) + { + int bit; + int first = 0; + int last = sizeof(section_list) / sizeof(uschar *); + int mid = last/2; + int n = Ustrlen(next_section); + + if (tolower(next_section[n-1]) != 's') Ustrcpy(next_section+n, "s"); + + for (;;) + { + int c = strcmpic(next_section, section_list[mid]); + if (c == 0) break; + if (c > 0) first = mid + 1; else last = mid; + if (first >= last) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "\"%.*s\" is not a known configuration section name", n, next_section); + mid = (last + first)/2; + } + + bit = 1 << mid; + if (((had ^= bit) & bit) == 0) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG_IN, + "\"%.*s\" section is repeated in the configuration file", n, + next_section); + + switch(mid) + { + case 0: readconf_acl(skip_acl); break; + case 1: auths_init(); break; + case 2: local_scan_init(); break; + case 3: readconf_retries(); break; + case 4: readconf_rewrites(); break; + case 5: route_init(); break; + case 6: transport_init(); break; + } + } + +fclose(config_file); +} + +/* End of readconf.c */ diff --git a/src/src/receive.c b/src/src/receive.c new file mode 100644 index 000000000..8f6db6cd1 --- /dev/null +++ b/src/src/receive.c @@ -0,0 +1,3247 @@ +/* $Cambridge: exim/src/src/receive.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Code for receiving a message and setting up spool files. */ + + +#include "exim.h" + + + +/************************************************* +* Local static variables * +*************************************************/ + +static FILE *data_file = NULL; +static int data_fd = -1; +static uschar spool_name[256]; + + + +/************************************************* +* Non-SMTP character reading functions * +*************************************************/ + +/* These are the default functions that are set up in the variables such as +receive_getc initially. They just call the standard functions, passing stdin as +the file. (When SMTP input is occurring, different functions are used by +changing the pointer variables.) */ + +int +stdin_getc(void) +{ +return getc(stdin); +} + +int +stdin_ungetc(int c) +{ +return ungetc(c, stdin); +} + +int +stdin_feof(void) +{ +return feof(stdin); +} + +int +stdin_ferror(void) +{ +return ferror(stdin); +} + + + + +/************************************************* +* Check that a set sender is allowed * +*************************************************/ + +/* This function is called when a local caller sets an explicit sender address. +It checks whether this is permitted, which it is for trusted callers. +Otherwise, it must match the pattern(s) in untrusted_set_sender. + +Arguments: the proposed sender address +Returns: TRUE for a trusted caller + TRUE if the address has been set, untrusted_set_sender has been + set, and the address matches something in the list + FALSE otherwise +*/ + +BOOL +receive_check_set_sender(uschar *newsender) +{ +uschar *qnewsender; +if (trusted_caller) return TRUE; +if (newsender == NULL || untrusted_set_sender == NULL) return FALSE; +qnewsender = (Ustrchr(newsender, '@') != NULL)? + newsender : string_sprintf("%s@%s", newsender, qualify_domain_sender); +return + match_address_list(qnewsender, TRUE, TRUE, &untrusted_set_sender, NULL, -1, + 0, NULL) == OK; +} + + + + +/************************************************* +* Check space on spool and log partitions * +*************************************************/ + +/* This function is called before accepting a message; if any thresholds are +set, it checks them. If a message_size is supplied, it checks that there is +enough space for that size plus the threshold - i.e. that the message won't +reduce the space to the threshold. Not all OS have statvfs(); for those that +don't, this function always returns TRUE. For some OS the old function and +struct name statfs is used; that is handled by a macro, defined in exim.h. + +Arguments: + msg_size the (estimated) size of an incoming message + +Returns: FALSE if there isn't enough space, or if the information cannot + be obtained + TRUE if no check was done or there is enough space +*/ + +BOOL +receive_check_fs(int msg_size) +{ +#ifdef HAVE_STATFS +BOOL rc = TRUE; +struct STATVFS statbuf; + +memset(&statbuf, 0, sizeof(statbuf)); + +/* The field names are macros, because not all OS have F_FAVAIL and it seems +tidier to have macros for F_BAVAIL and F_FILES as well. Some kinds of file +server do not have inodes, and they return -1 for the number available, so we +do the check only when this field is non-negative. + +Later: It turns out that some file systems that do not have the concept of +inodes return 0 rather than -1. Such systems should also return 0 for the total +number of inodes, so we require that to be greater than zero before doing the +test. */ + +if (check_spool_space > 0 || msg_size > 0 || check_spool_inodes > 0) + { + if (STATVFS(CS spool_directory, &statbuf) != 0) + { + log_write(0, LOG_MAIN|LOG_PANIC, "cannot accept message: failed to stat " + "spool directory %s: %s", spool_directory, strerror(errno)); + smtp_closedown(US"spool directory problem"); + exim_exit(EXIT_FAILURE); + } + + /* check_spool_space is held in K because disks are getting huge */ + + if (statbuf.F_BAVAIL < (unsigned long) + ((((double)check_spool_space) * 1024.0 + (double)msg_size) / + (double)statbuf.F_FRSIZE) + || + (statbuf.F_FILES > 0 && + statbuf.F_FAVAIL >= 0 && + statbuf.F_FAVAIL < check_spool_inodes)) + rc = FALSE; + + DEBUG(D_receive) + debug_printf("spool directory %s space = %d blocks; inodes = %d; " + "check_space = %dK (%d blocks); inodes = %d; msg_size = %d (%d blocks)\n", + spool_directory, (int)statbuf.F_BAVAIL, (int)statbuf.F_FAVAIL, + check_spool_space, + (int)(((double)check_spool_space * 1024.0) / (double)statbuf.F_FRSIZE), + check_spool_inodes, msg_size, (int)(msg_size / statbuf.F_FRSIZE)); + + if (!rc) + { + log_write(0, LOG_MAIN, "spool directory space check failed: space=%d " + "inodes=%d", (int)statbuf.F_BAVAIL, (int)statbuf.F_FAVAIL); + return FALSE; + } + } + +/* Need to cut down the log file path to the directory, and to ignore any +appearance of "syslog" in it. */ + +if (check_log_space > 0 || check_log_inodes > 0) + { + uschar *path; + int sep = ':'; /* Not variable - outside scripts use */ + uschar *cp; + uschar *p = log_file_path; + uschar buffer[1024]; + + /* An empty log_file_path means "use the default". This is the same as an + empty item in a list. */ + + if (*p == 0) p = US":"; + while ((path = string_nextinlist(&p, &sep, buffer, sizeof(buffer))) != NULL) + { + if (Ustrcmp(path, "syslog") != 0) break; + } + + if (path == NULL) return TRUE; /* No log files, so no problem */ + + /* An empty string means use the default */ + + if (path[0] == 0) + path = string_sprintf("%s/log/%%slog", spool_directory); + + if ((cp = Ustrrchr(path, '/')) == NULL) + { + DEBUG(D_receive) debug_printf("cannot find slash in %s\n", path); + return FALSE; + } + *cp = 0; + + if (STATVFS(CS path, &statbuf) != 0) + { + log_write(0, LOG_MAIN|LOG_PANIC, "cannot accept message: failed to stat " + "log directory %s: %s", path, strerror(errno)); + smtp_closedown(US"log directory problem"); + exim_exit(EXIT_FAILURE); + } + + if (statbuf.F_BAVAIL < (unsigned long) + (((double)check_log_space * 1024.0) / (double)statbuf.F_FRSIZE) + || + statbuf.F_FAVAIL < check_log_inodes) rc = FALSE; + + DEBUG(D_receive) + debug_printf("log directory %s space = %d blocks; inodes = %d; " + "check_space = %dK (%d blocks); inodes = %d\n", + path, (int)statbuf.F_BAVAIL, (int)statbuf.F_FAVAIL, + check_log_space, + (int)(((double)check_log_space * 1024.0) / (double)statbuf.F_FRSIZE), + check_log_inodes); + + if (!rc) + { + log_write(0, LOG_MAIN, "log directory space check failed: space=%d " + "inodes=%d", (int)statbuf.F_BAVAIL, (int)statbuf.F_FAVAIL); + return FALSE; + } + } + +#endif +return TRUE; +} + + + +/************************************************* +* Bomb out while reading a message * +*************************************************/ + +/* The common case of wanting to bomb out is if a SIGTERM or SIGINT is +received, or if there is a timeout. A rarer case might be if the log files are +screwed up and Exim can't open them to record a message's arrival. Handling +that case is done by setting a flag to cause the log functions to call this +function if there is an ultimate disaster. That is why it is globally +accessible. + +Arguments: SMTP response to give if in an SMTP session +Returns: it doesn't +*/ + +void +receive_bomb_out(uschar *msg) +{ +/* If spool_name is set, it contains the name of the data file that is being +written. Unlink it before closing so that it cannot be picked up by a delivery +process. Ensure that any header file is also removed. */ + +if (spool_name[0] != 0) + { + Uunlink(spool_name); + spool_name[Ustrlen(spool_name) - 1] = 'H'; + Uunlink(spool_name); + } + +/* Now close the file if it is open, either as a fd or a stream. */ + +if (data_file != NULL) fclose(data_file); + else if (data_fd >= 0) close(data_fd); + +/* Attempt to close down an SMTP connection tidily. */ + +if (smtp_input) + { + if (!smtp_batched_input) + { + smtp_printf("421 %s %s - closing connection.\r\n", smtp_active_hostname, + msg); + mac_smtp_fflush(); + } + + /* Control does not return from moan_smtp_batch(). */ + + else moan_smtp_batch(NULL, "421 %s - message abandoned", msg); + } + +/* Exit from the program (non-BSMTP cases) */ + +exim_exit(EXIT_FAILURE); +} + + +/************************************************* +* Data read timeout * +*************************************************/ + +/* Handler function for timeouts that occur while reading the data that +comprises a message. + +Argument: the signal number +Returns: nothing +*/ + +static void +data_timeout_handler(int sig) +{ +uschar *msg = NULL; + +sig = sig; /* Keep picky compilers happy */ + +if (smtp_input) + { + msg = US"SMTP incoming data timeout"; + log_write(L_lost_incoming_connection, + LOG_MAIN, "SMTP data timeout (message abandoned) on connection " + "from %s", + (sender_fullhost != NULL)? sender_fullhost : US"local process"); + } +else + { + fprintf(stderr, "exim: timed out while reading - message abandoned\n"); + log_write(L_lost_incoming_connection, + LOG_MAIN, "timed out while reading local message"); + } + +receive_bomb_out(msg); /* Does not return */ +} + + + +/************************************************* +* local_scan() timeout * +*************************************************/ + +/* Handler function for timeouts that occur while running a local_scan() +function. + +Argument: the signal number +Returns: nothing +*/ + +static void +local_scan_timeout_handler(int sig) +{ +sig = sig; /* Keep picky compilers happy */ +log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() function timed out - " + "message temporarily rejected (size %d)", message_size); +receive_bomb_out(US"local verification problem"); /* Does not return */ +} + + + +/************************************************* +* local_scan() crashed * +*************************************************/ + +/* Handler function for signals that occur while running a local_scan() +function. + +Argument: the signal number +Returns: nothing +*/ + +static void +local_scan_crash_handler(int sig) +{ +log_write(0, LOG_MAIN|LOG_REJECT, "local_scan() function crashed with " + "signal %d - message temporarily rejected (size %d)", sig, message_size); +receive_bomb_out(US"local verification problem"); /* Does not return */ +} + + +/************************************************* +* SIGTERM or SIGINT received * +*************************************************/ + +/* Handler for SIGTERM or SIGINT signals that occur while reading the +data that comprises a message. + +Argument: the signal number +Returns: nothing +*/ + +static void +data_sigterm_sigint_handler(int sig) +{ +uschar *msg = NULL; + +if (smtp_input) + { + msg = US"Service not available - SIGTERM or SIGINT received"; + log_write(0, LOG_MAIN, "%s closed after %s", smtp_get_connection_info(), + (sig == SIGTERM)? "SIGTERM" : "SIGINT"); + } +else + { + if (filter_test == NULL) + { + fprintf(stderr, "\nexim: %s received - message abandoned\n", + (sig == SIGTERM)? "SIGTERM" : "SIGINT"); + log_write(0, LOG_MAIN, "%s received while reading local message", + (sig == SIGTERM)? "SIGTERM" : "SIGINT"); + } + } + +receive_bomb_out(msg); /* Does not return */ +} + + + +/************************************************* +* Add new recipient to list * +*************************************************/ + +/* This function builds a list of recipient addresses in argc/argv +format. + +Arguments: + recipient the next address to add to recipients_list + pno parent number for fixed aliases; -1 otherwise + +Returns: nothing +*/ + +void +receive_add_recipient(uschar *recipient, int pno) +{ +if (recipients_count >= recipients_list_max) + { + recipient_item *oldlist = recipients_list; + int oldmax = recipients_list_max; + recipients_list_max = recipients_list_max? 2*recipients_list_max : 50; + recipients_list = store_get(recipients_list_max * sizeof(recipient_item)); + if (oldlist != NULL) + memcpy(recipients_list, oldlist, oldmax * sizeof(recipient_item)); + } + +recipients_list[recipients_count].address = recipient; +recipients_list[recipients_count].pno = pno; +recipients_list[recipients_count++].errors_to = NULL; +} + + + + +/************************************************* +* Remove a recipient from the list * +*************************************************/ + +/* This function is provided for local_scan() to use. + +Argument: + recipient address to remove + +Returns: TRUE if it did remove something; FALSE otherwise +*/ + +BOOL +receive_remove_recipient(uschar *recipient) +{ +int count; +DEBUG(D_receive) debug_printf("receive_remove_recipient(\"%s\") called\n", + recipient); +for (count = 0; count < recipients_count; count++) + { + if (Ustrcmp(recipients_list[count].address, recipient) == 0) + { + if ((--recipients_count - count) > 0) + memmove(recipients_list + count, recipients_list + count + 1, + (recipients_count - count)*sizeof(recipient_item)); + return TRUE; + } + } +return FALSE; +} + + + + + +/************************************************* +* Read data portion of a non-SMTP message * +*************************************************/ + +/* This function is called to read the remainder of a message (following the +header) when the input is not from SMTP - we are receiving a local message on +a standard input stream. The message is always terminated by EOF, and is also +terminated by a dot on a line by itself if the flag dot_ends is TRUE. Split the +two cases for maximum efficiency. + +Ensure that the body ends with a newline. This will naturally be the case when +the termination is "\n.\n" but may not be otherwise. The RFC defines messages +as "sequences of lines" - this of course strictly applies only to SMTP, but +deliveries into BSD-type mailbox files also require it. Exim used to have a +flag for doing this at delivery time, but as it was always set for all +transports, I decided to simplify things by putting the check here instead. + +There is at least one MUA (dtmail) that sends CRLF via this interface, and +other programs are known to do this as well. Exim used to have a option for +dealing with this: in July 2003, after much discussion, the code has been +changed to default to treat any of LF, CRLF, and bare CR as line terminators. + +However, for the case when a dot on a line by itself terminates a message, the +only recognized terminating sequences before and after the dot are LF and CRLF. +Otherwise, having read EOL . CR, you don't know whether to read another +character or not. + +Internally, in messages stored in Exim's spool files, LF is used as the line +terminator. Under the new regime, bare CRs will no longer appear in these +files. + +Arguments: + fout a FILE to which to write the message + +Returns: One of the END_xxx values indicating why it stopped reading +*/ + +static int +read_message_data(FILE *fout) +{ +int ch_state; +register int ch; + +/* Handle the case when only EOF terminates the message */ + +if (!dot_ends) + { + register int last_ch = '\n'; + + for (; (ch = (receive_getc)()) != EOF; last_ch = ch) + { + if (ch == 0) body_zerocount++; + if (last_ch == '\r' && ch != '\n') + { + if (fputc('\n', fout) == EOF) return END_WERROR; + message_size++; + body_linecount++; + } + if (ch == '\r') continue; + + if (fputc(ch, fout) == EOF) return END_WERROR; + if (ch == '\n') body_linecount++; + if (++message_size > thismessage_size_limit) return END_SIZE; + } + + if (last_ch != '\n') + { + if (fputc('\n', fout) == EOF) return END_WERROR; + message_size++; + body_linecount++; + } + + return END_EOF; + } + +/* Handle the case when a dot on a line on its own, or EOF, terminates. */ + +ch_state = 1; + +while ((ch = (receive_getc)()) != EOF) + { + if (ch == 0) body_zerocount++; + switch (ch_state) + { + case 0: /* Normal state (previous char written) */ + if (ch == '\n') + { body_linecount++; ch_state = 1; } + else if (ch == '\r') + { ch_state = 2; continue; } + break; + + case 1: /* After written "\n" */ + if (ch == '.') { ch_state = 3; continue; } + if (ch != '\n') ch_state = 0; + break; + + case 2: + body_linecount++; /* After unwritten "\r" */ + if (ch == '\n') + { ch_state = 1; } + else + { + if (message_size++, fputc('\n', fout) == EOF) return END_WERROR; + if (ch == '\r') continue; + ch_state = 0; + } + break; + + case 3: /* After "\n." (\n written, dot not) */ + if (ch == '\n') return END_DOT; + if (ch == '\r') { ch_state = 4; continue; } + message_size++; + if (fputc('.', fout) == EOF) return END_WERROR; + ch_state = 0; + break; + + case 4: /* After "\n.\r" (\n written, rest not) */ + if (ch == '\n') return END_DOT; + message_size += 2; + body_linecount++; + if (fputs(".\n", fout) == EOF) return END_WERROR; + if (ch == '\r') { ch_state = 2; continue; } + ch_state = 0; + break; + } + + if (fputc(ch, fout) == EOF) return END_WERROR; + if (++message_size > thismessage_size_limit) return END_SIZE; + } + +/* Get here if EOF read. Unless we have just written "\n", we need to ensure +the message ends with a newline, and we must also write any characters that +were saved up while testing for an ending dot. */ + +if (ch_state != 1) + { + static uschar *ends[] = { US"\n", NULL, US"\n", US".\n", US".\n" }; + if (fputs(CS ends[ch_state], fout) == EOF) return END_WERROR; + message_size += Ustrlen(ends[ch_state]); + body_linecount++; + } + +return END_EOF; +} + + + + +/************************************************* +* Read data portion of an SMTP message * +*************************************************/ + +/* This function is called to read the remainder of an SMTP message (after the +headers), or to skip over it when an error has occurred. In this case, the +output file is passed as NULL. + +If any line begins with a dot, that character is skipped. The input should only +be successfully terminated by CR LF . CR LF unless it is local (non-network) +SMTP, in which case the CRs are optional, but... + +FUDGE: It seems that sites on the net send out messages with just LF +terminators, despite the warnings in the RFCs, and other MTAs handle this. So +we make the CRs optional in all cases. + +July 2003: Bare CRs cause trouble. We now treat them as line terminators as +well, so that there are no CRs in spooled messages. However, the message +terminating dot is not recognized between two bare CRs. + +Arguments: + fout a FILE to which to write the message; NULL if skipping + +Returns: One of the END_xxx values indicating why it stopped reading +*/ + +static int +read_message_data_smtp(FILE *fout) +{ +int ch_state = 0; +register int ch; + +while ((ch = (receive_getc)()) != EOF) + { + if (ch == 0) body_zerocount++; + switch (ch_state) + { + case 0: /* After LF or CRLF */ + if (ch == '.') + { + ch_state = 3; + continue; /* Don't ever write . after LF */ + } + ch_state = 1; + + /* Else fall through to handle as normal uschar. */ + + case 1: /* Normal state */ + if (ch == '\n') + { + ch_state = 0; + body_linecount++; + } + else if (ch == '\r') + { + ch_state = 2; + continue; + } + break; + + case 2: /* After (unwritten) CR */ + body_linecount++; + if (ch == '\n') + { + ch_state = 0; + } + else + { + message_size++; + if (fout != NULL && fputc('\n', fout) == EOF) return END_WERROR; + if (ch != '\r') ch_state = 1; else continue; + } + break; + + case 3: /* After [CR] LF . */ + if (ch == '\n') + return END_DOT; + if (ch == '\r') + { + ch_state = 4; + continue; + } + ch_state = 1; /* The dot itself is removed */ + break; + + case 4: /* After [CR] LF . CR */ + if (ch == '\n') return END_DOT; + message_size++; + body_linecount++; + if (fout != NULL && fputc('\n', fout) == EOF) return END_WERROR; + if (ch == '\r') + { + ch_state = 2; + continue; + } + ch_state = 1; + break; + } + + /* Add the character to the spool file, unless skipping; then loop for the + next. */ + + message_size++; + if (fout != NULL) + { + if (fputc(ch, fout) == EOF) return END_WERROR; + if (message_size > thismessage_size_limit) return END_SIZE; + } + } + +/* Fall through here if EOF encountered. This indicates some kind of error, +since a correct message is terminated by [CR] LF . [CR] LF. */ + +return END_EOF; +} + + + + +/************************************************* +* Swallow SMTP message * +*************************************************/ + +/* This function is called when there has been some kind of error while reading +an SMTP message, and the remaining data may need to be swallowed. It is global +because it is called from smtp_closedown() to shut down an incoming call +tidily. + +Argument: a FILE from which to read the message +Returns: nothing +*/ + +void +receive_swallow_smtp(void) +{ +if (message_ended >= END_NOTENDED) + message_ended = read_message_data_smtp(NULL); +} + + + +/************************************************* +* Handle lost SMTP connection * +*************************************************/ + +/* This function logs connection loss incidents and generates an appropriate +SMTP response. + +Argument: additional data for the message +Returns: the SMTP response +*/ + +static uschar * +handle_lost_connection(uschar *s) +{ +log_write(L_lost_incoming_connection | L_smtp_connection, LOG_MAIN, + "%s lost while reading message data%s", smtp_get_connection_info(), s); +return US"421 Lost incoming connection"; +} + + + + +/************************************************* +* Handle a non-smtp reception error * +*************************************************/ + +/* This function is called for various errors during the reception of non-SMTP +messages. It either sends a message to the sender of the problem message, or it +writes to the standard error stream. + +Arguments: + errcode code for moan_to_sender(), identifying the error + text1 first message text, passed to moan_to_sender() + text2 second message text, used only for stderrr + error_rc code to pass to exim_exit if no problem + f FILE containing body of message (may be stdin) + hptr pointer to instore headers or NULL + +Returns: calls exim_exit(), which does not return +*/ + +static void +give_local_error(int errcode, uschar *text1, uschar *text2, int error_rc, + FILE *f, header_line *hptr) +{ +if (error_handling == ERRORS_SENDER) + { + error_block eblock; + eblock.next = NULL; + eblock.text1 = text1; + if (!moan_to_sender(errcode, &eblock, hptr, f, FALSE)) + error_rc = EXIT_FAILURE; + } +else fprintf(stderr, "exim: %s%s\n", text2, text1); /* Sic */ +fclose(f); +exim_exit(error_rc); +} + + + +/************************************************* +* Add header lines set up by ACL * +*************************************************/ + +/* This function is called to add the header lines that were set up by "warn" +statements in an ACL onto the list of headers in memory. It is done in two +stages like this, because when the ACL for RCPT is running, the other headers +have not yet been received. This function is called twice; once just before +running the DATA ACL, and once after. This is so that header lines added by +MAIL or RCPT are visible to the DATA ACL. + +Originally these header lines were added at the end. Now there is support for +three different places: top, bottom, and after the Received: header(s). There +will always be at least one Received: header, even if it is marked deleted, and +even if something else has been put in front of it. + +Arguments: + acl_name text to identify which ACL + +Returns: nothing +*/ + +static void +add_acl_headers(uschar *acl_name) +{ +header_line *h, *next; +header_line *last_received = NULL; + +if (acl_warn_headers == NULL) return; +DEBUG(D_receive|D_acl) debug_printf(">>Headers added by %s ACL:\n", acl_name); + +for (h = acl_warn_headers; h != NULL; h = next) + { + next = h->next; + + switch(h->type) + { + case htype_add_top: + h->next = header_list; + header_list = h; + DEBUG(D_receive|D_acl) debug_printf(" (at top)"); + break; + + case htype_add_rec: + if (last_received == NULL) + { + last_received = header_list; + while (!header_testname(last_received, US"Received", 8, FALSE)) + last_received = last_received->next; + while (last_received->next != NULL && + header_testname(last_received->next, US"Received", 8, FALSE)) + last_received = last_received->next; + } + h->next = last_received->next; + last_received->next = h; + DEBUG(D_receive|D_acl) debug_printf(" (after Received:)"); + break; + + default: + h->next = NULL; + header_last->next = h; + break; + } + + if (h->next == NULL) header_last = h; + + /* Check for one of the known header types (From:, To:, etc.) though in + practice most added headers are going to be "other". Lower case + identification letters are never stored with the header; they are used + for existence tests when messages are received. So discard any lower case + flag values. */ + + h->type = header_checkname(h, FALSE); + if (h->type >= 'a') h->type = htype_other; + + DEBUG(D_receive|D_acl) debug_printf(" %s", header_last->text); + } + +acl_warn_headers = NULL; +DEBUG(D_receive|D_acl) debug_printf(">>\n"); +} + + + +/************************************************* +* Add host information for log line * +*************************************************/ + +/* Called for acceptance and rejecting log lines. This adds information about +the calling host to a string that is being built dynamically. + +Arguments: + s the dynamic string + sizeptr points to the size variable + ptrptr points to the pointer variable + +Returns: the extended string +*/ + +static uschar * +add_host_info_for_log(uschar *s, int *sizeptr, int *ptrptr) +{ +if (sender_fullhost != NULL) + { + s = string_append(s, sizeptr, ptrptr, 2, US" H=", sender_fullhost); + if ((log_extra_selector & LX_incoming_interface) != 0 && + interface_address != NULL) + { + uschar *ss = string_sprintf(" I=[%s]:%d", interface_address, + interface_port); + s = string_cat(s, sizeptr, ptrptr, ss, Ustrlen(ss)); + } + } +if (sender_ident != NULL) + s = string_append(s, sizeptr, ptrptr, 2, US" U=", sender_ident); +if (received_protocol != NULL) + s = string_append(s, sizeptr, ptrptr, 2, US" P=", received_protocol); +return s; +} + + + + +/************************************************* +* Receive message * +*************************************************/ + +/* Receive a message on the given input, and put it into a pair of spool files. +Either a non-null list of recipients, or the extract flag will be true, or +both. The flag sender_local is true for locally generated messages. The flag +submission_mode is true if an ACL has obeyed "control = submission". The flag +smtp_input is true if the message is to be handled using SMTP conventions about +termination and lines starting with dots. For non-SMTP messages, dot_ends is +true for dot-terminated messages. + +If a message was successfully read, message_id[0] will be non-zero. + +The general actions of this function are: + + . Read the headers of the message (if any) into a chain of store + blocks. + + . If there is a "sender:" header and the message is locally originated, + throw it away, unless the caller is trusted, or unless local_sender_retain + is set - which can only happen if local_from_check is false. + + . If recipients are to be extracted from the message, build the + recipients list from the headers, removing any that were on the + original recipients list (unless extract_addresses_remove_arguments is + false), and at the same time, remove any bcc header that may be present. + + . Get the spool file for the data, sort out its unique name, open + and lock it (but don't give it the name yet). + + . Generate a "Message-Id" header if the message doesn't have one, for + locally-originated messages. + + . Generate a "Received" header. + + . Ensure the recipients list is fully qualified and rewritten if necessary. + + . If there are any rewriting rules, apply them to the sender address + and also to the headers. + + . If there is no from: header, generate one, for locally-generated messages + and messages in "submission mode" only. + + . If the sender is local, check that from: is correct, and if not, generate + a Sender: header, unless message comes from a trusted caller, or this + feature is disabled by no_local_from_check. + + . If there is no "date" header, generate one, for locally-originated + or submission mode messages only. + + . Copy the rest of the input, or up to a terminating "." if in SMTP or + dot_ends mode, to the data file. Leave it open, to hold the lock. + + . Write the envelope and the headers to a new file. + + . Set the name for the header file; close it. + + . Set the name for the data file; close it. + +Because this function can potentially be called many times in a single +SMTP connection, all store should be got by store_get(), so that it will be +automatically retrieved after the message is accepted. + +FUDGE: It seems that sites on the net send out messages with just LF +terminators, despite the warnings in the RFCs, and other MTAs handle this. So +we make the CRs optional in all cases. + +July 2003: Bare CRs in messages, especially in header lines, cause trouble. A +new regime is now in place in which bare CRs in header lines are turned into LF +followed by a space, so as not to terminate the header line. + +February 2004: A bare LF in a header line in a message whose first line was +terminated by CRLF is treated in the same way as a bare CR. + +Arguments: + extract_recip TRUE if recipients are to be extracted from the message's + headers + +Returns: TRUE there are more messages to be read (SMTP input) + FALSE there are no more messages to be read (non-SMTP input + or SMTP connection collapsed, or other failure) + +When reading a message for filter testing, the returned value indicates +whether the headers (which is all that is read) were terminated by '.' or +not. */ + +BOOL +receive_msg(BOOL extract_recip) +{ +int i, rc; +int msg_size = 0; +int process_info_len = Ustrlen(process_info); +int error_rc = (error_handling == ERRORS_SENDER)? + errors_sender_rc : EXIT_FAILURE; +int header_size = 256; +int start, end, domain, size, sptr; +int id_resolution; +int had_zero = 0; + +register int ptr = 0; + +BOOL contains_resent_headers = FALSE; +BOOL extracted_ignored = FALSE; +BOOL first_line_ended_crlf = TRUE_UNSET; +BOOL smtp_yield = TRUE; +BOOL yield = FALSE; + +BOOL resents_exist = FALSE; +uschar *resent_prefix = US""; +uschar *blackholed_by = NULL; + +flock_t lock_data; +error_block *bad_addresses = NULL; + +uschar *frozen_by = NULL; +uschar *queued_by = NULL; + +uschar *errmsg, *s; +struct stat statbuf; + +/* Final message to give to SMTP caller */ + +uschar *smtp_reply = NULL; + +/* Working header pointers */ + +header_line *h, *next; + +/* Flags for noting the existence of certain headers */ + +/**** No longer check for these (Nov 2003) +BOOL to_or_cc_header_exists = FALSE; +BOOL bcc_header_exists = FALSE; +****/ + +BOOL date_header_exists = FALSE; + +/* Pointers to receive the addresses of headers whose contents we need. */ + +header_line *from_header = NULL; +header_line *subject_header = NULL; +header_line *msgid_header = NULL; +header_line *received_header; + +/* Variables for use when building the Received: header. */ + +uschar *received; +uschar *timestamp; +int tslen; + +/* Release any open files that might have been cached while preparing to +accept the message - e.g. by verifying addresses - because reading a message +might take a fair bit of real time. */ + +search_tidyup(); + +/* Initialize the chain of headers by setting up a place-holder for Received: +header. Temporarily mark it as "old", i.e. not to be used. We keep header_last +pointing to the end of the chain to make adding headers simple. */ + +received_header = header_list = header_last = store_get(sizeof(header_line)); +header_list->next = NULL; +header_list->type = htype_old; +header_list->text = NULL; +header_list->slen = 0; + +/* Control block for the next header to be read. */ + +next = store_get(sizeof(header_line)); +next->text = store_get(header_size); + +/* Initialize message id to be null (indicating no message read), and the +header names list to be the normal list. Indicate there is no data file open +yet, initialize the size and warning count, and deal with no size limit. */ + +message_id[0] = 0; +data_file = NULL; +data_fd = -1; +spool_name[0] = 0; +message_size = 0; +warning_count = 0; +received_count = 1; /* For the one we will add */ + +if (thismessage_size_limit <= 0) thismessage_size_limit = INT_MAX; + +/* While reading the message, body_linecount and body_zerocount is computed. +The full message_ linecount is set up only when the headers are read back in +from the spool for delivery. */ + +body_linecount = body_zerocount = 0; + +/* Remember the time of reception. Exim uses time+pid for uniqueness of message +ids, and fractions of a second are required. See the comments that precede the +message id creation below. */ + +(void)gettimeofday(&message_id_tv, NULL); + +/* For other uses of the received time we can operate with granularity of one +second, and for that we use the global variable received_time. This is for +things like ultimate message timeouts. */ + +received_time = message_id_tv.tv_sec; + +/* If SMTP input, set the special handler for timeouts. The alarm() calls +happen in the smtp_getc() function when it refills its buffer. */ + +if (smtp_input) os_non_restarting_signal(SIGALRM, data_timeout_handler); + +/* If not SMTP input, timeout happens only if configured, and we just set a +single timeout for the whole message. */ + +else if (receive_timeout > 0) + { + os_non_restarting_signal(SIGALRM, data_timeout_handler); + alarm(receive_timeout); + } + +/* SIGTERM and SIGINT are caught always. */ + +signal(SIGTERM, data_sigterm_sigint_handler); +signal(SIGINT, data_sigterm_sigint_handler); + +/* Header lines in messages are not supposed to be very long, though when +unfolded, to: and cc: headers can take up a lot of store. We must also cope +with the possibility of junk being thrown at us. Start by getting 256 bytes for +storing the header, and extend this as necessary using string_cat(). + +To cope with total lunacies, impose an upper limit on the length of the header +section of the message, as otherwise the store will fill up. We must also cope +with the possibility of binary zeros in the data. Hence we cannot use fgets(). +Folded header lines are joined into one string, leaving the '\n' characters +inside them, so that writing them out reproduces the input. + +Loop for each character of each header; the next structure for chaining the +header is set up already, with ptr the offset of the next character in +next->text. */ + +for (;;) + { + int ch = (receive_getc)(); + + /* If we hit EOF on a SMTP connection, it's an error, since incoming + SMTP must have a correct "." terminator. */ + + if (ch == EOF && smtp_input /* && !smtp_batched_input */) + { + smtp_reply = handle_lost_connection(US" (header)"); + smtp_yield = FALSE; + goto TIDYUP; /* Skip to end of function */ + } + + /* See if we are at the current header's size limit - there must be at least + four bytes left. This allows for the new character plus a zero, plus two for + extra insertions when we are playing games with dots and carriage returns. If + we are at the limit, extend the text buffer. This could have been done + automatically using string_cat() but because this is a tightish loop storing + only one character at a time, we choose to do it inline. Normally + store_extend() will be able to extend the block; only at the end of a big + store block will a copy be needed. To handle the case of very long headers + (and sometimes lunatic messages can have ones that are 100s of K long) we + call store_release() for strings that have been copied - if the string is at + the start of a block (and therefore the only thing in it, because we aren't + doing any other gets), the block gets freed. We can only do this because we + know there are no other calls to store_get() going on. */ + + if (ptr >= header_size - 4) + { + int oldsize = header_size; + /* header_size += 256; */ + header_size *= 2; + if (!store_extend(next->text, oldsize, header_size)) + { + uschar *newtext = store_get(header_size); + memcpy(newtext, next->text, ptr); + store_release(next->text); + next->text = newtext; + } + } + + /* Cope with receiving a binary zero. There is dispute about whether + these should be allowed in RFC 822 messages. The middle view is that they + should not be allowed in headers, at least. Exim takes this attitude at + the moment. We can't just stomp on them here, because we don't know that + this line is a header yet. Set a flag to cause scanning later. */ + + if (ch == 0) had_zero++; + + /* Test for termination. Lines in remote SMTP are terminated by CRLF, while + those from data files use just LF. Treat LF in local SMTP input as a + terminator too. Treat EOF as a line terminator always. */ + + if (ch == EOF) goto EOL; + + /* FUDGE: There are sites out there that don't send CRs before their LFs, and + other MTAs accept this. We are therefore forced into this "liberalisation" + too, so we accept LF as a line terminator whatever the source of the message. + However, if the first line of the message ended with a CRLF, we treat a bare + LF specially by inserting a white space after it to ensure that the header + line is not terminated. */ + + if (ch == '\n') + { + if (first_line_ended_crlf == TRUE_UNSET) first_line_ended_crlf = FALSE; + else if (first_line_ended_crlf) receive_ungetc(' '); + goto EOL; + } + + /* This is not the end of the line. If this is SMTP input and this is + the first character in the line and it is a "." character, ignore it. + This implements the dot-doubling rule, though header lines starting with + dots aren't exactly common. They are legal in RFC 822, though. If the + following is CRLF or LF, this is the line that that terminates the + entire message. We set message_ended to indicate this has happened (to + prevent further reading), and break out of the loop, having freed the + empty header, and set next = NULL to indicate no data line. */ + + if (ptr == 0 && ch == '.' && (smtp_input || dot_ends)) + { + ch = (receive_getc)(); + if (ch == '\r') + { + ch = (receive_getc)(); + if (ch != '\n') + { + receive_ungetc(ch); + ch = '\r'; /* Revert to CR */ + } + } + if (ch == '\n') + { + message_ended = END_DOT; + store_reset(next); + next = NULL; + break; /* End character-reading loop */ + } + + /* For non-SMTP input, the dot at the start of the line was really a data + character. What is now in ch is the following character. We guaranteed + enough space for this above. */ + + if (!smtp_input) + { + next->text[ptr++] = '.'; + message_size++; + } + } + + /* If CR is immediately followed by LF, end the line, ignoring the CR, and + remember this case if this is the first line ending. */ + + if (ch == '\r') + { + ch = (receive_getc)(); + if (ch == '\n') + { + if (first_line_ended_crlf == TRUE_UNSET) first_line_ended_crlf = TRUE; + goto EOL; + } + + /* Otherwise, put back the character after CR, and turn the bare CR + into LF SP. */ + + ch = (receive_ungetc)(ch); + next->text[ptr++] = '\n'; + message_size++; + ch = ' '; + } + + /* We have a data character for the header line. */ + + next->text[ptr++] = ch; /* Add to buffer */ + message_size++; /* Total message size so far */ + + /* Handle failure due to a humungously long header section. The >= allows + for the terminating \n. Add what we have so far onto the headers list so + that it gets reflected in any error message, and back up the just-read + character. */ + + if (message_size >= header_maxsize) + { + next->text[ptr] = 0; + next->slen = ptr; + next->type = htype_other; + next->next = NULL; + header_last->next = next; + header_last = next; + + log_write(0, LOG_MAIN, "ridiculously long message header received from " + "%s (more than %d characters): message abandoned", + sender_host_unknown? sender_ident : sender_fullhost, header_maxsize); + + if (smtp_input) + { + smtp_reply = US"552 Message header is ridiculously long"; + receive_swallow_smtp(); + goto TIDYUP; /* Skip to end of function */ + } + + else + { + give_local_error(ERRMESS_VLONGHEADER, + string_sprintf("message header longer than %d characters received: " + "message not accepted", header_maxsize), US"", error_rc, stdin, + header_list->next); + /* Does not return */ + } + } + + continue; /* With next input character */ + + /* End of header line reached */ + + EOL: + receive_linecount++; /* For BSMTP errors */ + + /* Now put in the terminating newline. There is always space for + at least two more characters. */ + + next->text[ptr++] = '\n'; + message_size++; + + /* A blank line signals the end of the headers; release the unwanted + space and set next to NULL to indicate this. */ + + if (ptr == 1) + { + store_reset(next); + next = NULL; + break; + } + + /* There is data in the line; see if the next input character is a + whitespace character. If it is, we have a continuation of this header line. + There is always space for at least one character at this point. */ + + if (ch != EOF) + { + int nextch = (receive_getc)(); + if (nextch == ' ' || nextch == '\t') + { + next->text[ptr++] = nextch; + message_size++; + continue; /* Iterate the loop */ + } + else if (nextch != EOF) (receive_ungetc)(nextch); /* For next time */ + else ch = EOF; /* Cause main loop to exit at end */ + } + + /* We have got to the real line end. Terminate the string and release store + beyond it. If it turns out to be a real header, internal binary zeros will + be squashed later. */ + + next->text[ptr] = 0; + next->slen = ptr; + store_reset(next->text + ptr + 1); + + /* Check the running total size against the overall message size limit. We + don't expect to fail here, but if the overall limit is set less than MESSAGE_ + MAXSIZE and a big header is sent, we want to catch it. Just stop reading + headers - the code to read the body will then also hit the buffer. */ + + if (message_size > thismessage_size_limit) break; + + /* A line that is not syntactically correct for a header also marks + the end of the headers. In this case, we leave next containing the + first data line. This might actually be several lines because of the + continuation logic applied above, but that doesn't matter. + + It turns out that smail, and presumably sendmail, accept leading lines + of the form + + From ph10 Fri Jan 5 12:35 GMT 1996 + + in messages. The "mail" command on Solaris 2 sends such lines. I cannot + find any documentation of this, but for compatibility it had better be + accepted. Exim restricts it to the case of non-smtp messages, and + treats it as an alternative to the -f command line option. Thus it is + ignored except for trusted users or filter testing. Otherwise it is taken + as the sender address, unless -f was used (sendmail compatibility). + + It further turns out that some UUCPs generate the From_line in a different + format, e.g. + + From ph10 Fri, 7 Jan 97 14:00:00 GMT + + The regex for matching these things is now capable of recognizing both + formats (including 2- and 4-digit years in the latter). In fact, the regex + is now configurable, as is the expansion string to fish out the sender. + + Even further on it has been discovered that some broken clients send + these lines in SMTP messages. There is now an option to ignore them from + specified hosts or networks. Sigh. */ + + if (header_last == header_list && + (!smtp_input + || + (sender_host_address != NULL && + verify_check_host(&ignore_fromline_hosts) == OK) + || + (sender_host_address == NULL && ignore_fromline_local) + ) && + regex_match_and_setup(regex_From, next->text, 0, -1)) + { + if (!sender_address_forced) + { + uschar *uucp_sender = expand_string(uucp_from_sender); + if (uucp_sender == NULL) + { + log_write(0, LOG_MAIN|LOG_PANIC, + "expansion of \"%s\" failed after matching " + "\"From \" line: %s", uucp_from_sender, expand_string_message); + } + else + { + int start, end, domain; + uschar *errmess; + uschar *newsender = parse_extract_address(uucp_sender, &errmess, + &start, &end, &domain, TRUE); + if (newsender != NULL) + { + if (domain == 0 && newsender[0] != 0) + newsender = rewrite_address_qualify(newsender, FALSE); + + if (filter_test != NULL || receive_check_set_sender(newsender)) + { + sender_address = newsender; + + if (trusted_caller || filter_test != NULL) + { + authenticated_sender = NULL; + originator_name = US""; + sender_local = FALSE; + } + + if (filter_test != NULL) + printf("Sender taken from \"From \" line\n"); + } + } + } + } + } + + /* Not a leading "From " line. Check to see if it is a valid header line. + Header names may contain any non-control characters except space and colon, + amazingly. */ + + else + { + uschar *p = next->text; + + /* If not a valid header line, break from the header reading loop, leaving + next != NULL, indicating that it holds the first line of the body. */ + + if (isspace(*p)) break; + while (mac_isgraph(*p) && *p != ':') p++; + while (isspace(*p)) p++; + if (*p != ':') + { + body_zerocount = had_zero; + break; + } + + /* We have a valid header line. If there were any binary zeroes in + the line, stomp on them here. */ + + if (had_zero > 0) + for (p = next->text; p < next->text + ptr; p++) if (*p == 0) *p = '?'; + + /* It is perfectly legal to have an empty continuation line + at the end of a header, but it is confusing to humans + looking at such messages, since it looks like a blank line. + Reduce confusion by removing redundant white space at the + end. We know that there is at least one printing character + (the ':' tested for above) so there is no danger of running + off the end. */ + + p = next->text + ptr - 2; + for (;;) + { + while (*p == ' ' || *p == '\t') p--; + if (*p != '\n') break; + ptr = (p--) - next->text + 1; + message_size -= next->slen - ptr; + next->text[ptr] = 0; + next->slen = ptr; + } + + /* Add the header to the chain */ + + next->type = htype_other; + next->next = NULL; + header_last->next = next; + header_last = next; + + /* Check the limit for individual line lengths. This comes after adding to + the chain so that the failing line is reflected if a bounce is generated + (for a local message). */ + + if (header_line_maxsize > 0 && next->slen > header_line_maxsize) + { + log_write(0, LOG_MAIN, "overlong message header line received from " + "%s (more than %d characters): message abandoned", + sender_host_unknown? sender_ident : sender_fullhost, + header_line_maxsize); + + if (smtp_input) + { + smtp_reply = US"552 A message header line is too long"; + receive_swallow_smtp(); + goto TIDYUP; /* Skip to end of function */ + } + + else + { + give_local_error(ERRMESS_VLONGHDRLINE, + string_sprintf("message header line longer than %d characters " + "received: message not accepted", header_line_maxsize), US"", + error_rc, stdin, header_list->next); + /* Does not return */ + } + } + + /* Note if any resent- fields exist. */ + + if (!resents_exist && strncmpic(next->text, US"resent-", 7) == 0) + { + resents_exist = TRUE; + resent_prefix = US"Resent-"; + } + } + + /* The line has been handled. If we have hit EOF, break out of the loop, + indicating no pending data line. */ + + if (ch == EOF) { next = NULL; break; } + + /* Set up for the next header */ + + header_size = 256; + next = store_get(sizeof(header_line)); + next->text = store_get(header_size); + ptr = 0; + had_zero = 0; + } /* Continue, starting to read the next header */ + +/* At this point, we have read all the headers into a data structure in main +store. The first header is still the dummy placeholder for the Received: header +we are going to generate a bit later on. If next != NULL, it contains the first +data line - which terminated the headers before reaching a blank line (not the +normal case). */ + +DEBUG(D_receive) + { + debug_printf(">>Headers received:\n"); + for (h = header_list->next; h != NULL; h = h->next) + debug_printf("%s", h->text); + debug_printf("\n"); + } + +/* End of file on any SMTP connection is an error. If an incoming SMTP call +is dropped immediately after valid headers, the next thing we will see is EOF. +We must test for this specially, as further down the reading of the data is +skipped if already at EOF. */ + +if (smtp_input && (receive_feof)()) + { + smtp_reply = handle_lost_connection(US" (after header)"); + smtp_yield = FALSE; + goto TIDYUP; /* Skip to end of function */ + } + +/* If this is a filter test run and no headers were read, output a warning +in case there is a mistake in the test message. */ + +if (filter_test != NULL && header_list->next == NULL) + printf("Warning: no message headers read\n"); + + +/* Scan the headers to identify them. Some are merely marked for later +processing; some are dealt with here. */ + +for (h = header_list->next; h != NULL; h = h->next) + { + BOOL is_resent = strncmpic(h->text, US"resent-", 7) == 0; + if (is_resent) contains_resent_headers = TRUE; + + switch (header_checkname(h, is_resent)) + { + /* "Bcc:" gets flagged, and its existence noted, whether it's resent- or + not. */ + + case htype_bcc: + h->type = htype_bcc; + /**** + bcc_header_exists = TRUE; + ****/ + break; + + /* "Cc:" gets flagged, and the existence of a recipient header is noted, + whether it's resent- or not. */ + + case htype_cc: + h->type = htype_cc; + /**** + to_or_cc_header_exists = TRUE; + ****/ + break; + + /* Record whether a Date: or Resent-Date: header exists, as appropriate. */ + + case htype_date: + date_header_exists = !resents_exist || is_resent; + break; + + /* Same comments as about Return-Path: below. */ + + case htype_delivery_date: + if (delivery_date_remove) h->type = htype_old; + break; + + /* Same comments as about Return-Path: below. */ + + case htype_envelope_to: + if (envelope_to_remove) h->type = htype_old; + break; + + /* Mark all "From:" headers so they get rewritten. Save the one that is to + be used for Sender: checking. For Sendmail compatibility, if the "From:" + header consists of just the login id of the user who called Exim, rewrite + it with the gecos field first. Apply this rule to Resent-From: if there + are resent- fields. */ + + case htype_from: + h->type = htype_from; + if (!resents_exist || is_resent) + { + from_header = h; + if (!smtp_input) + { + uschar *s = Ustrchr(h->text, ':') + 1; + while (isspace(*s)) s++; + if (strncmpic(s, originator_login, h->slen - (s - h->text) - 1) == 0) + { + uschar *name = is_resent? US"Resent-From" : US"From"; + header_add(htype_from, "%s: %s <%s@%s>\n", name, originator_name, + originator_login, qualify_domain_sender); + from_header = header_last; + h->type = htype_old; + DEBUG(D_receive|D_rewrite) + debug_printf("rewrote \"%s:\" header using gecos\n", name); + } + } + } + break; + + /* Identify the Message-id: header for generating "in-reply-to" in the + autoreply transport. For incoming logging, save any resent- value. In both + cases, take just the first of any multiples. */ + + case htype_id: + if (msgid_header == NULL && (!resents_exist || is_resent)) + { + msgid_header = h; + h->type = htype_id; + } + break; + + /* Flag all Received: headers */ + + case htype_received: + h->type = htype_received; + received_count++; + break; + + /* "Reply-to:" is just noted (there is no resent-reply-to field) */ + + case htype_reply_to: + h->type = htype_reply_to; + break; + + /* The Return-path: header is supposed to be added to messages when + they leave the SMTP system. We shouldn't receive messages that already + contain Return-path. However, since Exim generates Return-path: on + local delivery, resent messages may well contain it. We therefore + provide an option (which defaults on) to remove any Return-path: headers + on input. Removal actually means flagging as "old", which prevents the + header being transmitted with the message. */ + + case htype_return_path: + if (return_path_remove) h->type = htype_old; + + /* If we are testing a mail filter file, use the value of the + Return-Path: header to set up the return_path variable, which is not + otherwise set. However, remove any <> that surround the address + because the variable doesn't have these. */ + + if (filter_test != NULL) + { + uschar *start = h->text + 12; + uschar *end = start + Ustrlen(start); + while (isspace(*start)) start++; + while (end > start && isspace(end[-1])) end--; + if (*start == '<' && end[-1] == '>') + { + start++; + end--; + } + return_path = string_copyn(start, end - start); + printf("Return-path taken from \"Return-path:\" header line\n"); + } + break; + + /* If there is a "Sender:" header and the message is locally originated, + and from an untrusted caller, or if we are in submission mode for a remote + message, mark it "old" so that it will not be transmitted with the message, + unless local_sender_retain is set. (This can only be true if + local_from_check is false.) If there are any resent- headers in the + message, apply this rule to Resent-Sender: instead of Sender:. Messages + with multiple resent- header sets cannot be tidily handled. (For this + reason, at least one MUA - Pine - turns old resent- headers into X-resent- + headers when resending, leaving just one set.) */ + + case htype_sender: + h->type = (( + (sender_local && !trusted_caller && !local_sender_retain) || + submission_mode + ) && + (!resents_exist||is_resent))? + htype_old : htype_sender; + break; + + /* Remember the Subject: header for logging. There is no Resent-Subject */ + + case htype_subject: + subject_header = h; + break; + + /* "To:" gets flagged, and the existence of a recipient header is noted, + whether it's resent- or not. */ + + case htype_to: + h->type = htype_to; + /**** + to_or_cc_header_exists = TRUE; + ****/ + break; + } + } + +/* Extract recipients from the headers if that is required (the -t option). +Note that this is documented as being done *before* any address rewriting takes +place. There are two possibilities: + +(1) According to sendmail documentation for Solaris, IRIX, and HP-UX, any +recipients already listed are to be REMOVED from the message. Smail 3 works +like this. We need to build a non-recipients tree for that list, because in +subsequent processing this data is held in a tree and that's what the +spool_write_header() function expects. Make sure that non-recipient addresses +are fully qualified and rewritten if necessary. + +(2) According to other sendmail documentation, -t ADDS extracted recipients to +those in the command line arguments (and it is rumoured some other MTAs do +this). Therefore, there is an option to make Exim behave this way. + +*** Notes on "Resent-" header lines *** + +The presence of resent-headers in the message makes -t horribly ambiguous. +Experiments with sendmail showed that it uses recipients for all resent- +headers, totally ignoring the concept of "sets of resent- headers" as described +in RFC 2822 section 3.6.6. Sendmail also amalgamates them into a single set +with all the addresses in one instance of each header. + +This seems to me not to be at all sensible. Before release 4.20, Exim 4 gave an +error for -t if there were resent- headers in the message. However, after a +discussion on the mailing list, I've learned that there are MUAs that use +resent- headers with -t, and also that the stuff about sets of resent- headers +and their ordering in RFC 2822 is generally ignored. An MUA that submits a +message with -t and resent- header lines makes sure that only *its* resent- +headers are present; previous ones are often renamed as X-resent- for example. + +Consequently, Exim has been changed so that, if any resent- header lines are +present, the recipients are taken from all of the appropriate resent- lines, +and not from the ordinary To:, Cc:, etc. */ + +if (extract_recip) + { + int rcount = 0; + error_block **bnext = &bad_addresses; + + if (extract_addresses_remove_arguments) + { + while (recipients_count-- > 0) + { + uschar *s = rewrite_address(recipients_list[recipients_count].address, + TRUE, TRUE, global_rewrite_rules, rewrite_existflags); + tree_add_nonrecipient(s); + } + recipients_list = NULL; + recipients_count = recipients_list_max = 0; + } + + parse_allow_group = TRUE; /* Allow address group syntax */ + + /* Now scan the headers */ + + for (h = header_list->next; h != NULL; h = h->next) + { + if ((h->type == htype_to || h->type == htype_cc || h->type == htype_bcc) && + (!contains_resent_headers || strncmpic(h->text, US"resent-", 7) == 0)) + { + uschar *s = Ustrchr(h->text, ':') + 1; + while (isspace(*s)) s++; + + while (*s != 0) + { + uschar *ss = parse_find_address_end(s, FALSE); + uschar *recipient, *errmess, *p, *pp; + int start, end, domain; + + /* Check on maximum */ + + if (recipients_max > 0 && ++rcount > recipients_max) + { + give_local_error(ERRMESS_TOOMANYRECIP, US"too many recipients", + US"message rejected: ", error_rc, stdin, NULL); + /* Does not return */ + } + + /* Make a copy of the address, and remove any internal newlines. These + may be present as a result of continuations of the header line. The + white space that follows the newline must not be removed - it is part + of the header. */ + + pp = recipient = store_get(ss - s + 1); + for (p = s; p < ss; p++) if (*p != '\n') *pp++ = *p; + *pp = 0; + recipient = parse_extract_address(recipient, &errmess, &start, &end, + &domain, FALSE); + + /* Keep a list of all the bad addresses so we can send a single + error message at the end. However, an empty address is not an error; + just ignore it. This can come from an empty group list like + + To: Recipients of list:; + + If there are no recipients at all, an error will occur later. */ + + if (recipient == NULL && Ustrcmp(errmess, "empty address") != 0) + { + int len = Ustrlen(s); + error_block *b = store_get(sizeof(error_block)); + while (len > 0 && isspace(s[len-1])) len--; + b->next = NULL; + b->text1 = string_printing(string_copyn(s, len)); + b->text2 = errmess; + *bnext = b; + bnext = &(b->next); + } + + /* If the recipient is already in the nonrecipients tree, it must + have appeared on the command line with the option extract_addresses_ + remove_arguments set. Do not add it to the recipients, and keep a note + that this has happened, in order to give a better error if there are + no recipients left. */ + + else if (recipient != NULL) + { + if (tree_search(tree_nonrecipients, recipient) == NULL) + receive_add_recipient(recipient, -1); + else + extracted_ignored = TRUE; + } + + /* Move on past this address */ + + s = ss + (*ss? 1:0); + while (isspace(*s)) s++; + } + + /* If this was the bcc: header, mark it "old", which means it + will be kept on the spool, but not transmitted as part of the + message. */ + + if (h->type == htype_bcc) + { + h->type = htype_old; + /**** + bcc_header_exists = FALSE; + ****/ + } + } /* For appropriate header line */ + } /* For each header line */ + + parse_allow_group = FALSE; /* Reset group syntax flags */ + parse_found_group = FALSE; + } + +/* Now build the unique message id. This has changed several times over the +lifetime of Exim. This description was rewritten for Exim 4.14 (February 2003). +Retaining all the history in the comment has become too unwieldy - read +previous release sources if you want it. + +The message ID has 3 parts: tttttt-pppppp-ss. Each part is a number in base 62. +The first part is the current time, in seconds. The second part is the current +pid. Both are large enough to hold 32-bit numbers in base 62. The third part +can hold a number in the range 0-3843. It used to be a computed sequence +number, but is now the fractional component of the current time in units of +1/2000 of a second (i.e. a value in the range 0-1999). After a message has been +received, Exim ensures that the timer has ticked at the appropriate level +before proceeding, to avoid duplication if the pid happened to be re-used +within the same time period. It seems likely that most messages will take at +least half a millisecond to be received, so no delay will normally be +necessary. At least for some time... + +There is a modification when localhost_number is set. Formerly this was allowed +to be as large as 255. Now it is restricted to the range 0-16, and the final +component of the message id becomes (localhost_number * 200) + fractional time +in units of 1/200 of a second (i.e. a value in the range 0-3399). + +Some not-really-Unix operating systems use case-insensitive file names (Darwin, +Cygwin). For these, we have to use base 36 instead of base 62. Luckily, this +still allows the tttttt field to hold a large enough number to last for some +more decades, and the final two-digit field can hold numbers up to 1295, which +is enough for milliseconds (instead of 1/2000 of a second). + +However, the pppppp field cannot hold a 32-bit pid, but it can hold a 31-bit +pid, so it is probably safe because pids have to be positive. The +localhost_number is restricted to 0-10 for these hosts, and when it is set, the +final field becomes (localhost_number * 100) + fractional time in centiseconds. + +Note that string_base62() returns its data in a static storage block, so it +must be copied before calling string_base62() again. It always returns exactly +6 characters. + +There doesn't seem to be anything in the RFC which requires a message id to +start with a letter, but Smail was changed to ensure this. The external form of +the message id (as supplied by string expansion) therefore starts with an +additional leading 'E'. The spool file names do not include this leading +letter and it is not used internally. + +NOTE: If ever the format of message ids is changed, the regular expression for +checking that a string is in this format must be updated in a corresponding +way. It appears in the initializing code in exim.c. The macro MESSAGE_ID_LENGTH +must also be changed to reflect the correct string length. Then, of course, +other programs that rely on the message id format will need updating too. */ + +Ustrncpy(message_id, string_base62((long int)(message_id_tv.tv_sec)), 6); +message_id[6] = '-'; +Ustrncpy(message_id + 7, string_base62((long int)getpid()), 6); + +/* Deal with the case where the host number is set. The value of the number was +checked when it was read, to ensure it isn't too big. The timing granularity is +left in id_resolution so that an appropriate wait can be done after receiving +the message, if necessary (we hope it won't be). */ + +if (host_number_string != NULL) + { + id_resolution = (BASE_62 == 62)? 5000 : 10000; + sprintf(CS(message_id + MESSAGE_ID_LENGTH - 3), "-%2s", + string_base62((long int)( + host_number * (1000000/id_resolution) + + message_id_tv.tv_usec/id_resolution)) + 4); + } + +/* Host number not set: final field is just the fractional time at an +appropriate resolution. */ + +else + { + id_resolution = (BASE_62 == 62)? 500 : 1000; + sprintf(CS(message_id + MESSAGE_ID_LENGTH - 3), "-%2s", + string_base62((long int)(message_id_tv.tv_usec/id_resolution)) + 4); + } + +/* Add the current message id onto the current process info string if +it will fit. */ + +(void)string_format(process_info + process_info_len, + PROCESS_INFO_SIZE - process_info_len, " id=%s", message_id); + +/* If we are using multiple input directories, set up the one for this message +to be the least significant base-62 digit of the time of arrival. Otherwise +ensure that it is an empty string. */ + +message_subdir[0] = split_spool_directory? message_id[5] : 0; + +/* Now that we have the message-id, if there is no message-id: header, generate +one, but only for local or submission mode messages. This can be +user-configured if required, but we had better flatten any illegal characters +therein. */ + +if (msgid_header == NULL && (sender_host_address == NULL || submission_mode)) + { + uschar *p; + uschar *id_text = US""; + uschar *id_domain = primary_hostname; + + /* Permit only letters, digits, dots, and hyphens in the domain */ + + if (message_id_domain != NULL) + { + uschar *new_id_domain = expand_string(message_id_domain); + if (new_id_domain == NULL) + { + if (!expand_string_forcedfail) + log_write(0, LOG_MAIN|LOG_PANIC, + "expansion of \"%s\" (message_id_header_domain) " + "failed: %s", message_id_domain, expand_string_message); + } + else if (*new_id_domain != 0) + { + id_domain = new_id_domain; + for (p = id_domain; *p != 0; p++) + if (!isalnum(*p) && *p != '.') *p = '-'; /* No need to test '-' ! */ + } + } + + /* Permit all characters except controls and RFC 2822 specials in the + additional text part. */ + + if (message_id_text != NULL) + { + uschar *new_id_text = expand_string(message_id_text); + if (new_id_text == NULL) + { + if (!expand_string_forcedfail) + log_write(0, LOG_MAIN|LOG_PANIC, + "expansion of \"%s\" (message_id_header_text) " + "failed: %s", message_id_text, expand_string_message); + } + else if (*new_id_text != 0) + { + id_text = new_id_text; + for (p = id_text; *p != 0; p++) + if (mac_iscntrl_or_special(*p)) *p = '-'; + } + } + + /* Add the header line */ + + header_add(htype_id, "%sMessage-Id: <%s%s%s@%s>\n", resent_prefix, + message_id_external, (*id_text == 0)? "" : ".", id_text, id_domain); + } + +/* If we are to log recipients, keep a copy of the raw ones before any possible +rewriting. Must copy the count, because later ACLs and the local_scan() +function may mess with the real recipients. */ + +if ((log_extra_selector & LX_received_recipients) != 0) + { + raw_recipients = store_get(recipients_count * sizeof(uschar *)); + for (i = 0; i < recipients_count; i++) + raw_recipients[i] = string_copy(recipients_list[i].address); + raw_recipients_count = recipients_count; + } + +/* Ensure the recipients list is fully qualified and rewritten. Unqualified +recipients will get here only if the conditions were right (allow_unqualified_ +recipient is TRUE). */ + +for (i = 0; i < recipients_count; i++) + recipients_list[i].address = + rewrite_address(recipients_list[i].address, TRUE, TRUE, + global_rewrite_rules, rewrite_existflags); + +/* If there is no From: header, generate one for local or submission_mode +messages. If there is no sender address, but the sender is local or this is a +local delivery error, use the originator login. This shouldn't happen for +genuine bounces, but might happen for autoreplies. The addition of From: must +be done *before* checking for the possible addition of a Sender: header, +because untrusted_set_sender allows an untrusted user to set anything in the +envelope (which might then get info From:) but we still want to ensure a valid +Sender: if it is required. */ + +if (from_header == NULL && (sender_host_address == NULL || submission_mode)) + { + /* Envelope sender is empty */ + + if (sender_address[0] == 0) + { + if (sender_local || local_error_message) + { + header_add(htype_from, "%sFrom: %s%s%s@%s%s\n", resent_prefix, + originator_name, + (originator_name[0] == 0)? "" : " <", + local_part_quote(originator_login), + qualify_domain_sender, + (originator_name[0] == 0)? "" : ">"); + } + else if (submission_mode && authenticated_id != NULL) + { + if (submission_domain == NULL) + { + header_add(htype_from, "%sFrom: %s@%s\n", resent_prefix, + local_part_quote(authenticated_id), qualify_domain_sender); + } + else if (submission_domain[0] == 0) /* empty => whole address set */ + { + header_add(htype_from, "%sFrom: %s\n", resent_prefix, + authenticated_id); + } + else + { + header_add(htype_from, "%sFrom: %s@%s\n", resent_prefix, + local_part_quote(authenticated_id), submission_domain); + } + from_header = header_last; /* To get it checked for Sender: */ + } + } + + /* There is a non-null envelope sender. Build the header using the original + sender address, before any rewriting that might have been done while + verifying it. */ + + else + { + if (!smtp_input || sender_local) + header_add(htype_from, "%sFrom: %s%s%s%s\n", + resent_prefix, originator_name, + (originator_name[0] == 0)? "" : " <", + (sender_address_unrewritten == NULL)? + sender_address : sender_address_unrewritten, + (originator_name[0] == 0)? "" : ">"); + else + header_add(htype_from, "%sFrom: %s\n", resent_prefix, sender_address); + + from_header = header_last; /* To get it checked for Sender: */ + } + } + + +/* If the sender is local, or if we are in submission mode and there is an +authenticated_id, check that an existing From: is correct, and if not, generate +a Sender: header, unless disabled. Any previously-existing Sender: header was +removed above. Note that sender_local, as well as being TRUE if the caller of +exim is not trusted, is also true if a trusted caller did not supply a -f +argument for non-smtp input. To allow trusted callers to forge From: without +supplying -f, we have to test explicitly here. If the From: header contains +more than one address, then the call to parse_extract_address fails, and a +Sender: header is inserted, as required. */ + +if (from_header != NULL && + ( + (sender_local && local_from_check && !trusted_caller) || + (submission_mode && authenticated_id != NULL) + )) + { + BOOL make_sender = TRUE; + int start, end, domain; + uschar *errmess; + uschar *from_address = + parse_extract_address(Ustrchr(from_header->text, ':') + 1, &errmess, + &start, &end, &domain, FALSE); + uschar *generated_sender_address; + + if (submission_mode) + { + if (submission_domain == NULL) + { + generated_sender_address = string_sprintf("%s@%s", + local_part_quote(authenticated_id), qualify_domain_sender); + } + else if (submission_domain[0] == 0) /* empty => full address */ + { + generated_sender_address = string_sprintf("%s", + authenticated_id); + } + else + { + generated_sender_address = string_sprintf("%s@%s", + local_part_quote(authenticated_id), submission_domain); + } + } + else + generated_sender_address = string_sprintf("%s@%s", + local_part_quote(originator_login), qualify_domain_sender); + + /* Remove permitted prefixes and suffixes from the local part of the From: + address before doing the comparison with the generated sender. */ + + if (from_address != NULL) + { + int slen; + uschar *at = (domain == 0)? NULL : from_address + domain - 1; + + if (at != NULL) *at = 0; + from_address += route_check_prefix(from_address, local_from_prefix); + slen = route_check_suffix(from_address, local_from_suffix); + if (slen > 0) + { + memmove(from_address+slen, from_address, Ustrlen(from_address)-slen); + from_address += slen; + } + if (at != NULL) *at = '@'; + + if (strcmpic(generated_sender_address, from_address) == 0 || + (domain == 0 && strcmpic(from_address, originator_login) == 0)) + make_sender = FALSE; + } + + /* We have to cause the Sender header to be rewritten if there are + appropriate rewriting rules. */ + + if (make_sender) + { + if (submission_mode) + header_add(htype_sender, "%sSender: %s\n", resent_prefix, + generated_sender_address); + else + header_add(htype_sender, "%sSender: %s <%s>\n", + resent_prefix, originator_name, generated_sender_address); + } + } + + +/* If there are any rewriting rules, apply them to the sender address, unless +it has already been rewritten as part of verification for SMTP input. */ + +if (global_rewrite_rules != NULL && sender_address_unrewritten == NULL && + sender_address[0] != 0) + { + sender_address = rewrite_address(sender_address, FALSE, TRUE, + global_rewrite_rules, rewrite_existflags); + DEBUG(D_receive|D_rewrite) + debug_printf("rewritten sender = %s\n", sender_address); + } + + +/* The headers must be run through rewrite_header(), because it ensures that +addresses are fully qualified, as well as applying any rewriting rules that may +exist. + +Qualification of header addresses in a message from a remote host happens only +if the host is in sender_unqualified_hosts or recipient_unqualified hosts, as +appropriate. For local messages, qualification always happens, unless -bnq is +used to explicitly suppress it. No rewriting is done for an unqualified address +that is left untouched. + +We start at the second header, skipping our own Received:. This rewriting is +documented as happening *after* recipient addresses are taken from the headers +by the -t command line option. An added Sender: gets rewritten here. */ + +for (h = header_list->next; h != NULL; h = h->next) + { + header_line *newh = rewrite_header(h, NULL, NULL, global_rewrite_rules, + rewrite_existflags, TRUE); + if (newh != NULL) h = newh; + } + + +/* An RFC 822 (sic) message is not legal unless it has at least one of "to", +"cc", or "bcc". Note that although the minimal examples in RFC822 show just +"to" or "bcc", the full syntax spec allows "cc" as well. If any resent- header +exists, this applies to the set of resent- headers rather than the normal set. + +The requirement for a recipient header has been removed in RFC 2822. Earlier +versions of Exim added a To: header for locally submitted messages, and an +empty Bcc: header for others or when always_bcc was set. In the light of the +changes in RFC 2822, we now always add Bcc: just in case there are still MTAs +out there that insist on the RFC 822 syntax. + +November 2003: While generally revising what Exim does to fix up headers, it +seems like a good time to remove this altogether. */ + +/****** +if (!to_or_cc_header_exists && !bcc_header_exists) + header_add(htype_bcc, "Bcc:\n"); +******/ + +/* If there is no date header, generate one if the message originates locally +(i.e. not over TCP/IP) or the submission mode flag is set. Messages without +Date: are not valid, but it seems to be more confusing if Exim adds one to +all remotely-originated messages. */ + +if (!date_header_exists && (sender_host_address == NULL || submission_mode)) + header_add(htype_other, "%sDate: %s\n", resent_prefix, tod_stamp(tod_full)); + +search_tidyup(); /* Free any cached resources */ + +/* Show the complete set of headers if debugging. Note that the first one (the +new Received:) has not yet been set. */ + +DEBUG(D_receive) + { + debug_printf(">>Headers after rewriting and local additions:\n"); + for (h = header_list->next; h != NULL; h = h->next) + debug_printf("%c %s", h->type, h->text); + debug_printf("\n"); + } + +/* The headers are now complete in store. If we are running in filter +testing mode, that is all this function does. Return TRUE if the message +ended with a dot. */ + +if (filter_test != NULL) + { + process_info[process_info_len] = 0; + return message_ended == END_DOT; + } + +/* Open a new spool file for the data portion of the message. We need +to access it both via a file descriptor and a stream. Try to make the +directory if it isn't there. Note re use of sprintf: spool_directory +is checked on input to be < 200 characters long. */ + +sprintf(CS spool_name, "%s/input/%s/%s-D", spool_directory, message_subdir, + message_id); +data_fd = Uopen(spool_name, O_RDWR|O_CREAT|O_EXCL, SPOOL_MODE); +if (data_fd < 0) + { + if (errno == ENOENT) + { + uschar temp[16]; + sprintf(CS temp, "input/%s", message_subdir); + if (message_subdir[0] == 0) temp[5] = 0; + (void)directory_make(spool_directory, temp, INPUT_DIRECTORY_MODE, TRUE); + data_fd = Uopen(spool_name, O_RDWR|O_CREAT|O_EXCL, SPOOL_MODE); + } + if (data_fd < 0) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Failed to create spool file %s: %s", + spool_name, strerror(errno)); + } + +/* Make sure the file's group is the Exim gid, and double-check the mode +because the group setting doesn't always get set automatically. */ + +fchown(data_fd, exim_uid, exim_gid); +fchmod(data_fd, SPOOL_MODE); + +/* We now have data file open. Build a stream for it and lock it. We lock only +the first line of the file (containing the message ID) because otherwise there +are problems when Exim is run under Cygwin (I'm told). See comments in +spool_in.c, where the same locking is done. */ + +data_file = fdopen(data_fd, "w+"); +lock_data.l_type = F_WRLCK; +lock_data.l_whence = SEEK_SET; +lock_data.l_start = 0; +lock_data.l_len = SPOOL_DATA_START_OFFSET; + +if (fcntl(data_fd, F_SETLK, &lock_data) < 0) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Cannot lock %s (%d): %s", spool_name, + errno, strerror(errno)); + +/* We have an open, locked data file. Write the message id to it to make it +self-identifying. Then read the remainder of the input of this message and +write it to the data file. If the variable next != NULL, it contains the first +data line (which was read as a header but then turned out not to have the right +format); write it (remembering that it might contain binary zeros). The result +of fwrite() isn't inspected; instead we call ferror() below. */ + +fprintf(data_file, "%s-D\n", message_id); +if (next != NULL) + { + uschar *s = next->text; + int len = next->slen; + fwrite(s, 1, len, data_file); + body_linecount++; /* Assumes only 1 line */ + } + +/* Note that we might already be at end of file, or the logical end of file +(indicated by '.'), or might have encountered an error while writing the +message id or "next" line. */ + +if (!ferror(data_file) && !(receive_feof)() && message_ended != END_DOT) + { + if (smtp_input) + { + message_ended = read_message_data_smtp(data_file); + receive_linecount++; /* The terminating "." line */ + } + else message_ended = read_message_data(data_file); + + receive_linecount += body_linecount; /* For BSMTP errors mainly */ + + /* Handle premature termination of SMTP */ + + if (smtp_input && message_ended == END_EOF) + { + Uunlink(spool_name); /* Lose data file when closed */ + message_id[0] = 0; /* Indicate no message accepted */ + smtp_reply = handle_lost_connection(US""); + smtp_yield = FALSE; + goto TIDYUP; /* Skip to end of function */ + } + + /* Handle message that is too big. Don't use host_or_ident() in the log + message; we want to see the ident value even for non-remote messages. */ + + if (message_ended == END_SIZE) + { + Uunlink(spool_name); /* Lose the data file when closed */ + if (smtp_input) receive_swallow_smtp(); /* Swallow incoming SMTP */ + + log_write(L_size_reject, LOG_MAIN|LOG_REJECT, "rejected from <%s>%s%s%s%s: " + "message too big: read=%d max=%d", + sender_address, + (sender_fullhost == NULL)? "" : " H=", + (sender_fullhost == NULL)? US"" : sender_fullhost, + (sender_ident == NULL)? "" : " U=", + (sender_ident == NULL)? US"" : sender_ident, + message_size, + thismessage_size_limit); + + if (smtp_input) + { + smtp_reply = US"552 Message size exceeds maximum permitted"; + message_id[0] = 0; /* Indicate no message accepted */ + goto TIDYUP; /* Skip to end of function */ + } + else + { + fseek(data_file, (long int)SPOOL_DATA_START_OFFSET, SEEK_SET); + give_local_error(ERRMESS_TOOBIG, + string_sprintf("message too big (max=%d)", thismessage_size_limit), + US"message rejected: ", error_rc, data_file, header_list); + /* Does not return */ + } + } + } + +/* Restore the standard SIGALRM handler for any subsequent processing. (For +example, there may be some expansion in an ACL that uses a timer.) */ + +os_non_restarting_signal(SIGALRM, sigalrm_handler); + +/* The message body has now been read into the data file. Call fflush() to +empty the buffers in C, and then call fsync() to get the data written out onto +the disk, as fflush() doesn't do this (or at least, it isn't documented as +having to do this). If there was an I/O error on either input or output, +attempt to send an error message, and unlink the spool file. For non-SMTP input +we can then give up. Note that for SMTP input we must swallow the remainder of +the input in cases of output errors, since the far end doesn't expect to see +anything until the terminating dot line is sent. */ + +if (fflush(data_file) == EOF || ferror(data_file) || + fsync(fileno(data_file)) < 0 || (receive_ferror)()) + { + uschar *msg_errno = US strerror(errno); + BOOL input_error = (receive_ferror)() != 0; + uschar *msg = string_sprintf("%s error (%s) while receiving message from %s", + input_error? "Input read" : "Spool write", + msg_errno, + (sender_fullhost != NULL)? sender_fullhost : sender_ident); + + log_write(0, LOG_MAIN, "Message abandoned: %s", msg); + Uunlink(spool_name); /* Lose the data file */ + + if (smtp_input) + { + if (input_error) + smtp_reply = US"451 Error while reading input data"; + else + { + smtp_reply = US"451 Error while writing spool file"; + receive_swallow_smtp(); + } + message_id[0] = 0; /* Indicate no message accepted */ + goto TIDYUP; /* Skip to end of function */ + } + + else + { + fseek(data_file, (long int)SPOOL_DATA_START_OFFSET, SEEK_SET); + give_local_error(ERRMESS_IOERR, msg, US"", error_rc, data_file, + header_list); + /* Does not return */ + } + } + + +/* No I/O errors were encountered while writing the data file. */ + +DEBUG(D_receive) debug_printf("Data file written for message %s\n", message_id); + + +/* If there were any bad addresses extracted by -t, or there were no recipients +left after -t, send a message to the sender of this message, or write it to +stderr if the error handling option is set that way. Note that there may +legitimately be no recipients for an SMTP message if they have all been removed +by "discard". + +We need to rewind the data file in order to read it. In the case of no +recipients or stderr error writing, throw the data file away afterwards, and +exit. (This can't be SMTP, which always ensures there's at least one +syntactically good recipient address.) */ + +if (extract_recip && (bad_addresses != NULL || recipients_count == 0)) + { + DEBUG(D_receive) + { + if (recipients_count == 0) debug_printf("*** No recipients\n"); + if (bad_addresses != NULL) + { + error_block *eblock = bad_addresses; + debug_printf("*** Bad address(es)\n"); + while (eblock != NULL) + { + debug_printf(" %s: %s\n", eblock->text1, eblock->text2); + eblock = eblock->next; + } + } + } + + fseek(data_file, (long int)SPOOL_DATA_START_OFFSET, SEEK_SET); + + /* If configured to send errors to the sender, but this fails, force + a failure error code. We use a special one for no recipients so that it + can be detected by the autoreply transport. Otherwise error_rc is set to + errors_sender_rc, which is EXIT_FAILURE unless -oee was given, in which case + it is EXIT_SUCCESS. */ + + if (error_handling == ERRORS_SENDER) + { + if (!moan_to_sender( + (bad_addresses == NULL)? + (extracted_ignored? ERRMESS_IGADDRESS : ERRMESS_NOADDRESS) : + (recipients_list == NULL)? ERRMESS_BADNOADDRESS : ERRMESS_BADADDRESS, + bad_addresses, header_list, data_file, FALSE)) + error_rc = (bad_addresses == NULL)? EXIT_NORECIPIENTS : EXIT_FAILURE; + } + else + { + if (bad_addresses == NULL) + { + if (extracted_ignored) + fprintf(stderr, "exim: all -t recipients overridden by command line\n"); + else + fprintf(stderr, "exim: no recipients in message\n"); + } + else + { + fprintf(stderr, "exim: invalid address%s", + (bad_addresses->next == NULL)? ":" : "es:\n"); + while (bad_addresses != NULL) + { + fprintf(stderr, " %s: %s\n", bad_addresses->text1, + bad_addresses->text2); + bad_addresses = bad_addresses->next; + } + } + } + + if (recipients_count == 0 || error_handling == ERRORS_STDERR) + { + Uunlink(spool_name); + fclose(data_file); + exim_exit(error_rc); + } + } + +/* Data file successfully written. Generate text for the Received: header by +expanding the configured string, and adding a timestamp. By leaving this +operation till now, we ensure that the timestamp is the time that message +reception was completed. However, this is deliberately done before calling the +data ACL and local_scan(). + +This Received: header may therefore be inspected by the data ACL and by code in +the local_scan() function. When they have run, we update the timestamp to be +the final time of reception. + +If there is just one recipient, set up its value in the $received_for variable +for use when we generate the Received: header. + +Note: the checking for too many Received: headers is handled by the delivery +code. */ + +timestamp = expand_string(US"${tod_full}"); +if (recipients_count == 1) received_for = recipients_list[0].address; +received = expand_string(received_header_text); +received_for = NULL; + +if (received == NULL) + { + Uunlink(spool_name); /* Lose the data file */ + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Expansion of \"%s\" " + "(received_header_text) failed: %s", string_printing(received_header_text), + expand_string_message); + } + +/* The first element on the header chain is reserved for the Received header, +so all we have to do is fill in the text pointer, and set the type. However, if +the result of the expansion is an empty string, we leave the header marked as +"old" so as to refrain from adding a Received header. */ + +if (received[0] == 0) + { + received_header->text = string_sprintf("Received: ; %s\n", timestamp); + received_header->type = htype_old; + } +else + { + received_header->text = string_sprintf("%s; %s\n", received, timestamp); + received_header->type = htype_received; + } + +received_header->slen = Ustrlen(received_header->text); + +DEBUG(D_receive) debug_printf(">>Generated Received: header line\n%c %s", + received_header->type, received_header->text); + +/* Set the value of message_body_size for the DATA ACL and for local_scan() */ + +message_body_size = (fstat(data_fd, &statbuf) == 0)? + statbuf.st_size - SPOOL_DATA_START_OFFSET : -1; + +/* If an ACL from any RCPT commands set up any warning headers to add, do so +now, before running the DATA ACL. */ + +add_acl_headers(US"MAIL or RCPT"); + +/* If an ACL is specified for checking things at this stage of reception of a +message, run it, unless all the recipients were removed by "discard" in earlier +ACLs. That is the only case in which recipients_count can be zero at this +stage. Set deliver_datafile to point to the data file so that $message_body and +$message_body_end can be extracted if needed. Allow $recipients in expansions. +*/ + +deliver_datafile = data_fd; + +if (recipients_count == 0) + { + blackholed_by = recipients_discarded? US"MAIL ACL" : US"RCPT ACL"; + } +else + { + enable_dollar_recipients = TRUE; + + /* Handle interactive SMTP messages */ + + if (smtp_input && !smtp_batched_input) + { + if (acl_smtp_data != NULL && recipients_count > 0) + { + uschar *user_msg, *log_msg; + rc = acl_check(ACL_WHERE_DATA, NULL, acl_smtp_data, &user_msg, &log_msg); + add_acl_headers(US"DATA"); + if (rc == DISCARD) + { + recipients_count = 0; + blackholed_by = US"DATA ACL"; + } + else if (rc != OK) + { + Uunlink(spool_name); + if (smtp_handle_acl_fail(ACL_WHERE_DATA, rc, user_msg, log_msg) != 0) + smtp_yield = FALSE; /* No more messsages after dropped connection */ + smtp_reply = US""; /* Indicate reply already sent */ + message_id[0] = 0; /* Indicate no message accepted */ + goto TIDYUP; /* Skip to end of function */ + } + } + } + + /* Handle non-SMTP and batch SMTP (i.e. non-interactive) messages. Note that + we cannot take different actions for permanent and temporary rejections. */ + + else if (acl_not_smtp != NULL) + { + uschar *user_msg, *log_msg; + rc = acl_check(ACL_WHERE_NOTSMTP, NULL, acl_not_smtp, &user_msg, &log_msg); + if (rc == DISCARD) + { + recipients_count = 0; + blackholed_by = US"non-SMTP ACL"; + } + else if (rc != OK) + { + Uunlink(spool_name); + log_write(0, LOG_MAIN|LOG_REJECT, "F=<%s> rejected by non-SMTP ACL: %s", + sender_address, log_msg); + if (smtp_batched_input) + { + moan_smtp_batch(NULL, "%d %s", 550, user_msg); + /* Does not return */ + } + else + { + fseek(data_file, (long int)SPOOL_DATA_START_OFFSET, SEEK_SET); + give_local_error(ERRMESS_LOCAL_ACL, user_msg, + US"message rejected by non-SMTP ACL: ", error_rc, data_file, + header_list); + /* Does not return */ + } + } + add_acl_headers(US"non-SMTP"); + } + + if (deliver_freeze) frozen_by = US"ACL"; /* for later logging */ + if (queue_only_policy) queued_by = US"ACL"; + + enable_dollar_recipients = FALSE; + } + +/* The final check on the message is to run the scan_local() function. The +version supplied with Exim always accepts, but this is a hook for sysadmins to +supply their own checking code. The local_scan() function is run even when all +the recipients have been discarded. */ + +lseek(data_fd, (long int)SPOOL_DATA_START_OFFSET, SEEK_SET); + +/* Arrange to catch crashes in local_scan(), so that the -D file gets +deleted, and the incident gets logged. */ + +os_non_restarting_signal(SIGSEGV, local_scan_crash_handler); +os_non_restarting_signal(SIGFPE, local_scan_crash_handler); +os_non_restarting_signal(SIGILL, local_scan_crash_handler); +os_non_restarting_signal(SIGBUS, local_scan_crash_handler); + +DEBUG(D_receive) debug_printf("calling local_scan(); timeout=%d\n", + local_scan_timeout); +local_scan_data = NULL; + +os_non_restarting_signal(SIGALRM, local_scan_timeout_handler); +if (local_scan_timeout > 0) alarm(local_scan_timeout); +rc = local_scan(data_fd, &local_scan_data); +alarm(0); +os_non_restarting_signal(SIGALRM, sigalrm_handler); + +store_pool = POOL_MAIN; /* In case changed */ +DEBUG(D_receive) debug_printf("local_scan() returned %d %s\n", rc, + local_scan_data); + +os_non_restarting_signal(SIGSEGV, SIG_DFL); +os_non_restarting_signal(SIGFPE, SIG_DFL); +os_non_restarting_signal(SIGILL, SIG_DFL); +os_non_restarting_signal(SIGBUS, SIG_DFL); + +/* The length check is paranoia against some runaway code, and also because +(for a success return) lines in the spool file are read into big_buffer. */ + +if (local_scan_data != NULL) + { + int len = Ustrlen(local_scan_data); + if (len > LOCAL_SCAN_MAX_RETURN) len = LOCAL_SCAN_MAX_RETURN; + local_scan_data = string_copyn(local_scan_data, len); + } + +if (rc == LOCAL_SCAN_ACCEPT_FREEZE) + { + if (!deliver_freeze) /* ACL might have already frozen */ + { + deliver_freeze = TRUE; + deliver_frozen_at = time(NULL); + frozen_by = US"local_scan()"; + } + rc = LOCAL_SCAN_ACCEPT; + } +else if (rc == LOCAL_SCAN_ACCEPT_QUEUE) + { + if (!queue_only_policy) /* ACL might have already queued */ + { + queue_only_policy = TRUE; + queued_by = US"local_scan()"; + } + rc = LOCAL_SCAN_ACCEPT; + } + +/* Message accepted: remove newlines in local_scan_data because otherwise +the spool file gets corrupted. Ensure that all recipients are qualified. */ + +if (rc == LOCAL_SCAN_ACCEPT) + { + if (local_scan_data != NULL) + { + uschar *s; + for (s = local_scan_data; *s != 0; s++) if (*s == '\n') *s = ' '; + } + for (i = 0; i < recipients_count; i++) + { + recipient_item *r = recipients_list + i; + r->address = rewrite_address_qualify(r->address, TRUE); + if (r->errors_to != NULL) + r->errors_to = rewrite_address_qualify(r->errors_to, TRUE); + } + if (recipients_count == 0 && blackholed_by == NULL) + blackholed_by = US"local_scan"; + } + +/* Message rejected: newlines permitted in local_scan_data to generate +multiline SMTP responses. */ + +else + { + uschar *istemp = US""; + uschar *s = NULL; + int size = 0; + int sptr = 0; + int code; + + errmsg = local_scan_data; + + Uunlink(spool_name); /* Cancel this message */ + switch(rc) + { + default: + log_write(0, LOG_MAIN, "invalid return %d from local_scan(). Temporary " + "rejection given", rc); + goto TEMPREJECT; + + case LOCAL_SCAN_REJECT_NOLOGHDR: + log_extra_selector &= ~LX_rejected_header; + /* Fall through */ + + case LOCAL_SCAN_REJECT: + code = 550; + if (errmsg == NULL) errmsg = US"Administrative prohibition"; + break; + + case LOCAL_SCAN_TEMPREJECT_NOLOGHDR: + log_extra_selector &= ~LX_rejected_header; + /* Fall through */ + + case LOCAL_SCAN_TEMPREJECT: + TEMPREJECT: + code = 451; + if (errmsg == NULL) errmsg = US"Temporary local problem"; + istemp = US"temporarily "; + break; + } + + s = string_append(s, &size, &sptr, 2, US"F=", + (sender_address[0] == 0)? US"<>" : sender_address); + s = add_host_info_for_log(s, &size, &sptr); + s[sptr] = 0; + + log_write(0, LOG_MAIN|LOG_REJECT, "%s %srejected by local_scan(): %.256s", + s, istemp, string_printing(errmsg)); + + if (smtp_input) + { + if (!smtp_batched_input) + { + smtp_respond(code, TRUE, errmsg); + message_id[0] = 0; /* Indicate no message accepted */ + smtp_reply = US""; /* Indicate reply already sent */ + goto TIDYUP; /* Skip to end of function */ + } + else + { + moan_smtp_batch(NULL, "%d %s", code, errmsg); + /* Does not return */ + } + } + else + { + fseek(data_file, (long int)SPOOL_DATA_START_OFFSET, SEEK_SET); + give_local_error(ERRMESS_LOCAL_SCAN, errmsg, + US"message rejected by local scan code: ", error_rc, data_file, + header_list); + /* Does not return */ + } + } + +/* Reset signal handlers to ignore signals that previously would have caused +the message to be abandoned. */ + +signal(SIGTERM, SIG_IGN); +signal(SIGINT, SIG_IGN); + +/* Ensure the first time flag is set in the newly-received message. */ + +deliver_firsttime = TRUE; + +/* Update the timstamp in our Received: header to account for any time taken by +an ACL or by local_scan(). The new time is the time that all reception +processing is complete. */ + +timestamp = expand_string(US"${tod_full}"); +tslen = Ustrlen(timestamp); + +memcpy(received_header->text + received_header->slen - tslen - 1, + timestamp, tslen); + +/* In MUA wrapper mode, ignore queueing actions set by ACL or local_scan() */ + +if (mua_wrapper) + { + deliver_freeze = FALSE; + queue_only_policy = FALSE; + } + +/* Keep the data file open until we have written the header file, in order to +hold onto the lock. In a -bh run, or if the message is to be blackholed, we +don't write the header file, and we unlink the data file. If writing the header +file fails, we have failed to accept this message. */ + +if (host_checking || blackholed_by != NULL) + { + header_line *h; + Uunlink(spool_name); + msg_size = 0; /* Compute size for log line */ + for (h = header_list; h != NULL; h = h->next) + if (h->type != '*') msg_size += h->slen; + } + +/* Write the -H file */ + +else + { + if ((msg_size = spool_write_header(message_id, SW_RECEIVING, &errmsg)) < 0) + { + log_write(0, LOG_MAIN, "Message abandoned: %s", errmsg); + Uunlink(spool_name); /* Lose the data file */ + + if (smtp_input) + { + smtp_reply = US"451 Error in writing spool file"; + message_id[0] = 0; /* Indicate no message accepted */ + goto TIDYUP; + } + else + { + fseek(data_file, (long int)SPOOL_DATA_START_OFFSET, SEEK_SET); + give_local_error(ERRMESS_IOERR, errmsg, US"", error_rc, data_file, + header_list); + /* Does not return */ + } + } + } + + +/* The message has now been successfully received. */ + +receive_messagecount++; + +/* In SMTP sessions we may receive several in one connection. After each one, +we wait for the clock to tick at the level of message-id granularity. This is +so that the combination of time+pid is unique, even on systems where the pid +can be re-used within our time interval. We can't shorten the interval without +re-designing the message-id. See comments above where the message id is +created. This is Something For The Future. */ + +message_id_tv.tv_usec = (message_id_tv.tv_usec/id_resolution) * id_resolution; +exim_wait_tick(&message_id_tv, id_resolution); + +/* Add data size to written header size. We do not count the initial file name +that is in the file, but we do add one extra for the notional blank line that +precedes the data. This total differs from message_size in that it include the +added Received: header and any other headers that got created locally. */ + +fflush(data_file); +fstat(data_fd, &statbuf); + +msg_size += statbuf.st_size - SPOOL_DATA_START_OFFSET + 1; + +/* Generate a "message received" log entry. We do this by building up a dynamic +string as required. Since we commonly want to add two items at a time, use a +macro to simplify the coding. We log the arrival of a new message while the +file is still locked, just in case the machine is *really* fast, and delivers +it first! Include any message id that is in the message - since the syntax of a +message id is actually an addr-spec, we can use the parse routine to canonicize +it. */ + +size = 256; +sptr = 0; +s = store_get(size); + +s = string_append(s, &size, &sptr, 2, US"<= ", + (sender_address[0] == 0)? US"<>" : sender_address); +if (message_reference != NULL) + s = string_append(s, &size, &sptr, 2, US" R=", message_reference); + +s = add_host_info_for_log(s, &size, &sptr); + +#ifdef SUPPORT_TLS +if ((log_extra_selector & LX_tls_cipher) != 0 && tls_cipher != NULL) + s = string_append(s, &size, &sptr, 2, US" X=", tls_cipher); +if ((log_extra_selector & LX_tls_certificate_verified) != 0 && + tls_cipher != NULL) + s = string_append(s, &size, &sptr, 2, US" CV=", + tls_certificate_verified? "yes":"no"); +if ((log_extra_selector & LX_tls_peerdn) != 0 && tls_peerdn != NULL) + s = string_append(s, &size, &sptr, 3, US" DN=\"", tls_peerdn, US"\""); +#endif + +if (sender_host_authenticated != NULL) + { + s = string_append(s, &size, &sptr, 2, US" A=", sender_host_authenticated); + if (authenticated_id != NULL) + s = string_append(s, &size, &sptr, 2, US":", authenticated_id); + } + +sprintf(CS big_buffer, "%d", msg_size); +s = string_append(s, &size, &sptr, 2, US" S=", big_buffer); + +/* If an addr-spec in a message-id contains a quoted string, it can contain +any characters except " \ and CR and so in particular it can contain NL! +Therefore, make sure we use a printing-characters only version for the log. +Also, allow for domain literals in the message id. */ + +if (msgid_header != NULL) + { + uschar *old_id; + BOOL save_allow_domain_literals = allow_domain_literals; + allow_domain_literals = TRUE; + old_id = parse_extract_address(Ustrchr(msgid_header->text, ':') + 1, + &errmsg, &start, &end, &domain, FALSE); + allow_domain_literals = save_allow_domain_literals; + if (old_id != NULL) + s = string_append(s, &size, &sptr, 2, US" id=", string_printing(old_id)); + } + +/* If subject logging is turned on, create suitable printing-character +text. By expanding $h_subject: we make use of the MIME decoding. */ + +if ((log_extra_selector & LX_subject) != 0 && subject_header != NULL) + { + int i; + uschar *p = big_buffer; + uschar *ss = expand_string(US"$h_subject:"); + + /* Backslash-quote any double quotes or backslashes so as to make a + a C-like string, and turn any non-printers into escape sequences. */ + + *p++ = '\"'; + if (*ss != 0) for (i = 0; i < 100 && ss[i] != 0; i++) + { + if (ss[i] == '\"' || ss[i] == '\\') *p++ = '\\'; + *p++ = ss[i]; + } + *p++ = '\"'; + *p = 0; + s = string_append(s, &size, &sptr, 2, US" T=", string_printing(big_buffer)); + } + +/* Terminate the string: string_cat() and string_append() leave room, but do +not put the zero in. */ + +s[sptr] = 0; + +/* While writing to the log, set a flag to cause a call to receive_bomb_out() +if the log cannot be opened. */ + +receive_call_bombout = TRUE; +log_write(0, LOG_MAIN | + (((log_extra_selector & LX_received_recipients) != 0)? LOG_RECIPIENTS : 0) | + (((log_extra_selector & LX_received_sender) != 0)? LOG_SENDER : 0), + "%s", s); +receive_call_bombout = FALSE; + +/* Log any control actions taken by an ACL or local_scan(). */ + +if (deliver_freeze) log_write(0, LOG_MAIN, "frozen by %s", frozen_by); +if (queue_only_policy) log_write(L_delay_delivery, LOG_MAIN, + "no immediate delivery: queued by %s", queued_by); + +/* Create a message log file if message logs are being used and this message is +not blackholed. Write the reception stuff to it. We used to leave message log +creation until the first delivery, but this has proved confusing for somep +people. */ + +if (message_logs && blackholed_by == NULL) + { + int fd; + + sprintf(CS spool_name, "%s/msglog/%s/%s", spool_directory, message_subdir, + message_id); + fd = Uopen(spool_name, O_WRONLY|O_APPEND|O_CREAT, SPOOL_MODE); + + if (fd < 0 && errno == ENOENT) + { + uschar temp[16]; + sprintf(CS temp, "msglog/%s", message_subdir); + if (message_subdir[0] == 0) temp[6] = 0; + (void)directory_make(spool_directory, temp, MSGLOG_DIRECTORY_MODE, TRUE); + fd = Uopen(spool_name, O_WRONLY|O_APPEND|O_CREAT, SPOOL_MODE); + } + + if (fd < 0) + { + log_write(0, LOG_MAIN|LOG_PANIC, "Couldn't open message log %s: %s", + spool_name, strerror(errno)); + } + + else + { + FILE *message_log = fdopen(fd, "a"); + if (message_log == NULL) + { + log_write(0, LOG_MAIN|LOG_PANIC, "Couldn't fdopen message log %s: %s", + spool_name, strerror(errno)); + close(fd); + } + else + { + uschar *now = tod_stamp(tod_log); + fprintf(message_log, "%s Received from %s\n", now, s+3); + if (deliver_freeze) fprintf(message_log, "%s frozen by %s\n", now, + frozen_by); + if (queue_only_policy) fprintf(message_log, + "%s no immediate delivery: queued by %s\n", now, queued_by); + fclose(message_log); + } + } + } + +store_reset(s); /* The store for the main log message can be reused */ + +/* If the message is frozen, and freeze_tell is set, do the telling. */ + +if (deliver_freeze && freeze_tell != NULL && freeze_tell[0] != 0) + { + moan_tell_someone(freeze_tell, NULL, US"Message frozen on arrival", + "Message %s was frozen on arrival by %s.\nThe sender is <%s>.\n", + message_id, frozen_by, sender_address); + } + + +/* Either a message has been successfully received and written to the two spool +files, or an error in writing the spool has occurred for an SMTP message, or +an SMTP message has been rejected because of a bad sender. (For a non-SMTP +message we will have already given up because there's no point in carrying on!) +In either event, we must now close (and thereby unlock) the data file. In the +successful case, this leaves the message on the spool, ready for delivery. In +the error case, the spool file will be deleted. Then tidy up store, interact +with an SMTP call if necessary, and return. + +A fflush() was done earlier in the expectation that any write errors on the +data file will be flushed(!) out thereby. Nevertheless, it is theoretically +possible for fclose() to fail - but what to do? What has happened to the lock +if this happens? */ + +TIDYUP: +process_info[process_info_len] = 0; /* Remove message id */ +if (data_file != NULL) fclose(data_file); /* Frees the lock */ + +/* Now reset signal handlers to their defaults */ + +signal(SIGTERM, SIG_DFL); +signal(SIGINT, SIG_DFL); + +/* Tell an SMTP caller the state of play, and arrange to return the SMTP return +value, which defaults TRUE - meaning there may be more incoming messages from +this connection. For non-SMTP callers (where there is only ever one message), +the default is FALSE. */ + +if (smtp_input) + { + yield = smtp_yield; + + /* Handle interactive SMTP callers. After several kinds of error, smtp_reply + is set to the response. However, after an ACL error or local_scan() error, + the response has already been sent, and smtp_reply is an empty string to + indicate this. */ + + if (!smtp_batched_input) + { + if (smtp_reply == NULL) + { + smtp_printf("250 OK id=%s\r\n", message_id); + if (host_checking) + fprintf(stdout, + "\n**** SMTP testing: that is not a real message id!\n\n"); + } + else if (smtp_reply[0] != 0) smtp_printf("%.1024s\r\n", smtp_reply); + } + + /* For batched SMTP, generate an error message on failure, and do + nothing on success. The function moan_smtp_batch() does not return - + it exits from the program with a non-zero return code. */ + + else if (smtp_reply != NULL) moan_smtp_batch(NULL, "%s", smtp_reply); + } + + +/* If blackholing, we can immediately log this message's sad fate. The data +file has already been unlinked, and the header file was never written to disk. +We must now indicate that nothing was received, to prevent a delivery from +starting. */ + +if (blackholed_by != NULL) + { + uschar *detail = (local_scan_data != NULL)? + string_printing(local_scan_data) : + string_sprintf("(%s discarded recipients)", blackholed_by); + log_write(0, LOG_MAIN, "=> blackhole %s", detail); + log_write(0, LOG_MAIN, "Completed"); + message_id[0] = 0; + } + +/* Reset headers so that logging of rejects for a subsequent message doesn't +include them. It is also important to set header_last = NULL before exiting +from this function, as this prevents certain rewrites that might happen during +subsequent verifying (of another incoming message) from trying to add headers +when they shouldn't. */ + +header_list = header_last = NULL; + +return yield; /* TRUE if more messages (SMTP only) */ +} + +/* End of receive.c */ diff --git a/src/src/retry.c b/src/src/retry.c new file mode 100644 index 000000000..4566da2e4 --- /dev/null +++ b/src/src/retry.c @@ -0,0 +1,881 @@ +/* $Cambridge: exim/src/src/retry.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Functions concerned with retrying unsuccessful deliveries. */ + + +#include "exim.h" + + + +/************************************************* +* Check the ultimate address timeout * +*************************************************/ + +/* This function tests whether a message has been on the queue longer than +the maximum retry time for a particular host. + +Arguments: + host_key the key to look up a host retry rule + domain the domain to look up a domain retry rule + basic_errno a specific error number, or zero if none + more_errno additional data for the error + now the time + +Returns: TRUE if the ultimate timeout has been reached +*/ + +static BOOL +ultimate_address_timeout(uschar *host_key, uschar *domain, int basic_errno, + int more_errno, time_t now) +{ +BOOL address_timeout = TRUE; /* no rule => timed out */ + +retry_config *retry = + retry_find_config(host_key+2, domain, basic_errno, more_errno); + +if (retry != NULL && retry->rules != NULL) + { + retry_rule *last_rule; + for (last_rule = retry->rules; + last_rule->next != NULL; + last_rule = last_rule->next); + address_timeout = (now - received_time > last_rule->timeout); + } + +return address_timeout; +} + + + +/************************************************* +* Set status of a host+address item * +*************************************************/ + +/* This function is passed a host_item which contains a host name and an +IP address string. Its job is to set the status of the address if it is not +already set (indicated by hstatus_unknown). The possible values are: + + hstatus_usable the address is not listed in the unusable tree, and does + not have a retry record, OR the time is past the next + try time, OR the message has been on the queue for more + than the maximum retry time for a failing host + + hstatus_unusable the address is listed in the unusable tree, or does have + a retry record, and the time is not yet at the next retry + time. + + hstatus_unusable_expired as above, but also the retry time has expired + for this address. + +The reason a delivery is permitted when a message has been around for a very +long time is to allow the ultimate address timeout to operate after a delivery +failure. Otherwise some messages may stick around without being tried for too +long. + +If a host retry record is retrieved from the hints database, the time of last +trying is filled into the last_try field of the host block. If a host is +generally usable, a check is made to see if there is a retry delay on this +specific message at this host. + +If a non-standard port is being used, it is added to the retry key. + +Arguments: + domain the address domain + host pointer to a host item + portstring "" for standard port, ":xxxx" for a non-standard port + include_ip_address TRUE to include the address in the key - this is + usual, but sometimes is not wanted + retry_host_key where to put a pointer to the key for the host-specific + retry record, if one is read and the host is usable + retry_message_key where to put a pointer to the key for the message+host + retry record, if one is read and the host is usable + +Returns: TRUE if the host has expired but is usable because + its retry time has come +*/ + +BOOL +retry_check_address(uschar *domain, host_item *host, uschar *portstring, + BOOL include_ip_address, uschar **retry_host_key, uschar **retry_message_key) +{ +BOOL yield = FALSE; +time_t now = time(NULL); +uschar *host_key, *message_key; +open_db dbblock; +open_db *dbm_file; +tree_node *node; +dbdata_retry *host_retry_record, *message_retry_record; + +*retry_host_key = *retry_message_key = NULL; + +DEBUG(D_transport|D_retry) debug_printf("checking status of %s\n", host->name); + +/* Do nothing if status already set; otherwise initialize status as usable. */ + +if (host->status != hstatus_unknown) return FALSE; +host->status = hstatus_usable; + +/* Generate the host key for the unusable tree and the retry database. Ensure +host names are lower cased (that's what %S does). */ + +host_key = include_ip_address? + string_sprintf("T:%S:%s%s", host->name, host->address, portstring) : + string_sprintf("T:%S%s", host->name, portstring); + +/* Generate the message-specific key */ + +message_key = string_sprintf("%s:%s", host_key, message_id); + +/* Search the tree of unusable IP addresses. This is filled in when deliveries +fail, because the retry database itself is not updated until the end of all +deliveries (so as to do it all in one go). The tree records addresses that have +become unusable during this delivery process (i.e. those that will get put into +the retry database when it is updated). */ + +node = tree_search(tree_unusable, host_key); +if (node != NULL) + { + DEBUG(D_transport|D_retry) debug_printf("found in tree of unusables\n"); + host->status = (node->data.val > 255)? + hstatus_unusable_expired : hstatus_unusable; + host->why = node->data.val & 255; + return FALSE; + } + +/* Open the retry database, giving up if there isn't one. Otherwise, search for +the retry records, and then close the database again. */ + +if ((dbm_file = dbfn_open(US"retry", O_RDONLY, &dbblock, FALSE)) == NULL) + { + DEBUG(D_deliver|D_retry|D_hints_lookup) + debug_printf("no retry data available\n"); + return FALSE; + } +host_retry_record = dbfn_read(dbm_file, host_key); +message_retry_record = dbfn_read(dbm_file, message_key); +dbfn_close(dbm_file); + +/* Ignore the data if it is too old - too long since it was written */ + +if (host_retry_record == NULL) + { + DEBUG(D_transport|D_retry) debug_printf("no host retry record\n"); + } +else if (now - host_retry_record->time_stamp > retry_data_expire) + { + host_retry_record = NULL; + DEBUG(D_transport|D_retry) debug_printf("host retry record too old\n"); + } + +if (message_retry_record == NULL) + { + DEBUG(D_transport|D_retry) debug_printf("no message retry record\n"); + } +else if (now - message_retry_record->time_stamp > retry_data_expire) + { + message_retry_record = NULL; + DEBUG(D_transport|D_retry) debug_printf("message retry record too old\n"); + } + +/* If there's a host-specific retry record, check for reaching the retry +time (or forcing). If not, and the host is not expired, check for the message +having been around for longer than the maximum retry time for this host or +address. Allow the delivery if it has. Otherwise set the appropriate unusable +flag and return FALSE. Otherwise arrange to return TRUE if this is an expired +host. */ + +if (host_retry_record != NULL) + { + *retry_host_key = host_key; + + /* We have not reached the next try time. Check for the ultimate address + timeout if the host has not expired. */ + + if (now < host_retry_record->next_try && !deliver_force) + { + DEBUG(D_transport|D_retry) + debug_printf("host retry time not reached: checking ultimate address " + "timeout\n"); + + if (!host_retry_record->expired && + ultimate_address_timeout(host_key, domain, + host_retry_record->basic_errno, host_retry_record->more_errno, now)) + { + DEBUG(D_transport|D_retry) + debug_printf("on queue longer than maximum retry for " + "address - allowing delivery\n"); + return FALSE; + } + + /* We have not hit the ultimate address timeout; host is unusable. */ + + host->status = (host_retry_record->expired)? + hstatus_unusable_expired : hstatus_unusable; + host->why = hwhy_retry; + host->last_try = host_retry_record->last_try; + return FALSE; + } + + /* Host is usable; set return TRUE if expired. */ + + yield = host_retry_record->expired; + } + +/* It's OK to try the host. If there's a message-specific retry record, check +for reaching its retry time (or forcing). If not, mark the host unusable, +unless the ultimate address timeout has been reached. */ + +if (message_retry_record != NULL) + { + *retry_message_key = message_key; + if (now < message_retry_record->next_try && !deliver_force) + { + DEBUG(D_transport|D_retry) + debug_printf("host+message retry time not reached: checking ultimate " + "address timeout\n"); + if (!ultimate_address_timeout(host_key, domain, 0, 0, now)) + { + host->status = hstatus_unusable; + host->why = hwhy_retry; + } + else + { + DEBUG(D_transport|D_retry) + debug_printf("on queue longer than maximum retry for " + "address - allowing delivery\n"); + } + return FALSE; + } + } + +return yield; +} + + + + +/************************************************* +* Add a retry item to an address * +*************************************************/ + +/* Retry items are chained onto an address when it is deferred either by router +or by a transport, or if it succeeds or fails and there was a previous retry +item that now needs to be deleted. Sometimes there can be both kinds of item: +for example, if routing was deferred but then succeeded, and delivery then +deferred. In that case there is a delete item for the routing retry, and an +updating item for the delivery. + +(But note that that is only visible at the outer level, because in remote +delivery subprocesses, the address starts "clean", with no retry items carried +in.) + +These items are used at the end of a delivery attempt to update the retry +database. The keys start R: for routing delays and T: for transport delays. + +Arguments: + addr the address block onto which to hang the item + key the retry key + flags delete, host, and message flags, copied into the block + +Returns: nothing +*/ + +void +retry_add_item(address_item *addr, uschar *key, int flags) +{ +retry_item *rti = store_get(sizeof(retry_item)); +rti->next = addr->retries; +addr->retries = rti; +rti->key = key; +rti->basic_errno = addr->basic_errno; +rti->more_errno = addr->more_errno; +rti->message = addr->message; +rti->flags = flags; + +DEBUG(D_transport|D_retry) + { + int letter = rti->more_errno & 255; + debug_printf("added retry item for %s: errno=%d more_errno=", rti->key, + rti->basic_errno); + if (letter == 'A' || letter == 'M') + debug_printf("%d,%c", (rti->more_errno >> 8) & 255, letter); + else + debug_printf("%d", rti->more_errno); + debug_printf(" flags=%d\n", flags); + } +} + + + +/************************************************* +* Find retry configuration data * +*************************************************/ + +/* Search the in-store retry information for the first retry item that applies +to a given destination. If the key contains an @ we are probably handling a +local delivery and have a complete address to search for; this happens when +retry_use_local_part is set on a router. Otherwise, the key is likely to be a +host name for a remote delivery, or a domain name for a local delivery. We +prepend *@ on the front of it so that it will match a retry item whose address +item pattern is independent of the local part. The alternate key, if set, is +always just a domain, so we treat it likewise. + +Arguments: + key key for which retry info is wanted + alternate alternative key, always just a domain + basic_errno specific error predicate on the retry rule, or zero + more_errno additional data for errno predicate + +Returns: pointer to retry rule, or NULL +*/ + +retry_config * +retry_find_config(uschar *key, uschar *alternate, int basic_errno, + int more_errno) +{ +int replace; +uschar *use_key, *use_alternate; +uschar *colon = Ustrchr(key, ':'); +retry_config *yield; + +/* If there's a colon in the key, temporarily replace it with +a zero to terminate the string there. */ + +if (colon != NULL) + { + replace = ':'; + } +else + { + colon = key + Ustrlen(key); + replace = 0; + } +*colon = 0; + +/* Sort out the keys */ + +use_key = (Ustrchr(key, '@') != NULL)? key : string_sprintf("*@%s", key); +use_alternate = (alternate == NULL)? NULL : string_sprintf("*@%s", alternate); + +/* Scan the configured retry items. */ + +for (yield = retries; yield != NULL; yield = yield->next) + { + uschar *plist = yield->pattern; + uschar *slist = yield->senders; + + /* If a specific error is set for this item, check that we are handling that + specific error, and if so, check any additional error information if + required. */ + + if (yield->basic_errno != 0) + { + /* Special code is required for quota errors, as these can either be system + quota errors, or Exim's own quota imposition, which has a different error + number. Full partitions are also treated in the same way as quota errors. + */ + + if (yield->basic_errno == ERRNO_EXIMQUOTA) + { + if ((basic_errno != ERRNO_EXIMQUOTA && basic_errno != errno_quota && + basic_errno != ENOSPC) || + (yield->more_errno != 0 && yield->more_errno > more_errno)) + continue; + } + + /* Handle 4xx responses to RCPT. The code that was received is in the 2nd + least significant byte of more_errno (with 400 subtracted). The required + value is coded in the 2nd least significant byte of the yield->more_errno + field as follows: + + 255 => any 4xx code + >= 100 => the decade must match the value less 100 + < 100 => the exact value must match + */ + + else if (yield->basic_errno == ERRNO_RCPT4XX) + { + int wanted; + if (basic_errno != ERRNO_RCPT4XX) continue; + wanted = (yield->more_errno >> 8) & 255; + if (wanted != 255) + { + int evalue = (more_errno >> 8) & 255; + if (wanted >= 100) + { + if ((evalue/10)*10 != wanted - 100) continue; + } + else if (evalue != wanted) continue; + } + } + + /* There are some special cases for timeouts */ + + else if (yield->basic_errno == ETIMEDOUT) + { + if (basic_errno != ETIMEDOUT) continue; + + /* Just RTEF_CTOUT in the rule => don't care about 'A'/'M' addresses */ + if (yield->more_errno == RTEF_CTOUT) + { + if ((more_errno & RTEF_CTOUT) == 0) continue; + } + + else if (yield->more_errno != 0) + { + int cf_errno = more_errno; + if ((yield->more_errno & RTEF_CTOUT) == 0) cf_errno &= ~RTEF_CTOUT; + if (yield->more_errno != cf_errno) continue; + } + } + + /* Default checks for exact match */ + + else + { + if (yield->basic_errno != basic_errno || + (yield->more_errno != 0 && yield->more_errno != more_errno)) + continue; + } + } + + /* If the "senders" condition is set, check it. Note that sender_address may + be null during -brt checking, in which case we do not use this rule. */ + + if (slist != NULL && (sender_address == NULL || + match_address_list(sender_address, TRUE, TRUE, &slist, NULL, -1, 0, + NULL) != OK)) + continue; + + /* Check for a match between the address list item at the start of this retry + rule and either the main or alternate keys. */ + + if (match_address_list(use_key, TRUE, TRUE, &plist, NULL, -1, UCHAR_MAX+1, + NULL) == OK || + (use_alternate != NULL && + match_address_list(use_alternate, TRUE, TRUE, &plist, NULL, -1, + UCHAR_MAX+1, NULL) == OK)) + break; + } + +*colon = replace; +return yield; +} + + + + +/************************************************* +* Update retry database * +*************************************************/ + +/* Update the retry data for any directing/routing/transporting that was +deferred, or delete it for those that succeeded after a previous defer. This is +done all in one go to minimize opening/closing/locking of the database file. + +Note that, because SMTP delivery involves a list of destinations to try, there +may be defer-type retry information for some of them even when the message was +successfully delivered. Likewise if it eventually failed. + +This function may move addresses from the defer to the failed queue if the +ultimate retry time has expired. + +Arguments: + addr_defer queue of deferred addresses + addr_failed queue of failed addresses + addr_succeed queue of successful addresses + +Returns: nothing +*/ + +void +retry_update(address_item **addr_defer, address_item **addr_failed, + address_item **addr_succeed) +{ +open_db dbblock; +open_db *dbm_file = NULL; +time_t now = time(NULL); +int i; + +DEBUG(D_retry) debug_printf("Processing retry items\n"); + +/* Three-times loop to handle succeeded, failed, and deferred addresses. +Deferred addresses must be handled after failed ones, because some may be moved +to the failed chain if they have timed out. */ + +for (i = 0; i < 3; i++) + { + address_item *endaddr, *addr; + address_item *last_first = NULL; + address_item **paddr = (i==0)? addr_succeed : + (i==1)? addr_failed : addr_defer; + address_item **saved_paddr = NULL; + + DEBUG(D_retry) debug_printf("%s addresses:\n", (i == 0)? "Succeeded" : + (i == 1)? "Failed" : "Deferred"); + + /* Loop for each address on the chain. For deferred addresses, the whole + address times out unless one of its retry addresses has a retry rule that + hasn't yet timed out. Deferred addresses should not be requesting deletion + of retry items, but just in case they do by accident, treat that case + as "not timed out". + + As well as handling the addresses themselves, we must also process any + retry items for any parent addresses - these are typically "delete" items, + because the parent must have succeeded in order to generate the child. */ + + while ((endaddr = *paddr) != NULL) + { + BOOL timed_out = FALSE; + retry_item *rti; + + for (addr = endaddr; addr != NULL; addr = addr->parent) + { + int update_count = 0; + int timedout_count = 0; + + DEBUG(D_retry) debug_printf("%s%s\n", addr->address, (addr->retries == NULL)? + ": no retry items" : ""); + + /* Loop for each retry item. */ + + for (rti = addr->retries; rti != NULL; rti = rti->next) + { + uschar *message; + int message_length, message_space, failing_interval, next_try; + retry_rule *rule, *final_rule; + retry_config *retry; + dbdata_retry *retry_record; + + /* Open the retry database if it is not already open; failure to open + the file is logged, but otherwise ignored - deferred addresses will + get retried at the next opportunity. Not opening earlier than this saves + opening if no addresses have retry items - common when none have yet + reached their retry next try time. */ + + if (dbm_file == NULL) + dbm_file = dbfn_open(US"retry", O_RDWR, &dbblock, TRUE); + + if (dbm_file == NULL) + { + DEBUG(D_deliver|D_retry|D_hints_lookup) + debug_printf("retry database not available for updating\n"); + return; + } + + /* If there are no deferred addresses, that is, if this message is + completing, and the retry item is for a message-specific SMTP error, + force it to be deleted, because there's no point in keeping data for + no-longer-existing messages. This situation can occur when a domain has + two hosts and a message-specific error occurs for the first of them, + but the address gets delivered to the second one. This optimization + doesn't succeed in cleaning out all the dead entries, but it helps. */ + + if (*addr_defer == NULL && (rti->flags & rf_message) != 0) + rti->flags |= rf_delete; + + /* Handle the case of a request to delete the retry info for this + destination. */ + + if ((rti->flags & rf_delete) != 0) + { + (void)dbfn_delete(dbm_file, rti->key); + DEBUG(D_retry) + debug_printf("deleted retry information for %s\n", rti->key); + continue; + } + + /* Count the number of non-delete retry items. This is so that we + can compare it to the count of timed_out ones, to check whether + all are timed out. */ + + update_count++; + + /* Get the retry information for this destination and error code, if + any. If this item is for a remote host with ip address, then pass + the domain name as an alternative to search for. If no retry + information is found, we can't generate a retry time, so there is + no point updating the database. This retry item is timed out. */ + + if ((retry = retry_find_config(rti->key + 2, + ((rti->flags & rf_host) != 0)? addr->domain : NULL, + rti->basic_errno, rti->more_errno)) == NULL) + { + DEBUG(D_retry) debug_printf("No configured retry item for %s%s%s\n", + rti->key, + ((rti->flags & rf_host) != 0)? US" or " : US"", + ((rti->flags & rf_host) != 0)? addr->domain : US""); + if (addr == endaddr) timedout_count++; + continue; + } + + DEBUG(D_retry) + { + if ((rti->flags & rf_host) != 0) + debug_printf("retry for %s (%s) = %s\n", rti->key, + addr->domain, retry->pattern); + else + debug_printf("retry for %s = %s\n", rti->key, retry->pattern); + } + + /* Set up the message for the database retry record. Because DBM + records have a maximum data length, we enforce a limit. There isn't + much point in keeping a huge message here, anyway. */ + + message = (rti->basic_errno > 0)? US strerror(rti->basic_errno) : + (rti->message == NULL)? + US"unknown error" : string_printing(rti->message); + message_length = Ustrlen(message); + if (message_length > 150) message_length = 150; + + /* Read a retry record from the database or construct a new one. + Ignore an old one if it is too old since it was last updated. */ + + retry_record = dbfn_read(dbm_file, rti->key); + if (retry_record != NULL && + now - retry_record->time_stamp > retry_data_expire) + retry_record = NULL; + + if (retry_record == NULL) + { + retry_record = store_get(sizeof(dbdata_retry) + message_length); + message_space = message_length; + retry_record->first_failed = now; + retry_record->last_try = now; + retry_record->next_try = now; + retry_record->expired = FALSE; + retry_record->text[0] = 0; /* just in case */ + } + else message_space = Ustrlen(retry_record->text); + + /* Compute how long this destination has been failing */ + + failing_interval = now - retry_record->first_failed; + + /* Search for the current retry rule. The cutoff time of the + last rule is handled differently to the others. The rule continues + to operate for ever (the global maximum interval will eventually + limit the gaps) but its cutoff time determines when an individual + destination times out. If there are no retry rules, the destination + always times out, but we can't compute a retry time. */ + + final_rule = NULL; + for (rule = retry->rules; rule != NULL; rule = rule->next) + { + if (failing_interval <= rule->timeout) break; + final_rule = rule; + } + + /* If there's an un-timed out rule, the destination has not + yet timed out, so the address as a whole has not timed out (but we are + interested in this only for the end address). Make sure the expired + flag is false (can be forced via fixdb from outside, but ensure it is + consistent with the rules whenever we go through here). */ + + if (rule != NULL) + { + retry_record->expired = FALSE; + } + + /* Otherwise, set the retry timeout expired, and set the final rule + as the one from which to compute the next retry time. Subsequent + messages will fail immediately until the retry time is reached (unless + there are other, still active, retries). */ + + else + { + rule = final_rule; + retry_record->expired = TRUE; + if (addr == endaddr) timedout_count++; + } + + /* There is a special case to consider when some messages get through + to a destination and others don't. This can happen locally when a + large message pushes a user over quota, and it can happen remotely + when a machine is on a dodgy Internet connection. The messages that + get through wipe the retry information, causing those that don't to + stay on the queue longer than the final retry time. In order to + avoid this, we check, using the time of arrival of the message, to + see if it has been on the queue for more than the final cutoff time, + and if so, cause this retry item to time out, and the retry time to + be set to "now" so that any subsequent messages in the same condition + also get tried. We search for the last rule onwards from the one that + is in use. If there are no retry rules for the item, rule will be null + and timedout_count will already have been updated. + + This implements "timeout this rule if EITHER the host (or routing or + directing) has been failing for more than the maximum time, OR if the + message has been on the queue for more than the maximum time." */ + + if (received_time <= retry_record->first_failed && + addr == endaddr && !retry_record->expired && rule != NULL) + { + retry_rule *last_rule; + for (last_rule = rule; + last_rule->next != NULL; + last_rule = last_rule->next); + if (now - received_time > last_rule->timeout) + { + DEBUG(D_retry) debug_printf("on queue longer than maximum retry\n"); + timedout_count++; + rule = NULL; + } + } + + /* Compute the next try time from the rule, subject to the global + maximum, and update the retry database. If rule == NULL it means + there were no rules at all (and the timeout will be set expired), + or we have a message that is older than the final timeout. In this + case set the next retry time to now, so that one delivery attempt + happens for subsequent messages. */ + + if (rule == NULL) next_try = now; else + { + if (rule->rule == 'F') next_try = now + rule->p1; + else /* assume rule = 'G' */ + { + int last_predicted_gap = + retry_record->next_try - retry_record->last_try; + int last_actual_gap = now - retry_record->last_try; + int lastgap = (last_predicted_gap < last_actual_gap)? + last_predicted_gap : last_actual_gap; + next_try = now + ((lastgap < rule->p1)? rule->p1 : + (lastgap * rule->p2)/1000); + } + } + + /* Impose a global retry max */ + + if (next_try - now > retry_interval_max) + next_try = now + retry_interval_max; + + /* If the new message length is greater than the previous one, we + have to copy the record first. */ + + if (message_length > message_space) + { + dbdata_retry *newr = store_get(sizeof(dbdata_retry) + message_length); + memcpy(newr, retry_record, sizeof(dbdata_retry)); + retry_record = newr; + } + + /* Set up the retry record; message_length may be less than the string + length for very long error strings. */ + + retry_record->last_try = now; + retry_record->next_try = next_try; + retry_record->basic_errno = rti->basic_errno; + retry_record->more_errno = rti->more_errno; + Ustrncpy(retry_record->text, message, message_length); + retry_record->text[message_length] = 0; + + DEBUG(D_retry) + { + int letter = retry_record->more_errno & 255; + debug_printf("Writing retry data for %s\n", rti->key); + debug_printf(" first failed=%d last try=%d next try=%d expired=%d\n", + (int)retry_record->first_failed, (int)retry_record->last_try, + (int)retry_record->next_try, retry_record->expired); + debug_printf(" errno=%d more_errno=", retry_record->basic_errno); + if (letter == 'A' || letter == 'M') + debug_printf("%d,%c", (retry_record->more_errno >> 8) & 255, + letter); + else + debug_printf("%d", retry_record->more_errno); + debug_printf(" %s\n", retry_record->text); + } + + (void)dbfn_write(dbm_file, rti->key, retry_record, + sizeof(dbdata_retry) + message_length); + } /* Loop for each retry item */ + + /* If all the non-delete retry items are timed out, the address is + timed out, provided that we didn't skip any hosts because their retry + time was not reached (or because of hosts_max_try). */ + + if (update_count > 0 && update_count == timedout_count) + { + if (!testflag(endaddr, af_retry_skipped)) + { + DEBUG(D_retry) debug_printf("timed out: all retries expired\n"); + timed_out = TRUE; + } + else + { + DEBUG(D_retry) + debug_printf("timed out but some hosts were skipped\n"); + } + } + } /* Loop for an address and its parents */ + + /* If this is a deferred address, and retry processing was requested by + means of one or more retry items, and they all timed out, move the address + to the failed queue, and restart this loop without updating paddr. + + If there were several addresses batched in the same remote delivery, only + the original top one will have host retry items attached to it, but we want + to handle all the same. Each will have a pointer back to its "top" address, + and they will now precede the item with the retries because addresses are + inverted when added to these final queues. We have saved information about + them in passing (below) so they can all be cut out at once. */ + + if (i == 2) /* Handling defers */ + { + if (endaddr->retries != NULL && timed_out) + { + if (last_first == endaddr) paddr = saved_paddr; + addr = *paddr; + *paddr = endaddr->next; + + endaddr->next = *addr_failed; + *addr_failed = addr; + + for (;; addr = addr->next) + { + setflag(addr, af_retry_timedout); + addr->message = (addr->message == NULL)? US"retry timeout exceeded" : + string_sprintf("%s: retry timeout exceeded", addr->message); + log_write(0, LOG_MAIN, "** %s%s%s%s: retry timeout exceeded", + addr->address, + (addr->parent == NULL)? US"" : US" <", + (addr->parent == NULL)? US"" : addr->parent->address, + (addr->parent == NULL)? US"" : US">"); + + if (addr == endaddr) break; + } + + continue; /* Restart from changed *paddr */ + } + + /* This address is to remain on the defer chain. If it has a "first" + pointer, save the pointer to it in case we want to fail the set of + addresses when we get to the first one. */ + + if (endaddr->first != last_first) + { + last_first = endaddr->first; + saved_paddr = paddr; + } + } + + /* All cases (succeed, fail, defer left on queue) */ + + paddr = &(endaddr->next); /* Advance to next address */ + } /* Loop for all addresses */ + } /* Loop for succeed, fail, defer */ + +/* Close and unlock the database */ + +if (dbm_file != NULL) dbfn_close(dbm_file); + +DEBUG(D_retry) debug_printf("end of retry processing\n"); +} + +/* End of retry.c */ diff --git a/src/src/rewrite.c b/src/src/rewrite.c new file mode 100644 index 000000000..f9338ba36 --- /dev/null +++ b/src/src/rewrite.c @@ -0,0 +1,830 @@ +/* $Cambridge: exim/src/src/rewrite.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Functions concerned with rewriting headers */ + + +#include "exim.h" + +/* Names for testing rewriting */ + +static char *rrname[] = { + " sender", + " from", + " to", + " cc", + " bcc", + "reply-to", + "env-from", + " env-to" +}; + +/* Structure and table for finding source of address for debug printing */ + +typedef struct where_list_block { + int bit; + uschar *string; +} where_list_block; + +static where_list_block where_list[] = { + { rewrite_sender, US"sender:" }, + { rewrite_from, US"from:" }, + { rewrite_to, US"to:" }, + { rewrite_cc, US"cc:" }, + { rewrite_bcc, US"bcc:" }, + { rewrite_replyto, US"reply-to:" }, + { rewrite_envfrom, US"env-from" }, + { rewrite_envto, US"env-to" }, + { rewrite_smtp, US"smtp recipient" }, + { rewrite_smtp|rewrite_smtp_sender, US"smtp sender" } +}; + +static int where_list_size = sizeof(where_list)/sizeof(where_list_block); + + + +/************************************************* +* Ensure an address is qualified * +*************************************************/ + +/* +Arguments: + s address to check + is_recipient TRUE if a recipient address; FALSE if a sender address + +Returns: fully-qualified address +*/ + +uschar * +rewrite_address_qualify(uschar *s, BOOL is_recipient) +{ +return (parse_find_at(s) != NULL)? s : + string_sprintf("%s@%s", s, + is_recipient? qualify_domain_recipient : qualify_domain_sender); +} + + + +/************************************************* +* Rewrite a single address * +*************************************************/ + +/* The yield is the input address if there is no rewriting to be done. Assume +the input is a valid address, except in the case of SMTP-time rewriting, which +is handled specially. When this function is called while processing filter and +forward files, the uid may be that of the user. Ensure it is reset while +expanding a replacement, in case that involves file lookups. + +Arguments: + s address to rewrite + flag indicates where this address comes from; it must match the + flags in the rewriting rule + whole if not NULL, set TRUE if any rewriting rule contained the + "whole" bit and it is a header that is being rewritten + add_header if TRUE and rewriting occurs, add an "X-rewrote-xxx" header + if headers are in existence; this should be TRUE only when + a message is being received, not during delivery + name name of header, for use when adding X-rewrote-xxxx + rewrite_rules chain of rewriting rules + +Returns: new address if rewritten; the input address if no change; + for a header rewrite, if the "whole" bit is set, the entire + rewritten address is returned, not just the active bit. +*/ + +uschar * +rewrite_one(uschar *s, int flag, BOOL *whole, BOOL add_header, uschar *name, + rewrite_rule *rewrite_rules) +{ +rewrite_rule *rule; +uschar *yield = s; +uschar *subject = s; +uschar *domain = NULL; +BOOL done = FALSE; +int rule_number = 1; +int yield_start = 0, yield_end = 0; + +if (whole != NULL) *whole = FALSE; + +/* Scan the rewriting rules */ + +for (rule = rewrite_rules; + rule != NULL && !done; + rule_number++, rule = rule->next) + { + int start, end, pdomain; + int count = 0; + uschar *save_localpart, *save_domain; + uschar *error, *new, *newparsed; + + /* Ensure that the flag matches the flags in the rule. */ + + if ((rule->flags & flag) == 0) continue; + + /* Come back here for a repeat after a successful rewrite. We do this + only so many times. */ + + REPEAT_RULE: + + /* If this is an SMTP-time rewrite, the pattern must be a regex and + the subject may have any structure. No local part or domain variables + can be set for the expansion. We expand the pattern in order to be consistent + with the other kinds of rewrite, where expansion happens inside + match_address_list(). */ + + if ((flag & rewrite_smtp) != 0) + { + uschar *key = expand_string(rule->key); + if (key == NULL) + { + if (!expand_string_forcedfail) + log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand \"%s\" while " + "checking for SMTP rewriting: %s", rule->key, expand_string_message); + continue; + } + if (match_check_string(subject, key, 0, TRUE, FALSE, FALSE, NULL) != OK) + continue; + new = expand_string(rule->replacement); + } + + /* All other rewrites expect the input to be a valid address, so local part + and domain variables can be set for expansion. For the first rule, to be + applied to this address, domain will be NULL and needs to be set. */ + + else + { + if (domain == NULL) domain = Ustrrchr(subject, '@') + 1; + + /* Use the general function for matching an address against a list (here + just one item, so use the "impossible value" separator UCHAR_MAX+1). */ + + if (match_address_list(subject, FALSE, TRUE, &(rule->key), NULL, 0, + UCHAR_MAX + 1, NULL) != OK) + continue; + + /* The source address matches, and numerical variables have been + set up. If the replacement string consists of precisely "*" then no + rewriting is required for this address - the behaviour is as for "fail" + in the replacement expansion, but assuming the quit flag. */ + + if (Ustrcmp(rule->replacement, "*") == 0) break; + + /* Otherwise, expand the replacement string. Set $local_part and $domain to + the appropriate values, restoring whatever value they previously had + afterwards. */ + + save_localpart = deliver_localpart; + save_domain = deliver_domain; + + /* We have subject pointing to "localpart@domain" and domain pointing to + the domain. Temporarily terminate the local part so that it can be + set up as an expansion variable */ + + domain[-1] = 0; + deliver_localpart = subject; + deliver_domain = domain; + + new = expand_string(rule->replacement); + + domain[-1] = '@'; + deliver_localpart = save_localpart; + deliver_domain = save_domain; + } + + /* If the expansion failed with the "forcedfail" flag, don't generate + an error - just give up on this rewriting rule. If the "q" flag is set, + give up altogether. For other expansion failures we have a configuration + error. */ + + if (new == NULL) + { + if (expand_string_forcedfail) + { if ((rule->flags & rewrite_quit) != 0) break; else continue; } + log_write(0, LOG_MAIN|LOG_PANIC, "Expansion of %s failed while rewriting: " + "%s", rule->replacement, expand_string_message); + break; + } + + /* Check the what has been generated is a valid RFC 2822 address. Only + envelope from or SMTP sender is permitted to be rewritten as <>.*/ + + newparsed = parse_extract_address(new, &error, &start, &end, &pdomain, + flag == rewrite_envfrom || flag == (rewrite_smtp|rewrite_smtp_sender)); + + if (newparsed == NULL) + { + log_write(0, LOG_MAIN|LOG_PANIC, "Rewrite of %s yielded unparseable " + "address: %s in address %s", subject, error, new); + break; /* Give up on this address */ + } + + /* A non-null unqualified address can be qualified if requested. Otherwise, + this is an error unless it's the empty address in circumstances where that is + permitted. */ + + if (pdomain == 0 && (*newparsed != 0 || + (flag != rewrite_envfrom && flag != (rewrite_smtp|rewrite_smtp_sender)))) + { + if ((rule->flags & rewrite_qualify) != 0) + { + newparsed = rewrite_address_qualify(newparsed, TRUE); + new = string_sprintf("%.*s%s%.*s", start, new, newparsed, + Ustrlen(new) - end, new + end); + end = start + Ustrlen(newparsed); + } + else + { + log_write(0, LOG_MAIN|LOG_PANIC, "Rewrite of %s yielded unqualified " + "address \"%s\"", subject, new); + break; /* Give up on this address */ + } + } + + /* We have a validly rewritten address */ + + if ((log_write_selector & L_address_rewrite) != 0 || + (debug_selector & D_rewrite) != 0) + { + int i; + uschar *where = US"?"; + + for (i = 0; i < where_list_size; i++) + { + if (flag == where_list[i].bit) + { + where = where_list[i].string; + break; + } + } + log_write(L_address_rewrite, + LOG_MAIN, "\"%s\" from %s rewritten as \"%s\" by rule %d", + yield, where, new, rule_number); + } + + /* A header will only actually be added if header_last is non-NULL, + i.e. during message reception or delivery, but add_header should not + be set TRUE during delivery, as otherwise multiple instances of the header + can fill up the -H file and make it embarrassingly large. We don't need + to set header_rewritten because the -H file always gets written at the end + of message reception. */ + + if (add_header) + header_add(htype_old, "X-rewrote-%s: %s\n", name, subject); + + /* Handle the case when replacement of the whole address is possible. + This happens only when whole is not NULL and we are rewriting a header. + If *whole is already TRUE it means that a previous rule had the w + flag set and so we must preserve the non-active portion of the current + subject unless the current rule also has the w flag set. */ + + if (whole != NULL && (flag & rewrite_all_headers) != 0) + { + /* Current rule has the w flag set. We must ensure the phrase parts + are syntactically valid if they are present. */ + + if ((rule->flags & rewrite_whole) != 0) + { + if (start > 0 && new[start-1] == '<') + { + uschar *p1 = new + start - 1; + uschar *p2 = new + end + 1; + uschar *pf1, *pf2; + uschar buff1[256], buff2[256]; + + while (p1 > new && p1[-1] == ' ') p1--; + pf1 = parse_fix_phrase(new, p1 - new, buff1, sizeof(buff1)); + while (*p2 == ' ') p2++; + pf2 = parse_fix_phrase(p2, Ustrlen(p2), buff2, sizeof(buff2)); + + /* Note that pf1 and pf2 are NOT necessarily buff1 and buff2. For + a non-RFC 2047 phrase that does not need to be RFC 2822 quoted, they + will be buff1+1 and buff2+1. */ + + start = Ustrlen(pf1) + start + new - p1; + end = start + Ustrlen(newparsed); + new = string_sprintf("%s%.*s%s", pf1, p2 - p1, p1, pf2); + } + + /* Now accept the whole thing */ + + yield = new; + yield_start = start; + yield_end = end; + subject = newparsed; + *whole = TRUE; + } + + /* Current rule does not have the w flag set; if not previously + done any whole rewriting, behave in non-whole manner. */ + + else if (!*whole) goto NEVER_WHOLE; + + /* Current rule does not have the w flag set, but a previous + rule did rewrite the whole address. Thus yield and subject will be + different. Preserve the previous non-active part of the address. */ + + else + { + subject = newparsed; + new = string_sprintf("%.*s%s%n%s", + yield_start, yield, subject, &end, yield + yield_end); + yield_end = end; + yield = new; + } + } + + /* Rule just rewrites active part, or handling an envelope. This + code is obeyed only when all rules so far have not done "whole" + replacement. */ + + else + { + NEVER_WHOLE: + subject = yield = newparsed; + } + + domain = NULL; /* Reset for next rule */ + + /* If no further rewrites are to be done, set the done flag. This allows + repeats of the current rule if configured before breaking the loop. */ + + if ((rule->flags & rewrite_quit) != 0) done = TRUE; + + /* Allow the current rule to be applied up to 10 times if + requested. */ + + if ((rule->flags & rewrite_repeat) != 0) + { + if (count++ < 10) goto REPEAT_RULE; + log_write(0, LOG_MAIN|LOG_PANIC, "rewrite rule repeat ignored after 10 " + "times"); + } + } + +/* Unset expansion numeric variables, and that's it. */ + +expand_nmax = -1; +return yield; +} + + + +/************************************************* +* Ensure qualification and rewrite * +*************************************************/ + +/* This function is called for envelope addresses, the boolean specifying +whether a recipient or a sender. It must first of all ensure the address is +fully qualified, and then apply any relevant re-writing rules. The add-header +flag causes a header to be added, recording the old address. This is marked +"old", so that it is never transported anywhere; it exists for local checking +and debugging purposes. + +Arguments: + s the address to be considered + is_recipient TRUE for recipient addresses; FALSE otherwise + add_header add "X-rewrote-xxx" header when rewriting; this is + set TRUE only for calls from the reception functions + rewrite_rules points to chain of rewrite rules + existflags bits indicating which headers there are rewrites for + (just an optimisation) + +Returns: possibly rewritten address +*/ + +uschar * +rewrite_address(uschar *s, BOOL is_recipient, BOOL add_header, + rewrite_rule *rewrite_rules, int existflags) +{ +int flag = is_recipient? rewrite_envto : rewrite_envfrom; +s = rewrite_address_qualify(s, is_recipient); +if ((existflags & flag) != 0) + { + uschar *new = rewrite_one(s, flag, NULL, add_header, is_recipient? + US"original-recipient" : US"sender", rewrite_rules); + if (new != s) s = new; + } +return s; +} + + + +/************************************************* +* Qualify and possibly rewrite one header * +*************************************************/ + +/* This is called only from rewrite_header() below, either when reading a +message. or when routing, in order to rewrite addresses that get changed by a +router. This is normally the addition of full qualification to a partial +domain. The first rewriting rule in this case is "change routed_old into +routed_new", and it applies to all header lines that contain addresses. Then +header-specific rewriting rules are applied. + +Before rewriting can be done, addresses without domains have to be qualified. +This should only be done for messages from "local" senders. This is a difficult +concept to pin down, what with the use of SMTP both as a submission and as a +transmission protocol. Exim normally requires incoming SMTP to contain fully- +qualified addresses, but there are options to permit unqualified ones from +certain hosts. For those hosts only, addresses in headers can also be +qualified. For other hosts, unqualified addresses in headers do not get touched +in any way. For locally sourced messages, unqualified addresses always get +qualified, except when -bnq is used to explicitly suppress this. + +Arguments: + h pointer to header line block + flag indicates which header this is + routed_old if not NULL, this is a rewrite caused by a router, changing + this domain into routed_new + routed_new new routed domain if routed_old is not NULL + rewrite_rules points to chain of rewriting rules + existflags bits indicating which rewrites exist + replace if TRUE, insert the new header in the chain after the old + one, and mark the old one "replaced" + +Returns: NULL if header unchanged; otherwise the rewritten header +*/ + +static header_line * +rewrite_one_header(header_line *h, int flag, uschar *routed_old, + uschar *routed_new, rewrite_rule *rewrite_rules, int existflags, BOOL replace) +{ +int lastnewline = 0; +header_line *newh = NULL; +void *function_reset_point = store_get(0); +uschar *s = Ustrchr(h->text, ':') + 1; +while (isspace(*s)) s++; + +DEBUG(D_rewrite) + debug_printf("rewrite_one_header: type=%c:\n %s", h->type, h->text); + +parse_allow_group = TRUE; /* Allow group syntax */ + +/* Loop for multiple addresses in the header. We have to go through them all +in case any need qualifying, even if there's no rewriting. Pathological headers +may have thousands of addresses in them, so cause the store to be reset for +any that don't actually get rewritten. We also play silly games for those that +_are_ rewritten so as to avoid runaway store usage for these kinds of header. +We want to avoid keeping store for any intermediate versions. */ + +while (*s != 0) + { + uschar *sprev; + uschar *ss = parse_find_address_end(s, FALSE); + uschar *recipient, *new, *errmess; + void *loop_reset_point = store_get(0); + BOOL changed = FALSE; + int terminator = *ss; + int start, end, domain; + + /* Temporarily terminate the string at this point, and extract the + operative address within. Then put back the terminator and prepare for + the next address, saving the start of the old one. */ + + *ss = 0; + recipient = parse_extract_address(s,&errmess,&start,&end,&domain,FALSE); + *ss = terminator; + sprev = s; + s = ss + (terminator? 1:0); + while (isspace(*s)) s++; + + /* There isn't much we can do for syntactic disasters at this stage. + Pro tem (possibly for ever) ignore them. */ + + if (recipient == NULL) + { + store_reset(loop_reset_point); + continue; + } + + /* If routed_old is not NULL, this is a rewrite caused by a router, + consisting of changing routed_old into routed_new, and applying to all + headers. If the header address has no domain, it is excluded, since a router + rewrite affects domains only. The new value should always be fully qualified, + but it may be something that has an explicit re-write rule set, so we need to + check the configured rules subsequently as well. (Example: there's an + explicit rewrite turning *.foo.com into foo.com, and an address is supplied + as abc@xyz, which the DNS lookup turns into abc@xyz.foo.com). However, if no + change is made here, don't bother carrying on. */ + + if (routed_old != NULL) + { + if (domain <= 0 || strcmpic(recipient+domain, routed_old) != 0) continue; + recipient[domain-1] = 0; + new = string_sprintf("%s@%s", recipient, routed_new); + DEBUG(D_rewrite) + { + recipient[domain-1] = '@'; + debug_printf("%s rewritten by router as %s\n", recipient, new); + } + recipient = new; + changed = TRUE; + } + + /* This is not a router-inspired rewrite. Ensure the address is fully + qualified if that is permitted. If an unqualified address was received + from a host that isn't listed, do not continue rewriting this address. + Sender, From or Reply-To headers are treated as senders, the rest as + recipients. This matters only when there are different qualify strings. */ + + else + { + BOOL is_recipient = + (flag & (rewrite_sender | rewrite_from | rewrite_replyto)) == 0; + new = rewrite_address_qualify(recipient, is_recipient); + changed = (new != recipient); + recipient = new; + + /* Can only qualify if permitted; if not, no rewrite. */ + + if (changed && ((is_recipient && !allow_unqualified_recipient) || + (!is_recipient && !allow_unqualified_sender))) + { + store_reset(loop_reset_point); + continue; + } + } + + /* If there are rewrite rules for this type of header, apply + them. This test is just for efficiency, to save scanning the rules + in cases when nothing is going to change. If any rewrite rule had the + "whole" flag set, adjust the pointers so that the whole address gets + replaced, except possibly a final \n. */ + + if ((existflags & flag) != 0) + { + BOOL whole; + new = rewrite_one(recipient, flag, &whole, FALSE, NULL, rewrite_rules); + if (new != recipient) + { + changed = TRUE; + if (whole) + { + start = 0; + end = ss - sprev; + if (sprev[end-1] == '\n') end--; + } + } + } + + /* If nothing has changed, lose all dynamic store obtained in this loop, and + move on to the next address. We can't reset to the function start store + point, because we may have a rewritten line from a previous time round the + loop. */ + + if (!changed) store_reset(loop_reset_point); + + /* If the address has changed, create a new header containing the + rewritten address. We do not need to set the chain pointers at this + stage. We want to avoid using more and more memory if the header is very long + and contains lots and lots of rewritten addresses. Therefore, we build the + new text string in malloc store, then at the end we reset dynamic store + before copying the new header to a new block (and then freeing the malloc + block). The header must end up in dynamic store so that it's freed at the end + of receiving a message. */ + + else + { + int remlen; + int newlen = Ustrlen(new); + int oldlen = end - start; + + header_line *prev = (newh == NULL)? h : newh; + uschar *newt = store_malloc(prev->slen - oldlen + newlen + 4); + uschar *newtstart = newt; + + int type = prev->type; + int slen = prev->slen - oldlen + newlen; + + /* Build the new header text by copying the old and putting in the + replacement. This process may make the header substantially longer + than it was before - qualification of a list of bare addresses can + often do this - so we stick in a newline after the re-written address + if it has increased in length and ends more than 40 characters in. In + fact, the code is not perfect, since it does not scan for existing + newlines in the header, but it doesn't seem worth going to that + amount of trouble. */ + + Ustrncpy(newt, prev->text, sprev - prev->text + start); + newt += sprev - prev->text + start; + *newt = 0; + Ustrcat(newt, new); + newt += newlen; + remlen = s - (sprev + end); + if (remlen > 0) + { + Ustrncpy(newt, sprev + end, remlen); + newt += remlen; + *newt = 0; + } + + /* Must check that there isn't a newline here anyway; in particular, there + will be one at the very end of the header, where we DON'T want to insert + another one! The pointer s has been skipped over white space, so just + look back to see if the last non-space-or-tab was a newline. */ + + if (newlen > oldlen && newt - newtstart - lastnewline > 40) + { + uschar *p = s - 1; + while (p >= prev->text && (*p == ' ' || *p == '\t')) p--; + if (*p != '\n') + { + lastnewline = newt - newtstart; + Ustrcat(newt, "\n\t"); + slen += 2; + } + } + + /* Finally, the remaining unprocessed addresses, if any. */ + + Ustrcat(newt, s); + + DEBUG(D_rewrite) debug_printf("newlen=%d newtype=%c newtext:\n%s", + slen, type, newtstart); + + /* Compute the length of the rest of the header line before we possibly + flatten a previously rewritten copy. */ + + remlen = (s - prev->text) - oldlen + newlen; + + /* We have the new text in a malloc block. That enables us to release all + the memory that has been used, back to the point at which the function was + entered. Then set up a new header in dynamic store. This will override a + rewritten copy from a previous time round this loop. */ + + store_reset(function_reset_point); + newh = store_get(sizeof(header_line)); + newh->type = type; + newh->slen = slen; + newh->text = string_copyn(newtstart, slen); + store_free(newtstart); + + /* Set up for scanning the rest of the header */ + + s = newh->text + remlen; + DEBUG(D_rewrite) debug_printf("remainder: %s", (*s == 0)? US"\n" : s); + } + } + +parse_allow_group = FALSE; /* Reset group flags */ +parse_found_group = FALSE; + +/* If a rewrite happened and "replace" is true, put the new header into the +chain following the old one, and mark the old one as replaced. */ + +if (newh != NULL && replace) + { + newh->next = h->next; + if (newh->next == NULL) header_last = newh; + h->type = htype_old; + h->next = newh; + } + +return newh; +} + + + + +/************************************************* +* Rewrite a header line * +*************************************************/ + +/* This function may be passed any old header line. It must detect those which +contain addresses, then then apply any rewriting rules that apply. If +routed_old is NULL, only the configured rewriting rules are consulted. +Otherwise, the rewriting rule is "change routed_old into routed_new", and it +applies to all header lines that contain addresses. Then header-specific +rewriting rules are applied. + +The old header line is flagged as "old". Old headers are saved on the spool for +debugging but are never sent to any recipients. + +Arguments: + h header line to rewrite + routed_old if not NULL, this is a rewrite caused by a router, changing + this domain into routed_new + routed_new new routed domain if routed_old is not NULL + rewrite_rules points to chain of rewrite rules + existflags bits indicating which rewrites exist + replace if TRUE, the new header is inserted into the header chain + after the old one, and the old one is marked replaced + +Returns: NULL if header unchanged; otherwise the rewritten header +*/ + +header_line * +rewrite_header(header_line *h, uschar *routed_old, uschar *routed_new, + rewrite_rule *rewrite_rules, int existflags, BOOL replace) +{ +switch (h->type) + { + case htype_sender: + return rewrite_one_header(h, rewrite_sender, routed_old, routed_new, + rewrite_rules, existflags, replace); + + case htype_from: + return rewrite_one_header(h, rewrite_from, routed_old, routed_new, + rewrite_rules, existflags, replace); + + case htype_to: + return rewrite_one_header(h, rewrite_to, routed_old, routed_new, + rewrite_rules, existflags, replace); + + case htype_cc: + return rewrite_one_header(h, rewrite_cc, routed_old, routed_new, + rewrite_rules, existflags, replace); + + case htype_bcc: + return rewrite_one_header(h, rewrite_bcc, routed_old, routed_new, + rewrite_rules, existflags, replace); + + case htype_reply_to: + return rewrite_one_header(h, rewrite_replyto, routed_old, routed_new, + rewrite_rules, existflags, replace); + } + +return NULL; +} + + + +/************************************************ +* Test rewriting rules * +************************************************/ + +/* Called from the mainline as a result of the -brw option. Test the +address for all possible cases. + +Argument: the address to test +Returns: nothing +*/ + +void rewrite_test(uschar *s) +{ +uschar *recipient, *error; +int i, start, end, domain; +BOOL done_smtp = FALSE; + +if (rewrite_existflags == 0) + { + printf("No rewrite rules are defined\n"); + return; + } + +/* Do SMTP rewrite only if a rule with the S flag exists. Allow <> by +pretending it is a sender. */ + +if ((rewrite_existflags & rewrite_smtp) != 0) + { + uschar *new = rewrite_one(s, rewrite_smtp|rewrite_smtp_sender, NULL, FALSE, + US"", global_rewrite_rules); + if (new != s) + { + if (*new == 0) + printf(" SMTP: <>\n"); + else + printf(" SMTP: %s\n", new); + done_smtp = TRUE; + } + } + +/* Do the other rewrites only if a rule without the S flag exists */ + +if ((rewrite_existflags & ~rewrite_smtp) == 0) return; + +/* Qualify if necessary before extracting the address */ + +if (parse_find_at(s) == NULL) + s = string_sprintf("%s@%s", s, qualify_domain_recipient); + +recipient = parse_extract_address(s, &error, &start, &end, &domain, FALSE); + +if (recipient == NULL) + { + if (!done_smtp) + printf("Syntax error in %s\n%c%s\n", s, toupper(error[0]), error+1); + return; + } + +for (i = 0; i < 8; i++) + { + BOOL whole = FALSE; + int flag = 1 << i; + uschar *new = rewrite_one(recipient, flag, &whole, FALSE, US"", + global_rewrite_rules); + printf("%s: ", rrname[i]); + if (*new == 0) + printf("<>\n"); + else if (whole || (flag & rewrite_all_headers) == 0) + printf("%s\n", CS new); + else printf("%.*s%s%s\n", start, s, new, s+end); + } +} + +/* End of rewrite.c */ diff --git a/src/src/rfc2047.c b/src/src/rfc2047.c new file mode 100644 index 000000000..1a4ecc251 --- /dev/null +++ b/src/src/rfc2047.c @@ -0,0 +1,351 @@ +/* $Cambridge: exim/src/src/rfc2047.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* This file contains a function for decoding message header lines that may +contain encoded "words" according to the rules described in + + RFC-2047 at http://www.ietf.org/rfc/rfc2047.txt + +The function is a rewritten version of code created by Norihisa Washitake. +The original could be used both inside Exim (as part of a patch) or in a +freestanding form. The original contained some built-in code conversions; I +have chosen only to do code conversions if iconv() is supported by the OS. +Because there were quite a lot of hacks to be done, for a variety of reasons, +I rewrote the code. + +You can find the latest version of the original library at + + http://washitake.com/mail/exim/mime/ + +The code below is almost completely unlike the original. */ + + +#include "exim.h" + + +/************************************************* +* Do a QP conversion * +*************************************************/ + +/* This function decodes "quoted printable" into bytes. + +Arguments: + string the string that includes QP escapes + ptrptr where to return pointer to the decoded string + +Returns: the length of the decoded string, or -1 on failure +*/ + +static int +rfc2047_qpdecode(uschar *string, uschar **ptrptr) +{ +int len = 0; +uschar *ptr; + +ptr = *ptrptr = store_get(Ustrlen(string) + 1); /* No longer than this */ + +while (*string != 0) + { + register int ch = *string++; + + if (ch == '_') *ptr++ = ' '; + else if (ch == '=') + { + int a = *string; + int b = (a == 0)? 0 : string[1]; + if (!isxdigit(a) || !isxdigit(b)) return -1; /* Bad QP string */ + *ptr++ = ((Ustrchr(hex_digits, tolower(a)) - hex_digits) << 4) + + Ustrchr(hex_digits, tolower(b)) - hex_digits; + string += 2; + } + else if (ch == ' ' || ch == '\t') return -1; /* Whitespace is illegal */ + else *ptr++ = ch; + + len++; + } + +*ptr = 0; +return len; +} + + + +/************************************************* +* Decode next MIME word * +*************************************************/ + +/* Scan a string to see if a MIME word exists; pass back the separator +points in the string. + +Arguments: + string subject string + lencheck TRUE to enforce maximum length check + q1ptr pass back address of first question mark + q2ptr pass back address of second question mark + endptr pass back address of final ?= + dlenptr pass back length of decoded string + dptrptr pass back pointer to decoded string + +Returns: address of =? or NULL if not present +*/ + +static uschar * +decode_mimeword(uschar *string, BOOL lencheck, uschar **q1ptr, uschar **q2ptr, + uschar **endptr, size_t *dlenptr, uschar **dptrptr) +{ +uschar *mimeword; +for (;; string = mimeword + 2) + { + int encoding; + int dlen = -1; + + if ((mimeword = Ustrstr(string, "=?")) == NULL || + (*q1ptr = Ustrchr(mimeword+2, '?')) == NULL || + (*q2ptr = Ustrchr(*q1ptr+1, '?')) == NULL || + (*endptr = Ustrstr(*q2ptr+1, "?=")) == NULL) return NULL; + + /* We have found =?xxx?xxx?xxx?= in the string. Optionally check the + length, and that the second field is just one character long. If not, + continue the loop to search again. We must start just after the initial =? + because we might have found =?xxx=?xxx?xxx?xxx?=. */ + + if ((lencheck && *endptr - mimeword > 73) || *q2ptr - *q1ptr != 2) continue; + + /* Get the encoding letter, and decode the data string. */ + + encoding = toupper((*q1ptr)[1]); + **endptr = 0; + if (encoding == 'B') + dlen = auth_b64decode(*q2ptr+1, dptrptr); + else if (encoding == 'Q') + dlen = rfc2047_qpdecode(*q2ptr+1, dptrptr); + **endptr = '?'; /* restore */ + + /* If the decoding succeeded, we are done. Set the length of the decoded + string, and pass back the initial pointer. Otherwise, the loop continues. */ + + if (dlen >= 0) + { + *dlenptr = (size_t)dlen; + return mimeword; + } + } + +/* Control should never actually get here */ +} + + + +/************************************************* +* Decode and convert an RFC 2047 string * +*************************************************/ + +/* There are two functions defined here. The original one was rfc2047_decode() +and it was documented in the local_scan() interface. I needed to add an extra +argument for use by expand_string(), so I created rfc2047_decode2() for that +purpose. The original function became a stub that just supplies NULL for the +new argument (sizeptr). + +An RFC 2047-encoded string may contain one or more "words", each of the +form =?...?.?...?= with the first ... specifying the character code, the +second being Q (for quoted printable) or B for Base64 encoding. The third ... +is the actual data. + +This function first decodes each "word" into bytes from the Q or B encoding. +Then, if provided with the name of a charset encoding, and if iconv() is +available, it attempts to translate the result to the named character set. +If this fails, the binary string is returned with an error message. + +If a binary zero is encountered in the decoded string, it is replaced by the +contents of the zeroval argument. For use with Exim headers, the value must not +be 0 because they are handled as zero-terminated strings. When zeroval==0, +lenptr should not be NULL. + +Arguments: + string the subject string + lencheck TRUE to enforce maximum MIME word length + target the name of the target encoding for MIME words, or NULL for + no charset translation + zeroval the value to use for binary zero bytes + lenptr if not NULL, the length of the result is returned via + this variable + sizeptr if not NULL, the length of a new store block in which the + result is built is placed here; if no new store is obtained, + the value is not changed + error for error messages; NULL if no problem; this can be set + when the yield is non-NULL if there was a charset + translation problem + +Returns: the decoded, converted string, or NULL on error; if there are + no MIME words in the string, the original string is returned +*/ + +uschar * +rfc2047_decode2(uschar *string, BOOL lencheck, uschar *target, int zeroval, + int *lenptr, int *sizeptr, uschar **error) +{ +int ptr = 0; +int size = Ustrlen(string); +size_t dlen; +uschar *dptr, *yield; +uschar *mimeword, *q1, *q2, *endword; + +*error = NULL; +mimeword = decode_mimeword(string, lencheck, &q1, &q2, &endword, &dlen, &dptr); + +if (mimeword == NULL) + { + if (lenptr != NULL) *lenptr = size; + return string; + } + +/* Scan through the string, decoding MIME words and copying intermediate text, +building the result as we go. The result may be longer than the input if it is +translated into a multibyte code such as UTF-8. That's why we use the dynamic +string building code. */ + +yield = store_get(++size); + +while (mimeword != NULL) + { + + #if HAVE_ICONV + iconv_t icd = (iconv_t)(-1); + #endif + + if (mimeword != string) + yield = string_cat(yield, &size, &ptr, string, mimeword - string); + + /* Do a charset translation if required. This is supported only on hosts + that have the iconv() function. Translation errors set error, but carry on, + using the untranslated data. If there is more than one error, the message + passed back refers to the final one. We use a loop to cater for the case + of long strings - the RFC puts limits on the length, but it's best to be + robust. */ + + #if HAVE_ICONV + *q1 = 0; + if (target != NULL && strcmpic(target, mimeword+2) != 0) + { + icd = iconv_open(CS target, CS(mimeword+2)); + + if (icd == (iconv_t)(-1)) + { + *error = string_sprintf("iconv_open(\"%s\", \"%s\") failed: %s%s", + target, mimeword+2, strerror(errno), + (errno == EINVAL)? " (maybe unsupported conversion)" : ""); + } + } + *q1 = '?'; + #endif + + while (dlen > 0) + { + uschar *tptr = NULL; /* Stops compiler warning */ + int tlen = -1; + + #if HAVE_ICONV + uschar tbuffer[256]; + uschar *outptr = tbuffer; + size_t outleft = sizeof(tbuffer); + + /* If translation is required, go for it. */ + + if (icd != (iconv_t)(-1)) + { + (void)iconv(icd, (ICONV_ARG2_TYPE)(&dptr), &dlen, CSS &outptr, &outleft); + + /* If outptr has been adjusted, there is some output. Set up to add it to + the output buffer. The function will have adjusted dptr and dlen. If + iconv() stopped because of an error, we'll pick it up next time when + there's no output. + + If there is no output, we expect there to have been a translation + error, because we know there was at least one input byte. We leave the + value of tlen as -1, which causes the rest of the input to be copied + verbatim. */ + + if (outptr > tbuffer) + { + tptr = tbuffer; + tlen = outptr - tbuffer; + } + else + { + DEBUG(D_any) debug_printf("iconv error translating \"%.*s\" to %s: " + "%s\n", endword + 2 - mimeword, mimeword, target, strerror(errno)); + } + } + + #endif + + /* No charset translation is happening or there was a translation error; + just set up the original as the string to be added, and mark it all used. + */ + + if (tlen == -1) + { + tptr = dptr; + tlen = dlen; + dlen = 0; + } + + /* Deal with zero values; convert them if requested. */ + + if (zeroval != 0) + { + int i; + for (i = 0; i < tlen; i++) + if (tptr[i] == 0) tptr[i] = zeroval; + } + + /* Add the new string onto the result */ + + yield = string_cat(yield, &size, &ptr, tptr, tlen); + } + + #if HAVE_ICONV + if (icd != (iconv_t)(-1)) iconv_close(icd); + #endif + + /* Update string past the MIME word; skip any white space if the next thing + is another MIME word. */ + + string = endword + 2; + mimeword = decode_mimeword(string, lencheck, &q1, &q2, &endword, &dlen, &dptr); + if (mimeword != NULL) + { + uschar *s = string; + while (isspace(*s)) s++; + if (s == mimeword) string = s; + } + } + +/* Copy the remaining characters of the string, zero-terminate it, and return +the length as well if requested. */ + +yield = string_cat(yield, &size, &ptr, string, Ustrlen(string)); +yield[ptr] = 0; +if (lenptr != NULL) *lenptr = ptr; +if (sizeptr != NULL) *sizeptr = size; +return yield; +} + + +/* This is the stub that provides the original interface without the sizeptr +argument. */ + +uschar * +rfc2047_decode(uschar *string, BOOL lencheck, uschar *target, int zeroval, + int *lenptr, uschar **error) +{ +return rfc2047_decode2(string, lencheck, target, zeroval, lenptr, NULL, error); +} + +/* End of rfc2047.c */ diff --git a/src/src/route.c b/src/src/route.c new file mode 100644 index 000000000..427570ef7 --- /dev/null +++ b/src/src/route.c @@ -0,0 +1,1889 @@ +/* $Cambridge: exim/src/src/route.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Functions concerned with routing, and the list of generic router options. */ + + +#include "exim.h" + + + +/* Generic options for routers, all of which live inside router_instance +data blocks and which therefore have the opt_public flag set. */ + +optionlist optionlist_routers[] = { + { "*expand_group", opt_stringptr | opt_hidden | opt_public, + (void *)(offsetof(router_instance, expand_gid)) }, + { "*expand_more", opt_stringptr | opt_hidden | opt_public, + (void *)(offsetof(router_instance, expand_more)) }, + { "*expand_unseen", opt_stringptr | opt_hidden | opt_public, + (void *)(offsetof(router_instance, expand_unseen)) }, + { "*expand_user", opt_stringptr | opt_hidden | opt_public, + (void *)(offsetof(router_instance, expand_uid)) }, + { "*set_group", opt_bool | opt_hidden | opt_public, + (void *)(offsetof(router_instance, gid_set)) }, + { "*set_user", opt_bool | opt_hidden | opt_public, + (void *)(offsetof(router_instance, uid_set)) }, + { "address_data", opt_stringptr|opt_public, + (void *)(offsetof(router_instance, address_data)) }, + { "address_test", opt_bool|opt_public, + (void *)(offsetof(router_instance, address_test)) }, + { "cannot_route_message", opt_stringptr | opt_public, + (void *)(offsetof(router_instance, cannot_route_message)) }, + { "caseful_local_part", opt_bool | opt_public, + (void *)(offsetof(router_instance, caseful_local_part)) }, + { "check_local_user", opt_bool | opt_public, + (void *)(offsetof(router_instance, check_local_user)) }, + { "condition", opt_stringptr|opt_public, + (void *)offsetof(router_instance, condition) }, + { "debug_print", opt_stringptr | opt_public, + (void *)offsetof(router_instance, debug_string) }, + { "disable_logging", opt_bool | opt_public, + (void *)offsetof(router_instance, disable_logging) }, + { "domains", opt_stringptr|opt_public, + (void *)offsetof(router_instance, domains) }, + { "driver", opt_stringptr|opt_public, + (void *)offsetof(router_instance, driver_name) }, + { "errors_to", opt_stringptr|opt_public, + (void *)(offsetof(router_instance, errors_to)) }, + { "expn", opt_bool|opt_public, + (void *)offsetof(router_instance, expn) }, + { "fail_verify", opt_bool_verify|opt_hidden|opt_public, + (void *)offsetof(router_instance, fail_verify_sender) }, + { "fail_verify_recipient", opt_bool|opt_public, + (void *)offsetof(router_instance, fail_verify_recipient) }, + { "fail_verify_sender", opt_bool|opt_public, + (void *)offsetof(router_instance, fail_verify_sender) }, + { "fallback_hosts", opt_stringptr|opt_public, + (void *)offsetof(router_instance, fallback_hosts) }, + { "group", opt_expand_gid | opt_public, + (void *)(offsetof(router_instance, gid)) }, + { "headers_add", opt_stringptr|opt_public, + (void *)offsetof(router_instance, extra_headers) }, + { "headers_remove", opt_stringptr|opt_public, + (void *)offsetof(router_instance, remove_headers) }, + { "ignore_target_hosts",opt_stringptr|opt_public, + (void *)offsetof(router_instance, ignore_target_hosts) }, + { "initgroups", opt_bool | opt_public, + (void *)(offsetof(router_instance, initgroups)) }, + { "local_part_prefix", opt_stringptr|opt_public, + (void *)offsetof(router_instance, prefix) }, + { "local_part_prefix_optional",opt_bool|opt_public, + (void *)offsetof(router_instance, prefix_optional) }, + { "local_part_suffix", opt_stringptr|opt_public, + (void *)offsetof(router_instance, suffix) }, + { "local_part_suffix_optional",opt_bool|opt_public, + (void *)offsetof(router_instance, suffix_optional) }, + { "local_parts", opt_stringptr|opt_public, + (void *)offsetof(router_instance, local_parts) }, + { "log_as_local", opt_bool|opt_public, + (void *)offsetof(router_instance, log_as_local) }, + { "more", opt_expand_bool|opt_public, + (void *)offsetof(router_instance, more) }, + { "pass_on_timeout", opt_bool|opt_public, + (void *)offsetof(router_instance, pass_on_timeout) }, + { "pass_router", opt_stringptr|opt_public, + (void *)offsetof(router_instance, pass_router_name) }, + { "redirect_router", opt_stringptr|opt_public, + (void *)offsetof(router_instance, redirect_router_name) }, + { "require_files", opt_stringptr|opt_public, + (void *)offsetof(router_instance, require_files) }, + { "retry_use_local_part", opt_bool|opt_public, + (void *)offsetof(router_instance, retry_use_local_part) }, + { "router_home_directory", opt_stringptr|opt_public, + (void *)offsetof(router_instance, router_home_directory) }, + { "self", opt_stringptr|opt_public, + (void *)(offsetof(router_instance, self)) }, + { "senders", opt_stringptr|opt_public, + (void *)offsetof(router_instance, senders) }, + #ifdef SUPPORT_TRANSLATE_IP_ADDRESS + { "translate_ip_address", opt_stringptr|opt_public, + (void *)offsetof(router_instance, translate_ip_address) }, + #endif + { "transport", opt_stringptr|opt_public, + (void *)offsetof(router_instance, transport_name) }, + { "transport_current_directory", opt_stringptr|opt_public, + (void *)offsetof(router_instance, current_directory) }, + { "transport_home_directory", opt_stringptr|opt_public, + (void *)offsetof(router_instance, home_directory) }, + { "unseen", opt_expand_bool|opt_public, + (void *)offsetof(router_instance, unseen) }, + { "user", opt_expand_uid | opt_public, + (void *)(offsetof(router_instance, uid)) }, + { "verify", opt_bool_verify|opt_hidden|opt_public, + (void *)offsetof(router_instance, verify_sender) }, + { "verify_only", opt_bool|opt_public, + (void *)offsetof(router_instance, verify_only) }, + { "verify_recipient", opt_bool|opt_public, + (void *)offsetof(router_instance, verify_recipient) }, + { "verify_sender", opt_bool|opt_public, + (void *)offsetof(router_instance, verify_sender) } +}; + +int optionlist_routers_size = sizeof(optionlist_routers)/sizeof(optionlist); + + + +/************************************************* +* Set router pointer from name * +*************************************************/ + +/* This function is used for the redirect_router and pass_router options and +called from route_init() below. + +Arguments: + r the current router + name new router name + ptr where to put the pointer + after TRUE if router must follow this one + +Returns: nothing. +*/ + +static void +set_router(router_instance *r, uschar *name, router_instance **ptr, BOOL after) +{ +BOOL afterthis = FALSE; +router_instance *rr; + +for (rr = routers; rr != NULL; rr = rr->next) + { + if (Ustrcmp(name, rr->name) == 0) + { + *ptr = rr; + break; + } + if (rr == r) afterthis = TRUE; + } + +if (rr == NULL) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG, + "new_router \"%s\" not found for \"%s\" router", name, r->name); + +if (after && !afterthis) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG, + "new_router \"%s\" does not follow \"%s\" router", name, r->name); +} + + + +/************************************************* +* Initialize router list * +*************************************************/ + +/* Read the routers section of the configuration file, and set up a chain of +router instances according to its contents. Each router has generic options and +may also have its own private options. This function is only ever called when +routers == NULL. We use generic code in readconf to do the work. It will set +values from the configuration file, and then call the driver's initialization +function. */ + +void +route_init(void) +{ +router_instance *r; + +readconf_driver_init(US"router", + (driver_instance **)(&routers), /* chain anchor */ + (driver_info *)routers_available, /* available drivers */ + sizeof(router_info), /* size of info blocks */ + &router_defaults, /* default values for generic options */ + sizeof(router_instance), /* size of instance block */ + optionlist_routers, /* generic options */ + optionlist_routers_size); + +for (r = routers; r != NULL; r = r->next) + { + uschar *s = r->self; + + /* If log_as_local is unset, its overall default is FALSE. (The accept + router defaults it to TRUE.) */ + + if (r->log_as_local == TRUE_UNSET) r->log_as_local = FALSE; + + /* Check for transport or no transport on certain routers */ + + if ((r->info->ri_flags & ri_yestransport) != 0 && + r->transport_name == NULL && + !r->verify_only) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG, "%s router:\n " + "a transport is required for this router", r->name); + + if ((r->info->ri_flags & ri_notransport) != 0 && + r->transport_name != NULL) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG, "%s router:\n " + "a transport must not be defined for this router", r->name); + + /* The "self" option needs to be decoded into a code value and possibly a + new domain string and a rewrite boolean. */ + + if (Ustrcmp(s, "freeze") == 0) r->self_code = self_freeze; + else if (Ustrcmp(s, "defer") == 0) r->self_code = self_defer; + else if (Ustrcmp(s, "send") == 0) r->self_code = self_send; + else if (Ustrcmp(s, "pass") == 0) r->self_code = self_pass; + else if (Ustrcmp(s, "fail") == 0) r->self_code = self_fail; + else if (Ustrncmp(s, "reroute:", 8) == 0) + { + s += 8; + while (isspace(*s)) s++; + if (Ustrncmp(s, "rewrite:", 8) == 0) + { + r->self_rewrite = TRUE; + s += 8; + while (isspace(*s)) s++; + } + r->self = s; + r->self_code = self_reroute; + } + + else log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n " + "%s is not valid for the self option", r->name, s); + + /* If any router has check_local_user set, default retry_use_local_part + TRUE; otherwise its default is FALSE. */ + + if (r->retry_use_local_part == TRUE_UNSET) + r->retry_use_local_part = r->check_local_user; + + /* Build a host list if fallback hosts is set. */ + + host_build_hostlist(&(r->fallback_hostlist), r->fallback_hosts, FALSE); + + /* Check redirect_router and pass_router are valid */ + + if (r->redirect_router_name != NULL) + set_router(r, r->redirect_router_name, &(r->redirect_router), FALSE); + + if (r->pass_router_name != NULL) + set_router(r, r->pass_router_name, &(r->pass_router), TRUE); + } +} + + + +/************************************************* +* Tidy up after routing * +*************************************************/ + +/* Routers are entitled to keep hold of certain resources in their instance +blocks so as to save setting them up each time. An example is an open file. +Such routers must provide a tidyup entry point which is called when all routing +is finished, via this function. */ + +void +route_tidyup(void) +{ +router_instance *r; +for (r = routers; r != NULL; r = r->next) + if (r->info->tidyup != NULL) (r->info->tidyup)(r); +} + + + +/************************************************* +* Check local part for prefix * +*************************************************/ + +/* This function is handed a local part and a list of possible prefixes; if any +one matches, return the prefix length. A prefix beginning with '*' is a +wildcard. + +Arguments: + local_part the local part to check + prefixes the list of prefixes + +Returns: length of matching prefix or zero +*/ + +int +route_check_prefix(uschar *local_part, uschar *prefixes) +{ +int sep = 0; +uschar *prefix; +uschar *listptr = prefixes; +uschar prebuf[64]; + +while ((prefix = string_nextinlist(&listptr, &sep, prebuf, sizeof(prebuf))) + != NULL) + { + int plen = Ustrlen(prefix); + if (prefix[0] == '*') + { + uschar *p; + prefix++; + for (p = local_part + Ustrlen(local_part) - (--plen); + p >= local_part; p--) + if (strncmpic(prefix, p, plen) == 0) return plen + p - local_part; + } + else + if (strncmpic(prefix, local_part, plen) == 0) return plen; + } + +return 0; +} + + + +/************************************************* +* Check local part for suffix * +*************************************************/ + +/* This function is handed a local part and a list of possible suffixes; +if any one matches, return the suffix length. A suffix ending with '*' +is a wildcard. + +Arguments: + local_part the local part to check + suffixes the list of suffixes + +Returns: length of matching suffix or zero +*/ + +int +route_check_suffix(uschar *local_part, uschar *suffixes) +{ +int sep = 0; +int alen = Ustrlen(local_part); +uschar *suffix; +uschar *listptr = suffixes; +uschar sufbuf[64]; + +while ((suffix = string_nextinlist(&listptr, &sep, sufbuf, sizeof(sufbuf))) + != NULL) + { + int slen = Ustrlen(suffix); + if (suffix[slen-1] == '*') + { + uschar *p, *pend; + pend = local_part + alen - (--slen) + 1; + for (p = local_part; p < pend; p++) + if (strncmpic(suffix, p, slen) == 0) return alen - (p - local_part); + } + else + if (alen > slen && strncmpic(suffix, local_part + alen - slen, slen) == 0) + return slen; + } + +return 0; +} + + + + +/************************************************* +* Check local part, domain, or sender * +*************************************************/ + +/* The checks in check_router_conditions() require similar code, so we use +this function to save repetition. + +Arguments: + rname router name for error messages + type type of check, for error message + list domains, local_parts, or senders list + anchorptr -> tree for possibly cached items (domains) + cache_bits cached bits pointer + listtype MCL_DOMAIN for domain check + MCL_LOCALPART for local part check + MCL_ADDRESS for sender check + domloc current domain, current local part, or NULL for sender check + ldata where to put lookup data + caseless passed on to match_isinlist() + perror where to put an error message + +Returns: OK item is in list + SKIP item is not in list, router is to be skipped + DEFER lookup or other defer +*/ + +static int +route_check_dls(uschar *rname, uschar *type, uschar *list, tree_node + **anchorptr, unsigned int *cache_bits, int listtype, uschar *domloc, + uschar **ldata, BOOL caseless, uschar **perror) +{ +int rc; + +if (list == NULL) return OK; /* Empty list always succeeds */ + +DEBUG(D_route) debug_printf("checking %s\n", type); + +/* The domain and local part use the same matching function, whereas sender +has its own code. */ + +if (domloc != NULL) + { + rc = match_isinlist(domloc, &list, 0, anchorptr, cache_bits, listtype, + caseless, ldata); + } +else + { + uschar *address = (sender_address == NULL)? US"" : sender_address; + rc = match_address_list(address, TRUE, TRUE, &list, cache_bits, -1, 0, + &sender_data); + } + +switch(rc) + { + case OK: + return OK; + + case FAIL: + *perror = string_sprintf("%s router skipped: %s mismatch", rname, type); + DEBUG(D_route) debug_printf("%s\n", *perror); + return SKIP; + + default: /* Paranoia, and keeps compilers happy */ + case DEFER: + *perror = string_sprintf("%s check lookup or other defer", type); + DEBUG(D_route) debug_printf("%s\n", *perror); + return DEFER; + } +} + + + +/************************************************* +* Check access by a given uid/gid * +*************************************************/ + +/* This function checks whether a given uid/gid has access to a given file or +directory. It is called only from check_files() below. This is hopefully a +cheapish check that does the job most of the time. Exim does *not* rely on this +test when actually accessing any file. The test is used when routing to make it +possible to take actions such as "if user x can access file y then run this +router". + +During routing, Exim is normally running as root, and so the test will work +except for NFS non-root mounts. When verifying during message reception, Exim +is running as "exim", so the test may not work. This is a limitation of the +Exim design. + +Code in check_files() below detects the case when it cannot stat() the file (as +root), and in that situation it uses a setuid subprocess in which to run this +test. + +Arguments: + path the path to check + uid the user + gid the group + bits the bits required in the final component + +Returns: TRUE + FALSE errno=EACCES or ENOENT (or others from realpath or stat) +*/ + +static BOOL +route_check_access(uschar *path, uid_t uid, gid_t gid, int bits) +{ +struct stat statbuf; +uschar *slash; +uschar *rp = US realpath(CS path, CS big_buffer); +uschar *sp = rp + 1; + +DEBUG(D_route) debug_printf("route_check_access(%s,%d,%d,%o)\n", path, + (int)uid, (int)gid, bits); + +if (rp == NULL) return FALSE; + +while ((slash = Ustrchr(sp, '/')) != NULL) + { + *slash = 0; + DEBUG(D_route) debug_printf("stat %s\n", rp); + if (Ustat(rp, &statbuf) < 0) return FALSE; + if ((statbuf.st_mode & + ((statbuf.st_uid == uid)? 0100 : (statbuf.st_gid == gid)? 0010 : 001) + ) == 0) + { + errno = EACCES; + return FALSE; + } + *slash = '/'; + sp = slash + 1; + } + +/* Down to the final component */ + +DEBUG(D_route) debug_printf("stat %s\n", rp); + +if (Ustat(rp, &statbuf) < 0) return FALSE; + +if (statbuf.st_uid == uid) bits = bits << 6; + else if (statbuf.st_gid == gid) bits = bits << 3; +if ((statbuf.st_mode & bits) != bits) + { + errno = EACCES; + return FALSE; + } + +DEBUG(D_route) debug_printf("route_check_access() succeeded\n"); +return TRUE; +} + + + +/************************************************* +* Do file existence tests * +*************************************************/ + +/* This function is given a colon-separated list of file tests, each of which +is expanded before use. A test consists of a file name, optionally preceded by +! (require non-existence) and/or + for handling permission denied (+ means +treat as non-existing). + +An item that contains no slashes is interpreted as a username or id, with an +optional group id, for checking access to the file. This cannot be done +"perfectly", but it is good enough for a number of applications. + +Arguments: + s a colon-separated list of file tests or NULL + perror a pointer to an anchor for an error text in the case of a DEFER + +Returns: OK if s == NULL or all tests are as required + DEFER if the existence of at least one of the files is + unclear (an error other than non-existence occurred); + DEFER if an expansion failed + DEFER if a name is not absolute + DEFER if problems with user/group + SKIP otherwise +*/ + +int +check_files(uschar *s, uschar **perror) +{ +int sep = 0; /* List has default separators */ +uid_t uid = 0; /* For picky compilers */ +gid_t gid = 0; /* For picky compilers */ +BOOL ugid_set = FALSE; +uschar *check, *listptr; +uschar buffer[1024]; + +if (s == NULL) return OK; + +DEBUG(D_route) debug_printf("checking require_files\n"); + +listptr = s; +while ((check = string_nextinlist(&listptr, &sep, buffer, sizeof(buffer))) + != NULL) + { + int rc; + int eacces_code = 0; + BOOL invert = FALSE; + struct stat statbuf; + uschar *ss = expand_string(check); + + if (ss == NULL) + { + if (expand_string_forcedfail) continue; + *perror = string_sprintf("failed to expand \"%s\" for require_files: %s", + check, expand_string_message); + goto RETURN_DEFER; + } + + /* Empty items are just skipped */ + + if (*ss == 0) continue; + + /* If there are no slashes in the string, we have a user name or uid, with + optional group/gid. */ + + if (Ustrchr(ss, '/') == NULL) + { + BOOL ok; + struct passwd *pw; + uschar *comma = Ustrchr(ss, ','); + + /* If there's a comma, temporarily terminate the user name/number + at that point. Then set the uid. */ + + if (comma != NULL) *comma = 0; + ok = route_finduser(ss, &pw, &uid); + if (comma != NULL) *comma = ','; + + if (!ok) + { + *perror = string_sprintf("user \"%s\" for require_files not found", ss); + goto RETURN_DEFER; + } + + /* If there was no comma, the gid is that associated with the user. */ + + if (comma == NULL) + { + if (pw != NULL) gid = pw->pw_gid; else + { + *perror = string_sprintf("group missing after numerical uid %d for " + "require_files", (int)uid); + goto RETURN_DEFER; + } + } + else + { + if (!route_findgroup(comma + 1, &gid)) + { + *perror = string_sprintf("group \"%s\" for require_files not found\n", + comma + 1); + goto RETURN_DEFER; + } + } + + /* Note that we have values set, and proceed to next item */ + + DEBUG(D_route) + debug_printf("check subsequent files for access by %s\n", ss); + ugid_set = TRUE; + continue; + } + + /* Path, possibly preceded by + and ! */ + + if (*ss == '+') + { + eacces_code = 1; + while (isspace((*(++ss)))); + } + + if (*ss == '!') + { + invert = TRUE; + while (isspace((*(++ss)))); + } + + if (*ss != '/') + { + *perror = string_sprintf("require_files: \"%s\" is not absolute", ss); + goto RETURN_DEFER; + } + + /* Stat the file, either as root (while routing) or as exim (while verifying + during message reception). */ + + rc = Ustat(ss, &statbuf); + + DEBUG(D_route) + { + debug_printf("file check: %s\n", check); + if (ss != check) debug_printf("expanded file: %s\n", ss); + debug_printf("stat() yielded %d\n", rc); + } + + /* If permission is denied, and we are running as root (i.e. routing for + delivery rather than verifying), and the requirement is to test for access by + a particular uid/gid, it must mean that the file is on a non-root-mounted NFS + system. In this case, we have to use a subprocess that runs as the relevant + uid in order to do the test. */ + + if (rc != 0 && errno == EACCES && ugid_set && getuid() == root_uid) + { + int status; + pid_t pid; + void (*oldsignal)(int); + + DEBUG(D_route) debug_printf("root is denied access: forking to check " + "in subprocess\n"); + + /* Before forking, ensure that SIGCHLD is set to SIG_DFL before forking, so + that the child process can be waited for, just in case get here with it set + otherwise. Save the old state for resetting on the wait. */ + + oldsignal = signal(SIGCHLD, SIG_DFL); + pid = fork(); + + /* If fork() fails, reinstate the original error and behave as if + this block of code were not present. This is the same behavious as happens + when Exim is not running as root at this point. */ + + if (pid < 0) + { + DEBUG(D_route) + debug_printf("require_files: fork failed: %s\n", strerror(errno)); + errno = EACCES; + goto HANDLE_ERROR; + } + + /* In the child process, change uid and gid, and then do the check using + the route_check_access() function. This does more than just stat the file; + it tests permissions as well. Return 0 for OK and 1 for failure. */ + + if (pid == 0) + { + exim_setugid(uid, gid, TRUE, + string_sprintf("require_files check, file=%s", ss)); + if (route_check_access(ss, uid, gid, 4)) _exit(0); + DEBUG(D_route) debug_printf("route_check_access() failed\n"); + _exit(1); + } + + /* In the parent, wait for the child to finish */ + + while (waitpid(pid, &status, 0) < 0) + { + if (errno != EINTR) /* unexpected error, interpret as failure */ + { + status = 1; + break; + } + } + + signal(SIGCHLD, oldsignal); /* restore */ + if ((status == 0) == invert) return SKIP; + continue; /* to test the next file */ + } + + /* Control reaches here if the initial stat() succeeds, or fails with an + error other than EACCES, or no uid/gid is set, or we are not running as root. + If we know the file exists and uid/gid are set, try to check read access for + that uid/gid as best we can. */ + + if (rc == 0 && ugid_set && !route_check_access(ss, uid, gid, 4)) + { + DEBUG(D_route) debug_printf("route_check_access() failed\n"); + rc = -1; + } + + /* Handle error returns from stat() or route_check_access(). The EACESS error + is handled specially. At present, we can force it to be treated as + non-existence. Write the code so that it will be easy to add forcing for + existence if required later. */ + + HANDLE_ERROR: + if (rc < 0) + { + DEBUG(D_route) debug_printf("errno = %d\n", errno); + if (errno == EACCES) + { + if (eacces_code == 1) + { + DEBUG(D_route) debug_printf("EACCES => ENOENT\n"); + errno = ENOENT; /* Treat as non-existent */ + } + } + if (errno != ENOENT) + { + *perror = string_sprintf("require_files: error for %s: %s", ss, + strerror(errno)); + goto RETURN_DEFER; + } + } + + /* At this point, rc < 0 => non-existence; rc >= 0 => existence */ + + if ((rc >= 0) == invert) return SKIP; + } + +return OK; + +/* Come here on any of the errors that return DEFER. */ + +RETURN_DEFER: +DEBUG(D_route) debug_printf("%s\n", *perror); +return DEFER; +} + + + + + +/************************************************* +* Check for router skipping * +*************************************************/ + +/* This function performs various checks to see whether a router should be +skipped. The order in which they are performed is important. + +Arguments: + r pointer to router instance block + addr address that is being handled + verify the verification type + pw ptr to ptr to passwd structure for local user + perror for lookup errors + +Returns: OK if all the tests succeed + SKIP if router is to be skipped + DEFER for a lookup defer + FAIL for address to be failed +*/ + +static BOOL +check_router_conditions(router_instance *r, address_item *addr, int verify, + struct passwd **pw, uschar **perror) +{ +int rc; +uschar *check_local_part; + +/* Reset variables to hold a home directory and data from lookup of a domain or +local part, and ensure search_find_defer is unset, in case there aren't any +actual lookups. */ + +deliver_home = NULL; +deliver_domain_data = NULL; +deliver_localpart_data = NULL; +sender_data = NULL; +local_user_gid = (gid_t)(-1); +local_user_uid = (uid_t)(-1); +search_find_defer = FALSE; + +/* Skip this router if not verifying and it has verify_only set */ + +if ((verify == v_none || verify == v_expn) && r->verify_only) + { + DEBUG(D_route) debug_printf("%s router skipped: verify_only set\n", r->name); + return SKIP; + } + +/* Skip this router if testing an address (-bt) and address_test is not set */ + +if (address_test_mode && !r->address_test) + { + DEBUG(D_route) debug_printf("%s router skipped: address_test is unset\n", + r->name); + return SKIP; + } + +/* Skip this router if verifying and it hasn't got the appropriate verify flag +set. */ + +if ((verify == v_sender && !r->verify_sender) || + (verify == v_recipient && !r->verify_recipient)) + { + DEBUG(D_route) debug_printf("%s router skipped: verify %d %d %d\n", + r->name, verify, r->verify_sender, r->verify_recipient); + return SKIP; + } + +/* Skip this router if processing EXPN and it doesn't have expn set */ + +if (verify == v_expn && !r->expn) + { + DEBUG(D_route) debug_printf("%s router skipped: no_expn set\n", r->name); + return SKIP; + } + +/* Skip this router if there's a domain mismatch. */ + +if ((rc = route_check_dls(r->name, US"domains", r->domains, &domainlist_anchor, + addr->domain_cache, TRUE, addr->domain, &deliver_domain_data, MCL_DOMAIN, + perror)) != OK) + return rc; + +/* Skip this router if there's a local part mismatch. We want to pass over the +caseful local part, so that +caseful can restore it, even if this router is +handling local parts caselessly. However, we can't just pass cc_local_part, +because that doesn't have the prefix or suffix stripped. A bit of massaging is +required. */ + +if (addr->prefix == NULL && addr->suffix == NULL) + check_local_part = addr->cc_local_part; +else + { + check_local_part = string_copy(addr->cc_local_part); + if (addr->prefix != NULL) + check_local_part += Ustrlen(addr->prefix); + if (addr->suffix != NULL) + check_local_part[Ustrlen(check_local_part) - Ustrlen(addr->suffix)] = 0; + } + +if ((rc = route_check_dls(r->name, US"local_parts", r->local_parts, + &localpartlist_anchor, addr->localpart_cache, MCL_LOCALPART, + check_local_part, &deliver_localpart_data, !r->caseful_local_part, + perror)) != OK) + return rc; + +/* If the check_local_user option is set, check that the local_part is the +login of a local user. Note: the third argument to route_finduser() must be +NULL here, to prevent a numeric string being taken as a numeric uid. If the +user is found, set deliver_home to the home directory, and also set +local_user_{uid,gid}. */ + +if (r->check_local_user) + { + DEBUG(D_route) debug_printf("checking for local user\n"); + if (!route_finduser(addr->local_part, pw, NULL)) + { + DEBUG(D_route) debug_printf("%s router skipped: %s is not a local user\n", + r->name, addr->local_part); + return SKIP; + } + deliver_home = string_copy(US (*pw)->pw_dir); + local_user_gid = (*pw)->pw_gid; + local_user_uid = (*pw)->pw_uid; + } + +/* Set (or override in the case of check_local_user) the home directory if +router_home_directory is set. This is done here so that it overrides $home from +check_local_user before any subsequent expansions are done. Otherwise, $home +could mean different things for different options, which would be extremely +confusing. */ + +if (r->router_home_directory != NULL) + { + uschar *router_home = expand_string(r->router_home_directory); + if (router_home == NULL) + { + if (!expand_string_forcedfail) + { + *perror = string_sprintf("failed to expand \"%s\" for " + "router_home_directory: %s", r->router_home_directory, + expand_string_message); + return DEFER; + } + } + else + { + setflag(addr, af_home_expanded); /* Note set from router_home_directory */ + deliver_home = router_home; + } + } + +/* Skip if the sender condition is not met. We leave this one till after the +local user check so that $home is set - enabling the possiblity of letting +individual recipients specify lists of acceptable/unacceptable senders. */ + +if ((rc = route_check_dls(r->name, US"senders", r->senders, NULL, + sender_address_cache, MCL_ADDRESS, NULL, NULL, FALSE, perror)) != OK) + return rc; + +/* This is the point at which we print out the router's debugging string if it +is set. We wait till here so as to have $home available for local users (and +anyway, we don't want too much stuff for skipped routers). */ + +debug_print_string(r->debug_string); + +/* Perform file existence tests. */ + +if ((rc = check_files(r->require_files, perror)) != OK) + { + DEBUG(D_route) debug_printf("%s router %s: file check\n", r->name, + (rc == SKIP)? "skipped" : "deferred"); + return rc; + } + +/* Now the general condition test. */ + +if (r->condition != NULL) + { + DEBUG(D_route) debug_printf("checking \"condition\"\n"); + if (!expand_check_condition(r->condition, r->name, US"router")) + { + if (search_find_defer) + { + *perror = US"condition check lookup defer"; + DEBUG(D_route) debug_printf("%s\n", *perror); + return DEFER; + } + DEBUG(D_route) + debug_printf("%s router skipped: condition failure\n", r->name); + return SKIP; + } + } + +/* All the checks passed. */ + +return OK; +} + + + + +/************************************************* +* Find a local user * +*************************************************/ + +/* Try several times (if configured) to find a local user, in case delays in +NIS or NFS whatever cause an incorrect refusal. It's a pity that getpwnam() +doesn't have some kind of indication as to why it has failed. If the string +given consists entirely of digits, and the third argument is not NULL, assume +the string is the numerical value of the uid. Otherwise it is looked up using +getpwnam(). The uid is passed back via return_uid, if not NULL, and the +pointer to a passwd structure, if found, is passed back via pw, if not NULL. + +Because this may be called several times in succession for the same user for +different routers, cache the result of the previous getpwnam call so that it +can be re-used. Note that we can't just copy the structure, as the store it +points to can get trashed. + +Arguments: + s the login name or textual form of the numerical uid of the user + pw if not NULL, return the result of getpwnam here, or set NULL + if no call to getpwnam is made (s numeric, return_uid != NULL) + return_uid if not NULL, return the uid via this address + +Returns: TRUE if s is numerical or was looked up successfully + +*/ + +static struct passwd pwcopy; +static struct passwd *lastpw = NULL; +static uschar lastname[48] = { 0 }; +static uschar lastdir[128]; +static uschar lastgecos[128]; +static uschar lastshell[128]; + +BOOL +route_finduser(uschar *s, struct passwd **pw, uid_t *return_uid) +{ +if (Ustrcmp(lastname, s) != 0) + { + int i = 0; + + if (return_uid != NULL && (isdigit(*s) || *s == '-') && + s[Ustrspn(s+1, "0123456789")+1] == 0) + { + *return_uid = (uid_t)Uatoi(s); + if (pw != NULL) *pw = NULL; + return TRUE; + } + + (void)string_format(lastname, sizeof(lastname), "%s", s); + + /* Force failure if string length is greater than given maximum */ + + if (max_username_length > 0 && Ustrlen(lastname) > max_username_length) + { + DEBUG(D_uid) debug_printf("forced failure of finduser(): string " + "length of %s is greater than %d\n", lastname, max_username_length); + lastpw = NULL; + } + + /* Try a few times if so configured; this handles delays in NIS etc. */ + + else for (;;) + { + if ((lastpw = getpwnam(CS s)) != NULL) break; + if (++i > finduser_retries) break; + sleep(1); + } + + if (lastpw != NULL) + { + pwcopy.pw_uid = lastpw->pw_uid; + pwcopy.pw_gid = lastpw->pw_gid; + (void)string_format(lastdir, sizeof(lastdir), "%s", lastpw->pw_dir); + (void)string_format(lastgecos, sizeof(lastgecos), "%s", lastpw->pw_gecos); + (void)string_format(lastshell, sizeof(lastshell), "%s", lastpw->pw_shell); + pwcopy.pw_name = CS lastname; + pwcopy.pw_dir = CS lastdir; + pwcopy.pw_gecos = CS lastgecos; + pwcopy.pw_shell = CS lastshell; + lastpw = &pwcopy; + } + } +else + { + DEBUG(D_uid) debug_printf("finduser used cached passwd data for %s\n", s); + } + +if (lastpw == NULL) return FALSE; + +if (return_uid != NULL) *return_uid = lastpw->pw_uid; +if (pw != NULL) *pw = lastpw; + +return TRUE; +} + + + + +/************************************************* +* Find a local group * +*************************************************/ + +/* Try several times (if configured) to find a local group, in case delays in +NIS or NFS whatever cause an incorrect refusal. It's a pity that getgrnam() +doesn't have some kind of indication as to why it has failed. + +Arguments: + s the group namd or textual form of the numerical gid + return_gid return the gid via this address + +Returns: TRUE if the group was found; FALSE otherwise + +*/ + +BOOL +route_findgroup(uschar *s, gid_t *return_gid) +{ +int i = 0; +struct group *gr; + +if ((isdigit(*s) || *s == '-') && s[Ustrspn(s+1, "0123456789")+1] == 0) + { + *return_gid = (gid_t)Uatoi(s); + return TRUE; + } + +for (;;) + { + if ((gr = getgrnam(CS s)) != NULL) + { + *return_gid = gr->gr_gid; + return TRUE; + } + if (++i > finduser_retries) break; + sleep(1); + } + +return FALSE; +} + + + + +/************************************************* +* Find user by expanding string * +*************************************************/ + +/* Expands a string, and then looks up the result in the passwd file. + +Arguments: + string the string to be expanded, yielding a login name or a numerical + uid value (to be passed to route_finduser()) + driver_name caller name for panic error message (only) + driver_type caller type for panic error message (only) + pw return passwd entry via this pointer + uid return uid via this pointer + errmsg where to point a message on failure + +Returns: TRUE if user found, FALSE otherwise +*/ + +BOOL +route_find_expanded_user(uschar *string, uschar *driver_name, + uschar *driver_type, struct passwd **pw, uid_t *uid, uschar **errmsg) +{ +uschar *user = expand_string(string); + +if (user == NULL) + { + *errmsg = string_sprintf("Failed to expand user string \"%s\" for the " + "%s %s: %s", string, driver_name, driver_type, expand_string_message); + log_write(0, LOG_MAIN|LOG_PANIC, "%s", *errmsg); + return FALSE; + } + +if (route_finduser(user, pw, uid)) return TRUE; + +*errmsg = string_sprintf("Failed to find user \"%s\" from expanded string " + "\"%s\" for the %s %s", user, string, driver_name, driver_type); +log_write(0, LOG_MAIN|LOG_PANIC, "%s", *errmsg); +return FALSE; +} + + + +/************************************************* +* Find group by expanding string * +*************************************************/ + +/* Expands a string and then looks up the result in the group file. + +Arguments: + string the string to be expanded, yielding a group name or a numerical + gid value (to be passed to route_findgroup()) + driver_name caller name for panic error message (only) + driver_type caller type for panic error message (only) + gid return gid via this pointer + errmsg return error message via this pointer + +Returns: TRUE if found group, FALSE otherwise +*/ + +BOOL +route_find_expanded_group(uschar *string, uschar *driver_name, uschar *driver_type, + gid_t *gid, uschar **errmsg) +{ +BOOL yield = TRUE; +uschar *group = expand_string(string); + +if (group == NULL) + { + *errmsg = string_sprintf("Failed to expand group string \"%s\" for the " + "%s %s: %s", string, driver_name, driver_type, expand_string_message); + log_write(0, LOG_MAIN|LOG_PANIC, "%s", *errmsg); + return FALSE; + } + +if (!route_findgroup(group, gid)) + { + *errmsg = string_sprintf("Failed to find group \"%s\" from expanded string " + "\"%s\" for the %s %s", group, string, driver_name, driver_type); + log_write(0, LOG_MAIN|LOG_PANIC, "%s", *errmsg); + yield = FALSE; + } + +return yield; +} + + + +/************************************************* +* Sort out "more" or "unseen" * +*************************************************/ + +/* These values are usually fixed boolean values, but they are permitted to be +expanded strings. + +Arguments: + addr address being routed + rname the router name + oname the option name + bvalue the router's boolean value + svalue the router's string value + rvalue where to put the returned value + +Returns: OK value placed in rvalue + DEFER expansion failed +*/ + +static int +exp_bool(address_item *addr, uschar *rname, uschar *oname, BOOL bvalue, + uschar *svalue, BOOL *rvalue) +{ +uschar *expanded; +if (svalue == NULL) { *rvalue = bvalue; return OK; } + +expanded = expand_string(svalue); +if (expanded == NULL) + { + if (expand_string_forcedfail) + { + DEBUG(D_route) debug_printf("expansion of \"%s\" forced failure\n", oname); + *rvalue = bvalue; + return OK; + } + addr->message = string_sprintf("failed to expand \"%s\" in %s router: %s", + oname, rname, expand_string_message); + DEBUG(D_route) debug_printf("%s\n", addr->message); + return DEFER; + } + +DEBUG(D_route) debug_printf("expansion of \"%s\" yields \"%s\"\n", oname, + expanded); + +if (strcmpic(expanded, US"true") == 0 || strcmpic(expanded, US"yes") == 0) + *rvalue = TRUE; +else if (strcmpic(expanded, US"false") == 0 || strcmpic(expanded, US"no") == 0) + *rvalue = FALSE; +else + { + addr->message = string_sprintf("\"%s\" is not a valid value for the " + "\"%s\" option in the %s router", expanded, oname, rname); + return DEFER; + } + +return OK; +} + + + + +/************************************************* +* Handle an unseen routing * +*************************************************/ + +/* This function is called when an address is routed by a router with "unseen" +set. It must make a clone of the address, for handling by subsequent drivers. +The clone is set to start routing at the next router. + +The original address must be replaced by an invented "parent" which has the +routed address plus the clone as its children. This is necessary in case the +address is at the top level - we don't want to mark it complete until both +deliveries have been done. + +A new unique field must be made, so that the record of the delivery isn't a +record of the original address, and checking for already delivered has +therefore to be done here. If the delivery has happened, then take the base +address off whichever delivery queue it is on - it will always be the top item. + +Arguments: + name router name + addr address that was routed + paddr_local chain of local-delivery addresses + paddr_remote chain of remote-delivery addresses + addr_new chain for newly created addresses + +Returns: nothing +*/ + +static void +route_unseen(uschar *name, address_item *addr, address_item **paddr_local, + address_item **paddr_remote, address_item **addr_new) +{ +address_item *parent = deliver_make_addr(addr->address, TRUE); +address_item *new = deliver_make_addr(addr->address, TRUE); + +/* The invented parent is a copy that replaces the original; note that +this copies its parent pointer. It has two children, and its errors_address is +from the original address' parent, if present, otherwise unset. */ + +*parent = *addr; +parent->child_count = 2; +parent->p.errors_address = + (addr->parent == NULL)? NULL : addr->parent->p.errors_address; + +/* The routed address gets a new parent. */ + +addr->parent = parent; + +/* The clone has this parent too. Set its errors address from the parent. This +was set from the original parent (or to NULL) - see above. We do NOT want to +take the errors address from the unseen router. */ + +new->parent = parent; +new->p.errors_address = parent->p.errors_address; + +/* Copy the propagated flags and address_data from the original. */ + +copyflag(new, addr, af_propagate); +new->p.address_data = addr->p.address_data; + + +/* As it has turned out, we haven't set headers_add or headers_remove for the + * clone. Thinking about it, it isn't entirely clear whether they should be + * copied from the original parent, like errors_address, or taken from the + * unseen router, like address_data and the flags. Until somebody brings this + * up, I propose to leave the code as it is. + */ + + +/* Set the cloned address to start at the next router, and put it onto the +chain of new addresses. */ + +new->start_router = addr->router->next; +new->next = *addr_new; +*addr_new = new; + +DEBUG(D_route) debug_printf("\"unseen\" set: replicated %s\n", addr->address); + +/* Make a new unique field, to distinguish from the normal one. */ + +addr->unique = string_sprintf("%s/%s", addr->unique, name); + +/* If the address has been routed to a transport, see if it was previously +delivered. If so, we take it off the relevant queue so that it isn't delivered +again. Otherwise, it was an alias or something, and the addresses it generated +are handled in the normal way. */ + +if (addr->transport != NULL && + tree_search(tree_nonrecipients, addr->unique) != NULL) + { + DEBUG(D_route) + debug_printf("\"unseen\" delivery previously done - discarded\n"); + parent->child_count--; + if (*paddr_remote == addr) *paddr_remote = addr->next; + if (*paddr_local == addr) *paddr_local = addr->next; + } +} + + + +/************************************************* +* Route one address * +*************************************************/ + +/* This function is passed in one address item, for processing by the routers. +The verify flag is set if this is being called for verification rather than +delivery. If the router doesn't have its "verify" flag set, it is skipped. + +Arguments: + addr address to route + paddr_local chain of local-delivery addresses + paddr_remote chain of remote-delivery addresses + addr_new chain for newly created addresses + addr_succeed chain for completed addresses + verify v_none if not verifying + v_sender if verifying a sender address + v_recipient if verifying a recipient address + v_expn if processing an EXPN address + +Returns: OK => address successfully routed + DISCARD => address was discarded + FAIL => address could not be routed + DEFER => some temporary problem + ERROR => some major internal or configuration failure +*/ + +int +route_address(address_item *addr, address_item **paddr_local, + address_item **paddr_remote, address_item **addr_new, + address_item **addr_succeed, int verify) +{ +int yield = OK; +BOOL unseen; +router_instance *r, *nextr; +uschar *old_domain = addr->domain; + +HDEBUG(D_route) + { + debug_printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"); + debug_printf("routing %s\n", addr->address); + } + +/* Loop through all router instances until a router succeeds, fails, defers, or +encounters an error. If the address has start_router set, we begin from there +instead of at the first router. */ + +for (r = (addr->start_router == NULL)? routers : addr->start_router; + r != NULL; r = nextr) + { + uschar *error; + struct passwd *pw = NULL; + struct passwd pwcopy; + address_item *parent; + BOOL loop_detected = FALSE; + BOOL more; + int loopcount = 0; + int rc; + + DEBUG(D_route) debug_printf("--------> %s router <--------\n", r->name); + + /* Reset any search error message from the previous router. */ + + search_error_message = NULL; + + /* There are some weird cases where logging is disabled */ + + disable_logging = r->disable_logging; + + /* Record the last router to handle the address, and set the default + next router. */ + + addr->router = r; + nextr = r->next; + + /* Loop protection: If this address has an ancestor with the same address, + and that ancestor was routed by this router, we skip this router. This + prevents a variety of looping states when a new address is created by + redirection or by the use of "unseen" on a router. + + If no_repeat_use is set on the router, we skip if _any_ ancestor was routed + by this router, even if it was different to the current address. + + Just in case someone does put it into a loop (possible with redirection + continally adding to an address, for example), put a long stop counter on + the number of parents. */ + + for (parent = addr->parent; parent != NULL; parent = parent->parent) + { + if (parent->router == r) + { + BOOL break_loop = !r->repeat_use; + + /* When repeat_use is set, first check the active addresses caselessly. + If they match, we have to do a further caseful check of the local parts + when caseful_local_part is set. This is assumed to be rare, which is why + the code is written this way. */ + + if (!break_loop) + { + break_loop = strcmpic(parent->address, addr->address) == 0; + if (break_loop && r->caseful_local_part) + break_loop = Ustrncmp(parent->address, addr->address, + Ustrrchr(addr->address, '@') - addr->address) == 0; + } + + if (break_loop) + { + DEBUG(D_route) debug_printf("%s router skipped: previously routed %s\n", + r->name, parent->address); + loop_detected = TRUE; + break; + } + } + + /* Continue with parents, limiting the size of the dynasty. */ + + if (loopcount++ > 100) + { + log_write(0, LOG_MAIN|LOG_PANIC, "routing loop for %s", addr->address); + yield = DEFER; + goto ROUTE_EXIT; + } + } + + if (loop_detected) continue; + + /* Default no affixes and select whether to use a caseful or caseless local + part in this router. */ + + addr->prefix = addr->suffix = NULL; + addr->local_part = r->caseful_local_part? + addr->cc_local_part : addr->lc_local_part; + + DEBUG(D_route) debug_printf("local_part=%s domain=%s\n", addr->local_part, + addr->domain); + + /* Handle any configured prefix by replacing the local_part address, + and setting the prefix. Skip the router if the prefix doesn't match, + unless the prefix is optional. */ + + if (r->prefix != NULL) + { + int plen = route_check_prefix(addr->local_part, r->prefix); + if (plen > 0) + { + addr->prefix = string_copyn(addr->local_part, plen); + addr->local_part += plen; + DEBUG(D_route) debug_printf("stripped prefix %s\n", addr->prefix); + } + else if (!r->prefix_optional) + { + DEBUG(D_route) debug_printf("%s router skipped: prefix mismatch\n", + r->name); + continue; + } + } + + /* Handle any configured suffix likewise. */ + + if (r->suffix != NULL) + { + int slen = route_check_suffix(addr->local_part, r->suffix); + if (slen > 0) + { + int lplen = Ustrlen(addr->local_part) - slen; + addr->suffix = addr->local_part + lplen; + addr->local_part = string_copyn(addr->local_part, lplen); + DEBUG(D_route) debug_printf("stripped suffix %s\n", addr->suffix); + } + else if (!r->suffix_optional) + { + DEBUG(D_route) debug_printf("%s router skipped: suffix mismatch\n", + r->name); + continue; + } + } + + /* Set the expansion variables now that we have the affixes and the case of + the local part sorted. */ + + deliver_set_expansions(addr); + + /* For convenience, the pre-router checks are in a separate function, which + returns OK, SKIP, FAIL, or DEFER. */ + + if ((rc = check_router_conditions(r, addr, verify, &pw, &error)) != OK) + { + if (rc == SKIP) continue; + addr->message = error; + yield = rc; + goto ROUTE_EXIT; + } + + /* All pre-conditions have been met. Reset any search error message from + pre-condition tests. These can arise in negated tests where the failure of + the lookup leads to a TRUE pre-condition. */ + + search_error_message = NULL; + + /* Finally, expand the address_data field in the router. Forced failure + behaves as if the router declined. Any other failure is more serious. On + success, the string is attached to the address for all subsequent processing. + */ + + if (r->address_data != NULL) + { + DEBUG(D_route) debug_printf("processing address_data\n"); + deliver_address_data = expand_string(r->address_data); + if (deliver_address_data == NULL) + { + if (expand_string_forcedfail) + { + DEBUG(D_route) debug_printf("forced failure in expansion of \"%s\" " + "(address_data): decline action taken\n", r->address_data); + + /* Expand "more" if necessary; DEFER => an expansion failed */ + + yield = exp_bool(addr, r->name, US"more", r->more, r->expand_more, + &more); + if (yield != OK) goto ROUTE_EXIT; + + if (!more) + { + DEBUG(D_route) + debug_printf("\"more\"=false: skipping remaining routers\n"); + r = NULL; + break; + } + else continue; /* With next router */ + } + + else + { + addr->message = string_sprintf("expansion of \"%s\" failed " + "in %s router: %s", r->address_data, r->name, expand_string_message); + yield = DEFER; + goto ROUTE_EXIT; + } + } + addr->p.address_data = deliver_address_data; + } + + /* We are finally cleared for take-off with this router. Clear the the flag + that records that a local host was removed from a routed host list. Make a + copy of relevant fields in the password information from check_local_user, + because it will be overwritten if check_local_user is invoked again while + verifying an errors_address setting. */ + + clearflag(addr, af_local_host_removed); + + if (pw != NULL) + { + pwcopy.pw_name = CS string_copy(US pw->pw_name); + pwcopy.pw_uid = pw->pw_uid; + pwcopy.pw_gid = pw->pw_gid; + pwcopy.pw_gecos = CS string_copy(US pw->pw_gecos); + pwcopy.pw_dir = CS string_copy(US pw->pw_dir); + pwcopy.pw_shell = CS string_copy(US pw->pw_shell); + pw = &pwcopy; + } + + /* Run the router, and handle the consequences. */ + + HDEBUG(D_route) debug_printf("calling %s router\n", r->name); + + yield = (r->info->code)(r, addr, pw, verify != v_none, paddr_local, + paddr_remote, addr_new, addr_succeed); + + if (yield == FAIL) + { + HDEBUG(D_route) debug_printf("%s router forced address failure\n", r->name); + goto ROUTE_EXIT; + } + + /* If succeeded while verifying but fail_verify is set, convert into + a failure, and take it off the local or remote delivery list. */ + + if (((verify == v_sender && r->fail_verify_sender) || + (verify == v_recipient && r->fail_verify_recipient)) && + (yield == OK || yield == PASS)) + { + addr->message = string_sprintf("%s router forced verify failure", r->name); + if (*paddr_remote == addr) *paddr_remote = addr->next; + if (*paddr_local == addr) *paddr_local = addr->next; + yield = FAIL; + goto ROUTE_EXIT; + } + + /* PASS and DECLINE are the only two cases where the loop continues. For all + other returns, we break the loop and handle the result below. */ + + if (yield != PASS && yield != DECLINE) break; + + HDEBUG(D_route) + { + debug_printf("%s router %s for %s\n", r->name, + (yield == PASS)? "passed" : "declined", addr->address); + if (Ustrcmp(old_domain, addr->domain) != 0) + debug_printf("domain %s rewritten\n", old_domain); + } + + /* PASS always continues to another router; DECLINE does so if "more" + is true. Initialization insists that pass_router is always a following + router. Otherwise, break the loop as if at the end of the routers. */ + + if (yield == PASS) + { + if (r->pass_router != NULL) nextr = r->pass_router; + } + else + { + /* Expand "more" if necessary */ + + yield = exp_bool(addr, r->name, US"more", r->more, r->expand_more, &more); + if (yield != OK) goto ROUTE_EXIT; + + if (!more) + { + HDEBUG(D_route) + debug_printf("\"more\" is false: skipping remaining routers\n"); + r = NULL; + break; + } + } + } /* Loop for all routers */ + +/* On exit from the routers loop, if r == NULL we have run out of routers, +either genuinely, or as a result of no_more. Otherwise, the loop ended +prematurely, either because a router succeeded, or because of some special +router response. Note that FAIL errors and errors detected before actually +running a router go direct to ROUTE_EXIT from code above. */ + +if (r == NULL) + { + HDEBUG(D_route) debug_printf("no more routers\n"); + if (addr->message == NULL) + { + uschar *message = US"Unrouteable address"; + if (addr->router != NULL && addr->router->cannot_route_message != NULL) + { + uschar *expmessage = expand_string(addr->router->cannot_route_message); + if (expmessage == NULL) + { + if (!expand_string_forcedfail) + log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand " + "cannot_route_message in %s router: %s", addr->router->name, + expand_string_message); + } + else message = expmessage; + } + addr->user_message = addr->message = message; + } + addr->router = NULL; /* For logging */ + yield = FAIL; + goto ROUTE_EXIT; + } + +if (yield == DEFER) + { + HDEBUG(D_route) + { + debug_printf("%s router: defer for %s\n", r->name, addr->address); + debug_printf(" message: %s\n", (addr->message == NULL)? + US"" : addr->message); + } + goto ROUTE_EXIT; + } + +if (yield == DISCARD) goto ROUTE_EXIT; + +/* The yield must be either OK or REROUTED. */ + +if (yield != OK && yield != REROUTED) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s router returned unknown value %d", + r->name, yield); + +/* If the yield was REROUTED, the router put a child address on the new chain +as a result of a domain change of some sort (widening, typically). */ + +if (yield == REROUTED) + { + HDEBUG(D_route) debug_printf("re-routed to %s\n", addr->address); + yield = OK; + goto ROUTE_EXIT; + } + +/* The only remaining possibility is that the router succeeded. If the +translate_ip_address options is set and host addresses were associated with the +address, run them through the translation. This feature is for weird and +wonderful situations (the amateur packet radio people need it) or very broken +networking, so it is included in the binary only if requested. */ + +#ifdef SUPPORT_TRANSLATE_IP_ADDRESS + +if (r->translate_ip_address != NULL) + { + int rc; + int old_pool = store_pool; + host_item *h; + for (h = addr->host_list; h != NULL; h = h->next) + { + uschar *newaddress; + uschar *oldaddress, *oldname; + + if (h->address == NULL) continue; + + deliver_host_address = h->address; + newaddress = expand_string(r->translate_ip_address); + deliver_host_address = NULL; + + if (newaddress == NULL) + { + if (expand_string_forcedfail) continue; + addr->basic_errno = ERRNO_EXPANDFAIL; + addr->message = string_sprintf("translate_ip_address expansion " + "failed: %s", expand_string_message); + yield = DEFER; + goto ROUTE_EXIT; + } + + DEBUG(D_route) debug_printf("%s [%s] translated to %s\n", + h->name, h->address, newaddress); + if (string_is_ip_address(newaddress, NULL)) + { + h->address = newaddress; + continue; + } + + oldname = h->name; + oldaddress = h->address; + h->name = newaddress; + h->address = NULL; + h->mx = MX_NONE; + + store_pool = POOL_PERM; + rc = host_find_byname(h, NULL, NULL, TRUE); + store_pool = old_pool; + + if (rc == HOST_FIND_FAILED || rc == HOST_FIND_AGAIN) + { + addr->basic_errno = ERRNO_UNKNOWNHOST; + addr->message = string_sprintf("host %s not found when " + "translating %s [%s]", h->name, oldname, oldaddress); + yield = DEFER; + goto ROUTE_EXIT; + } + } + } +#endif /* SUPPORT_TRANSLATE_IP_ADDRESS */ + +/* See if this is an unseen routing; first expand the option if necessary. +DEFER can be given if the expansion fails */ + +yield = exp_bool(addr, r->name, US"unseen", r->unseen, r->expand_unseen, + &unseen); +if (yield != OK) goto ROUTE_EXIT; + + + +/* Debugging output recording a successful routing */ + +HDEBUG(D_route) + { + debug_printf("routed by %s router%s\n", r->name, + unseen? " (unseen)" : ""); + } + +DEBUG(D_route) + { + host_item *h; + + debug_printf(" envelope to: %s\n", addr->address); + debug_printf(" transport: %s\n", (addr->transport == NULL)? + US"" : addr->transport->name); + + if (addr->p.errors_address != NULL) + debug_printf(" errors to %s\n", addr->p.errors_address); + + for (h = addr->host_list; h != NULL; h = h->next) + { + debug_printf(" host %s", h->name); + if (h->address != NULL) debug_printf(" [%s]", h->address); + if (h->mx >= 0) debug_printf(" MX=%d", h->mx); + else if (h->mx != MX_NONE) debug_printf(" rgroup=%d", h->mx); + if (h->port != PORT_NONE) debug_printf(" port=%d", h->port); + debug_printf("\n"); + } + } + +/* Clear any temporary error message set by a router that declined, and handle +the "unseen" option (ignore if there are no further routers). */ + +addr->message = NULL; +if (unseen && r->next != NULL) + route_unseen(r->name, addr, paddr_local, paddr_remote, addr_new); + +/* Unset the address expansions, and return the final result. */ + +ROUTE_EXIT: +deliver_set_expansions(NULL); +disable_logging = FALSE; +return yield; +} + +/* End of route.c */ diff --git a/src/src/search.c b/src/src/search.c new file mode 100644 index 000000000..3dfd80e32 --- /dev/null +++ b/src/src/search.c @@ -0,0 +1,817 @@ +/* $Cambridge: exim/src/src/search.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* A set of functions to search databases in various formats. An open +database is represented by a void * value which is returned from a lookup- +specific "open" function. These are now all held in individual modules in the +lookups subdirectory and the functions here form a generic interface. + +Caching is used to improve performance. Open files are cached until a tidyup +function is called, and for each file the result of the last lookup is cached. +However, if too many files are opened, some of those that are not in use have +to be closed. Those open items that use real files are kept on a LRU chain to +help with this. + +All the data is held in permanent store so as to be independent of the stacking +pool that is reset from time to time. In fact, we use malloc'd store so that it +can be freed when the caches are tidied up. It isn't actually clear whether +this is a benefit or not, to be honest. */ + +#include "exim.h" + + +/* Tree in which to cache open files until tidyup called. */ + +static tree_node *search_tree = NULL; + +/* Two-way chain of open databases that use real files. This is maintained in +recently-used order for the purposes of closing the least recently used when +too many files are open. */ + +static tree_node *open_top = NULL; +static tree_node *open_bot = NULL; + +/* Count of open databases that use real files */ + +static int open_filecount = 0; + +/* Allow us to reset store used for lookups and lookup caching */ + +static void *search_reset_point = NULL; + + + +/************************************************* +* Validate a plain lookup type name * +*************************************************/ + +/* Only those names that are recognized and whose code is included in the +binary give an OK response. Use a binary chop search now that the list has got +so long. + +Arguments: + name lookup type name - not necessarily zero terminated (e.g. dbm*) + len length of the name + +Returns: +ve => valid lookup name; value is offset in lookup_list + -ve => invalid name; message in search_error_message. +*/ + +int +search_findtype(uschar *name, int len) +{ +int bot = 0; +int top = lookup_list_count; +while (top > bot) + { + int mid = (top + bot)/2; + int c = Ustrncmp(name, lookup_list[mid].name, len); + + /* If c == 0 we have matched the incoming name with the start of the search + type name. However, some search types are substrings of others (e.g. nis and + nisplus) so we need to check that the lengths are the same. The length of the + type name cannot be shorter (else c would not be 0); if it is not equal it + must be longer, and in that case, the incoming name comes before the name we + are testing. By leaving c == 0 when the lengths are different, and doing a + > 0 test below, this all falls out correctly. */ + + if (c == 0 && Ustrlen(lookup_list[mid].name) == len) + { + if (lookup_list[mid].find != NULL) return mid; + search_error_message = string_sprintf("lookup type \"%.*s\" is not " + "available (not in the binary - check buildtime LOOKUP configuration)", + len, name); + return -1; + } + + if (c > 0) bot = mid + 1; else top = mid; + } + +search_error_message = string_sprintf("unknown lookup type \"%.*s\"",len,name); +return -1; +} + + + +/************************************************* +* Validate a full lookup type name * +*************************************************/ + +/* This function recognizes the "partial-" prefix and also terminating * and *@ +suffixes. + +Arguments: + name the full lookup type name + ptypeptr where to put the partial type + after subtraction of 1024 or 2048: + negative => no partial matching + non-negative => minimum number of non-wild components + ptypeaff where to put a pointer to the affix + the affix is within name if supplied therein + otherwise it's a literal string + afflen the length of the affix + starflags where to put the SEARCH_STAR and SEARCH_STARAT flags + +Returns: +ve => valid lookup name; value is offset in lookup_list + -ve => invalid name; message in search_error_message. +*/ + +int +search_findtype_partial(uschar *name, int *ptypeptr, uschar **ptypeaff, + int *afflen, int *starflags) +{ +int len, stype; +int pv = -1; +uschar *ss = name; + +*starflags = 0; +*ptypeaff = NULL; + +/* Check for a partial matching type. It must start with "partial", optionally +followed by a sequence of digits. If this is followed by "-", the affix is the +default "*." string. Otherwise we expect an affix in parentheses. Affixes are a +limited number of characters, not including parens. */ + +if (Ustrncmp(name, "partial", 7) == 0) + { + ss += 7; + if (isdigit (*ss)) + { + pv = 0; + while (isdigit(*ss)) pv = pv*10 + *ss++ - '0'; + } + else pv = 2; /* Default number of wild components */ + + if (*ss == '(') + { + *ptypeaff = ++ss; + while (ispunct(*ss) && *ss != ')') ss++; + if (*ss != ')') goto BAD_TYPE; + *afflen = ss++ - *ptypeaff; + } + else if (*ss++ == '-') + { + *ptypeaff = US "*."; + *afflen = 2; + } + else + { + BAD_TYPE: + search_error_message = string_sprintf("format error in lookup type \"%s\"", + name); + return -1; + } + } + +/* Now we are left with a lookup name, possibly followed by * or *@. */ + +len = Ustrlen(ss); +if (len >= 2 && Ustrncmp(ss + len - 2, "*@", 2) == 0) + { + *starflags |= SEARCH_STARAT; + len -= 2; + } +else if (len >= 1 && ss[len-1] == '*') + { + *starflags |= SEARCH_STAR; + len--; + } + +/* Check for the individual search type. Only those that are actually in the +binary are valid. For query-style types, "partial" is an error. */ + +stype = search_findtype(ss, len); +if (pv >= 0 && mac_islookup(stype, lookup_querystyle)) + { + search_error_message = string_sprintf("\"partial\" is not permitted " + "for lookup type \"%s\"", ss); + return -1; + } + +/* All is well; pass back the partial type and return the lookup type. */ + +*ptypeptr = pv; +return stype; +} + + + +/************************************************* +* Release cached resources * +*************************************************/ + +/* When search_open is called it caches the "file" that it opens in +search_tree. The name of the tree node is a concatenation of the search type +with the file name. For query-style lookups, the file name is empty. Real files +are normally closed only when this tidyup routine is called, typically at the +end of sections of code where a number of lookups might occur. However, if too +many files are open simultaneously, some get closed beforehand. They can't be +removed from the tree. There is also a general tidyup function which is called +for the lookup driver, if it exists. + +First, there is an internal, recursive subroutine. + +Argument: a pointer to a search_openfile tree node +Returns: nothing +*/ + +static void +tidyup_subtree(tree_node *t) +{ +search_cache *c = (search_cache *)(t->data.ptr); +if (t->left != NULL) tidyup_subtree(t->left); +if (t->right != NULL) tidyup_subtree(t->right); +if (c != NULL && + c->handle != NULL && + lookup_list[c->search_type].close != NULL) + lookup_list[c->search_type].close(c->handle); +} + + +/* The external entry point + +Argument: none +Returns: nothing +*/ + +void +search_tidyup(void) +{ +int i; +int old_pool = store_pool; + +DEBUG(D_lookup) debug_printf("search_tidyup called\n"); + +/* Close individually each cached open file. */ + +store_pool = POOL_SEARCH; +if (search_tree != NULL) + { + tidyup_subtree(search_tree); + search_tree = NULL; + } +open_top = open_bot = NULL; +open_filecount = 0; + +/* Call the general tidyup entry for any drivers that have one. */ + +for (i = 0; i < lookup_list_count; i++) + if (lookup_list[i].tidy != NULL) (lookup_list[i].tidy)(); + +if (search_reset_point != NULL) store_reset(search_reset_point); +search_reset_point = NULL; +store_pool = old_pool; +} + + + + +/************************************************* +* Open search database * +*************************************************/ + +/* A mode, and lists of owners and groups, are passed over for checking in +the cases where the database is one or more files. Return NULL, with a message +pointed to by message, in cases of error. + +For search types that use a file or files, check up on the mode after +opening. It is tempting to do a stat before opening the file, and use it as +an existence check. However, doing that opens a small security loophole in +that the status could be changed before the file is opened. Can't quite see +what problems this might lead to, but you can't be too careful where security +is concerned. Fstat() on an open file can normally be expected to succeed, +but there are some NFS states where it does not. + +There are two styles of query: (1) in the "single-key+file" style, a single +key string and a file name are given, for example, for linear searches, DBM +files, or for NIS. (2) In the "query" style, no "filename" is given; instead +just a single query string is passed. This applies to multiple-key lookup +types such as NIS+. + +Before opening, scan the tree of cached files to see if this file is already +open for the correct search type. If so, return the saved handle. If not, put +the handle in the tree for possible subsequent use. See search_tidyup above for +closing all the cached files. + +A count of open databases which use real files is maintained, and if this +gets too large, we have to close a cached file. Its entry remains in the tree, +but is marked closed. + +Arguments: + filename the name of the file for single-key+file style lookups, + NULL for query-style lookups + search_type the type of search required + modemask if a real single file is used, this specifies mode bits that + must not be set; otherwise it is ignored + owners if a real single file is used, this specifies the possible + owners of the file; otherwise it is ignored + owngroups if a real single file is used, this specifies the possible + group owners of the file; otherwise it is ignored + +Returns: an identifying handle for the open database; + this is the pointer to the tree block in the + cache of open files; return NULL on open failure, with + a message in search_error_message +*/ + +void * +search_open(uschar *filename, int search_type, int modemask, uid_t *owners, + gid_t *owngroups) +{ +void *handle; +tree_node *t; +search_cache *c; +lookup_info *lk = lookup_list + search_type; +uschar keybuffer[256]; +int old_pool = store_pool; + +/* Change to the search store pool and remember our reset point */ + +store_pool = POOL_SEARCH; +if (search_reset_point == NULL) search_reset_point = store_get(0); + +DEBUG(D_lookup) debug_printf("search_open: %s \"%s\"\n", lk->name, + (filename == NULL)? US"NULL" : filename); + +/* See if we already have this open for this type of search, and if so, +pass back the tree block as the handle. The key for the tree node is the search +type plus '0' concatenated with the file name. There may be entries in the tree +with closed files if a lot of files have been opened. */ + +sprintf(CS keybuffer, "%c%.254s", search_type + '0', + (filename == NULL)? US"" : filename); + +if ((t = tree_search(search_tree, keybuffer)) != NULL) + { + c = (search_cache *)(t->data.ptr); + if (c->handle != NULL) + { + DEBUG(D_lookup) debug_printf(" cached open\n"); + store_pool = old_pool; + return t; + } + DEBUG(D_lookup) debug_printf(" cached closed\n"); + } + +/* Otherwise, we need to open the file or database - each search type has its +own code, which is now split off into separately compiled modules. Before doing +this, if the search type is one that uses real files, check on the number that +we are holding open in the cache. If the limit is reached, close the least +recently used one. */ + +if (lk->type == lookup_absfile && open_filecount >= lookup_open_max) + { + if (open_bot == NULL) + log_write(0, LOG_MAIN|LOG_PANIC, "too many lookups open, but can't find " + "one to close"); + else + { + search_cache *c = (search_cache *)(open_bot->data.ptr); + DEBUG(D_lookup) debug_printf("Too many lookup files open\n closing %s\n", + open_bot->name); + open_bot = c->up; + if (open_bot != NULL) + ((search_cache *)(open_bot->data.ptr))->down = NULL; + else + open_top = NULL; + ((lookup_list + c->search_type)->close)(c->handle); + c->handle = NULL; + open_filecount--; + } + } + +/* If opening is successful, call the file-checking function if there is one, +and if all is still well, enter the open database into the tree. */ + +handle = lk->open(filename, &search_error_message); +if (handle == NULL) + { + store_pool = old_pool; + return NULL; + } + +if (lk->check != NULL && + !lk->check(handle, filename, modemask, owners, owngroups, + &search_error_message)) + { + lk->close(handle); + store_pool = old_pool; + return NULL; + } + +/* If this is a search type that uses real files, keep count. */ + +if (lk->type == lookup_absfile) open_filecount++; + +/* If we found a previously opened entry in the tree, re-use it; otherwise +insert a new entry. On re-use, leave any cached lookup data and the lookup +count alone. */ + +if (t == NULL) + { + t = store_get(sizeof(tree_node) + Ustrlen(keybuffer)); + t->data.ptr = c = store_get(sizeof(search_cache)); + c->item_cache = NULL; + Ustrcpy(t->name, keybuffer); + tree_insertnode(&search_tree, t); + } +else c = t->data.ptr; + +c->handle = handle; +c->search_type = search_type; +c->up = c->down = NULL; + +store_pool = old_pool; +return t; +} + + + + + +/************************************************* +* Internal function: Find one item in database * +*************************************************/ + +/* The answer is always put into dynamic store. The last lookup for each handle +is cached. + +Arguments: + handle the handle from search_open; points to tree node + filename the filename that was handed to search_open, or + NULL for query-style searches + keystring the keystring for single-key+file lookups, or + the querystring for query-style lookups + +Returns: a pointer to a dynamic string containing the answer, + or NULL if the query failed or was deferred; in the + latter case, search_find_defer is set TRUE; after an unusual + failure, there may be a message in search_error_message. +*/ + +static uschar * +internal_search_find(void *handle, uschar *filename, uschar *keystring) +{ +tree_node *t = (tree_node *)handle; +search_cache *c = (search_cache *)(t->data.ptr); +uschar *data = NULL; +int search_type = t->name[0] - '0'; +int old_pool = store_pool; + +/* Lookups that return DEFER may not always set an error message. So that +the callers don't have to test for NULL, set an empty string. */ + +search_error_message = US""; +search_find_defer = FALSE; + +DEBUG(D_lookup) debug_printf("internal_search_find: file=\"%s\"\n " + "type=%s key=\"%s\"\n", filename, + lookup_list[search_type].name, keystring); + +/* Insurance. If the keystring is empty, just fail. */ + +if (keystring[0] == 0) return NULL; + +/* Use the special store pool for search data */ + +store_pool = POOL_SEARCH; + +/* Look up the data for the key, unless it is already in the cache for this +file. No need to check c->item_cache for NULL, tree_search will do so. */ + +if ((t = tree_search(c->item_cache, keystring)) == NULL) + { + BOOL do_cache = TRUE; + int keylength = Ustrlen(keystring); + + DEBUG(D_lookup) + { + if (filename != NULL) + debug_printf("file lookup required for %s\n in %s\n", + keystring, filename); + else + debug_printf("database lookup required for %s\n", keystring); + } + + /* Call the code for the different kinds of search. DEFER is handled + like FAIL, except that search_find_defer is set so the caller can + distinguish if necessary. */ + + if (lookup_list[search_type].find(c->handle, filename, keystring, keylength, + &data, &search_error_message, &do_cache) == DEFER) + { + search_find_defer = TRUE; + } + + /* A record that has been found is now in data, which is either NULL + or points to a bit of dynamic store. Cache the result of the lookup if + caching is permitted. Lookups can disable caching, when they did something + that changes their data. The mysql and pgsql lookups do this when an + UPDATE/INSERT query was executed. */ + + else if (do_cache) + { + int len = keylength + 1; + t = store_get(sizeof(tree_node) + len); + memcpy(t->name, keystring, len); + t->data.ptr = data; + tree_insertnode(&c->item_cache, t); + } + + /* If caching was disabled, empty the cache tree. We just set the cache + pointer to NULL here, because we cannot release the store at this stage. */ + + else + { + DEBUG(D_lookup) debug_printf("lookup forced cache cleanup\n"); + c->item_cache = NULL; + } + } + +/* Data was in the cache already; set the pointer from the tree node */ + +else + { + data = US t->data.ptr; + DEBUG(D_lookup) debug_printf("cached data used for lookup of %s%s%s\n", + keystring, + (filename == NULL)? US"" : US"\n in ", + (filename == NULL)? US"" : filename); + } + +/* Debug: output the answer */ + +DEBUG(D_lookup) + { + if (data == NULL) + { + if (search_find_defer) debug_printf("lookup deferred: %s\n", + search_error_message); + else debug_printf("lookup failed\n"); + } + else debug_printf("lookup yielded: %s\n", data); + } + +/* Return it in new dynamic store in the regular pool */ + +store_pool = old_pool; +return (data == NULL)? NULL : string_copy(data); +} + + + + +/************************************************* +* Find one item in database, possibly wildcarded * +*************************************************/ + +/* This function calls the internal function above; once only if there +is no partial matching, but repeatedly when partial matching is requested. + +Arguments: + handle the handle from search_open + filename the filename that was handed to search_open, or + NULL for query-style searches + keystring the keystring for single-key+file lookups, or + the querystring for query-style lookups + partial -1 means no partial matching; + otherwise it's the minimum number of components; + affix the affix string for partial matching + affixlen the length of the affix string + starflags SEARCH_STAR and SEARCH_STARAT flags + expand_setup pointer to offset for setting up expansion strings; + don't do any if < 0 + +Returns: a pointer to a dynamic string containing the answer, + or NULL if the query failed or was deferred; in the + latter case, search_find_defer is set TRUE +*/ + +uschar * +search_find(void *handle, uschar *filename, uschar *keystring, int partial, + uschar *affix, int affixlen, int starflags, int *expand_setup) +{ +tree_node *t = (tree_node *)handle; +BOOL set_null_wild = FALSE; +uschar *yield; + +DEBUG(D_lookup) + { + if (partial < 0) affixlen = 99; /* So that "NULL" prints */ + debug_printf("search_find: file=\"%s\"\n key=\"%s\" " + "partial=%d affix=%.*s starflags=%x\n", + (filename == NULL)? US"NULL" : filename, + keystring, partial, affixlen, affix, starflags); + } + +/* Arrange to put this database at the top of the LRU chain if it is a type +that opens real files. */ + +if (open_top != (tree_node *)handle && + lookup_list[t->name[0]-'0'].type == lookup_absfile) + { + search_cache *c = (search_cache *)(t->data.ptr); + tree_node *up = c->up; + tree_node *down = c->down; + + /* Cut it out of the list. A newly opened file will a NULL up pointer. + Otherwise there will be a non-NULL up pointer, since we checked above that + this block isn't already at the top of the list. */ + + if (up != NULL) + { + ((search_cache *)(up->data.ptr))->down = down; + if (down != NULL) + ((search_cache *)(down->data.ptr))->up = up; + else open_bot = up; + } + + /* Now put it at the head of the list. */ + + c->up = NULL; + c->down = open_top; + if (open_top == NULL) open_bot = t; else + ((search_cache *)(open_top->data.ptr))->up = t; + open_top = t; + } + +DEBUG(D_lookup) + { + tree_node *t = open_top; + debug_printf("LRU list:\n"); + while (t != NULL) + { + search_cache *c = (search_cache *)(t->data.ptr); + debug_printf(" %s\n", t->name); + if (t == open_bot) debug_printf(" End\n"); + t = c->down; + } + } + +/* First of all, try to match the key string verbatim. If matched a complete +entry but could have been partial, flag to set up variables. */ + +yield = internal_search_find(handle, filename, keystring); +if (search_find_defer) return NULL; +if (yield != NULL) { if (partial >= 0) set_null_wild = TRUE; } + +/* Not matched a complete entry; handle partial lookups, but only if the full +search didn't defer. Don't use string_sprintf() to construct the initial key, +just in case the original key is too long for the string_sprintf() buffer (it +*has* happened!). The case of a zero-length affix has to be treated specially. +*/ + +else if (partial >= 0) + { + int len = Ustrlen(keystring); + uschar *keystring2; + + /* Try with the affix on the front, except for a zero-length affix */ + + if (affixlen == 0) keystring2 = keystring; else + { + keystring2 = store_get(len + affixlen + 1); + Ustrncpy(keystring2, affix, affixlen); + Ustrcpy(keystring2 + affixlen, keystring); + DEBUG(D_lookup) debug_printf("trying partial match %s\n", keystring2); + yield = internal_search_find(handle, filename, keystring2); + if (search_find_defer) return NULL; + } + + /* The key in its entirety did not match a wild entry; try chopping off + leading components. */ + + if (yield == NULL) + { + int dotcount = 0; + uschar *keystring3 = keystring2 + affixlen; + uschar *s = keystring3; + while (*s != 0) if (*s++ == '.') dotcount++; + + while (dotcount-- >= partial) + { + while (*keystring3 != 0 && *keystring3 != '.') keystring3++; + + /* If we get right to the end of the string (which will be the last time + through this loop), we've failed if the affix is null. Otherwise do one + last lookup for the affix itself, but if it is longer than 1 character, + remove the last character if it is ".". */ + + if (*keystring3 == 0) + { + if (affixlen < 1) break; + if (affixlen > 1 && affix[affixlen-1] == '.') affixlen--; + Ustrncpy(keystring2, affix, affixlen); + keystring2[affixlen] = 0; + keystring3 = keystring2; + } + else + { + keystring3 -= affixlen - 1; + if (affixlen > 0) Ustrncpy(keystring3, affix, affixlen); + } + + DEBUG(D_lookup) debug_printf("trying partial match %s\n", keystring3); + yield = internal_search_find(handle, filename, keystring3); + if (search_find_defer) return NULL; + if (yield != NULL) + { + /* First variable is the wild part; second is the fixed part. Take care + to get it right when keystring3 is just "*". */ + + if (expand_setup != NULL && *expand_setup >= 0) + { + int fixedlength = Ustrlen(keystring3) - affixlen; + int wildlength = Ustrlen(keystring) - fixedlength - 1; + *expand_setup += 1; + expand_nstring[*expand_setup] = keystring; + expand_nlength[*expand_setup] = wildlength; + *expand_setup += 1; + expand_nstring[*expand_setup] = keystring + wildlength + 1; + expand_nlength[*expand_setup] = (fixedlength < 0)? 0 : fixedlength; + } + break; + } + keystring3 += affixlen; + } + } + + else set_null_wild = TRUE; /* Matched a wild entry without any wild part */ + } + +/* If nothing has been matched, but the option to look for "*@" is set, try +replacing everthing to the left of @ by *. After a match, the wild part +is set to the string to the left of the @. */ + +if (yield == NULL && (starflags & SEARCH_STARAT) != 0) + { + uschar *atat = Ustrrchr(keystring, '@'); + if (atat != NULL && atat > keystring) + { + int savechar; + savechar = *(--atat); + *atat = '*'; + + DEBUG(D_lookup) debug_printf("trying default match %s\n", atat); + yield = internal_search_find(handle, filename, atat); + *atat = savechar; + if (search_find_defer) return NULL; + + if (yield != NULL && expand_setup != NULL && *expand_setup >= 0) + { + *expand_setup += 1; + expand_nstring[*expand_setup] = keystring; + expand_nlength[*expand_setup] = atat - keystring + 1; + *expand_setup += 1; + expand_nstring[*expand_setup] = keystring; + expand_nlength[*expand_setup] = 0; + } + } + } + +/* If we still haven't matched anything, and the option to look for "*" is set, +try that. If we do match, the first variable (the wild part) is the whole key, +and the second is empty. */ + +if (yield == NULL && (starflags & (SEARCH_STAR|SEARCH_STARAT)) != 0) + { + DEBUG(D_lookup) debug_printf("trying to match *\n"); + yield = internal_search_find(handle, filename, US"*"); + if (yield != NULL && expand_setup != NULL && *expand_setup >= 0) + { + *expand_setup += 1; + expand_nstring[*expand_setup] = keystring; + expand_nlength[*expand_setup] = Ustrlen(keystring); + *expand_setup += 1; + expand_nstring[*expand_setup] = keystring; + expand_nlength[*expand_setup] = 0; + } + } + +/* If this was a potentially partial lookup, and we matched either a +complete non-wild domain entry, or we matched a wild-carded entry without +chopping off any of the domain components, set up the expansion variables +(if required) so that the first one is empty, and the second one is the +fixed part of the domain. The set_null_wild flag is set only when yield is not +NULL. */ + +if (set_null_wild && expand_setup != NULL && *expand_setup >= 0) + { + *expand_setup += 1; + expand_nstring[*expand_setup] = keystring; + expand_nlength[*expand_setup] = 0; + *expand_setup += 1; + expand_nstring[*expand_setup] = keystring; + expand_nlength[*expand_setup] = Ustrlen(keystring); + } + +return yield; +} + +/* End of search.c */ diff --git a/src/src/sieve.c b/src/src/sieve.c new file mode 100644 index 000000000..4301d1ab7 --- /dev/null +++ b/src/src/sieve.c @@ -0,0 +1,2780 @@ +/* $Cambridge: exim/src/src/sieve.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) Michael Haardt 2003,2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* This code was contributed by Michael Haardt. */ + + +/* Sieve mail filter. */ + +#include +#include +#include +#include +#include + +#include "exim.h" + +#if HAVE_ICONV +#include +#endif + +/* Define this for RFC compliant \r\n end-of-line terminators. */ +/* Undefine it for UNIX-style \n end-of-line terminators (default). */ +#undef RFC_EOL + +/* Define this for development of the subaddress Sieve extension. */ +/* The code is currently broken. */ +#undef SUBADDRESS + +/* Define this for development of the vacation Sieve extension. */ +/* The code is not yet finished. */ +#define VACATION + +/* Must be >= 1 */ +#define VACATION_MIN_DAYS 1 +/* Must be >= VACATION_MIN_DAYS, must be > 7, should be > 30 */ +#define VACATION_MAX_DAYS 31 + +/* Keep this at 75 to accept only RFC compliant MIME words. */ +/* Increase it if you want to match headers from buggy MUAs. */ +#define MIMEWORD_LENGTH 75 + +struct Sieve + { + uschar *filter; + const uschar *pc; + int line; + const uschar *errmsg; + int keep; + int require_envelope; + int require_fileinto; +#ifdef SUBADDRESS + int require_subaddress; +#endif +#ifdef VACATION + int require_vacation; + int vacation_ran; +#endif + uschar *vacation_directory; + int require_copy; + int require_iascii_numeric; + }; + +enum Comparator { COMP_OCTET, COMP_ASCII_CASEMAP, COMP_ASCII_NUMERIC }; +enum MatchType { MATCH_IS, MATCH_CONTAINS, MATCH_MATCHES }; +#ifdef SUBADDRESS +enum AddressPart { ADDRPART_USER, ADDRPART_DETAIL, ADDRPART_LOCALPART, ADDRPART_DOMAIN, ADDRPART_ALL }; +#else +enum AddressPart { ADDRPART_LOCALPART, ADDRPART_DOMAIN, ADDRPART_ALL }; +#endif +enum RelOp { LT, LE, EQ, GE, GT, NE }; + +struct String + { + uschar *character; + int length; + }; + +static int parse_test(struct Sieve *filter, int *cond, int exec); +static int parse_commands(struct Sieve *filter, int exec, address_item **generated); + +static uschar str_from_c[]="From"; +static const struct String str_from={ str_from_c, 4 }; +static uschar str_to_c[]="To"; +static const struct String str_to={ str_to_c, 2 }; +static uschar str_cc_c[]="Cc"; +static const struct String str_cc={ str_cc_c, 2 }; +static uschar str_bcc_c[]="Bcc"; +static const struct String str_bcc={ str_bcc_c, 3 }; +static uschar str_sender_c[]="Sender"; +static const struct String str_sender={ str_sender_c, 6 }; +static uschar str_resent_from_c[]="Resent-From"; +static const struct String str_resent_from={ str_resent_from_c, 11 }; +static uschar str_resent_to_c[]="Resent-To"; +static const struct String str_resent_to={ str_resent_to_c, 9 }; +static uschar str_fileinto_c[]="fileinto"; +static const struct String str_fileinto={ str_fileinto_c, 8 }; +static uschar str_envelope_c[]="envelope"; +static const struct String str_envelope={ str_envelope_c, 8 }; +#ifdef SUBADDRESS +static uschar str_subaddress_c[]="subaddress"; +static const struct String str_subaddress={ str_subaddress_c, 10 }; +#endif +#ifdef VACATION +static uschar str_vacation_c[]="vacation"; +static const struct String str_vacation={ str_vacation_c, 8 }; +static uschar str_subject_c[]="Subject"; +static const struct String str_subject={ str_subject_c, 7 }; +#endif +static uschar str_copy_c[]="copy"; +static const struct String str_copy={ str_copy_c, 4 }; +static uschar str_iascii_casemap_c[]="i;ascii-casemap"; +static const struct String str_iascii_casemap={ str_iascii_casemap_c, 15 }; +static uschar str_ioctet_c[]="i;octet"; +static const struct String str_ioctet={ str_ioctet_c, 7 }; +static uschar str_iascii_numeric_c[]="i;ascii-numeric"; +static const struct String str_iascii_numeric={ str_iascii_numeric_c, 15 }; +static uschar str_comparator_iascii_casemap_c[]="comparator-i;ascii-casemap"; +static const struct String str_comparator_iascii_casemap={ str_comparator_iascii_casemap_c, 26 }; +static uschar str_comparator_ioctet_c[]="comparator-i;octet"; +static const struct String str_comparator_ioctet={ str_comparator_ioctet_c, 18 }; +static uschar str_comparator_iascii_numeric_c[]="comparator-i;ascii-numeric"; +static const struct String str_comparator_iascii_numeric={ str_comparator_iascii_numeric_c, 26 }; + + +/************************************************* +* Encode to quoted-printable * +*************************************************/ + +/* +Arguments: + src UTF-8 string +*/ + +static struct String *quoted_printable_encode(const struct String *src, struct String *dst) +{ +int pass; +const uschar *start,*end; +uschar *new = NULL; +uschar ch; +size_t line; + +for (pass=0; pass<=1; ++pass) + { + line=0; + if (pass==0) + dst->length=0; + else + { + dst->character=store_get(dst->length+1); /* plus one for \0 */ + new=dst->character; + } + for (start=src->character,end=start+src->length; start=73) + { + if (pass==0) + dst->length+=2; + else + { + *new++='='; + *new++='\n'; + } + line=0; + } + if + ( + (ch>=33 && ch<=60) + || (ch>=62 && ch<=126) + || + ( + (ch==9 || ch==32) +#ifdef RFC_EOL + && start+2length; + else + *new++=*start; + ++line; + } +#ifdef RFC_EOL + else if (ch=='\r' && start+1length; + line=0; + ++start; + } + else + *new++='\n'; + line=0; + } +#else + else if (ch=='\n') + { + if (pass==0) + ++dst->length; + else + *new++=*start; + ++line; + } +#endif + else + { + if (pass==0) + dst->length+=3; + else + { + sprintf(CS new,"=%02X",ch); + new+=3; + } + line+=3; + } + } + } + *new='\0'; /* not included in length, but nice */ + return dst; +} + + +/************************************************* +* Octet-wise string comparison * +*************************************************/ + +/* +Arguments: + needle UTF-8 string to search ... + haystack ... inside the haystack + match_prefix 1 to compare if needle is a prefix of haystack + +Returns: 0 needle not found in haystack + 1 needle found +*/ + +static int eq_octet(const struct String *needle, + const struct String *haystack, int match_prefix) +{ +size_t nl,hl; +const uschar *n,*h; + +nl=needle->length; +n=needle->character; +hl=haystack->length; +h=haystack->character; +while (nl>0 && hl>0) + { +#if !HAVE_ICONV + if (*n&0x80) return 0; + if (*h&0x80) return 0; +#endif + if (*n!=*h) return 0; + ++n; + ++h; + --nl; + --hl; + } +return (match_prefix ? nl==0 : nl==0 && hl==0); +} + + +/************************************************* +* ASCII case-insensitive string comparison * +*************************************************/ + +/* +Arguments: + needle UTF-8 string to search ... + haystack ... inside the haystack + match_prefix 1 to compare if needle is a prefix of haystack + +Returns: 0 needle not found in haystack + 1 needle found +*/ + +static int eq_asciicase(const struct String *needle, + const struct String *haystack, int match_prefix) +{ +size_t nl,hl; +const uschar *n,*h; +uschar nc,hc; + +nl=needle->length; +n=needle->character; +hl=haystack->length; +h=haystack->character; +while (nl>0 && hl>0) + { + nc=*n; + hc=*h; +#if !HAVE_ICONV + if (nc&0x80) return 0; + if (hc&0x80) return 0; +#endif + /* tolower depends on the locale and only ASCII case must be insensitive */ + if ((nc&0x80) || (hc&0x80)) { if (nc!=hc) return 0; } + else if ((nc>='A' && nc<='Z' ? nc|0x20 : nc) != (hc>='A' && hc<='Z' ? hc|0x20 : hc)) return 0; + ++n; + ++h; + --nl; + --hl; + } +return (match_prefix ? nl==0 : nl==0 && hl==0); +} + + +/************************************************* +* Octet-wise glob pattern search * +*************************************************/ + +/* +Arguments: + needle pattern to search ... + haystack ... inside the haystack + +Returns: 0 needle not found in haystack + 1 needle found +*/ + +static int eq_octetglob(const struct String *needle, + const struct String *haystack) +{ +struct String n,h; + +n=*needle; +h=*haystack; +while (n.length) + { + switch (n.character[0]) + { + case '*': + { + int currentLength; + + ++n.character; + --n.length; + /* The greedy match is not yet well tested. Some day we may */ + /* need to refer to the matched parts, so the code is already */ + /* prepared for that. */ +#if 1 + /* greedy match */ + currentLength=h.length; + h.character+=h.length; + h.length=0; + while (h.length<=currentLength) + { + if (eq_octetglob(&n,&h)) return 1; + else /* go back one octet */ + { + --h.character; + ++h.length; + } + } + return 0; +#else + /* minimal match */ + while (h.length) + { + if (eq_octetglob(&n,&h)) return 1; + else /* advance one octet */ + { + ++h.character; + --h.length; + } + } + break; +#endif + } + case '?': + { + if (h.length) + { + ++h.character; + --h.length; + ++n.character; + --n.length; + } + else return 0; + break; + } + case '\\': + { + ++n.character; + --n.length; + /* FALLTHROUGH */ + } + default: + { + if + ( + h.length==0 || +#if !HAVE_ICONV + (h.character[0]&0x80) || (n.character[0]&0x80) || +#endif + h.character[0]!=n.character[0] + ) return 0; + else + { + ++h.character; + --h.length; + ++n.character; + --n.length; + }; + } + } + } +return (h.length==0); +} + + +/************************************************* +* ASCII case-insensitive glob pattern search * +*************************************************/ + +/* +Arguments: + needle UTF-8 pattern to search ... + haystack ... inside the haystack + +Returns: 0 needle not found in haystack + 1 needle found +*/ + +static int eq_asciicaseglob(const struct String *needle, + const struct String *haystack) +{ +struct String n,h; + +n=*needle; +h=*haystack; +while (n.length) + { + switch (n.character[0]) + { + case '*': + { + int currentLength; + + ++n.character; + --n.length; + /* The greedy match is not yet well tested. Some day we may */ + /* need to refer to the matched parts, so the code is already */ + /* prepared for that. */ +#if 1 + /* greedy match */ + currentLength=h.length; + h.character+=h.length; + h.length=0; + while (h.length<=currentLength) + { + if (eq_asciicaseglob(&n,&h)) return 1; + else /* go back one UTF-8 character */ + { + if (h.length==currentLength) return 0; + --h.character; + ++h.length; + if (h.character[0]&0x80) + { + while (h.length='A' && nc<='Z' ? nc|0x20 : nc) != (hc>='A' && hc<='Z' ? hc|0x20 : hc)) return 0; + ++h.character; + --h.length; + ++n.character; + --n.length; + } + } + } +return (h.length==0); +} + + +/************************************************* +* ASCII numeric comparison * +*************************************************/ + +/* +Arguments: + a first numeric string + b second numeric string + relop relational operator + +Returns: 0 not (a relop b) + 1 a relop b +*/ + +static int eq_asciinumeric(const struct String *a, + const struct String *b, enum RelOp relop) +{ +size_t al,bl; +const uschar *as,*aend,*bs,*bend; +int cmp; + +as=a->character; +aend=a->character+a->length; +bs=b->character; +bend=b->character+b->length; + +while (*as>='0' && *as<='9' && ascharacter; +while (*bs>='0' && *bs<='9' && bscharacter; + +if (al && bl==0) cmp=-1; +else if (al==0 && bl==0) cmp=0; +else if (al==0 && bl) cmp=1; +else + { + cmp=al-bl; + if (cmp==0) cmp=memcmp(a->character,b->character,al); + } +switch (relop) + { + case LT: return cmp<0; + case LE: return cmp<=0; + case EQ: return cmp==0; + case GE: return cmp>=0; + case GT: return cmp>0; + case NE: return cmp!=0; + } + /*NOTREACHED*/ + return -1; +} + + +/************************************************* +* Compare strings * +*************************************************/ + +/* +Arguments: + needle UTF-8 pattern or string to search ... + haystack ... inside the haystack + co comparator to use + mt match type to use + +Returns: 0 needle not found in haystack + 1 needle found + -1 comparator does not offer matchtype +*/ + +static int compare(struct Sieve *filter, const struct String *needle, const struct String *haystack, + enum Comparator co, enum MatchType mt) +{ +int r=0; + +if ((filter_test != NULL && debug_selector != 0) || + (debug_selector & D_filter) != 0) + { + debug_printf("String comparison (match "); + switch (mt) + { + case MATCH_IS: debug_printf(":is"); break; + case MATCH_CONTAINS: debug_printf(":contains"); break; + case MATCH_MATCHES: debug_printf(":matches"); break; + } + debug_printf(", comparison \""); + switch (co) + { + case COMP_OCTET: debug_printf("i;octet"); break; + case COMP_ASCII_CASEMAP: debug_printf("i;ascii-casemap"); break; + case COMP_ASCII_NUMERIC: debug_printf("i;ascii-numeric"); break; + } + debug_printf("\"):\n"); + debug_printf(" Search = %s (%d chars)\n", needle->character,needle->length); + debug_printf(" Inside = %s (%d chars)\n", haystack->character,haystack->length); + } +switch (mt) + { + case MATCH_IS: + { + switch (co) + { + case COMP_OCTET: + { + if (eq_octet(needle,haystack,0)) r=1; + break; + } + case COMP_ASCII_CASEMAP: + { + if (eq_asciicase(needle,haystack,0)) r=1; + break; + } + case COMP_ASCII_NUMERIC: + { + if (!filter->require_iascii_numeric) + { + filter->errmsg=CUS "missing previous require \"comparator-i;ascii-numeric\";"; + return -1; + } + if (eq_asciinumeric(needle,haystack,EQ)) r=1; + break; + } + } + break; + } + case MATCH_CONTAINS: + { + struct String h; + + switch (co) + { + case COMP_OCTET: + { + for (h=*haystack; h.length; ++h.character,--h.length) if (eq_octet(needle,&h,1)) { r=1; break; } + break; + } + case COMP_ASCII_CASEMAP: + { + for (h=*haystack; h.length; ++h.character,--h.length) if (eq_asciicase(needle,&h,1)) { r=1; break; } + break; + } + default: + { + filter->errmsg=CUS "comparator does not offer specified matchtype"; + return -1; + } + } + break; + } + case MATCH_MATCHES: + { + switch (co) + { + case COMP_OCTET: + { + if (eq_octetglob(needle,haystack)) r=1; + break; + } + case COMP_ASCII_CASEMAP: + { + if (eq_asciicaseglob(needle,haystack)) r=1; + break; + } + default: + { + filter->errmsg=CUS "comparator does not offer specified matchtype"; + return -1; + } + } + break; + } + } +if ((filter_test != NULL && debug_selector != 0) || + (debug_selector & D_filter) != 0) + debug_printf(" Result %s\n",r?"true":"false"); +return r; +} + + +/************************************************* +* Check header field syntax * +*************************************************/ + +/* +RFC 2822, section 3.6.8 says: + + field-name = 1*ftext + + ftext = %d33-57 / ; Any character except + %d59-126 ; controls, SP, and + ; ":". + +That forbids 8-bit header fields. This implementation accepts them, since +all of Exim is 8-bit clean, so it adds %d128-%d255. + +Arguments: + header header field to quote for suitable use in Exim expansions + +Returns: 0 string is not a valid header field + 1 string is a value header field +*/ + +static int is_header(const struct String *header) +{ +size_t l; +const uschar *h; + +l=header->length; +h=header->character; +if (l==0) return 0; +while (l) + { + if (((unsigned char)*h)<33 || ((unsigned char)*h)==':' || ((unsigned char)*h)==127) return 0; + else + { + ++h; + --l; + } + } +return 1; +} + + +/************************************************* +* Quote special characters string * +*************************************************/ + +/* +Arguments: + header header field to quote for suitable use in Exim expansions + or as debug output + +Returns: quoted string +*/ + +static const uschar *quote(const struct String *header) +{ +uschar *quoted=NULL; +int size=0,ptr=0; +size_t l; +const uschar *h; + +l=header->length; +h=header->character; +while (l) + { + switch (*h) + { + case '\0': + { + quoted=string_cat(quoted,&size,&ptr,CUS "\\0",2); + break; + } + case '$': + case '{': + case '}': + { + quoted=string_cat(quoted,&size,&ptr,CUS "\\",1); + } + default: + { + quoted=string_cat(quoted,&size,&ptr,h,1); + } + } + ++h; + --l; + } +quoted=string_cat(quoted,&size,&ptr,CUS "",1); +return quoted; +} + + +/************************************************* +* Add address to list of generated addresses * +*************************************************/ + +/* +According to RFC 3028, duplicate delivery to the same address must +not happen, so the list is first searched for the address. + +Arguments: + generated list of generated addresses + addr new address to add + file address denotes a file + +Returns: nothing +*/ + +static void add_addr(address_item **generated, uschar *addr, int file, int maxage, int maxmessages, int maxstorage) +{ +address_item *new_addr; + +for (new_addr=*generated; new_addr; new_addr=new_addr->next) + { + if (Ustrcmp(new_addr->address,addr)==0 && (file ? testflag(new_addr, af_pfr|af_file) : 1)) + { + if ((filter_test != NULL && debug_selector != 0) || (debug_selector & D_filter) != 0) + { + debug_printf("Repeated %s `%s' ignored.\n",file ? "fileinto" : "redirect", addr); + } + return; + } + } + +if ((filter_test != NULL && debug_selector != 0) || (debug_selector & D_filter) != 0) + { + debug_printf("%s `%s'\n",file ? "fileinto" : "redirect", addr); + } +new_addr=deliver_make_addr(addr,TRUE); +if (file) + { + setflag(new_addr, af_pfr|af_file); + new_addr->mode = 0; + } +new_addr->p.errors_address = NULL; +new_addr->next = *generated; +*generated = new_addr; +} + + +/************************************************* +* Return decoded header field * +*************************************************/ + +/* +Arguments: + value returned value of the field + header name of the header field + +Returns: nothing The expanded string is empty + in case there is no such header +*/ + +static void expand_header(struct String *value, const struct String *header) +{ +uschar *s,*r,*t; +uschar *errmsg; + +value->length=0; +value->character=(uschar*)0; + +t=r=s=expand_string(string_sprintf("$rheader_%s",quote(header))); +while (*r==' ') ++r; +while (*r) + { + if (*r=='\n') + { + ++r; + while (*r==' ' || *r=='\t') ++r; + if (*r) *t++=' '; + } + else + *t++=*r++; + } +*t++='\0'; +value->character=rfc2047_decode(s,TRUE,US"utf-8",'\0',&value->length,&errmsg); +} + + +/************************************************* +* Parse remaining hash comment * +*************************************************/ + +/* +Token definition: + Comment up to terminating CRLF + +Arguments: + filter points to the Sieve filter including its state + +Returns: 1 success + -1 syntax error +*/ + +static int parse_hashcomment(struct Sieve *filter) +{ +++filter->pc; +while (*filter->pc) + { +#ifdef RFC_EOL + if (*filter->pc=='\r' && *(filter->pc+1)=='\n') +#else + if (*filter->pc=='\n') +#endif + { +#ifdef RFC_EOL + filter->pc+=2; +#else + ++filter->pc; +#endif + ++filter->line; + return 1; + } + else ++filter->pc; + } +filter->errmsg=CUS "missing end of comment"; +return -1; +} + + +/************************************************* +* Parse remaining C-style comment * +*************************************************/ + +/* +Token definition: + Everything up to star slash + +Arguments: + filter points to the Sieve filter including its state + +Returns: 1 success + -1 syntax error +*/ + +static int parse_comment(struct Sieve *filter) +{ + filter->pc+=2; + while (*filter->pc) + { + if (*filter->pc=='*' && *(filter->pc+1)=='/') + { + filter->pc+=2; + return 1; + } + else ++filter->pc; + } + filter->errmsg=CUS "missing end of comment"; + return -1; +} + + +/************************************************* +* Parse optional white space * +*************************************************/ + +/* +Token definition: + Spaces, tabs, CRLFs, hash comments or C-style comments + +Arguments: + filter points to the Sieve filter including its state + +Returns: 1 success + -1 syntax error +*/ + +static int parse_white(struct Sieve *filter) +{ +while (*filter->pc) + { + if (*filter->pc==' ' || *filter->pc=='\t') ++filter->pc; +#ifdef RFC_EOL + else if (*filter->pc=='\r' && *(filter->pc+1)=='\n') +#else + else if (*filter->pc=='\n') +#endif + { +#ifdef RFC_EOL + filter->pc+=2; +#else + ++filter->pc; +#endif + ++filter->line; + } + else if (*filter->pc=='#') + { + if (parse_hashcomment(filter)==-1) return -1; + } + else if (*filter->pc=='/' && *(filter->pc+1)=='*') + { + if (parse_comment(filter)==-1) return -1; + } + else break; + } +return 1; +} + + +/************************************************* +* Parse a optional string * +*************************************************/ + +/* +Token definition: + quoted-string = DQUOTE *CHAR DQUOTE + ;; in general, \ CHAR inside a string maps to CHAR + ;; so \" maps to " and \\ maps to \ + ;; note that newlines and other characters are all allowed + ;; in strings + + multi-line = "text:" *(SP / HTAB) (hash-comment / CRLF) + *(multi-line-literal / multi-line-dotstuff) + "." CRLF + multi-line-literal = [CHAR-NOT-DOT *CHAR-NOT-CRLF] CRLF + multi-line-dotstuff = "." 1*CHAR-NOT-CRLF CRLF + ;; A line containing only "." ends the multi-line. + ;; Remove a leading '.' if followed by another '.'. + string = quoted-string / multi-line + +Arguments: + filter points to the Sieve filter including its state + id specifies identifier to match + +Returns: 1 success + -1 syntax error + 0 identifier not matched +*/ + +static int parse_string(struct Sieve *filter, struct String *data) +{ +int dataCapacity=0; + +data->length=0; +data->character=(uschar*)0; +if (*filter->pc=='"') /* quoted string */ + { + ++filter->pc; + while (*filter->pc) + { + if (*filter->pc=='"') /* end of string */ + { + int foo=data->length; + + ++filter->pc; + data->character=string_cat(data->character,&dataCapacity,&foo,CUS "",1); + return 1; + } + else if (*filter->pc=='\\' && *(filter->pc+1)) /* quoted character */ + { + if (*(filter->pc+1)=='0') data->character=string_cat(data->character,&dataCapacity,&data->length,CUS "",1); + else data->character=string_cat(data->character,&dataCapacity,&data->length,filter->pc+1,1); + filter->pc+=2; + } + else /* regular character */ + { + data->character=string_cat(data->character,&dataCapacity,&data->length,filter->pc,1); + filter->pc++; + } + } + filter->errmsg=CUS "missing end of string"; + return -1; + } +else if (Ustrncmp(filter->pc,CUS "text:",5)==0) /* multiline string */ + { + filter->pc+=5; + /* skip optional white space followed by hashed comment or CRLF */ + while (*filter->pc==' ' || *filter->pc=='\t') ++filter->pc; + if (*filter->pc=='#') + { + if (parse_hashcomment(filter)==-1) return -1; + } +#ifdef RFC_EOL + else if (*filter->pc=='\r' && *(filter->pc+1)=='\n') +#else + else if (*filter->pc=='\n') +#endif + { +#ifdef RFC_EOL + filter->pc+=2; +#else + ++filter->pc; +#endif + ++filter->line; + } + else + { + filter->errmsg=CUS "syntax error"; + return -1; + } + while (*filter->pc) + { +#ifdef RFC_EOL + if (*filter->pc=='\r' && *(filter->pc+1)=='\n') /* end of line */ +#else + if (*filter->pc=='\n') /* end of line */ +#endif + { + data->character=string_cat(data->character,&dataCapacity,&data->length,CUS "\r\n",2); +#ifdef RFC_EOL + filter->pc+=2; +#else + ++filter->pc; +#endif + ++filter->line; +#ifdef RFC_EOL + if (*filter->pc=='.' && *(filter->pc+1)=='\r' && *(filter->pc+2)=='\n') /* end of string */ +#else + if (*filter->pc=='.' && *(filter->pc+1)=='\n') /* end of string */ +#endif + { + data->character=string_cat(data->character,&dataCapacity,&data->length,CUS "",1); +#ifdef RFC_EOL + filter->pc+=3; +#else + filter->pc+=2; +#endif + ++filter->line; + return 1; + } + else if (*filter->pc=='.' && *(filter->pc+1)=='.') /* remove dot stuffing */ + { + data->character=string_cat(data->character,&dataCapacity,&data->length,CUS ".",1); + filter->pc+=2; + } + } + else /* regular character */ + { + data->character=string_cat(data->character,&dataCapacity,&data->length,filter->pc,1); + filter->pc++; + } + } + filter->errmsg=CUS "missing end of multi line string"; + return -1; + } +else return 0; +} + + +/************************************************* +* Parse a specific identifier * +*************************************************/ + +/* +Token definition: + identifier = (ALPHA / "_") *(ALPHA DIGIT "_") + +Arguments: + filter points to the Sieve filter including its state + id specifies identifier to match + +Returns: 1 success + 0 identifier not matched +*/ + +static int parse_identifier(struct Sieve *filter, const uschar *id) +{ + size_t idlen=Ustrlen(id); + + if (Ustrncmp(filter->pc,id,idlen)==0) + { + uschar next=filter->pc[idlen]; + + if ((next>='A' && next<='Z') || (next>='a' && next<='z') || next=='_' || (next>='0' && next<='9')) return 0; + filter->pc+=idlen; + return 1; + } + else return 0; +} + + +/************************************************* +* Parse a number * +*************************************************/ + +/* +Token definition: + number = 1*DIGIT [QUANTIFIER] + QUANTIFIER = "K" / "M" / "G" + +Arguments: + filter points to the Sieve filter including its state + data returns value + +Returns: 1 success + -1 no string list found +*/ + +static int parse_number(struct Sieve *filter, unsigned long *data) +{ +unsigned long d,u; + +if (*filter->pc>='0' && *filter->pc<='9') + { + uschar *e; + + errno=0; + d=Ustrtoul(filter->pc,&e,10); + if (errno==ERANGE) + { + filter->errmsg=CUstrerror(ERANGE); + return -1; + } + filter->pc=e; + u=1; + if (*filter->pc=='K') { u=1024; ++filter->pc; } + else if (*filter->pc=='M') { u=1024*1024; ++filter->pc; } + else if (*filter->pc=='G') { u=1024*1024*1024; ++filter->pc; } + if (d>(ULONG_MAX/u)) + { + filter->errmsg=CUstrerror(ERANGE); + return -1; + } + d*=u; + *data=d; + return 1; + } +else + { + filter->errmsg=CUS "missing number"; + return -1; + } +} + + +/************************************************* +* Parse a string list * +*************************************************/ + +/* +Grammar: + string-list = "[" string *("," string) "]" / string + +Arguments: + filter points to the Sieve filter including its state + data returns string list + +Returns: 1 success + -1 no string list found +*/ + +static int parse_stringlist(struct Sieve *filter, struct String **data) +{ +const uschar *orig=filter->pc; +int dataCapacity=0; +int dataLength=0; +struct String *d=(struct String*)0; +int m; + +if (*filter->pc=='[') /* string list */ + { + ++filter->pc; + for (;;) + { + if (parse_white(filter)==-1) goto error; + if ((dataLength+1)>=dataCapacity) /* increase buffer */ + { + struct String *new; + int newCapacity; /* Don't amalgamate with next line; some compilers grumble */ + newCapacity=dataCapacity?(dataCapacity*=2):(dataCapacity=4); + if ((new=(struct String*)store_get(sizeof(struct String)*newCapacity))==(struct String*)0) + { + filter->errmsg=CUstrerror(errno); + goto error; + } + if (d) memcpy(new,d,sizeof(struct String)*dataLength); + d=new; + dataCapacity=newCapacity; + } + m=parse_string(filter,&d[dataLength]); + if (m==0) + { + if (dataLength==0) break; + else + { + filter->errmsg=CUS "missing string"; + goto error; + } + } + else if (m==-1) goto error; + else ++dataLength; + if (parse_white(filter)==-1) goto error; + if (*filter->pc==',') ++filter->pc; + else break; + } + if (*filter->pc==']') + { + d[dataLength].character=(uschar*)0; + d[dataLength].length=-1; + ++filter->pc; + *data=d; + return 1; + } + else + { + filter->errmsg=CUS "missing closing bracket"; + goto error; + } + } +else /* single string */ + { + if ((d=store_get(sizeof(struct String)*2))==(struct String*)0) + { + return -1; + } + m=parse_string(filter,&d[0]); + if (m==-1) + { + return -1; + } + else if (m==0) + { + filter->pc=orig; + return 0; + } + else + { + d[1].character=(uschar*)0; + d[1].length=-1; + *data=d; + return 1; + } + } +error: +filter->errmsg=CUS "missing string list"; +return -1; +} + + +/************************************************* +* Parse an optional address part specifier * +*************************************************/ + +/* +Grammar: + address-part = ":localpart" / ":domain" / ":all" + address-part =/ ":user" / ":detail" + +Arguments: + filter points to the Sieve filter including its state + a returns address part specified + +Returns: 1 success + 0 no comparator found + -1 syntax error +*/ + +static int parse_addresspart(struct Sieve *filter, enum AddressPart *a) +{ +#ifdef SUBADDRESS +if (parse_identifier(filter,CUS ":user")==1) + { + if (!filter->require_subaddress) + { + filter->errmsg=CUS "missing previous require \"subaddress\";"; + return -1; + } + *a=ADDRPART_USER; + return 1; + } +else if (parse_identifier(filter,CUS ":detail")==1) + { + if (!filter->require_subaddress) + { + filter->errmsg=CUS "missing previous require \"subaddress\";"; + return -1; + } + *a=ADDRPART_DETAIL; + return 1; + } +else +#endif +if (parse_identifier(filter,CUS ":localpart")==1) + { + *a=ADDRPART_LOCALPART; + return 1; + } +else if (parse_identifier(filter,CUS ":domain")==1) + { + *a=ADDRPART_DOMAIN; + return 1; + } +else if (parse_identifier(filter,CUS ":all")==1) + { + *a=ADDRPART_ALL; + return 1; + } +else return 0; +} + + +/************************************************* +* Parse an optional comparator * +*************************************************/ + +/* +Grammar: + comparator = ":comparator" + +Arguments: + filter points to the Sieve filter including its state + c returns comparator + +Returns: 1 success + 0 no comparator found + -1 incomplete comparator found +*/ + +static int parse_comparator(struct Sieve *filter, enum Comparator *c) +{ +struct String comparator_name; + +if (parse_identifier(filter,CUS ":comparator")==0) return 0; +if (parse_white(filter)==-1) return -1; +switch (parse_string(filter,&comparator_name)) + { + case -1: return -1; + case 0: + { + filter->errmsg=CUS "missing comparator"; + return -1; + } + default: + { + int match; + + if (eq_asciicase(&comparator_name,&str_ioctet,0)) + { + *c=COMP_OCTET; + match=1; + } + else if (eq_asciicase(&comparator_name,&str_iascii_casemap,0)) + { + *c=COMP_ASCII_CASEMAP; + match=1; + } + else if (eq_asciicase(&comparator_name,&str_iascii_numeric,0)) + { + *c=COMP_ASCII_NUMERIC; + match=1; + } + else + { + filter->errmsg=CUS "invalid comparator"; + match=-1; + } + return match; + } + } +} + + +/************************************************* +* Parse an optional match type * +*************************************************/ + +/* +Grammar: + match-type = ":is" / ":contains" / ":matches" + +Arguments: + filter points to the Sieve filter including its state + m returns match type + +Returns: 1 success + 0 no match type found +*/ + +static int parse_matchtype(struct Sieve *filter, enum MatchType *m) +{ + if (parse_identifier(filter,CUS ":is")==1) + { + *m=MATCH_IS; + return 1; + } + else if (parse_identifier(filter,CUS ":contains")==1) + { + *m=MATCH_CONTAINS; + return 1; + } + else if (parse_identifier(filter,CUS ":matches")==1) + { + *m=MATCH_MATCHES; + return 1; + } + else return 0; +} + + +/************************************************* +* Parse and interpret an optional test list * +*************************************************/ + +/* +Grammar: + test-list = "(" test *("," test) ")" + +Arguments: + filter points to the Sieve filter including its state + n total number of tests + true number of passed tests + exec Execute parsed statements + +Returns: 1 success + 0 no test list found + -1 syntax or execution error +*/ + +static int parse_testlist(struct Sieve *filter, int *n, int *true, int exec) +{ +if (parse_white(filter)==-1) return -1; +if (*filter->pc=='(') + { + ++filter->pc; + *n=0; + *true=0; + for (;;) + { + int cond; + + switch (parse_test(filter,&cond,exec)) + { + case -1: return -1; + case 0: filter->errmsg=CUS "missing test"; return -1; + default: ++*n; if (cond) ++*true; break; + } + if (parse_white(filter)==-1) return -1; + if (*filter->pc==',') ++filter->pc; + else break; + } + if (*filter->pc==')') + { + ++filter->pc; + return 1; + } + else + { + filter->errmsg=CUS "missing closing paren"; + return -1; + } + } +else return 0; +} + + +/************************************************* +* Parse and interpret an optional test * +*************************************************/ + +/* +Arguments: + filter points to the Sieve filter including its state + cond returned condition status + exec Execute parsed statements + +Returns: 1 success + 0 no test found + -1 syntax or execution error +*/ + +static int parse_test(struct Sieve *filter, int *cond, int exec) +{ +if (parse_white(filter)==-1) return -1; +if (parse_identifier(filter,CUS "address")) + { + /* + address-test = "address" { [address-part] [comparator] [match-type] } + + + header-list From, To, Cc, Bcc, Sender, Resent-From, Resent-To + */ + + enum AddressPart addressPart=ADDRPART_ALL; + enum Comparator comparator=COMP_ASCII_CASEMAP; + enum MatchType matchType=MATCH_IS; + struct String *hdr,*h,*key,*k; + int m; + int ap=0,co=0,mt=0; + + for (;;) + { + if (parse_white(filter)==-1) return -1; + if ((m=parse_addresspart(filter,&addressPart))!=0) + { + if (m==-1) return -1; + if (ap) + { + filter->errmsg=CUS "address part already specified"; + return -1; + } + else ap=1; + } + else if ((m=parse_comparator(filter,&comparator))!=0) + { + if (m==-1) return -1; + if (co) + { + filter->errmsg=CUS "comparator already specified"; + return -1; + } + else co=1; + } + else if ((m=parse_matchtype(filter,&matchType))!=0) + { + if (m==-1) return -1; + if (mt) + { + filter->errmsg=CUS "match type already specified"; + return -1; + } + else mt=1; + } + else break; + } + if (parse_white(filter)==-1) return -1; + if ((m=parse_stringlist(filter,&hdr))!=1) + { + if (m==0) filter->errmsg=CUS "header string list expected"; + return -1; + } + if (parse_white(filter)==-1) return -1; + if ((m=parse_stringlist(filter,&key))!=1) + { + if (m==0) filter->errmsg=CUS "key string list expected"; + return -1; + } + *cond=0; + for (h=hdr; h->length!=-1 && !*cond; ++h) + { + uschar *header_value=(uschar*)0,*extracted_addr,*end_addr; + + if + ( + !eq_asciicase(h,&str_from,0) + && !eq_asciicase(h,&str_to,0) + && !eq_asciicase(h,&str_cc,0) + && !eq_asciicase(h,&str_bcc,0) + && !eq_asciicase(h,&str_sender,0) + && !eq_asciicase(h,&str_resent_from,0) + && !eq_asciicase(h,&str_resent_to,0) + ) + { + filter->errmsg=CUS "invalid header field"; + return -1; + } + if (exec) + { + /* We are only interested in addresses below, so no MIME decoding */ + header_value=expand_string(string_sprintf("$rheader_%s",quote(h))); + if (header_value == NULL) + { + filter->errmsg=CUS "header string expansion failed"; + return -1; + } + parse_allow_group = TRUE; + while (*header_value && !*cond) + { + uschar *error; + int start, end, domain; + int saveend; + uschar *part=NULL; + + end_addr = parse_find_address_end(header_value, FALSE); + saveend = *end_addr; + *end_addr = 0; + extracted_addr = parse_extract_address(header_value, &error, &start, &end, &domain, FALSE); + + if (extracted_addr) switch (addressPart) + { + case ADDRPART_ALL: part=extracted_addr; break; +#ifdef SUBADDRESS + case ADDRPART_USER: +#endif + case ADDRPART_LOCALPART: part=extracted_addr; part[domain-1]='\0'; break; + case ADDRPART_DOMAIN: part=extracted_addr+domain; break; +#ifdef SUBADDRESS + case ADDRPART_DETAIL: + part=NULL; +#endif + break; + } + + *end_addr = saveend; + if (part) + { + for (k=key; k->length!=-1; ++k) + { + struct String partStr; + + partStr.character=part; + partStr.length=Ustrlen(part); + if (extracted_addr) + { + *cond=compare(filter,k,&partStr,comparator,matchType); + if (*cond==-1) return -1; + if (*cond) break; + } + } + } + if (saveend == 0) break; + header_value = end_addr + 1; + } + } + } + return 1; + } +else if (parse_identifier(filter,CUS "allof")) + { + /* + allof-test = "allof" + */ + + int n,true; + + switch (parse_testlist(filter,&n,&true,exec)) + { + case -1: return -1; + case 0: filter->errmsg=CUS "missing test list"; return -1; + default: *cond=(n==true); return 1; + } + } +else if (parse_identifier(filter,CUS "anyof")) + { + /* + anyof-test = "anyof" + */ + + int n,true; + + switch (parse_testlist(filter,&n,&true,exec)) + { + case -1: return -1; + case 0: filter->errmsg=CUS "missing test list"; return -1; + default: *cond=(true>0); return 1; + } + } +else if (parse_identifier(filter,CUS "exists")) + { + /* + exists-test = "exists" + */ + + struct String *hdr,*h; + int m; + + if (parse_white(filter)==-1) return -1; + if ((m=parse_stringlist(filter,&hdr))!=1) + { + if (m==0) filter->errmsg=CUS "header string list expected"; + return -1; + } + if (exec) + { + *cond=1; + for (h=hdr; h->length!=-1 && *cond; ++h) + { + uschar *header_def; + + header_def=expand_string(string_sprintf("${if def:header_%s {true}{false}}",quote(h))); + if (header_def == NULL) + { + filter->errmsg=CUS "header string expansion failed"; + return -1; + } + if (Ustrcmp(header_def,"false")==0) *cond=0; + } + } + return 1; + } +else if (parse_identifier(filter,CUS "false")) + { + /* + false-test = "false" + */ + + *cond=0; + return 1; + } +else if (parse_identifier(filter,CUS "header")) + { + /* + header-test = "header" { [comparator] [match-type] } + + */ + + enum Comparator comparator=COMP_ASCII_CASEMAP; + enum MatchType matchType=MATCH_IS; + struct String *hdr,*h,*key,*k; + int m; + int co=0,mt=0; + + for (;;) + { + if (parse_white(filter)==-1) return -1; + if ((m=parse_comparator(filter,&comparator))!=0) + { + if (m==-1) return -1; + if (co) + { + filter->errmsg=CUS "comparator already specified"; + return -1; + } + else co=1; + } + else if ((m=parse_matchtype(filter,&matchType))!=0) + { + if (m==-1) return -1; + if (mt) + { + filter->errmsg=CUS "match type already specified"; + return -1; + } + else mt=1; + } + else break; + } + if (parse_white(filter)==-1) return -1; + if ((m=parse_stringlist(filter,&hdr))!=1) + { + if (m==0) filter->errmsg=CUS "header string list expected"; + return -1; + } + if (parse_white(filter)==-1) return -1; + if ((m=parse_stringlist(filter,&key))!=1) + { + if (m==0) filter->errmsg=CUS "key string list expected"; + return -1; + } + *cond=0; + for (h=hdr; h->length!=-1 && !*cond; ++h) + { + if (!is_header(h)) + { + filter->errmsg=CUS "invalid header field"; + return -1; + } + if (exec) + { + struct String header_value; + uschar *header_def; + + expand_header(&header_value,h); + header_def=expand_string(string_sprintf("${if def:header_%s {true}{false}}",quote(h))); + if (header_value.character == NULL || header_def == NULL) + { + filter->errmsg=CUS "header string expansion failed"; + return -1; + } + for (k=key; k->length!=-1; ++k) + { + if (Ustrcmp(header_def,"true")==0) + { + *cond=compare(filter,k,&header_value,comparator,matchType); + if (*cond==-1) return -1; + if (*cond) break; + } + } + } + } + return 1; + } +else if (parse_identifier(filter,CUS "not")) + { + if (parse_white(filter)==-1) return -1; + switch (parse_test(filter,cond,exec)) + { + case -1: return -1; + case 0: filter->errmsg=CUS "missing test"; return -1; + default: *cond=!*cond; return 1; + } + } +else if (parse_identifier(filter,CUS "size")) + { + /* + relop = ":over" / ":under" + size-test = "size" relop + */ + + unsigned long limit; + int overNotUnder; + + if (parse_white(filter)==-1) return -1; + if (parse_identifier(filter,CUS ":over")) overNotUnder=1; + else if (parse_identifier(filter,CUS ":under")) overNotUnder=0; + else + { + filter->errmsg=CUS "missing :over or :under"; + return -1; + } + if (parse_white(filter)==-1) return -1; + if (parse_number(filter,&limit)==-1) return -1; + *cond=(overNotUnder ? (message_size>limit) : (message_size + + envelope-part is case insensitive "from" or "to" + */ + + enum Comparator comparator=COMP_ASCII_CASEMAP; + enum AddressPart addressPart=ADDRPART_ALL; + enum MatchType matchType=MATCH_IS; + struct String *env,*e,*key,*k; + int m; + int co=0,ap=0,mt=0; + + if (!filter->require_envelope) + { + filter->errmsg=CUS "missing previous require \"envelope\";"; + return -1; + } + for (;;) + { + if (parse_white(filter)==-1) return -1; + if ((m=parse_comparator(filter,&comparator))!=0) + { + if (m==-1) return -1; + if (co) + { + filter->errmsg=CUS "comparator already specified"; + return -1; + } + else co=1; + } + else if ((m=parse_addresspart(filter,&addressPart))!=0) + { + if (m==-1) return -1; + if (ap) + { + filter->errmsg=CUS "address part already specified"; + return -1; + } + else ap=1; + } + else if ((m=parse_matchtype(filter,&matchType))!=0) + { + if (m==-1) return -1; + if (mt) + { + filter->errmsg=CUS "match type already specified"; + return -1; + } + else mt=1; + } + else break; + } + if (parse_white(filter)==-1) return -1; + if ((m=parse_stringlist(filter,&env))!=1) + { + if (m==0) filter->errmsg=CUS "envelope string list expected"; + return -1; + } + if (parse_white(filter)==-1) return -1; + if ((m=parse_stringlist(filter,&key))!=1) + { + if (m==0) filter->errmsg=CUS "key string list expected"; + return -1; + } + *cond=0; + for (e=env; e->character; ++e) + { + const uschar *envelopeExpr=CUS 0; + uschar *envelope=US 0; + + if (eq_asciicase(e,&str_from,0)) + { + switch (addressPart) + { + case ADDRPART_ALL: envelopeExpr=CUS "$sender_address"; break; +#ifdef SUBADDRESS + case ADDRPART_USER: +#endif + case ADDRPART_LOCALPART: envelopeExpr=CUS "${local_part:$sender_address}"; break; + case ADDRPART_DOMAIN: envelopeExpr=CUS "${domain:$sender_address}"; break; +#ifdef SUBADDRESS + case ADDRPART_DETAIL: + envelopeExpr=CUS 0; + break; +#endif + } + } + else if (eq_asciicase(e,&str_to,0)) + { + switch (addressPart) + { + case ADDRPART_ALL: envelopeExpr=CUS "$local_part_prefix$local_part$local_part_suffix@$domain"; break; +#ifdef SUBADDRESS + case ADDRPART_USER: envelopeExpr=CUS "$local_part_prefix$local_part"; break; + case ADDRPART_DETAIL: envelopeExpr=CUS "$local_part_suffix"; break; +#endif + case ADDRPART_LOCALPART: envelopeExpr=CUS "$local_part_prefix$local_part$local_part_suffix"; break; + case ADDRPART_DOMAIN: envelopeExpr=CUS "$domain"; break; + } + } + else + { + filter->errmsg=CUS "invalid envelope string"; + return -1; + } + if (exec && envelopeExpr) + { + if ((envelope=expand_string(US envelopeExpr)) == NULL) + { + filter->errmsg=CUS "header string expansion failed"; + return -1; + } + for (k=key; k->length!=-1; ++k) + { + struct String envelopeStr; + + envelopeStr.character=envelope; + envelopeStr.length=Ustrlen(envelope); + *cond=compare(filter,&envelopeStr,k,comparator,matchType); + if (*cond==-1) return -1; + if (*cond) break; + } + } + } + return 1; + } +else return 0; +} + + +/************************************************* +* Parse and interpret an optional block * +*************************************************/ + +/* +Arguments: + filter points to the Sieve filter including its state + exec Execute parsed statements + generated where to hang newly-generated addresses + +Returns: 2 success by stop + 1 other success + 0 no block command found + -1 syntax or execution error +*/ + +static int parse_block(struct Sieve *filter, int exec, + address_item **generated) +{ +int r; + +if (parse_white(filter)==-1) return -1; +if (*filter->pc=='{') + { + ++filter->pc; + if ((r=parse_commands(filter,exec,generated))==-1 || r==2) return r; + if (*filter->pc=='}') + { + ++filter->pc; + return 1; + } + else + { + filter->errmsg=CUS "missing closing brace"; + return -1; + } + } +else return 0; +} + + +/************************************************* +* Match a semicolon * +*************************************************/ + +/* +Arguments: + filter points to the Sieve filter including its state + +Returns: 1 success + -1 syntax error +*/ + +static int parse_semicolon(struct Sieve *filter) +{ + if (parse_white(filter)==-1) return -1; + if (*filter->pc==';') + { + ++filter->pc; + return 1; + } + else + { + filter->errmsg=CUS "missing semicolon"; + return -1; + } +} + + +/************************************************* +* Parse and interpret a Sieve command * +*************************************************/ + +/* +Arguments: + filter points to the Sieve filter including its state + exec Execute parsed statements + generated where to hang newly-generated addresses + +Returns: 2 success by stop + 1 other success + -1 syntax or execution error +*/ +static int parse_commands(struct Sieve *filter, int exec, + address_item **generated) +{ +while (*filter->pc) + { + if (parse_white(filter)==-1) return -1; + if (parse_identifier(filter,CUS "if")) + { + /* + if-command = "if" test block *( "elsif" test block ) [ else block ] + */ + + int cond,m,unsuccessful; + + /* test block */ + if (parse_white(filter)==-1) return -1; + if ((m=parse_test(filter,&cond,exec))==-1) return -1; + if (m==0) + { + filter->errmsg=CUS "missing test"; + return -1; + } + m=parse_block(filter,exec ? cond : 0, generated); + if (m==-1 || m==2) return m; + if (m==0) + { + filter->errmsg=CUS "missing block"; + return -1; + } + unsuccessful = !cond; + for (;;) /* elsif test block */ + { + if (parse_white(filter)==-1) return -1; + if (parse_identifier(filter,CUS "elsif")) + { + if (parse_white(filter)==-1) return -1; + m=parse_test(filter,&cond,exec && unsuccessful); + if (m==-1 || m==2) return m; + if (m==0) + { + filter->errmsg=CUS "missing test"; + return -1; + } + m=parse_block(filter,exec && unsuccessful ? cond : 0, generated); + if (m==-1 || m==2) return m; + if (m==0) + { + filter->errmsg=CUS "missing block"; + return -1; + } + if (exec && unsuccessful && cond) unsuccessful = 0; + } + else break; + } + /* else block */ + if (parse_white(filter)==-1) return -1; + if (parse_identifier(filter,CUS "else")) + { + m=parse_block(filter,exec && unsuccessful, generated); + if (m==-1 || m==2) return m; + if (m==0) + { + filter->errmsg=CUS "missing block"; + return -1; + } + } + } + else if (parse_identifier(filter,CUS "stop")) + { + /* + stop-command = "stop" { stop-options } ";" + stop-options = + */ + + if (parse_semicolon(filter)==-1) return -1; + if (exec) + { + filter->pc+=Ustrlen(filter->pc); + return 2; + } + } + else if (parse_identifier(filter,CUS "keep")) + { + /* + keep-command = "keep" { keep-options } ";" + keep-options = + */ + + if (parse_semicolon(filter)==-1) return -1; + if (exec) + { + add_addr(generated,US"inbox",1,0,0,0); + filter->keep = 0; + } + } + else if (parse_identifier(filter,CUS "discard")) + { + /* + discard-command = "discard" { discard-options } ";" + discard-options = + */ + + if (parse_semicolon(filter)==-1) return -1; + if (exec) filter->keep=0; + } + else if (parse_identifier(filter,CUS "redirect")) + { + /* + redirect-command = "redirect" redirect-options "string" ";" + redirect-options = + redirect-options =) ":copy" + */ + + struct String recipient; + int m; + int copy=0; + + for (;;) + { + if (parse_white(filter)==-1) return -1; + if (parse_identifier(filter,CUS ":copy")==1) + { + if (!filter->require_copy) + { + filter->errmsg=CUS "missing previous require \"copy\";"; + return -1; + } + copy=1; + } + else break; + } + if (parse_white(filter)==-1) return -1; + if ((m=parse_string(filter,&recipient))!=1) + { + if (m==0) filter->errmsg=CUS "missing redirect recipient string"; + return -1; + } + if (strchr(CCS recipient.character,'@')==(char*)0) + { + filter->errmsg=CUS "unqualified recipient address"; + return -1; + } + if (exec) + { + add_addr(generated,recipient.character,0,0,0,0); + if (!copy) filter->keep = 0; + } + if (parse_semicolon(filter)==-1) return -1; + } + else if (parse_identifier(filter,CUS "fileinto")) + { + /* + fileinto-command = "fileinto" { fileinto-options } string ";" + fileinto-options = + fileinto-options =) [ ":copy" ] + */ + + struct String folder; + uschar *s; + int m; + unsigned long maxage, maxmessages, maxstorage; + int copy=0; + + maxage = maxmessages = maxstorage = 0; + if (!filter->require_fileinto) + { + filter->errmsg=CUS "missing previous require \"fileinto\";"; + return -1; + } + for (;;) + { + if (parse_white(filter)==-1) return -1; + if (parse_identifier(filter,CUS ":copy")==1) + { + if (!filter->require_copy) + { + filter->errmsg=CUS "missing previous require \"copy\";"; + return -1; + } + copy=1; + } + else break; + } + if (parse_white(filter)==-1) return -1; + if ((m=parse_string(filter,&folder))!=1) + { + if (m==0) filter->errmsg=CUS "missing fileinto folder string"; + return -1; + } + m=0; s=folder.character; + if (folder.length==0) m=1; + if (Ustrcmp(s,"..")==0 || Ustrncmp(s,"../",3)==0) m=1; + else while (*s) + { + if (Ustrcmp(s,"/..")==0 || Ustrncmp(s,"/../",4)==0) { m=1; break; } + ++s; + } + if (m) + { + filter->errmsg=CUS "invalid folder"; + return -1; + } + if (exec) + { + add_addr(generated, folder.character, 1, maxage, maxmessages, maxstorage); + if (!copy) filter->keep = 0; + } + if (parse_semicolon(filter)==-1) return -1; + } +#ifdef VACATION + else if (parse_identifier(filter,CUS "vacation")) + { + /* + vacation-command = "vacation" { vacation-options } ";" + vacation-options = [":days" number] + [":addresses" string-list] + [":subject" string] + [":mime"] + */ + + int m; + unsigned long days; + struct String *addresses=(struct String*)0; + struct String subject; + int reason_is_mime; + string_item *aliases; + struct String reason; + + if (!filter->require_vacation) + { + filter->errmsg=CUS "missing previous require \"vacation\";"; + return -1; + } + if (exec) + { + if (filter->vacation_ran) + { + filter->errmsg=CUS "trying to execute vacation more than once"; + return -1; + } + filter->vacation_ran=1; + } + days=VACATION_MIN_DAYS>7 ? VACATION_MIN_DAYS : 7; + subject.character=(uschar*)0; + subject.length=-1; + aliases=NULL; + reason_is_mime=0; + for (;;) + { + if (parse_white(filter)==-1) return -1; + if (parse_identifier(filter,CUS ":days")==1) + { + if (parse_white(filter)==-1) return -1; + if (parse_number(filter,&days)==-1) return -1; + if (daysVACATION_MAX_DAYS) days=VACATION_MAX_DAYS; + } + else if (parse_identifier(filter,CUS ":addresses")==1) + { + struct String *a; + + if (parse_white(filter)==-1) return -1; + if ((m=parse_stringlist(filter,&addresses))!=1) + { + if (m==0) filter->errmsg=CUS "addresses string list expected"; + return -1; + } + for (a=addresses; a->length!=-1; ++a) + { + string_item *new; + + new=store_get(sizeof(string_item)); + new->text=store_get(a->length+1); + if (a->length) memcpy(new->text,a->character,a->length); + new->text[a->length]='\0'; + new->next=aliases; + aliases=new; + } + } + else if (parse_identifier(filter,CUS ":subject")==1) + { + if (parse_white(filter)==-1) return -1; + if ((m=parse_string(filter,&subject))!=1) + { + if (m==0) filter->errmsg=CUS "subject string expected"; + return -1; + } + } + else if (parse_identifier(filter,CUS ":mime")==1) + reason_is_mime=1; + else break; + } + if (parse_white(filter)==-1) return -1; + if ((m=parse_string(filter,&reason))!=1) + { + if (m==0) filter->errmsg=CUS "missing reason string"; + return -1; + } + if (parse_semicolon(filter)==-1) return -1; + + if (exec) + { + address_item *addr; + int capacity,start; + uschar *buffer; + int buffer_capacity; + struct String key; + md5 base; + uschar digest[16]; + uschar hexdigest[33]; + int i; + uschar *once; + + if (filter_personal(aliases,TRUE)) + { + + /* ensure oncelog directory exists; failure will be detected later */ + + (void)directory_make(NULL, filter->vacation_directory, 0700, FALSE); + + /* build oncelog filename */ + + key.character=(uschar*)0; + key.length=0; + capacity=0; + if (subject.length!=-1) key.character=string_cat(key.character,&capacity,&key.length,subject.character,subject.length); + key.character=string_cat(key.character,&capacity,&key.length,reason_is_mime?US"1":US"0",1); + key.character=string_cat(key.character,&capacity,&key.length,reason.character,reason.length); + md5_start(&base); + md5_end(&base, key.character, key.length, digest); + for (i = 0; i < 16; i++) sprintf(CS (hexdigest+2*i), "%02X", digest[i]); + capacity=Ustrlen(filter->vacation_directory); + start=capacity; + once=string_cat(filter->vacation_directory,&capacity,&start,US"/",1); + once=string_cat(once,&capacity,&start,hexdigest,33); + + /* process subject */ + + if (subject.length==-1) + { + expand_header(&subject,&str_subject); + while (subject.length>=4 && Ustrncmp(subject.character,"Re: ",4)==0) + { + subject.character+=4; + subject.length-=4; + } + capacity=6; + start=6; + subject.character=string_cat(US"Auto: ",&capacity,&start,subject.character,subject.length); + subject.length=start; + } + + /* add address to list of generated addresses */ + + addr = deliver_make_addr(string_sprintf(">%.256s", sender_address), FALSE); + setflag(addr, af_pfr); + setflag(addr, af_ignore_error); + addr->next = *generated; + *generated = addr; + addr->reply = store_get(sizeof(reply_item)); + memset(addr->reply,0,sizeof(reply_item)); /* XXX */ + addr->reply->to = string_copy(sender_address); + /* Allocation is larger than neccessary, but enough even for split MIME words */ + buffer_capacity=16+4*subject.length; + buffer=store_get(buffer_capacity); + addr->reply->subject=parse_quote_2047(subject.character, subject.length, US"utf-8", buffer, buffer_capacity); + addr->reply->oncelog=once; + addr->reply->once_repeat=days*86400; + + /* build body and MIME headers */ + + if (reason_is_mime) + { + uschar *mime_body,*reason_end; +#ifdef RFC_EOL + static const uschar nlnl[]="\r\n\r\n"; +#else + static const uschar nlnl[]="\n\n"; +#endif + + for + ( + mime_body=reason.character,reason_end=reason.character+reason.length; + mime_body<(reason_end-sizeof(nlnl)-1) && memcmp(mime_body,nlnl,sizeof(nlnl)-1); + ++mime_body + ); + capacity = 0; + start = 0; + addr->reply->headers = string_cat(NULL,&capacity,&start,reason.character,mime_body-reason.character); + capacity = 0; + start = 0; + if (mime_bodyreply->text = string_cat(NULL,&capacity,&start,mime_body,reason_end-mime_body); + } + else + { + struct String qp; + + capacity = 0; + start = reason.length; + addr->reply->headers = US"MIME-Version: 1.0\n" + "Content-Type: text/plain;\n" + "\tcharset=\"utf-8\"\n" + "Content-Transfer-Encoding: quoted-printable"; + addr->reply->text = quoted_printable_encode(&reason,&qp)->character; + } + } + } + } +#endif + else break; + } +return 1; +} + + +/************************************************* +* Parse and interpret a sieve filter * +*************************************************/ + +/* +Arguments: + filter points to the Sieve filter including its state + exec Execute parsed statements + generated where to hang newly-generated addresses + +Returns: 1 success + -1 syntax or execution error +*/ + +static int parse_start(struct Sieve *filter, int exec, + address_item **generated) +{ +filter->pc=filter->filter; +filter->line=1; +filter->keep=1; +filter->require_envelope=0; +filter->require_fileinto=0; +#ifdef SUBADDRESS +filter->require_subaddress=0; +#endif +#ifdef VACATION +filter->require_vacation=0; +filter->vacation_ran=0; +#endif +filter->require_copy=0; +filter->require_iascii_numeric=0; + +if (parse_white(filter)==-1) return -1; + +if (exec && filter->vacation_directory != NULL) /* 2nd test added by PH */ + { + DIR *oncelogdir; + struct dirent *oncelog; + struct stat properties; + time_t now; + + /* clean up old vacation log databases */ + + oncelogdir=opendir(CS filter->vacation_directory); + + if (oncelogdir ==(DIR*)0 && errno != ENOENT) + { + filter->errmsg=CUS "unable to open vacation directory"; + return -1; + } + + if (oncelogdir != NULL) + { + time(&now); + + while ((oncelog=readdir(oncelogdir))!=(struct dirent*)0) + { + if (strlen(oncelog->d_name)==32) + { + uschar *s=string_sprintf("%s/%s",filter->vacation_directory,oncelog->d_name); + if (Ustat(s,&properties)==0 && (properties.st_mtime+VACATION_MAX_DAYS*86400) + */ + + struct String *cap,*check; + int m; + + if (parse_white(filter)==-1) return -1; + if ((m=parse_stringlist(filter,&cap))!=1) + { + if (m==0) filter->errmsg=CUS "capability string list expected"; + return -1; + } + for (check=cap; check->character; ++check) + { + if (eq_asciicase(check,&str_envelope,0)) filter->require_envelope=1; + else if (eq_asciicase(check,&str_fileinto,0)) filter->require_fileinto=1; +#ifdef SUBADDRESS + else if (eq_asciicase(check,&str_subaddress,0)) filter->require_subaddress=1; +#endif +#ifdef VACATION + else if (eq_asciicase(check,&str_vacation,0)) + { + if (filter->vacation_directory == NULL) + { + filter->errmsg=CUS "vacation disabled"; + return -1; + } + filter->require_vacation=1; + } +#endif + else if (eq_asciicase(check,&str_copy,0)) filter->require_copy=1; + else if (eq_asciicase(check,&str_comparator_ioctet,0)) ; + else if (eq_asciicase(check,&str_comparator_iascii_casemap,0)) ; + else if (eq_asciicase(check,&str_comparator_iascii_numeric,0)) filter->require_iascii_numeric=1; + else + { + filter->errmsg=CUS "unknown capability"; + return -1; + } + } + if (parse_semicolon(filter)==-1) return -1; + } + if (parse_commands(filter,exec,generated)==-1) return -1; + if (*filter->pc) + { + filter->errmsg=CUS "syntax error"; + return -1; + } + return 1; +} + + +/************************************************* +* Interpret a sieve filter file * +*************************************************/ + +/* +Arguments: + filter points to the entire file, read into store as a single string + options controls whether various special things are allowed, and requests + special actions (not currently used) + sieve_vacation_directory where to store vacation "once" files + generated where to hang newly-generated addresses + error where to pass back an error text + +Returns: FF_DELIVERED success, a significant action was taken + FF_NOTDELIVERED success, no significant action + FF_DEFER defer requested + FF_FAIL fail requested + FF_FREEZE freeze requested + FF_ERROR there was a problem +*/ + +int +sieve_interpret(uschar *filter, int options, uschar *vacation_directory, + address_item **generated, uschar **error) +{ +struct Sieve sieve; +int r; +uschar *msg; + +options = options; /* Keep picky compilers happy */ +error = error; + +DEBUG(D_route) debug_printf("Sieve: start of processing\n"); +sieve.filter=filter; + +if (vacation_directory == NULL) + sieve.vacation_directory = NULL; +else + { + sieve.vacation_directory=expand_string(vacation_directory); + if (sieve.vacation_directory == NULL) + { + *error = string_sprintf("failed to expand \"%s\" " + "(sieve_vacation_directory): %s", vacation_directory, + expand_string_message); + return FF_ERROR; + } + } + +#ifdef COMPILE_SYNTAX_CHECKER +if (parse_start(&sieve,0,generated)==1) +#else +if (parse_start(&sieve,1,generated)==1) +#endif + { + if (sieve.keep) + { + add_addr(generated,US"inbox",1,0,0,0); + msg = string_sprintf("Keep"); + r = FF_DELIVERED; + } + else + { + msg = string_sprintf("No keep"); + r = FF_DELIVERED; + } + } +else + { + msg = string_sprintf("Sieve error: %s in line %d",sieve.errmsg,sieve.line); +#ifdef COMPILE_SYNTAX_CHECKER + r = FF_ERROR; + *error = msg; +#else + add_addr(generated,US"inbox",1,0,0,0); + r = FF_DELIVERED; +#endif + } + +#ifndef COMPILE_SYNTAX_CHECKER +if (filter_test != NULL) printf("%s\n", (const char*) msg); + else debug_printf("%s\n", msg); +#endif + +DEBUG(D_route) debug_printf("Sieve: end of processing\n"); +return r; +} diff --git a/src/src/smtp_in.c b/src/src/smtp_in.c new file mode 100644 index 000000000..8bc12debf --- /dev/null +++ b/src/src/smtp_in.c @@ -0,0 +1,3547 @@ +/* $Cambridge: exim/src/src/smtp_in.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Functions for handling an incoming SMTP call. */ + + +#include "exim.h" + + +/* Initialize for TCP wrappers if so configured. It appears that the macro +HAVE_IPV6 is used in some versions of the tcpd.h header, so we unset it before +including that header, and restore its value afterwards. */ + +#ifdef USE_TCP_WRAPPERS + + #if HAVE_IPV6 + #define EXIM_HAVE_IPV6 + #endif + #undef HAVE_IPV6 + #include + #undef HAVE_IPV6 + #ifdef EXIM_HAVE_IPV6 + #define HAVE_IPV6 TRUE + #endif + +int allow_severity = LOG_INFO; +int deny_severity = LOG_NOTICE; +#endif + + +/* Size of buffer for reading SMTP commands */ + +#define cmd_buffer_size 512 /* Ref. RFC 821 */ + +/* Size of buffer for reading SMTP incoming packets */ + +#define in_buffer_size 8192 + +/* Structure for SMTP command list */ + +typedef struct { + char *name; + int len; + short int cmd; + short int has_arg; + short int is_mail_cmd; +} smtp_cmd_list; + +/* Codes for identifying commands. We order them so that those that come first +are those for which synchronization is always required. Checking this can help +block some spam. */ + +enum { + /* These commands are required to be synchronized, i.e. to be the last in a + block of commands when pipelining. */ + + HELO_CMD, EHLO_CMD, DATA_CMD, /* These are listed in the pipelining */ + VRFY_CMD, EXPN_CMD, NOOP_CMD, /* RFC as requiring synchronization */ + ETRN_CMD, /* This by analogy with TURN from the RFC */ + STARTTLS_CMD, /* Required by the STARTTLS RFC */ + + /* This is a dummy to identify the non-sync commands when pipelining */ + + NON_SYNC_CMD_PIPELINING, + + /* These commands need not be synchronized when pipelining */ + + MAIL_CMD, RCPT_CMD, RSET_CMD, + + /* This is a dummy to identify the non-sync commands when not pipelining */ + + NON_SYNC_CMD_NON_PIPELINING, + + /* I have been unable to find a statement about the use of pipelining + with AUTH, so to be on the safe side it is here, though I kind of feel + it should be up there with the synchronized commands. */ + + AUTH_CMD, + + /* I'm not sure about these, but I don't think they matter. */ + + QUIT_CMD, HELP_CMD, + + /* These are specials that don't correspond to actual commands */ + + EOF_CMD, OTHER_CMD, BADARG_CMD, BADCHAR_CMD, BADSYN_CMD, + TOO_MANY_NONMAIL_CMD }; + + + +/************************************************* +* Local static variables * +*************************************************/ + +static auth_instance *authenticated_by; +static BOOL auth_advertised; +#ifdef SUPPORT_TLS +static BOOL tls_advertised; +#endif +static BOOL esmtp; +static BOOL helo_required = FALSE; +static BOOL helo_verify = FALSE; +static BOOL helo_seen; +static BOOL helo_accept_junk; +static BOOL count_nonmail; +static BOOL pipelining_advertised; +static int nonmail_command_count; +static int synprot_error_count; +static int unknown_command_count; +static int sync_cmd_limit; +static int smtp_write_error = 0; + +static uschar *smtp_data; + +static uschar *cmd_buffer; + +/* We need to know the position of RSET, HELO, EHLO, AUTH, and STARTTLS. Their +final fields of all except AUTH are forced TRUE at the start of a new message +setup, to allow one of each between messages that is not counted as a nonmail +command. (In fact, only one of HELO/EHLO is not counted.) Also, we have to +allow a new EHLO after starting up TLS. + +AUTH is "falsely" labelled as a mail command initially, so that it doesn't get +counted. However, the flag is changed when AUTH is received, so that multiple +failing AUTHs will eventually hit the limit. After a successful AUTH, another +AUTH is already forbidden. After a TLS session is started, AUTH's flag is again +forced TRUE, to allow for the re-authentication that can happen at that point. + +QUIT is also "falsely" labelled as a mail command so that it doesn't up the +count of non-mail commands and possibly provoke an error. */ + +static smtp_cmd_list cmd_list[] = { + { "rset", sizeof("rset")-1, RSET_CMD, FALSE, FALSE }, /* First */ + { "helo", sizeof("helo")-1, HELO_CMD, TRUE, FALSE }, + { "ehlo", sizeof("ehlo")-1, EHLO_CMD, TRUE, FALSE }, + { "auth", sizeof("auth")-1, AUTH_CMD, TRUE, TRUE }, + #ifdef SUPPORT_TLS + { "starttls", sizeof("starttls")-1, STARTTLS_CMD, FALSE, FALSE }, + #endif + +/* If you change anything above here, also fix the definitions below. */ + + { "mail from:", sizeof("mail from:")-1, MAIL_CMD, TRUE, TRUE }, + { "rcpt to:", sizeof("rcpt to:")-1, RCPT_CMD, TRUE, TRUE }, + { "data", sizeof("data")-1, DATA_CMD, FALSE, TRUE }, + { "quit", sizeof("quit")-1, QUIT_CMD, FALSE, TRUE }, + { "noop", sizeof("noop")-1, NOOP_CMD, TRUE, FALSE }, + { "etrn", sizeof("etrn")-1, ETRN_CMD, TRUE, FALSE }, + { "vrfy", sizeof("vrfy")-1, VRFY_CMD, TRUE, FALSE }, + { "expn", sizeof("expn")-1, EXPN_CMD, TRUE, FALSE }, + { "help", sizeof("help")-1, HELP_CMD, TRUE, FALSE } +}; + +static smtp_cmd_list *cmd_list_end = + cmd_list + sizeof(cmd_list)/sizeof(smtp_cmd_list); + +#define CMD_LIST_RSET 0 +#define CMD_LIST_HELO 1 +#define CMD_LIST_EHLO 2 +#define CMD_LIST_AUTH 3 +#define CMD_LIST_STARTTLS 4 + +static uschar *protocols[] = { + US"local-smtp", + US"local-esmtp", + US"local-esmtpa", + US"local-esmtps", + US"local-esmtpsa" + }; + +#define pnormal 0 +#define pextend 1 +#define pauthed 1 /* added to pextend */ +#define pcrpted 2 /* added to pextend */ +#define pnlocal 6 /* offset to remove "local" */ + +/* When reading SMTP from a remote host, we have to use our own versions of the +C input-reading functions, in order to be able to flush the SMTP output only +when about to read more data from the socket. This is the only way to get +optimal performance when the client is using pipelining. Flushing for every +command causes a separate packet and reply packet each time; saving all the +responses up (when pipelining) combines them into one packet and one response. + +For simplicity, these functions are used for *all* SMTP input, not only when +receiving over a socket. However, after setting up a secure socket (SSL), input +is read via the OpenSSL library, and another set of functions is used instead +(see tls.c). + +These functions are set in the receive_getc etc. variables and called with the +same interface as the C functions. However, since there can only ever be +one incoming SMTP call, we just use a single buffer and flags. There is no need +to implement a complicated private FILE-like structure.*/ + +static uschar *smtp_inbuffer; +static uschar *smtp_inptr; +static uschar *smtp_inend; +static int smtp_had_eof; +static int smtp_had_error; + + +/************************************************* +* SMTP version of getc() * +*************************************************/ + +/* This gets the next byte from the SMTP input buffer. If the buffer is empty, +it flushes the output, and refills the buffer, with a timeout. The signal +handler is set appropriately by the calling function. This function is not used +after a connection has negotated itself into an TLS/SSL state. + +Arguments: none +Returns: the next character or EOF +*/ + +int +smtp_getc(void) +{ +if (smtp_inptr >= smtp_inend) + { + int rc, save_errno; + fflush(smtp_out); + if (smtp_receive_timeout > 0) alarm(smtp_receive_timeout); + rc = read(fileno(smtp_in), smtp_inbuffer, in_buffer_size); + save_errno = errno; + alarm(0); + if (rc <= 0) + { + /* Must put the error text in fixed store, because this might be during + header reading, where it releases unused store above the header. */ + if (rc < 0) + { + smtp_had_error = save_errno; + smtp_read_error = string_copy_malloc( + string_sprintf(" (error: %s)", strerror(save_errno))); + } + else smtp_had_eof = 1; + return EOF; + } + smtp_inend = smtp_inbuffer + rc; + smtp_inptr = smtp_inbuffer; + } +return *smtp_inptr++; +} + + + +/************************************************* +* SMTP version of ungetc() * +*************************************************/ + +/* Puts a character back in the input buffer. Only ever +called once. + +Arguments: + ch the character + +Returns: the character +*/ + +int +smtp_ungetc(int ch) +{ +*(--smtp_inptr) = ch; +return ch; +} + + + + +/************************************************* +* SMTP version of feof() * +*************************************************/ + +/* Tests for a previous EOF + +Arguments: none +Returns: non-zero if the eof flag is set +*/ + +int +smtp_feof(void) +{ +return smtp_had_eof; +} + + + + +/************************************************* +* SMTP version of ferror() * +*************************************************/ + +/* Tests for a previous read error, and returns with errno +restored to what it was when the error was detected. + +Arguments: none +Returns: non-zero if the error flag is set +*/ + +int +smtp_ferror(void) +{ +errno = smtp_had_error; +return smtp_had_error; +} + + + + +/************************************************* +* Write formatted string to SMTP channel * +*************************************************/ + +/* This is a separate function so that we don't have to repeat everything for +TLS support or debugging. It is global so that the daemon and the +authentication functions can use it. It does not return any error indication, +because major problems such as dropped connections won't show up till an output +flush for non-TLS connections. The smtp_fflush() function is available for +checking that: for convenience, TLS output errors are remembered here so that +they are also picked up later by smtp_fflush(). + +Arguments: + format format string + ... optional arguments + +Returns: nothing +*/ + +void +smtp_printf(char *format, ...) +{ +va_list ap; + +DEBUG(D_receive) + { + va_start(ap, format); + (void) string_vformat(big_buffer, big_buffer_size, format, ap); + debug_printf("SMTP>> %s", big_buffer); + } + +va_start(ap, format); + +/* If in a TLS session we have to format the string, and then write it using a +TLS function. */ + +#ifdef SUPPORT_TLS +if (tls_active >= 0) + { + if (!string_vformat(big_buffer, big_buffer_size, format, ap)) + { + log_write(0, LOG_MAIN|LOG_PANIC, "string too large in smtp_printf"); + smtp_closedown(US"Unexpected error"); + exim_exit(EXIT_FAILURE); + } + if (tls_write(big_buffer, Ustrlen(big_buffer)) < 0) smtp_write_error = -1; + } +else +#endif + +/* Otherwise, just use the standard library function. */ + +if (vfprintf(smtp_out, format, ap) < 0) smtp_write_error = -1; +va_end(ap); +} + + + +/************************************************* +* Flush SMTP out and check for error * +*************************************************/ + +/* This function isn't currently used within Exim (it detects errors when it +tries to read the next SMTP input), but is available for use in local_scan(). +For non-TLS connections, it flushes the output and checks for errors. For +TLS-connections, it checks for a previously-detected TLS write error. + +Arguments: none +Returns: 0 for no error; -1 after an error +*/ + +int +smtp_fflush(void) +{ +if (tls_active < 0 && fflush(smtp_out) != 0) smtp_write_error = -1; +return smtp_write_error; +} + + + +/************************************************* +* SMTP command read timeout * +*************************************************/ + +/* Signal handler for timing out incoming SMTP commands. This attempts to +finish off tidily. + +Argument: signal number (SIGALRM) +Returns: nothing +*/ + +static void +command_timeout_handler(int sig) +{ +sig = sig; /* Keep picky compilers happy */ +log_write(L_lost_incoming_connection, + LOG_MAIN, "SMTP command timeout on%s connection from %s", + (tls_active >= 0)? " TLS" : "", + host_and_ident(FALSE)); +if (smtp_batched_input) + moan_smtp_batch(NULL, "421 SMTP command timeout"); /* Does not return */ +smtp_printf("421 %s: SMTP command timeout - closing connection\r\n", + smtp_active_hostname); +mac_smtp_fflush(); +exim_exit(EXIT_FAILURE); +} + + + +/************************************************* +* SIGTERM received * +*************************************************/ + +/* Signal handler for handling SIGTERM. Again, try to finish tidily. + +Argument: signal number (SIGTERM) +Returns: nothing +*/ + +static void +command_sigterm_handler(int sig) +{ +sig = sig; /* Keep picky compilers happy */ +log_write(0, LOG_MAIN, "%s closed after SIGTERM", smtp_get_connection_info()); +if (smtp_batched_input) + moan_smtp_batch(NULL, "421 SIGTERM received"); /* Does not return */ +smtp_printf("421 %s: Service not available - closing connection\r\n", + smtp_active_hostname); +exim_exit(EXIT_FAILURE); +} + + + +/************************************************* +* Read one command line * +*************************************************/ + +/* Strictly, SMTP commands coming over the net are supposed to end with CRLF. +There are sites that don't do this, and in any case internal SMTP probably +should check only for LF. Consequently, we check here for LF only. The line +ends up with [CR]LF removed from its end. If we get an overlong line, treat as +an unknown command. The command is read into the static cmd_buffer. + +The character reading routine sets up a timeout for each block actually read +from the input (which may contain more than one command). We set up a special +signal handler that closes down the session on a timeout. Control does not +return when it runs. + +Arguments: + check_sync if TRUE, check synchronization rules if global option is TRUE + +Returns: a code identifying the command (enumerated above) +*/ + +static int +smtp_read_command(BOOL check_sync) +{ +int c; +int ptr = 0; +smtp_cmd_list *p; +BOOL hadnull = FALSE; + +os_non_restarting_signal(SIGALRM, command_timeout_handler); + +while ((c = (receive_getc)()) != '\n' && c != EOF) + { + if (ptr >= cmd_buffer_size) + { + os_non_restarting_signal(SIGALRM, sigalrm_handler); + return OTHER_CMD; + } + if (c == 0) + { + hadnull = TRUE; + c = '?'; + } + cmd_buffer[ptr++] = c; + } + +receive_linecount++; /* For BSMTP errors */ +os_non_restarting_signal(SIGALRM, sigalrm_handler); + +/* If hit end of file, return pseudo EOF command. Whether we have a +part-line already read doesn't matter, since this is an error state. */ + +if (c == EOF) return EOF_CMD; + +/* Remove any CR and white space at the end of the line, and terminate the +string. */ + +while (ptr > 0 && isspace(cmd_buffer[ptr-1])) ptr--; +cmd_buffer[ptr] = 0; + +DEBUG(D_receive) debug_printf("SMTP<< %s\n", cmd_buffer); + +/* NULLs are not allowed in SMTP commands */ + +if (hadnull) return BADCHAR_CMD; + +/* Scan command list and return identity, having set the data pointer +to the start of the actual data characters. Check for SMTP synchronization +if required. */ + +for (p = cmd_list; p < cmd_list_end; p++) + { + if (strncmpic(cmd_buffer, US p->name, p->len) == 0) + { + if (smtp_inptr < smtp_inend && /* Outstanding input */ + p->cmd < sync_cmd_limit && /* Command should sync */ + check_sync && /* Local flag set */ + smtp_enforce_sync && /* Global flag set */ + sender_host_address != NULL && /* Not local input */ + !sender_host_notsocket) /* Really is a socket */ + return BADSYN_CMD; + + /* Point after the command, but don't skip over leading spaces till after + the following test, so that if it fails, the command name can easily be + logged. */ + + smtp_data = cmd_buffer + p->len; + + /* Count non-mail commands from those hosts that are controlled in this + way. The default is all hosts. We don't waste effort checking the list + until we get a non-mail command, but then cache the result to save checking + again. If there's a DEFER while checking the host, assume it's in the list. + + Note that one instance of RSET, EHLO/HELO, and STARTTLS is allowed at the + start of each incoming message by fiddling with the value in the table. */ + + if (!p->is_mail_cmd) + { + if (count_nonmail == TRUE_UNSET) count_nonmail = + verify_check_host(&smtp_accept_max_nonmail_hosts) != FAIL; + if (count_nonmail && ++nonmail_command_count > smtp_accept_max_nonmail) + return TOO_MANY_NONMAIL_CMD; + } + + /* Get the data pointer over leading spaces and return; if there is no data + for a command that expects it, we give the error centrally here. */ + + while (isspace(*smtp_data)) smtp_data++; + return (p->has_arg || *smtp_data == 0)? p->cmd : BADARG_CMD; + } + } + +/* Enforce synchronization for unknown commands */ + +if (smtp_inptr < smtp_inend && /* Outstanding input */ + check_sync && /* Local flag set */ + smtp_enforce_sync && /* Global flag set */ + sender_host_address != NULL && /* Not local input */ + !sender_host_notsocket) /* Really is a socket */ + return BADSYN_CMD; + +return OTHER_CMD; +} + + + +/************************************************* +* Forced closedown of call * +*************************************************/ + +/* This function is called from log.c when Exim is dying because of a serious +disaster, and also from some other places. If an incoming non-batched SMTP +channel is open, it swallows the rest of the incoming message if in the DATA +phase, sends the reply string, and gives an error to all subsequent commands +except QUIT. The existence of an SMTP call is detected by the non-NULLness of +smtp_in. + +Argument: SMTP reply string to send, excluding the code +Returns: nothing +*/ + +void +smtp_closedown(uschar *message) +{ +if (smtp_in == NULL || smtp_batched_input) return; +receive_swallow_smtp(); +smtp_printf("421 %s\r\n", message); + +for (;;) + { + switch(smtp_read_command(FALSE)) + { + case EOF_CMD: + return; + + case QUIT_CMD: + smtp_printf("221 %s closing connection\r\n", smtp_active_hostname); + mac_smtp_fflush(); + return; + + case RSET_CMD: + smtp_printf("250 Reset OK\r\n"); + break; + + default: + smtp_printf("421 %s\r\n", message); + break; + } + } +} + + + + +/************************************************* +* Set up connection info for logging * +*************************************************/ + +/* This function is called when logging information about an SMTP connection. +It sets up appropriate source information, depending on the type of connection. + +Argument: none +Returns: a string describing the connection +*/ + +uschar * +smtp_get_connection_info(void) +{ +if (host_checking) + return string_sprintf("SMTP connection from %s", sender_fullhost); + +if (sender_host_unknown || sender_host_notsocket) + return string_sprintf("SMTP connection from %s", sender_ident); + +if (is_inetd) + return string_sprintf("SMTP connection from %s (via inetd)", sender_fullhost); + +if ((log_extra_selector & LX_incoming_interface) != 0 && + interface_address != NULL) + return string_sprintf("SMTP connection from %s I=[%s]:%d", sender_fullhost, + interface_address, interface_port); + +return string_sprintf("SMTP connection from %s", sender_fullhost); +} + + + +/************************************************* +* Check HELO line and set sender_helo_name * +*************************************************/ + +/* Check the format of a HELO line. The data for HELO/EHLO is supposed to be +the domain name of the sending host, or an ip literal in square brackets. The +arrgument is placed in sender_helo_name, which is in malloc store, because it +must persist over multiple incoming messages. If helo_accept_junk is set, this +host is permitted to send any old junk (needed for some broken hosts). +Otherwise, helo_allow_chars can be used for rogue characters in general +(typically people want to let in underscores). + +Argument: + s the data portion of the line (already past any white space) + +Returns: TRUE or FALSE +*/ + +static BOOL +check_helo(uschar *s) +{ +uschar *start = s; +uschar *end = s + Ustrlen(s); +BOOL yield = helo_accept_junk; + +/* Discard any previous helo name */ + +if (sender_helo_name != NULL) + { + store_free(sender_helo_name); + sender_helo_name = NULL; + } + +/* Skip tests if junk is permitted. */ + +if (!yield) + { + /* Allow the new standard form for IPv6 address literals, namely, + [IPv6:....], and because someone is bound to use it, allow an equivalent + IPv4 form. Allow plain addresses as well. */ + + if (*s == '[') + { + if (end[-1] == ']') + { + end[-1] = 0; + if (strncmpic(s, US"[IPv6:", 6) == 0) + yield = (string_is_ip_address(s+6, NULL) == 6); + else if (strncmpic(s, US"[IPv4:", 6) == 0) + yield = (string_is_ip_address(s+6, NULL) == 4); + else + yield = (string_is_ip_address(s+1, NULL) != 0); + end[-1] = ']'; + } + } + + /* Non-literals must be alpha, dot, hyphen, plus any non-valid chars + that have been configured (usually underscore - sigh). */ + + else if (*s != 0) + { + yield = TRUE; + while (*s != 0) + { + if (!isalnum(*s) && *s != '.' && *s != '-' && + Ustrchr(helo_allow_chars, *s) == NULL) + { + yield = FALSE; + break; + } + s++; + } + } + } + +/* Save argument if OK */ + +if (yield) sender_helo_name = string_copy_malloc(start); +return yield; +} + + + + + +/************************************************* +* Extract SMTP command option * +*************************************************/ + +/* This function picks the next option setting off the end of smtp_data. It +is called for MAIL FROM and RCPT TO commands, to pick off the optional ESMTP +things that can appear there. + +Arguments: + name point this at the name + value point this at the data string + +Returns: TRUE if found an option +*/ + +static BOOL +extract_option(uschar **name, uschar **value) +{ +uschar *n; +uschar *v = smtp_data + Ustrlen(smtp_data) -1; +while (isspace(*v)) v--; +v[1] = 0; + +while (v > smtp_data && *v != '=' && !isspace(*v)) v--; +if (*v != '=') return FALSE; + +n = v; +while(isalpha(n[-1])) n--; + +if (n[-1] != ' ') return FALSE; + +n[-1] = 0; +*name = n; +*v++ = 0; +*value = v; +return TRUE; +} + + + + + + + +/************************************************* +* Reset for new message * +*************************************************/ + +/* This function is called whenever the SMTP session is reset from +within either of the setup functions. + +Argument: the stacking pool storage reset point +Returns: nothing +*/ + +static void +smtp_reset(void *reset_point) +{ +int i; +store_reset(reset_point); +recipients_list = NULL; +rcpt_count = rcpt_defer_count = rcpt_fail_count = + raw_recipients_count = recipients_count = recipients_list_max = 0; +message_size = -1; +acl_warn_headers = NULL; +queue_only_policy = FALSE; +deliver_freeze = FALSE; /* Can be set by ACL */ +submission_mode = FALSE; /* Can be set by ACL */ +sender_address = NULL; +raw_sender = NULL; /* After SMTP rewrite, before qualifying */ +sender_address_unrewritten = NULL; /* Set only after verify rewrite */ +sender_verified_list = NULL; /* No senders verified */ +memset(sender_address_cache, 0, sizeof(sender_address_cache)); +memset(sender_domain_cache, 0, sizeof(sender_domain_cache)); +authenticated_sender = NULL; +body_linecount = body_zerocount = 0; + +for (i = 0; i < ACL_M_MAX; i++) acl_var[ACL_C_MAX + i] = NULL; + +/* The message body variables use malloc store. They may be set if this is +not the first message in an SMTP session and the previous message caused them +to be referenced in an ACL. */ + +if (message_body != NULL) + { + store_free(message_body); + message_body = NULL; + } + +if (message_body_end != NULL) + { + store_free(message_body_end); + message_body_end = NULL; + } + +/* Warning log messages are also saved in malloc store. They are saved to avoid +repetition in the same message, but it seems right to repeat them for different +messagess. */ + +while (acl_warn_logged != NULL) + { + string_item *this = acl_warn_logged; + acl_warn_logged = acl_warn_logged->next; + store_free(this); + } +} + + + + + +/************************************************* +* Initialize for incoming batched SMTP message * +*************************************************/ + +/* This function is called from smtp_setup_msg() in the case when +smtp_batched_input is true. This happens when -bS is used to pass a whole batch +of messages in one file with SMTP commands between them. All errors must be +reported by sending a message, and only MAIL FROM, RCPT TO, and DATA are +relevant. After an error on a sender, or an invalid recipient, the remainder +of the message is skipped. The value of received_protocol is already set. + +Argument: none +Returns: > 0 message successfully started (reached DATA) + = 0 QUIT read or end of file reached + < 0 should not occur +*/ + +static int +smtp_setup_batch_msg(void) +{ +int done = 0; +void *reset_point = store_get(0); + +/* Save the line count at the start of each transaction - single commands +like HELO and RSET count as whole transactions. */ + +bsmtp_transaction_linecount = receive_linecount; + +if ((receive_feof)()) return 0; /* Treat EOF as QUIT */ + +smtp_reset(reset_point); /* Reset for start of message */ + +/* Deal with SMTP commands. This loop is exited by setting done to a POSITIVE +value. The values are 2 larger than the required yield of the function. */ + +while (done <= 0) + { + uschar *errmess; + uschar *recipient = NULL; + int start, end, sender_domain, recipient_domain; + + switch(smtp_read_command(FALSE)) + { + /* The HELO/EHLO commands set sender_address_helo if they have + valid data; otherwise they are ignored, except that they do + a reset of the state. */ + + case HELO_CMD: + case EHLO_CMD: + + check_helo(smtp_data); + /* Fall through */ + + case RSET_CMD: + smtp_reset(reset_point); + bsmtp_transaction_linecount = receive_linecount; + break; + + + /* The MAIL FROM command requires an address as an operand. All we + do here is to parse it for syntactic correctness. The form "<>" is + a special case which converts into an empty string. The start/end + pointers in the original are not used further for this address, as + it is the canonical extracted address which is all that is kept. */ + + case MAIL_CMD: + if (sender_address != NULL) + /* The function moan_smtp_batch() does not return. */ + moan_smtp_batch(cmd_buffer, "503 Sender already given"); + + if (smtp_data[0] == 0) + /* The function moan_smtp_batch() does not return. */ + moan_smtp_batch(cmd_buffer, "501 MAIL FROM must have an address operand"); + + /* Reset to start of message */ + + smtp_reset(reset_point); + + /* Apply SMTP rewrite */ + + raw_sender = ((rewrite_existflags & rewrite_smtp) != 0)? + rewrite_one(smtp_data, rewrite_smtp|rewrite_smtp_sender, NULL, FALSE, + US"", global_rewrite_rules) : smtp_data; + + /* Extract the address; the TRUE flag allows <> as valid */ + + raw_sender = + parse_extract_address(raw_sender, &errmess, &start, &end, &sender_domain, + TRUE); + + if (raw_sender == NULL) + /* The function moan_smtp_batch() does not return. */ + moan_smtp_batch(cmd_buffer, "501 %s", errmess); + + sender_address = string_copy(raw_sender); + + /* Qualify unqualified sender addresses if permitted to do so. */ + + if (sender_domain == 0 && sender_address[0] != 0 && sender_address[0] != '@') + { + if (allow_unqualified_sender) + { + sender_address = rewrite_address_qualify(sender_address, FALSE); + DEBUG(D_receive) debug_printf("unqualified address %s accepted " + "and rewritten\n", raw_sender); + } + /* The function moan_smtp_batch() does not return. */ + else moan_smtp_batch(cmd_buffer, "501 sender address must contain " + "a domain"); + } + break; + + + /* The RCPT TO command requires an address as an operand. All we do + here is to parse it for syntactic correctness. There may be any number + of RCPT TO commands, specifying multiple senders. We build them all into + a data structure that is in argc/argv format. The start/end values + given by parse_extract_address are not used, as we keep only the + extracted address. */ + + case RCPT_CMD: + if (sender_address == NULL) + /* The function moan_smtp_batch() does not return. */ + moan_smtp_batch(cmd_buffer, "503 No sender yet given"); + + if (smtp_data[0] == 0) + /* The function moan_smtp_batch() does not return. */ + moan_smtp_batch(cmd_buffer, "501 RCPT TO must have an address operand"); + + /* Check maximum number allowed */ + + if (recipients_max > 0 && recipients_count + 1 > recipients_max) + /* The function moan_smtp_batch() does not return. */ + moan_smtp_batch(cmd_buffer, "%s too many recipients", + recipients_max_reject? "552": "452"); + + /* Apply SMTP rewrite, then extract address. Don't allow "<>" as a + recipient address */ + + recipient = ((rewrite_existflags & rewrite_smtp) != 0)? + rewrite_one(smtp_data, rewrite_smtp, NULL, FALSE, US"", + global_rewrite_rules) : smtp_data; + + /* rfc821_domains = TRUE; << no longer needed */ + recipient = parse_extract_address(recipient, &errmess, &start, &end, + &recipient_domain, FALSE); + /* rfc821_domains = FALSE; << no longer needed */ + + if (recipient == NULL) + /* The function moan_smtp_batch() does not return. */ + moan_smtp_batch(cmd_buffer, "501 %s", errmess); + + /* If the recipient address is unqualified, qualify it if permitted. Then + add it to the list of recipients. */ + + if (recipient_domain == 0) + { + if (allow_unqualified_recipient) + { + DEBUG(D_receive) debug_printf("unqualified address %s accepted\n", + recipient); + recipient = rewrite_address_qualify(recipient, TRUE); + } + /* The function moan_smtp_batch() does not return. */ + else moan_smtp_batch(cmd_buffer, "501 recipient address must contain " + "a domain"); + } + receive_add_recipient(recipient, -1); + break; + + + /* The DATA command is legal only if it follows successful MAIL FROM + and RCPT TO commands. This function is complete when a valid DATA + command is encountered. */ + + case DATA_CMD: + if (sender_address == NULL || recipients_count <= 0) + { + /* The function moan_smtp_batch() does not return. */ + if (sender_address == NULL) + moan_smtp_batch(cmd_buffer, + "503 MAIL FROM: command must precede DATA"); + else + moan_smtp_batch(cmd_buffer, + "503 RCPT TO: must precede DATA"); + } + else + { + done = 3; /* DATA successfully achieved */ + message_ended = END_NOTENDED; /* Indicate in middle of message */ + } + break; + + + /* The VRFY, EXPN, HELP, ETRN, and NOOP commands are ignored. */ + + case VRFY_CMD: + case EXPN_CMD: + case HELP_CMD: + case NOOP_CMD: + case ETRN_CMD: + bsmtp_transaction_linecount = receive_linecount; + break; + + + case EOF_CMD: + case QUIT_CMD: + done = 2; + break; + + + case BADARG_CMD: + /* The function moan_smtp_batch() does not return. */ + moan_smtp_batch(cmd_buffer, "501 Unexpected argument data"); + break; + + + case BADCHAR_CMD: + /* The function moan_smtp_batch() does not return. */ + moan_smtp_batch(cmd_buffer, "501 Unexpected NULL in SMTP command"); + break; + + + default: + /* The function moan_smtp_batch() does not return. */ + moan_smtp_batch(cmd_buffer, "500 Command unrecognized"); + break; + } + } + +return done - 2; /* Convert yield values */ +} + + + + +/************************************************* +* Start an SMTP session * +*************************************************/ + +/* This function is called at the start of an SMTP session. Thereafter, +smtp_setup_msg() is called to initiate each separate message. This +function does host-specific testing, and outputs the banner line. + +Arguments: none +Returns: FALSE if the session can not continue; something has + gone wrong, or the connection to the host is blocked +*/ + +BOOL +smtp_start_session(void) +{ +int size = 256; +int i, ptr; +uschar *p, *s, *ss; + +helo_seen = esmtp = helo_accept_junk = FALSE; +count_nonmail = TRUE_UNSET; +synprot_error_count = unknown_command_count = nonmail_command_count = 0; +smtp_delay_mail = smtp_rlm_base; +auth_advertised = FALSE; +pipelining_advertised = FALSE; +sync_cmd_limit = NON_SYNC_CMD_NON_PIPELINING; + +memset(sender_host_cache, 0, sizeof(sender_host_cache)); + +sender_host_authenticated = NULL; +authenticated_by = NULL; + +#ifdef SUPPORT_TLS +tls_cipher = tls_peerdn = NULL; +tls_advertised = FALSE; +#endif + +/* Reset ACL connection variables */ + +for (i = 0; i < ACL_C_MAX; i++) acl_var[i] = NULL; + +cmd_buffer = (uschar *)malloc(cmd_buffer_size + 1); /* allow for trailing 0 */ +if (cmd_buffer == NULL) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, + "malloc() failed for SMTP command buffer"); + +/* For batched input, the protocol setting can be overridden from the +command line by a trusted caller. */ + +if (smtp_batched_input) + { + if (received_protocol == NULL) received_protocol = US"local-bsmtp"; + } + +/* For non-batched SMTP input, the protocol setting is forced here. It will be +reset later if any of EHLO/AUTH/STARTTLS are received. */ + +else + received_protocol = + protocols[pnormal] + ((sender_host_address != NULL)? pnlocal : 0); + +/* Set up the buffer for inputting using direct read() calls, and arrange to +call the local functions instead of the standard C ones. */ + +smtp_inbuffer = (uschar *)malloc(in_buffer_size); +if (smtp_inbuffer == NULL) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "malloc() failed for SMTP input buffer"); +receive_getc = smtp_getc; +receive_ungetc = smtp_ungetc; +receive_feof = smtp_feof; +receive_ferror = smtp_ferror; +smtp_inptr = smtp_inend = smtp_inbuffer; +smtp_had_eof = smtp_had_error = 0; + +/* Set up the message size limit; this may be host-specific */ + +thismessage_size_limit = expand_string_integer(message_size_limit); +if (thismessage_size_limit < 0) + { + if (thismessage_size_limit == -1) + log_write(0, LOG_MAIN|LOG_PANIC, "unable to expand message_size_limit: " + "%s", expand_string_message); + else + log_write(0, LOG_MAIN|LOG_PANIC, "invalid message_size_limit: " + "%s", expand_string_message); + smtp_closedown(US"Temporary local problem - please try later"); + return FALSE; + } + +/* When a message is input locally via the -bs or -bS options, sender_host_ +unknown is set unless -oMa was used to force an IP address, in which case it +is checked like a real remote connection. When -bs is used from inetd, this +flag is not set, causing the sending host to be checked. The code that deals +with IP source routing (if configured) is never required for -bs or -bS and +the flag sender_host_notsocket is used to suppress it. + +If smtp_accept_max and smtp_accept_reserve are set, keep some connections in +reserve for certain hosts and/or networks. */ + +if (!sender_host_unknown) + { + int rc; + BOOL reserved_host = FALSE; + + /* Look up IP options (source routing info) on the socket if this is not an + -oMa "host", and if any are found, log them and drop the connection. + + Linux (and others now, see below) is different to everyone else, so there + has to be some conditional compilation here. Versions of Linux before 2.1.15 + used a structure whose name was "options". Somebody finally realized that + this name was silly, and it got changed to "ip_options". I use the + newer name here, but there is a fudge in the script that sets up os.h + to define a macro in older Linux systems. + + Sigh. Linux is a fast-moving target. Another generation of Linux uses + glibc 2, which has chosen ip_opts for the structure name. This is now + really a glibc thing rather than a Linux thing, so the condition name + has been changed to reflect this. It is relevant also to GNU/Hurd. + + Mac OS 10.x (Darwin) is like the later glibc versions, but without the + setting of the __GLIBC__ macro, so we can't detect it automatically. There's + a special macro defined in the os.h file. + + Some DGUX versions on older hardware appear not to support IP options at + all, so there is now a general macro which can be set to cut out this + support altogether. + + How to do this properly in IPv6 is not yet known. */ + + #if !HAVE_IPV6 && !defined(NO_IP_OPTIONS) + + #ifdef GLIBC_IP_OPTIONS + #if (!defined __GLIBC__) || (__GLIBC__ < 2) + #define OPTSTYLE 1 + #else + #define OPTSTYLE 2 + #endif + #elif defined DARWIN_IP_OPTIONS + #define OPTSTYLE 2 + #else + #define OPTSTYLE 3 + #endif + + if (!host_checking && !sender_host_notsocket) + { + #if OPTSTYLE == 1 + SOCKLEN_T optlen = sizeof(struct ip_options) + MAX_IPOPTLEN; + struct ip_options *ipopt = store_get(optlen); + #elif OPTSTYLE == 2 + struct ip_opts ipoptblock; + struct ip_opts *ipopt = &ipoptblock; + SOCKLEN_T optlen = sizeof(ipoptblock); + #else + struct ipoption ipoptblock; + struct ipoption *ipopt = &ipoptblock; + SOCKLEN_T optlen = sizeof(ipoptblock); + #endif + + /* Occasional genuine failures of getsockopt() have been seen - for + example, "reset by peer". Therefore, just log and give up on this + call, unless the error is ENOPROTOOPT. This error is given by systems + that have the interfaces but not the mechanism - e.g. GNU/Hurd at the time + of writing. So for that error, carry on - we just can't do an IP options + check. */ + + DEBUG(D_receive) debug_printf("checking for IP options\n"); + + if (getsockopt(fileno(smtp_out), IPPROTO_IP, IP_OPTIONS, (uschar *)(ipopt), + &optlen) < 0) + { + if (errno != ENOPROTOOPT) + { + log_write(0, LOG_MAIN, "getsockopt() failed from %s: %s", + host_and_ident(FALSE), strerror(errno)); + smtp_printf("451 SMTP service not available\r\n"); + return FALSE; + } + } + + /* Deal with any IP options that are set. On the systems I have looked at, + the value of MAX_IPOPTLEN has been 40, meaning that there should never be + more logging data than will fit in big_buffer. Nevertheless, after somebody + questioned this code, I've added in some paranoid checking. */ + + else if (optlen > 0) + { + uschar *p = big_buffer; + uschar *pend = big_buffer + big_buffer_size; + uschar *opt, *adptr; + int optcount; + struct in_addr addr; + + #if OPTSTYLE == 1 + uschar *optstart = (uschar *)(ipopt->__data); + #elif OPTSTYLE == 2 + uschar *optstart = (uschar *)(ipopt->ip_opts); + #else + uschar *optstart = (uschar *)(ipopt->ipopt_list); + #endif + + DEBUG(D_receive) debug_printf("IP options exist\n"); + + Ustrcpy(p, "IP options on incoming call:"); + p += Ustrlen(p); + + for (opt = optstart; opt != NULL && + opt < (uschar *)(ipopt) + optlen;) + { + switch (*opt) + { + case IPOPT_EOL: + opt = NULL; + break; + + case IPOPT_NOP: + opt++; + break; + + case IPOPT_SSRR: + case IPOPT_LSRR: + if (!string_format(p, pend-p, " %s [@%s", + (*opt == IPOPT_SSRR)? "SSRR" : "LSRR", + #if OPTSTYLE == 1 + inet_ntoa(*((struct in_addr *)(&(ipopt->faddr)))))) + #elif OPTSTYLE == 2 + inet_ntoa(ipopt->ip_dst))) + #else + inet_ntoa(ipopt->ipopt_dst))) + #endif + { + opt = NULL; + break; + } + + p += Ustrlen(p); + optcount = (opt[1] - 3) / sizeof(struct in_addr); + adptr = opt + 3; + while (optcount-- > 0) + { + memcpy(&addr, adptr, sizeof(addr)); + if (!string_format(p, pend - p - 1, "%s%s", + (optcount == 0)? ":" : "@", inet_ntoa(addr))) + { + opt = NULL; + break; + } + p += Ustrlen(p); + adptr += sizeof(struct in_addr); + } + *p++ = ']'; + opt += opt[1]; + break; + + default: + { + int i; + if (pend - p < 4 + 3*opt[1]) { opt = NULL; break; } + Ustrcat(p, "[ "); + p += 2; + for (i = 0; i < opt[1]; i++) + { + sprintf(CS p, "%2.2x ", opt[i]); + p += 3; + } + *p++ = ']'; + } + opt += opt[1]; + break; + } + } + + *p = 0; + log_write(0, LOG_MAIN, "%s", big_buffer); + + /* Refuse any call with IP options. This is what tcpwrappers 7.5 does. */ + + log_write(0, LOG_MAIN|LOG_REJECT, + "connection from %s refused (IP options)", host_and_ident(FALSE)); + + smtp_printf("554 SMTP service not available\r\n"); + return FALSE; + } + + /* Length of options = 0 => there are no options */ + + else DEBUG(D_receive) debug_printf("no IP options found\n"); + } + #endif /* HAVE_IPV6 && !defined(NO_IP_OPTIONS) */ + + /* Set keep-alive in socket options. The option is on by default. This + setting is an attempt to get rid of some hanging connections that stick in + read() when the remote end (usually a dialup) goes away. */ + + if (smtp_accept_keepalive && !sender_host_notsocket) + ip_keepalive(fileno(smtp_out), sender_host_address, FALSE); + + /* If the current host matches host_lookup, set the name by doing a + reverse lookup. On failure, sender_host_name will be NULL and + host_lookup_failed will be TRUE. This may or may not be serious - optional + checks later. */ + + if (verify_check_host(&host_lookup) == OK) + { + (void)host_name_lookup(); + host_build_sender_fullhost(); + } + + /* Delay this until we have the full name, if it is looked up. */ + + set_process_info("handling incoming connection from %s", + host_and_ident(FALSE)); + + /* Start up TLS if tls_on_connect is set. This is for supporting the legacy + smtps port for use with older style SSL MTAs. */ + + #ifdef SUPPORT_TLS + if (tls_on_connect && tls_server_start(tls_require_ciphers) != OK) + return FALSE; + #endif + + /* Test for explicit connection rejection */ + + if (verify_check_host(&host_reject_connection) == OK) + { + log_write(L_connection_reject, LOG_MAIN|LOG_REJECT, "refused connection " + "from %s (host_reject_connection)", host_and_ident(FALSE)); + smtp_printf("554 SMTP service not available\r\n"); + return FALSE; + } + + /* Test with TCP Wrappers if so configured */ + + #ifdef USE_TCP_WRAPPERS + if (!hosts_ctl("exim", + (sender_host_name == NULL)? STRING_UNKNOWN : CS sender_host_name, + (sender_host_address == NULL)? STRING_UNKNOWN : CS sender_host_address, + (sender_ident == NULL)? STRING_UNKNOWN : CS sender_ident)) + { + HDEBUG(D_receive) debug_printf("tcp wrappers rejection\n"); + log_write(L_connection_reject, + LOG_MAIN|LOG_REJECT, "refused connection from %s " + "(tcp wrappers)", host_and_ident(FALSE)); + smtp_printf("554 SMTP service not available\r\n"); + return FALSE; + } + #endif + + /* Check for reserved slots. Note that the count value doesn't include + this process, as it gets upped in the parent process. */ + + if (smtp_accept_max > 0 && + smtp_accept_count + 1 > smtp_accept_max - smtp_accept_reserve) + { + if ((rc = verify_check_host(&smtp_reserve_hosts)) != OK) + { + log_write(L_connection_reject, + LOG_MAIN, "temporarily refused connection from %s: not in " + "reserve list: connected=%d max=%d reserve=%d%s", + host_and_ident(FALSE), smtp_accept_count, smtp_accept_max, + smtp_accept_reserve, (rc == DEFER)? " (lookup deferred)" : ""); + smtp_printf("421 %s: Too many concurrent SMTP connections; " + "please try again later\r\n", smtp_active_hostname); + return FALSE; + } + reserved_host = TRUE; + } + + /* If a load level above which only messages from reserved hosts are + accepted is set, check the load. For incoming calls via the daemon, the + check is done in the superior process if there are no reserved hosts, to + save a fork. In all cases, the load average will already be available + in a global variable at this point. */ + + if (smtp_load_reserve >= 0 && + load_average > smtp_load_reserve && + !reserved_host && + verify_check_host(&smtp_reserve_hosts) != OK) + { + log_write(L_connection_reject, + LOG_MAIN, "temporarily refused connection from %s: not in " + "reserve list and load average = %.2f", host_and_ident(FALSE), + (double)load_average/1000.0); + smtp_printf("421 %s: Too much load; please try again later\r\n", + smtp_active_hostname); + return FALSE; + } + + /* Determine whether unqualified senders or recipients are permitted + for this host. Unfortunately, we have to do this every time, in order to + set the flags so that they can be inspected when considering qualifying + addresses in the headers. For a site that permits no qualification, this + won't take long, however. */ + + allow_unqualified_sender = + verify_check_host(&sender_unqualified_hosts) == OK; + + allow_unqualified_recipient = + verify_check_host(&recipient_unqualified_hosts) == OK; + + /* Determine whether HELO/EHLO is required for this host. The requirement + can be hard or soft. */ + + helo_required = verify_check_host(&helo_verify_hosts) == OK; + if (!helo_required) + helo_verify = verify_check_host(&helo_try_verify_hosts) == OK; + + /* Determine whether this hosts is permitted to send syntactic junk + after a HELO or EHLO command. */ + + helo_accept_junk = verify_check_host(&helo_accept_junk_hosts) == OK; + } + +/* For batch SMTP input we are now done. */ + +if (smtp_batched_input) return TRUE; + +/* Run the ACL if it exists */ + +if (acl_smtp_connect != NULL) + { + int rc; + uschar *user_msg, *log_msg; + smtp_data = US"in \"connect\" ACL"; /* For logged failure message */ + rc = acl_check(ACL_WHERE_CONNECT, US"", acl_smtp_connect, &user_msg, + &log_msg); + if (rc != OK) + { + (void)smtp_handle_acl_fail(ACL_WHERE_CONNECT, rc, user_msg, log_msg); + return FALSE; + } + } + +/* Output the initial message for a two-way SMTP connection. It may contain +newlines, which then cause a multi-line response to be given. */ + +s = expand_string(smtp_banner); +if (s == NULL) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "Expansion of \"%s\" (smtp_banner) " + "failed: %s", smtp_banner, expand_string_message); + +/* Remove any terminating newlines; might as well remove trailing space too */ + +p = s + Ustrlen(s); +while (p > s && isspace(p[-1])) p--; +*p = 0; + +/* It seems that CC:Mail is braindead, and assumes that the greeting message +is all contained in a single IP packet. The original code wrote out the +greeting using several calls to fprint/fputc, and on busy servers this could +cause it to be split over more than one packet - which caused CC:Mail to fall +over when it got the second part of the greeting after sending its first +command. Sigh. To try to avoid this, build the complete greeting message +first, and output it in one fell swoop. This gives a better chance of it +ending up as a single packet. */ + +ss = store_get(size); +ptr = 0; + +p = s; +do /* At least once, in case we have an empty string */ + { + int len; + uschar *linebreak = Ustrchr(p, '\n'); + if (linebreak == NULL) + { + len = Ustrlen(p); + ss = string_cat(ss, &size, &ptr, US"220 ", 4); + } + else + { + len = linebreak - p; + ss = string_cat(ss, &size, &ptr, US"220-", 4); + } + ss = string_cat(ss, &size, &ptr, p, len); + ss = string_cat(ss, &size, &ptr, US"\r\n", 2); + p += len; + if (linebreak != NULL) p++; + } +while (*p != 0); + +ss[ptr] = 0; /* string_cat leaves room for this */ + +/* Before we write the banner, check that there is no input pending, unless +this synchronisation check is disabled. */ + +if (smtp_enforce_sync && sender_host_address != NULL && !sender_host_notsocket) + { + fd_set fds; + struct timeval tzero; + tzero.tv_sec = 0; + tzero.tv_usec = 0; + FD_ZERO(&fds); + FD_SET(fileno(smtp_in), &fds); + if (select(fileno(smtp_in) + 1, (SELECT_ARG2_TYPE *)&fds, NULL, NULL, + &tzero) > 0) + { + log_write(0, LOG_MAIN|LOG_REJECT, "SMTP protocol violation: " + "synchronization error (input sent without waiting for greeting): " + "rejected connection from %s", host_and_ident(TRUE)); + smtp_printf("554 SMTP synchronization error\r\n"); + return FALSE; + } + } + +/* Now output the banner */ + +smtp_printf("%s", ss); +return TRUE; +} + + + + + +/************************************************* +* Handle SMTP syntax and protocol errors * +*************************************************/ + +/* Write to the log for SMTP syntax errors in incoming commands, if configured +to do so. Then transmit the error response. The return value depends on the +number of syntax and protocol errors in this SMTP session. + +Arguments: + type error type, given as a log flag bit + code response code; <= 0 means don't send a response + data data to reflect in the response (can be NULL) + errmess the error message + +Returns: -1 limit of syntax/protocol errors NOT exceeded + +1 limit of syntax/protocol errors IS exceeded + +These values fit in with the values of the "done" variable in the main +processing loop in smtp_setup_msg(). */ + +static int +synprot_error(int type, int code, uschar *data, uschar *errmess) +{ +int yield = -1; + +log_write(type, LOG_MAIN, "SMTP %s error in \"%s\" %s %s", + (type == L_smtp_syntax_error)? "syntax" : "protocol", + string_printing(cmd_buffer), host_and_ident(TRUE), errmess); + +if (++synprot_error_count > smtp_max_synprot_errors) + { + yield = 1; + log_write(0, LOG_MAIN|LOG_REJECT, "SMTP call from %s dropped: too many " + "syntax or protocol errors (last command was \"%s\")", + host_and_ident(FALSE), cmd_buffer); + } + +if (code > 0) + { + smtp_printf("%d%c%s%s%s\r\n", code, (yield == 1)? '-' : ' ', + (data == NULL)? US"" : data, (data == NULL)? US"" : US": ", errmess); + if (yield == 1) + smtp_printf("%d Too many syntax or protocol errors\r\n", code); + } + +return yield; +} + + + + +/************************************************* +* Log incomplete transactions * +*************************************************/ + +/* This function is called after a transaction has been aborted by RSET, QUIT, +connection drops or other errors. It logs the envelope information received +so far in order to preserve address verification attempts. + +Argument: string to indicate what aborted the transaction +Returns: nothing +*/ + +static void +incomplete_transaction_log(uschar *what) +{ +if (sender_address == NULL || /* No transaction in progress */ + (log_write_selector & L_smtp_incomplete_transaction) == 0 /* Not logging */ + ) return; + +/* Build list of recipients for logging */ + +if (recipients_count > 0) + { + int i; + raw_recipients = store_get(recipients_count * sizeof(uschar *)); + for (i = 0; i < recipients_count; i++) + raw_recipients[i] = recipients_list[i].address; + raw_recipients_count = recipients_count; + } + +log_write(L_smtp_incomplete_transaction, LOG_MAIN|LOG_SENDER|LOG_RECIPIENTS, + "%s incomplete transaction (%s)", host_and_ident(TRUE), what); +} + + + + +/************************************************* +* Send SMTP response, possibly multiline * +*************************************************/ + +/* There are, it seems, broken clients out there that cannot handle multiline +responses. If no_multiline_responses is TRUE (it can be set from an ACL), we +output nothing for non-final calls, and only the first line for anything else. + +Arguments: + code SMTP code + final FALSE if the last line isn't the final line + msg message text, possibly containing newlines + +Returns: nothing +*/ + +void +smtp_respond(int code, BOOL final, uschar *msg) +{ +if (!final && no_multiline_responses) return; + +for (;;) + { + uschar *nl = Ustrchr(msg, '\n'); + if (nl == NULL) + { + smtp_printf("%d%c%s\r\n", code, final? ' ':'-', msg); + return; + } + else if (nl[1] == 0 || no_multiline_responses) + { + smtp_printf("%d%c%.*s\r\n", code, final? ' ':'-', (int)(nl - msg), msg); + return; + } + else + { + smtp_printf("%d-%.*s\r\n", code, (int)(nl - msg), msg); + msg = nl + 1; + while (isspace(*msg)) msg++; + } + } +} + + + + +/************************************************* +* Handle an ACL failure * +*************************************************/ + +/* This function is called when acl_check() fails. As well as calls from within +this module, it is called from receive.c for an ACL after DATA. It sorts out +logging the incident, and sets up the error response. A message containing +newlines is turned into a multiline SMTP response, but for logging, only the +first line is used. + +There's a table of the response codes to use in globals.c, along with the table +of names. VFRY is special. Despite RFC1123 it defaults disabled in Exim. +However, discussion in connection with RFC 821bis (aka RFC 2821) has concluded +that the response should be 252 in the disabled state, because there are broken +clients that try VRFY before RCPT. A 5xx response should be given only when the +address is positively known to be undeliverable. Sigh. Also, for ETRN, 458 is +given on refusal, and for AUTH, 503. + +Arguments: + where where the ACL was called from + rc the failure code + user_msg a message that can be included in an SMTP response + log_msg a message for logging + +Returns: 0 in most cases + 2 if the failure code was FAIL_DROP, in which case the + SMTP connection should be dropped (this value fits with the + "done" variable in smtp_setup_msg() below) +*/ + +int +smtp_handle_acl_fail(int where, int rc, uschar *user_msg, uschar *log_msg) +{ +int code = acl_wherecodes[where]; +BOOL drop = rc == FAIL_DROP; +uschar *lognl; +uschar *sender_info = US""; +uschar *what = (where == ACL_WHERE_PREDATA)? US"DATA" : + (where == ACL_WHERE_DATA)? US"after DATA" : + string_sprintf("%s %s", acl_wherenames[where], smtp_data); + +if (drop) rc = FAIL; + +/* We used to have sender_address here; however, there was a bug that was not +updating sender_address after a rewrite during a verify. When this bug was +fixed, sender_address at this point became the rewritten address. I'm not sure +this is what should be logged, so I've changed to logging the unrewritten +address to retain backward compatibility. */ + +if (where == ACL_WHERE_RCPT || where == ACL_WHERE_DATA) + { + sender_info = string_sprintf("F=<%s> ", (sender_address_unrewritten != NULL)? + sender_address_unrewritten : sender_address); + } + +/* If there's been a sender verification failure with a specific message, and +we have not sent a response about it yet, do so now, as a preliminary line for +failures, but not defers. However, log it in both cases. */ + +if (sender_verified_failed != NULL && + !testflag(sender_verified_failed, af_sverify_told)) + { + setflag(sender_verified_failed, af_sverify_told); + + log_write(0, LOG_MAIN|LOG_REJECT, "%s sender verify %s for <%s>%s", + host_and_ident(TRUE), + ((sender_verified_failed->special_action & 255) == DEFER)? "defer" : "fail", + sender_verified_failed->address, + (sender_verified_failed->message == NULL)? US"" : + string_sprintf(": %s", sender_verified_failed->message)); + + if (rc == FAIL && sender_verified_failed->user_message != NULL) + smtp_respond(code, FALSE, string_sprintf( + testflag(sender_verified_failed, af_verify_pmfail)? + "Postmaster verification failed while checking <%s>\n%s\n" + "Several RFCs state that you are required to have a postmaster\n" + "mailbox for each mail domain. This host does not accept mail\n" + "from domains whose servers reject the postmaster address." + : + testflag(sender_verified_failed, af_verify_nsfail)? + "Callback setup failed while verifying <%s>\n%s\n" + "The initial connection, or a HELO or MAIL FROM:<> command was\n" + "rejected. Refusing MAIL FROM:<> does not help fight spam, disregards\n" + "RFC requirements, and stops you from receiving standard bounce\n" + "messages. This host does not accept mail from domains whose servers\n" + "refuse bounces." + : + "Verification failed for <%s>\n%s", + sender_verified_failed->address, + sender_verified_failed->user_message)); + } + +/* Sort out text for logging */ + +log_msg = (log_msg == NULL)? US"" : string_sprintf(": %s", log_msg); +lognl = Ustrchr(log_msg, '\n'); +if (lognl != NULL) *lognl = 0; + +/* Send permanent failure response to the command, but the code used isn't +always a 5xx one - see comments at the start of this function. If the original +rc was FAIL_DROP we drop the connection and yield 2. */ + +if (rc == FAIL) smtp_respond(code, TRUE, (user_msg == NULL)? + US"Administrative prohibition" : user_msg); + +/* Send temporary failure response to the command. Don't give any details, +unless acl_temp_details is set. This is TRUE for a callout defer, a "defer" +verb, and for a header verify when smtp_return_error_details is set. + +This conditional logic is all somewhat of a mess because of the odd +interactions between temp_details and return_error_details. One day it should +be re-implemented in a tidier fashion. */ + +else + { + if (acl_temp_details && user_msg != NULL) + { + if (smtp_return_error_details && + sender_verified_failed != NULL && + sender_verified_failed->message != NULL) + { + smtp_respond(451, FALSE, sender_verified_failed->message); + } + smtp_respond(451, TRUE, user_msg); + } + else + smtp_printf("451 Temporary local problem - please try later\r\n"); + } + +/* Log the incident. If the connection is not forcibly to be dropped, return 0. +Otherwise, log why it is closing if required and return 2. */ + +log_write(0, LOG_MAIN|LOG_REJECT, "%s %s%srejected %s%s", + host_and_ident(TRUE), + sender_info, (rc == FAIL)? US"" : US"temporarily ", what, log_msg); + +if (!drop) return 0; + +log_write(L_smtp_connection, LOG_MAIN, "%s closed by DROP in ACL", + smtp_get_connection_info()); +return 2; +} + + + + +/************************************************* +* Initialize for SMTP incoming message * +*************************************************/ + +/* This function conducts the initial dialogue at the start of an incoming SMTP +message, and builds a list of recipients. However, if the incoming message +is part of a batch (-bS option) a separate function is called since it would +be messy having tests splattered about all over this function. This function +therefore handles the case where interaction is occurring. The input and output +files are set up in smtp_in and smtp_out. + +The global recipients_list is set to point to a vector of recipient_item +blocks, whose number is given by recipients_count. This is extended by the +receive_add_recipient() function. The global variable sender_address is set to +the sender's address. The yield is +1 if a message has been successfully +started, 0 if a QUIT command was encountered or the connection was refused from +the particular host, or -1 if the connection was lost. + +Argument: none + +Returns: > 0 message successfully started (reached DATA) + = 0 QUIT read or end of file reached or call refused + < 0 lost connection +*/ + +int +smtp_setup_msg(void) +{ +int done = 0; +BOOL toomany = FALSE; +BOOL discarded = FALSE; +BOOL last_was_rej_mail = FALSE; +BOOL last_was_rcpt = FALSE; +void *reset_point = store_get(0); + +DEBUG(D_receive) debug_printf("smtp_setup_msg entered\n"); + +/* Reset for start of new message. We allow one RSET not to be counted as a +nonmail command, for those MTAs that insist on sending it between every +message. Ditto for EHLO/HELO and for STARTTLS, to allow for going in and out of +TLS between messages (an Exim client may do this if it has messages queued up +for the host). Note: we do NOT reset AUTH at this point. */ + +smtp_reset(reset_point); +message_ended = END_NOTSTARTED; + +cmd_list[CMD_LIST_RSET].is_mail_cmd = TRUE; +cmd_list[CMD_LIST_HELO].is_mail_cmd = TRUE; +cmd_list[CMD_LIST_EHLO].is_mail_cmd = TRUE; +#ifdef SUPPORT_TLS +cmd_list[CMD_LIST_STARTTLS].is_mail_cmd = TRUE; +#endif + +/* Set the local signal handler for SIGTERM - it tries to end off tidily */ + +os_non_restarting_signal(SIGTERM, command_sigterm_handler); + +/* Batched SMTP is handled in a different function. */ + +if (smtp_batched_input) return smtp_setup_batch_msg(); + +/* Deal with SMTP commands. This loop is exited by setting done to a POSITIVE +value. The values are 2 larger than the required yield of the function. */ + +while (done <= 0) + { + uschar **argv; + uschar *etrn_command; + uschar *etrn_serialize_key; + uschar *errmess; + uschar *user_msg, *log_msg; + uschar *recipient = NULL; + uschar *hello = NULL; + uschar *set_id = NULL; + uschar *s, *ss; + BOOL was_rej_mail = FALSE; + BOOL was_rcpt = FALSE; + void (*oldsignal)(int); + pid_t pid; + int start, end, sender_domain, recipient_domain; + int ptr, size, rc; + int c; + auth_instance *au; + + switch(smtp_read_command(TRUE)) + { + /* The AUTH command is not permitted to occur inside a transaction, and may + occur successfully only once per connection, and then only when we've + advertised it. Actually, that isn't quite true. When TLS is started, all + previous information about a connection must be discarded, so a new AUTH is + permitted at that time. + + AUTH is initially labelled as a "nonmail command" so that one occurrence + doesn't get counted. We change the label here so that multiple failing + AUTHS will eventually hit the nonmail threshold. */ + + case AUTH_CMD: + authentication_failed = TRUE; + cmd_list[CMD_LIST_AUTH].is_mail_cmd = FALSE; + + if (!auth_advertised) + { + done = synprot_error(L_smtp_protocol_error, 503, NULL, + US"AUTH command used when not advertised"); + break; + } + if (sender_host_authenticated != NULL) + { + done = synprot_error(L_smtp_protocol_error, 503, NULL, + US"already authenticated"); + break; + } + if (sender_address != NULL) + { + done = synprot_error(L_smtp_protocol_error, 503, NULL, + US"not permitted in mail transaction"); + break; + } + + /* Check the ACL */ + + if (acl_smtp_auth != NULL) + { + rc = acl_check(ACL_WHERE_AUTH, smtp_data, acl_smtp_auth, &user_msg, + &log_msg); + if (rc != OK) + { + done = smtp_handle_acl_fail(ACL_WHERE_AUTH, rc, user_msg, log_msg); + break; + } + } + + /* Find the name of the requested authentication mechanism. */ + + s = smtp_data; + while ((c = *smtp_data) != 0 && !isspace(c)) + { + if (!isalnum(c) && c != '-' && c != '_') + { + done = synprot_error(L_smtp_syntax_error, 501, NULL, + US"invalid character in authentication mechanism name"); + goto COMMAND_LOOP; + } + smtp_data++; + } + + /* If not at the end of the line, we must be at white space. Terminate the + name and move the pointer on to any data that may be present. */ + + if (*smtp_data != 0) + { + *smtp_data++ = 0; + while (isspace(*smtp_data)) smtp_data++; + } + + /* Search for an authentication mechanism which is configured for use + as a server and which has been advertised. */ + + for (au = auths; au != NULL; au = au->next) + { + if (strcmpic(s, au->public_name) == 0 && au->server && + au->advertised) break; + } + + if (au == NULL) + { + done = synprot_error(L_smtp_protocol_error, 504, NULL, + string_sprintf("%s authentication mechanism not supported", s)); + break; + } + + /* Run the checking code, passing the remainder of the command + line as data. Initialize $0 empty. The authenticator may set up + other numeric variables. Afterwards, have a go at expanding the set_id + string, even if authentication failed - for bad passwords it can be useful + to log the userid. On success, require set_id to expand and exist, and + put it in authenticated_id. Save this in permanent store, as the working + store gets reset at HELO, RSET, etc. */ + + expand_nmax = 0; + expand_nlength[0] = 0; /* $0 contains nothing */ + + c = (au->info->servercode)(au, smtp_data); + if (au->set_id != NULL) set_id = expand_string(au->set_id); + expand_nmax = -1; /* Reset numeric variables */ + + /* For the non-OK cases, set up additional logging data if set_id + is not empty. */ + + if (c != OK) + { + if (set_id != NULL && *set_id != 0) + set_id = string_sprintf(" (set_id=%s)", set_id); + else set_id = US""; + } + + /* Switch on the result */ + + switch(c) + { + case OK: + if (au->set_id == NULL || set_id != NULL) /* Complete success */ + { + if (set_id != NULL) authenticated_id = string_copy_malloc(set_id); + sender_host_authenticated = au->name; + authentication_failed = FALSE; + received_protocol = + protocols[pextend + pauthed + ((tls_active >= 0)? pcrpted:0)] + + ((sender_host_address != NULL)? pnlocal : 0); + s = ss = US"235 Authentication succeeded"; + authenticated_by = au; + break; + } + + /* Authentication succeeded, but we failed to expand the set_id string. + Treat this as a temporary error. */ + + auth_defer_msg = expand_string_message; + /* Fall through */ + + case DEFER: + s = string_sprintf("435 Unable to authenticate at present%s", + auth_defer_user_msg); + ss = string_sprintf("435 Unable to authenticate at present%s: %s", + set_id, auth_defer_msg); + break; + + case BAD64: + s = ss = US"501 Invalid base64 data"; + break; + + case CANCELLED: + s = ss = US"501 Authentication cancelled"; + break; + + case UNEXPECTED: + s = ss = US"553 Initial data not expected"; + break; + + case FAIL: + s = US"535 Incorrect authentication data"; + ss = string_sprintf("535 Incorrect authentication data%s", set_id); + break; + + default: + s = US"435 Internal error"; + ss = string_sprintf("435 Internal error%s: return %d from authentication " + "check", set_id, c); + break; + } + + smtp_printf("%s\r\n", s); + if (c != OK) + log_write(0, LOG_MAIN|LOG_REJECT, "%s authenticator failed for %s: %s", + au->name, host_and_ident(FALSE), ss); + + break; /* AUTH_CMD */ + + /* The HELO/EHLO commands are permitted to appear in the middle of a + session as well as at the beginning. They have the effect of a reset in + addition to their other functions. Their absence at the start cannot be + taken to be an error. + + RFC 2821 says: + + If the EHLO command is not acceptable to the SMTP server, 501, 500, + or 502 failure replies MUST be returned as appropriate. The SMTP + server MUST stay in the same state after transmitting these replies + that it was in before the EHLO was received. + + Therefore, we do not do the reset until after checking the command for + acceptability. This change was made for Exim release 4.11. Previously + it did the reset first. */ + + case HELO_CMD: + hello = US"HELO"; + esmtp = FALSE; + goto HELO_EHLO; + + case EHLO_CMD: + hello = US"EHLO"; + esmtp = TRUE; + + HELO_EHLO: /* Common code for HELO and EHLO */ + cmd_list[CMD_LIST_HELO].is_mail_cmd = FALSE; + cmd_list[CMD_LIST_EHLO].is_mail_cmd = FALSE; + + /* Reject the HELO if its argument was invalid or non-existent. A + successful check causes the argument to be saved in malloc store. */ + + if (!check_helo(smtp_data)) + { + smtp_printf("501 Syntactically invalid %s argument(s)\r\n", hello); + + log_write(0, LOG_MAIN|LOG_REJECT, "rejected %s from %s: syntactically " + "invalid argument(s): %s", hello, host_and_ident(FALSE), + (*smtp_data == 0)? US"(no argument given)" : + string_printing(smtp_data)); + + if (++synprot_error_count > smtp_max_synprot_errors) + { + log_write(0, LOG_MAIN|LOG_REJECT, "SMTP call from %s dropped: too many " + "syntax or protocol errors (last command was \"%s\")", + host_and_ident(FALSE), cmd_buffer); + done = 1; + } + + break; + } + + /* If sender_host_unknown is true, we have got here via the -bs interface, + not called from inetd. Otherwise, we are running an IP connection and the + host address will be set. If the helo name is the primary name of this + host and we haven't done a reverse lookup, force one now. If helo_required + is set, ensure that the HELO name matches the actual host. If helo_verify + is set, do the same check, but softly. */ + + if (!sender_host_unknown) + { + BOOL old_helo_verified = helo_verified; + uschar *p = smtp_data; + + while (*p != 0 && !isspace(*p)) { *p = tolower(*p); p++; } + *p = 0; + + /* Force a reverse lookup if HELO quoted something in helo_lookup_domains + because otherwise the log can be confusing. */ + + if (sender_host_name == NULL && + (deliver_domain = sender_helo_name, /* set $domain */ + match_isinlist(sender_helo_name, &helo_lookup_domains, 0, + &domainlist_anchor, NULL, MCL_DOMAIN, TRUE, NULL)) == OK) + (void)host_name_lookup(); + + /* Rebuild the fullhost info to include the HELO name (and the real name + if it was looked up.) */ + + host_build_sender_fullhost(); /* Rebuild */ + set_process_info("handling%s incoming connection from %s", + (tls_active >= 0)? " TLS" : "", host_and_ident(FALSE)); + + /* Verify if configured. This doesn't give much security, but it does + make some people happy to be able to do it. Note that HELO is legitimately + allowed to quote an address literal. Allow for IPv6 ::ffff: literals. */ + + helo_verified = FALSE; + if (helo_required || helo_verify) + { + BOOL tempfail = FALSE; + + HDEBUG(D_receive) debug_printf("verifying %s %s\n", hello, + sender_helo_name); + if (sender_helo_name[0] == '[') + { + helo_verified = Ustrncmp(sender_helo_name+1, sender_host_address, + Ustrlen(sender_host_address)) == 0; + + #if HAVE_IPV6 + if (!helo_verified) + { + if (strncmpic(sender_host_address, US"::ffff:", 7) == 0) + helo_verified = Ustrncmp(sender_helo_name + 1, + sender_host_address + 7, Ustrlen(sender_host_address) - 7) == 0; + } + #endif + + HDEBUG(D_receive) + { if (helo_verified) debug_printf("matched host address\n"); } + } + + /* Do a reverse lookup if one hasn't already given a positive or + negative response. If that fails, or the name doesn't match, try + checking with a forward lookup. */ + + else + { + if (sender_host_name == NULL && !host_lookup_failed) + tempfail = host_name_lookup() == DEFER; + + /* If a host name is known, check it and all its aliases. */ + + if (sender_host_name != NULL) + { + helo_verified = strcmpic(sender_host_name, sender_helo_name) == 0; + + if (helo_verified) + { + HDEBUG(D_receive) debug_printf("matched host name\n"); + } + else + { + uschar **aliases = sender_host_aliases; + while (*aliases != NULL) + { + helo_verified = strcmpic(*aliases++, sender_helo_name) == 0; + if (helo_verified) break; + } + HDEBUG(D_receive) + { + if (helo_verified) + debug_printf("matched alias %s\n", *(--aliases)); + } + } + } + + /* Final attempt: try a forward lookup of the helo name */ + + if (!helo_verified) + { + int rc; + host_item h; + h.name = sender_helo_name; + h.address = NULL; + h.mx = MX_NONE; + h.next = NULL; + HDEBUG(D_receive) debug_printf("getting IP address for %s\n", + sender_helo_name); + rc = host_find_byname(&h, NULL, NULL, TRUE); + if (rc == HOST_FOUND || rc == HOST_FOUND_LOCAL) + { + host_item *hh = &h; + while (hh != NULL) + { + if (Ustrcmp(hh->address, sender_host_address) == 0) + { + helo_verified = TRUE; + HDEBUG(D_receive) + debug_printf("IP address for %s matches calling address\n", + sender_helo_name); + break; + } + hh = hh->next; + } + } + } + } + + /* Verification failed. A temporary lookup failure gives a temporary + error. */ + + if (!helo_verified) + { + if (helo_required) + { + smtp_printf("%d %s argument does not match calling host\r\n", + tempfail? 451 : 550, hello); + log_write(0, LOG_MAIN|LOG_REJECT, "%srejected \"%s %s\" from %s", + tempfail? "temporarily " : "", + hello, sender_helo_name, host_and_ident(FALSE)); + helo_verified = old_helo_verified; + break; /* End of HELO/EHLO processing */ + } + HDEBUG(D_all) debug_printf("%s verification failed but host is in " + "helo_try_verify_hosts\n", hello); + } + } + } + + /* Apply an ACL check if one is defined */ + + if (acl_smtp_helo != NULL) + { + rc = acl_check(ACL_WHERE_HELO, smtp_data, acl_smtp_helo, &user_msg, + &log_msg); + if (rc != OK) + { + done = smtp_handle_acl_fail(ACL_WHERE_HELO, rc, user_msg, log_msg); + sender_helo_name = NULL; + host_build_sender_fullhost(); /* Rebuild */ + break; + } + } + + /* The EHLO/HELO command is acceptable. Reset the protocol and the state, + abandoning any previous message. */ + + received_protocol = (esmtp? + protocols[pextend + + ((sender_host_authenticated != NULL)? pauthed : 0) + + ((tls_active >= 0)? pcrpted : 0)] + : + protocols[pnormal]) + + + ((sender_host_address != NULL)? pnlocal : 0); + + smtp_reset(reset_point); + toomany = FALSE; + + /* Generate an OK reply, including the ident if present, and also + the IP address if present. Reflecting back the ident is intended + as a deterrent to mail forgers. For maximum efficiency, and also + because some broken systems expect each response to be in a single + packet, arrange that it is sent in one write(). */ + + auth_advertised = FALSE; + pipelining_advertised = FALSE; + #ifdef SUPPORT_TLS + tls_advertised = FALSE; + #endif + + s = string_sprintf("250 %s Hello %s%s%s", + smtp_active_hostname, + (sender_ident == NULL)? US"" : sender_ident, + (sender_ident == NULL)? US"" : US" at ", + (sender_host_name == NULL)? sender_helo_name : sender_host_name); + + ptr = Ustrlen(s); + size = ptr + 1; + + if (sender_host_address != NULL) + { + s = string_cat(s, &size, &ptr, US" [", 2); + s = string_cat(s, &size, &ptr, sender_host_address, + Ustrlen(sender_host_address)); + s = string_cat(s, &size, &ptr, US"]", 1); + } + + s = string_cat(s, &size, &ptr, US"\r\n", 2); + + /* If we received EHLO, we must create a multiline response which includes + the functions supported. */ + + if (esmtp) + { + s[3] = '-'; + + /* I'm not entirely happy with this, as an MTA is supposed to check + that it has enough room to accept a message of maximum size before + it sends this. However, there seems little point in not sending it. + The actual size check happens later at MAIL FROM time. By postponing it + till then, VRFY and EXPN can be used after EHLO when space is short. */ + + if (thismessage_size_limit > 0) + { + sprintf(CS big_buffer, "250-SIZE %d\r\n", thismessage_size_limit); + s = string_cat(s, &size, &ptr, big_buffer, Ustrlen(big_buffer)); + } + else + { + s = string_cat(s, &size, &ptr, US"250-SIZE\r\n", 10); + } + + /* Exim does not do protocol conversion or data conversion. It is 8-bit + clean; if it has an 8-bit character in its hand, it just sends it. It + cannot therefore specify 8BITMIME and remain consistent with the RFCs. + However, some users want this option simply in order to stop MUAs + mangling messages that contain top-bit-set characters. It is therefore + provided as an option. */ + + if (accept_8bitmime) + s = string_cat(s, &size, &ptr, US"250-8BITMIME\r\n", 14); + + /* Advertise ETRN if there's an ACL checking whether a host is + permitted to issue it; a check is made when any host actually tries. */ + + if (acl_smtp_etrn != NULL) + { + s = string_cat(s, &size, &ptr, US"250-ETRN\r\n", 10); + } + + /* Advertise EXPN if there's an ACL checking whether a host is + permitted to issue it; a check is made when any host actually tries. */ + + if (acl_smtp_expn != NULL) + { + s = string_cat(s, &size, &ptr, US"250-EXPN\r\n", 10); + } + + /* Exim is quite happy with pipelining, so let the other end know that + it is safe to use it, unless advertising is disabled. */ + + if (verify_check_host(&pipelining_advertise_hosts) == OK) + { + s = string_cat(s, &size, &ptr, US"250-PIPELINING\r\n", 16); + sync_cmd_limit = NON_SYNC_CMD_PIPELINING; + pipelining_advertised = TRUE; + } + + /* If any server authentication mechanisms are configured, advertise + them if the current host is in auth_advertise_hosts. The problem with + advertising always is that some clients then require users to + authenticate (and aren't configurable otherwise) even though it may not + be necessary (e.g. if the host is in host_accept_relay). + + RFC 2222 states that SASL mechanism names contain only upper case + letters, so output the names in upper case, though we actually recognize + them in either case in the AUTH command. */ + + if (auths != NULL) + { + if (verify_check_host(&auth_advertise_hosts) == OK) + { + auth_instance *au; + BOOL first = TRUE; + for (au = auths; au != NULL; au = au->next) + { + if (au->server && (au->advertise_condition == NULL || + expand_check_condition(au->advertise_condition, au->name, + US"authenticator"))) + { + int saveptr; + if (first) + { + s = string_cat(s, &size, &ptr, US"250-AUTH", 8); + first = FALSE; + auth_advertised = TRUE; + } + saveptr = ptr; + s = string_cat(s, &size, &ptr, US" ", 1); + s = string_cat(s, &size, &ptr, au->public_name, + Ustrlen(au->public_name)); + while (++saveptr < ptr) s[saveptr] = toupper(s[saveptr]); + au->advertised = TRUE; + } + else au->advertised = FALSE; + } + if (!first) s = string_cat(s, &size, &ptr, US"\r\n", 2); + } + } + + /* Advertise TLS (Transport Level Security) aka SSL (Secure Socket Layer) + if it has been included in the binary, and the host matches + tls_advertise_hosts. We must *not* advertise if we are already in a + secure connection. */ + + #ifdef SUPPORT_TLS + if (tls_active < 0 && + verify_check_host(&tls_advertise_hosts) != FAIL) + { + s = string_cat(s, &size, &ptr, US"250-STARTTLS\r\n", 14); + tls_advertised = TRUE; + } + #endif + + /* Finish off the multiline reply with one that is always available. */ + + s = string_cat(s, &size, &ptr, US"250 HELP\r\n", 10); + } + + /* Terminate the string (for debug), write it, and note that HELO/EHLO + has been seen. */ + + s[ptr] = 0; + + #ifdef SUPPORT_TLS + if (tls_active >= 0) (void)tls_write(s, ptr); else + #endif + + fwrite(s, 1, ptr, smtp_out); + DEBUG(D_receive) debug_printf("SMTP>> %s", s); + helo_seen = TRUE; + break; /* HELO/EHLO */ + + + /* The MAIL command requires an address as an operand. All we do + here is to parse it for syntactic correctness. The form "<>" is + a special case which converts into an empty string. The start/end + pointers in the original are not used further for this address, as + it is the canonical extracted address which is all that is kept. */ + + case MAIL_CMD: + smtp_mailcmd_count++; /* Count for limit and ratelimit */ + was_rej_mail = TRUE; /* Reset if accepted */ + + if (helo_required && !helo_seen) + { + smtp_printf("503 HELO or EHLO required\r\n"); + log_write(0, LOG_MAIN|LOG_REJECT, "rejected MAIL from %s: no " + "HELO/EHLO given", host_and_ident(FALSE)); + break; + } + + if (sender_address != NULL) + { + done = synprot_error(L_smtp_protocol_error, 503, NULL, + US"sender already given"); + break; + } + + if (smtp_data[0] == 0) + { + done = synprot_error(L_smtp_protocol_error, 501, NULL, + US"MAIL must have an address operand"); + break; + } + + /* Check to see if the limit for messages per connection would be + exceeded by accepting further messages. */ + + if (smtp_accept_max_per_connection > 0 && + smtp_mailcmd_count > smtp_accept_max_per_connection) + { + smtp_printf("421 too many messages in this connection\r\n"); + log_write(0, LOG_MAIN|LOG_REJECT, "rejected MAIL command %s: too many " + "messages in one connection", host_and_ident(TRUE)); + break; + } + + /* Reset for start of message - even if this is going to fail, we + obviously need to throw away any previous data. */ + + smtp_reset(reset_point); + toomany = FALSE; + sender_data = recipient_data = NULL; + + /* Loop, checking for ESMTP additions to the MAIL FROM command. */ + + if (esmtp) for(;;) + { + uschar *name, *value, *end; + unsigned long int size; + + if (!extract_option(&name, &value)) break; + + /* Handle SIZE= by reading the value. We don't do the check till later, + in order to be able to log the sender address on failure. */ + + if (strcmpic(name, US"SIZE") == 0 && + ((size = (int)Ustrtoul(value, &end, 10)), *end == 0)) + { + if ((size == ULONG_MAX && errno == ERANGE) || size > INT_MAX) + size = INT_MAX; + message_size = (int)size; + } + + /* If this session was initiated with EHLO and accept_8bitmime is set, + Exim will have indicated that it supports the BODY=8BITMIME option. In + fact, it does not support this according to the RFCs, in that it does not + take any special action for forwarding messages containing 8-bit + characters. That is why accept_8bitmime is not the default setting, but + some sites want the action that is provided. We recognize both "8BITMIME" + and "7BIT" as body types, but take no action. */ + + else if (accept_8bitmime && strcmpic(name, US"BODY") == 0 && + (strcmpic(value, US"8BITMIME") == 0 || + strcmpic(value, US"7BIT") == 0)) {} + + /* Handle the AUTH extension. If the value given is not "<>" and either + the ACL says "yes" or there is no ACL but the sending host is + authenticated, we set it up as the authenticated sender. However, if the + authenticator set a condition to be tested, we ignore AUTH on MAIL unless + the condition is met. The value of AUTH is an xtext, which means that +, + = and cntrl chars are coded in hex; however "<>" is unaffected by this + coding. */ + + else if (strcmpic(name, US"AUTH") == 0) + { + if (Ustrcmp(value, "<>") != 0) + { + int rc; + uschar *ignore_msg; + + if (auth_xtextdecode(value, &authenticated_sender) < 0) + { + /* Put back terminator overrides for error message */ + name[-1] = ' '; + value[-1] = '='; + done = synprot_error(L_smtp_syntax_error, 501, NULL, + US"invalid data for AUTH"); + goto COMMAND_LOOP; + } + + if (acl_smtp_mailauth == NULL) + { + ignore_msg = US"client not authenticated"; + rc = (sender_host_authenticated != NULL)? OK : FAIL; + } + else + { + ignore_msg = US"rejected by ACL"; + rc = acl_check(ACL_WHERE_MAILAUTH, NULL, acl_smtp_mailauth, + &user_msg, &log_msg); + } + + switch (rc) + { + case OK: + if (authenticated_by == NULL || + authenticated_by->mail_auth_condition == NULL || + expand_check_condition(authenticated_by->mail_auth_condition, + authenticated_by->name, US"authenticator")) + break; /* Accept the AUTH */ + + ignore_msg = US"server_mail_auth_condition failed"; + if (authenticated_id != NULL) + ignore_msg = string_sprintf("%s: authenticated ID=\"%s\"", + ignore_msg, authenticated_id); + + /* Fall through */ + + case FAIL: + authenticated_sender = NULL; + log_write(0, LOG_MAIN, "ignoring AUTH=%s from %s (%s)", + value, host_and_ident(TRUE), ignore_msg); + break; + + /* Should only get DEFER or ERROR here. Put back terminator + overrides for error message */ + + default: + name[-1] = ' '; + value[-1] = '='; + (void)smtp_handle_acl_fail(ACL_WHERE_MAILAUTH, rc, user_msg, + log_msg); + goto COMMAND_LOOP; + } + } + } + + /* Unknown option. Stick back the terminator characters and break + the loop. An error for a malformed address will occur. */ + + else + { + name[-1] = ' '; + value[-1] = '='; + break; + } + } + + /* If we have passed the threshold for rate limiting, apply the current + delay, and update it for next time, provided this is a limited host. */ + + if (smtp_mailcmd_count > smtp_rlm_threshold && + verify_check_host(&smtp_ratelimit_hosts) == OK) + { + DEBUG(D_receive) debug_printf("rate limit MAIL: delay %.3g sec\n", + smtp_delay_mail/1000.0); + millisleep((int)smtp_delay_mail); + smtp_delay_mail *= smtp_rlm_factor; + if (smtp_delay_mail > (double)smtp_rlm_limit) + smtp_delay_mail = (double)smtp_rlm_limit; + } + + /* Now extract the address, first applying any SMTP-time rewriting. The + TRUE flag allows "<>" as a sender address. */ + + raw_sender = ((rewrite_existflags & rewrite_smtp) != 0)? + rewrite_one(smtp_data, rewrite_smtp, NULL, FALSE, US"", + global_rewrite_rules) : smtp_data; + + /* rfc821_domains = TRUE; << no longer needed */ + raw_sender = + parse_extract_address(raw_sender, &errmess, &start, &end, &sender_domain, + TRUE); + /* rfc821_domains = FALSE; << no longer needed */ + + if (raw_sender == NULL) + { + done = synprot_error(L_smtp_syntax_error, 501, smtp_data, errmess); + break; + } + + sender_address = raw_sender; + + /* If there is a configured size limit for mail, check that this message + doesn't exceed it. The check is postponed to this point so that the sender + can be logged. */ + + if (thismessage_size_limit > 0 && message_size > thismessage_size_limit) + { + smtp_printf("552 Message size exceeds maximum permitted\r\n"); + log_write(L_size_reject, + LOG_MAIN|LOG_REJECT, "rejected MAIL FROM:<%s> %s: " + "message too big: size%s=%d max=%d", + sender_address, + host_and_ident(TRUE), + (message_size == INT_MAX)? ">" : "", + message_size, + thismessage_size_limit); + sender_address = NULL; + break; + } + + /* Check there is enough space on the disk unless configured not to. + When smtp_check_spool_space is set, the check is for thismessage_size_limit + plus the current message - i.e. we accept the message only if it won't + reduce the space below the threshold. Add 5000 to the size to allow for + overheads such as the Received: line and storing of recipients, etc. + By putting the check here, even when SIZE is not given, it allow VRFY + and EXPN etc. to be used when space is short. */ + + if (!receive_check_fs( + (smtp_check_spool_space && message_size >= 0)? + message_size + 5000 : 0)) + { + smtp_printf("452 Space shortage, please try later\r\n"); + sender_address = NULL; + break; + } + + /* If sender_address is unqualified, reject it, unless this is a locally + generated message, or the sending host or net is permitted to send + unqualified addresses - typically local machines behaving as MUAs - + in which case just qualify the address. The flag is set above at the start + of the SMTP connection. */ + + if (sender_domain == 0 && sender_address[0] != 0) + { + if (allow_unqualified_sender) + { + sender_domain = Ustrlen(sender_address) + 1; + sender_address = rewrite_address_qualify(sender_address, FALSE); + DEBUG(D_receive) debug_printf("unqualified address %s accepted\n", + raw_sender); + } + else + { + smtp_printf("501 %s: sender address must contain a domain\r\n", + smtp_data); + log_write(L_smtp_syntax_error, + LOG_MAIN|LOG_REJECT, + "unqualified sender rejected: <%s> %s%s", + raw_sender, + host_and_ident(TRUE), + host_lookup_msg); + sender_address = NULL; + break; + } + } + + /* Apply an ACL check if one is defined, before responding */ + + rc = (acl_smtp_mail == NULL)? OK : + acl_check(ACL_WHERE_MAIL, NULL, acl_smtp_mail, &user_msg, &log_msg); + + if (rc == OK || rc == DISCARD) + { + smtp_printf("250 OK\r\n"); + smtp_delay_rcpt = smtp_rlr_base; + recipients_discarded = (rc == DISCARD); + was_rej_mail = FALSE; + } + + else + { + done = smtp_handle_acl_fail(ACL_WHERE_MAIL, rc, user_msg, log_msg); + sender_address = NULL; + } + break; + + + /* The RCPT command requires an address as an operand. All we do + here is to parse it for syntactic correctness. There may be any number + of RCPT commands, specifying multiple senders. We build them all into + a data structure that is in argc/argv format. The start/end values + given by parse_extract_address are not used, as we keep only the + extracted address. */ + + case RCPT_CMD: + rcpt_count++; + was_rcpt = TRUE; + + /* There must be a sender address; if the sender was rejected and + pipelining was advertised, we assume the client was pipelining, and do not + count this as a protocol error. Reset was_rej_mail so that further RCPTs + get the same treatment. */ + + if (sender_address == NULL) + { + if (pipelining_advertised && last_was_rej_mail) + { + smtp_printf("503 sender not yet given\r\n"); + was_rej_mail = TRUE; + } + else + { + done = synprot_error(L_smtp_protocol_error, 503, NULL, + US"sender not yet given"); + was_rcpt = FALSE; /* Not a valid RCPT */ + } + rcpt_fail_count++; + break; + } + + /* Check for an operand */ + + if (smtp_data[0] == 0) + { + done = synprot_error(L_smtp_syntax_error, 501, NULL, + US"RCPT must have an address operand"); + rcpt_fail_count++; + break; + } + + /* Apply SMTP rewriting then extract the working address. Don't allow "<>" + as a recipient address */ + + recipient = ((rewrite_existflags & rewrite_smtp) != 0)? + rewrite_one(smtp_data, rewrite_smtp, NULL, FALSE, US"", + global_rewrite_rules) : smtp_data; + + /* rfc821_domains = TRUE; << no longer needed */ + recipient = parse_extract_address(recipient, &errmess, &start, &end, + &recipient_domain, FALSE); + /* rfc821_domains = FALSE; << no longer needed */ + + if (recipient == NULL) + { + done = synprot_error(L_smtp_syntax_error, 501, smtp_data, errmess); + rcpt_fail_count++; + break; + } + + /* If the recipient address is unqualified, reject it, unless this is a + locally generated message. However, unqualified addresses are permitted + from a configured list of hosts and nets - typically when behaving as + MUAs rather than MTAs. Sad that SMTP is used for both types of traffic, + really. The flag is set at the start of the SMTP connection. + + RFC 1123 talks about supporting "the reserved mailbox postmaster"; I always + assumed this meant "reserved local part", but the revision of RFC 821 and + friends now makes it absolutely clear that it means *mailbox*. Consequently + we must always qualify this address, regardless. */ + + if (recipient_domain == 0) + { + if (allow_unqualified_recipient || + strcmpic(recipient, US"postmaster") == 0) + { + DEBUG(D_receive) debug_printf("unqualified address %s accepted\n", + recipient); + recipient_domain = Ustrlen(recipient) + 1; + recipient = rewrite_address_qualify(recipient, TRUE); + } + else + { + rcpt_fail_count++; + smtp_printf("501 %s: recipient address must contain a domain\r\n", + smtp_data); + log_write(L_smtp_syntax_error, + LOG_MAIN|LOG_REJECT, "unqualified recipient rejected: " + "<%s> %s%s", recipient, host_and_ident(TRUE), + host_lookup_msg); + break; + } + } + + /* Check maximum allowed */ + + if (rcpt_count > recipients_max && recipients_max > 0) + { + if (recipients_max_reject) + { + rcpt_fail_count++; + smtp_printf("552 too many recipients\r\n"); + if (!toomany) + log_write(0, LOG_MAIN|LOG_REJECT, "too many recipients: message " + "rejected: sender=<%s> %s", sender_address, host_and_ident(TRUE)); + } + else + { + rcpt_defer_count++; + smtp_printf("452 too many recipients\r\n"); + if (!toomany) + log_write(0, LOG_MAIN|LOG_REJECT, "too many recipients: excess " + "temporarily rejected: sender=<%s> %s", sender_address, + host_and_ident(TRUE)); + } + + toomany = TRUE; + break; + } + + /* If we have passed the threshold for rate limiting, apply the current + delay, and update it for next time, provided this is a limited host. */ + + if (rcpt_count > smtp_rlr_threshold && + verify_check_host(&smtp_ratelimit_hosts) == OK) + { + DEBUG(D_receive) debug_printf("rate limit RCPT: delay %.3g sec\n", + smtp_delay_rcpt/1000.0); + millisleep((int)smtp_delay_rcpt); + smtp_delay_rcpt *= smtp_rlr_factor; + if (smtp_delay_rcpt > (double)smtp_rlr_limit) + smtp_delay_rcpt = (double)smtp_rlr_limit; + } + + /* If the MAIL ACL discarded all the recipients, we bypass ACL checking + for them. Otherwise, check the access control list for this recipient. */ + + rc = recipients_discarded? DISCARD : + acl_check(ACL_WHERE_RCPT, recipient, acl_smtp_rcpt, &user_msg, &log_msg); + + /* The ACL was happy */ + + if (rc == OK) + { + smtp_printf("250 Accepted\r\n"); + receive_add_recipient(recipient, -1); + } + + /* The recipient was discarded */ + + else if (rc == DISCARD) + { + smtp_printf("250 Accepted\r\n"); + rcpt_fail_count++; + discarded = TRUE; + log_write(0, LOG_MAIN|LOG_REJECT, "%s F=<%s> rejected RCPT %s: " + "discarded by %s ACL%s%s", host_and_ident(TRUE), + (sender_address_unrewritten != NULL)? + sender_address_unrewritten : sender_address, + smtp_data, recipients_discarded? "MAIL" : "RCPT", + (log_msg == NULL)? US"" : US": ", + (log_msg == NULL)? US"" : log_msg); + } + + /* Either the ACL failed the address, or it was deferred. */ + + else + { + if (rc == FAIL) rcpt_fail_count++; else rcpt_defer_count++; + done = smtp_handle_acl_fail(ACL_WHERE_RCPT, rc, user_msg, log_msg); + } + break; + + + /* The DATA command is legal only if it follows successful MAIL FROM + and RCPT TO commands. However, if pipelining is advertised, a bad DATA is + not counted as a protocol error if it follows RCPT (which must have been + rejected if there are no recipients.) This function is complete when a + valid DATA command is encountered. + + Note concerning the code used: RFC 2821 says this: + + - If there was no MAIL, or no RCPT, command, or all such commands + were rejected, the server MAY return a "command out of sequence" + (503) or "no valid recipients" (554) reply in response to the + DATA command. + + The example in the pipelining RFC 2920 uses 554, but I use 503 here + because it is the same whether pipelining is in use or not. */ + + case DATA_CMD: + if (!discarded && recipients_count <= 0) + { + if (pipelining_advertised && last_was_rcpt) + smtp_printf("503 valid RCPT command must precede DATA\r\n"); + else + done = synprot_error(L_smtp_protocol_error, 503, NULL, + US"valid RCPT command must precede DATA"); + break; + } + + if (toomany && recipients_max_reject) + { + sender_address = NULL; /* This will allow a new MAIL without RSET */ + sender_address_unrewritten = NULL; + smtp_printf("554 Too many recipients\r\n"); + break; + } + + rc = (acl_smtp_predata == NULL)? OK : + acl_check(ACL_WHERE_PREDATA, NULL, acl_smtp_predata, &user_msg, &log_msg); + + if (rc == OK) + { + smtp_printf("354 Enter message, ending with \".\" on a line by itself\r\n"); + done = 3; + message_ended = END_NOTENDED; /* Indicate in middle of data */ + } + + /* Either the ACL failed the address, or it was deferred. */ + + else + done = smtp_handle_acl_fail(ACL_WHERE_PREDATA, rc, user_msg, log_msg); + + break; + + + case VRFY_CMD: + rc = acl_check(ACL_WHERE_VRFY, smtp_data, acl_smtp_vrfy, &user_msg, + &log_msg); + if (rc != OK) + done = smtp_handle_acl_fail(ACL_WHERE_VRFY, rc, user_msg, log_msg); + else + { + uschar *address; + uschar *s = NULL; + + /* rfc821_domains = TRUE; << no longer needed */ + address = parse_extract_address(smtp_data, &errmess, &start, &end, + &recipient_domain, FALSE); + /* rfc821_domains = FALSE; << no longer needed */ + + if (address == NULL) + s = string_sprintf("501 %s", errmess); + else + { + address_item *addr = deliver_make_addr(address, FALSE); + switch(verify_address(addr, NULL, vopt_is_recipient | vopt_qualify, -1, + -1, NULL, NULL, NULL)) + { + case OK: + s = string_sprintf("250 <%s> is deliverable", address); + break; + + case DEFER: + s = (addr->message != NULL)? + string_sprintf("451 <%s> %s", address, addr->message) : + string_sprintf("451 Cannot resolve <%s> at this time", address); + break; + + case FAIL: + s = (addr->message != NULL)? + string_sprintf("550 <%s> %s", address, addr->message) : + string_sprintf("550 <%s> is not deliverable", address); + log_write(0, LOG_MAIN, "VRFY failed for %s %s", + smtp_data, host_and_ident(TRUE)); + break; + } + } + + smtp_printf("%s\r\n", s); + } + break; + + + case EXPN_CMD: + rc = acl_check(ACL_WHERE_EXPN, smtp_data, acl_smtp_expn, &user_msg, + &log_msg); + if (rc != OK) + done = smtp_handle_acl_fail(ACL_WHERE_EXPN, rc, user_msg, log_msg); + else + { + BOOL save_log_testing_mode = log_testing_mode; + address_test_mode = log_testing_mode = TRUE; + (void) verify_address(deliver_make_addr(smtp_data, FALSE), smtp_out, + vopt_is_recipient | vopt_qualify | vopt_expn, -1, -1, NULL, NULL, NULL); + address_test_mode = FALSE; + log_testing_mode = save_log_testing_mode; /* true for -bh */ + } + break; + + + #ifdef SUPPORT_TLS + + case STARTTLS_CMD: + if (!tls_advertised) + { + done = synprot_error(L_smtp_protocol_error, 503, NULL, + US"STARTTLS command used when not advertised"); + break; + } + + /* Apply an ACL check if one is defined */ + + if (acl_smtp_starttls != NULL) + { + rc = acl_check(ACL_WHERE_STARTTLS, NULL, acl_smtp_starttls, &user_msg, + &log_msg); + if (rc != OK) + { + done = smtp_handle_acl_fail(ACL_WHERE_STARTTLS, rc, user_msg, log_msg); + break; + } + } + + /* RFC 2487 is not clear on when this command may be sent, though it + does state that all information previously obtained from the client + must be discarded if a TLS session is started. It seems reasonble to + do an implied RSET when STARTTLS is received. */ + + incomplete_transaction_log(US"STARTTLS"); + smtp_reset(reset_point); + toomany = FALSE; + cmd_list[CMD_LIST_STARTTLS].is_mail_cmd = FALSE; + + /* Attempt to start up a TLS session, and if successful, discard all + knowledge that was obtained previously. At least, that's what the RFC says, + and that's what happens by default. However, in order to work round YAEB, + there is an option to remember the esmtp state. Sigh. + + We must allow for an extra EHLO command and an extra AUTH command after + STARTTLS that don't add to the nonmail command count. */ + + if ((rc = tls_server_start(tls_require_ciphers)) == OK) + { + if (!tls_remember_esmtp) + helo_seen = esmtp = auth_advertised = pipelining_advertised = FALSE; + cmd_list[CMD_LIST_EHLO].is_mail_cmd = TRUE; + cmd_list[CMD_LIST_AUTH].is_mail_cmd = TRUE; + if (sender_helo_name != NULL) + { + store_free(sender_helo_name); + sender_helo_name = NULL; + host_build_sender_fullhost(); /* Rebuild */ + set_process_info("handling incoming TLS connection from %s", + host_and_ident(FALSE)); + } + received_protocol = (esmtp? + protocols[pextend + pcrpted + + ((sender_host_authenticated != NULL)? pauthed : 0)] + : + protocols[pnormal]) + + + ((sender_host_address != NULL)? pnlocal : 0); + + sender_host_authenticated = NULL; + authenticated_id = NULL; + sync_cmd_limit = NON_SYNC_CMD_NON_PIPELINING; + DEBUG(D_tls) debug_printf("TLS active\n"); + break; /* Successful STARTTLS */ + } + + /* Some local configuration problem was discovered before actually trying + to do a TLS handshake; give a temporary error. */ + + else if (rc == DEFER) + { + smtp_printf("454 TLS currently unavailable\r\n"); + break; + } + + /* Hard failure. Reject everything except QUIT or closed connection. One + cause for failure is a nested STARTTLS, in which case tls_active remains + set, but we must still reject all incoming commands. */ + + DEBUG(D_tls) debug_printf("TLS failed to start\n"); + while (done <= 0) + { + switch(smtp_read_command(FALSE)) + { + case EOF_CMD: + log_write(L_smtp_connection, LOG_MAIN, "%s closed by EOF", + smtp_get_connection_info()); + done = 2; + break; + + case QUIT_CMD: + smtp_printf("221 %s closing connection\r\n", smtp_active_hostname); + log_write(L_smtp_connection, LOG_MAIN, "%s closed by QUIT", + smtp_get_connection_info()); + done = 2; + break; + + default: + smtp_printf("554 Security failure\r\n"); + break; + } + } + tls_close(TRUE); + break; + #endif + + + /* The ACL for QUIT is provided for gathering statistical information or + similar; it does not affect the response code, but it can supply a custom + message. */ + + case QUIT_CMD: + incomplete_transaction_log(US"QUIT"); + + if (acl_smtp_quit != NULL) + { + rc = acl_check(ACL_WHERE_QUIT, US"", acl_smtp_quit,&user_msg,&log_msg); + if (rc == ERROR) + log_write(0, LOG_MAIN|LOG_PANIC, "ACL for QUIT returned ERROR: %s", + log_msg); + } + else user_msg = NULL; + + if (user_msg == NULL) + smtp_printf("221 %s closing connection\r\n", smtp_active_hostname); + else + smtp_printf("221 %s\r\n", user_msg); + + #ifdef SUPPORT_TLS + tls_close(TRUE); + #endif + + done = 2; + log_write(L_smtp_connection, LOG_MAIN, "%s closed by QUIT", + smtp_get_connection_info()); + break; + + + case RSET_CMD: + incomplete_transaction_log(US"RSET"); + smtp_reset(reset_point); + toomany = FALSE; + smtp_printf("250 Reset OK\r\n"); + cmd_list[CMD_LIST_RSET].is_mail_cmd = FALSE; + break; + + + case NOOP_CMD: + smtp_printf("250 OK\r\n"); + break; + + + /* Show ETRN/EXPN/VRFY if there's + an ACL for checking hosts; if actually used, a check will be done for + permitted hosts. */ + + case HELP_CMD: + smtp_printf("214-Commands supported:\r\n"); + { + uschar buffer[256]; + buffer[0] = 0; + Ustrcat(buffer, " AUTH"); + #ifdef SUPPORT_TLS + Ustrcat(buffer, " STARTTLS"); + #endif + Ustrcat(buffer, " HELO EHLO MAIL RCPT DATA"); + Ustrcat(buffer, " NOOP QUIT RSET HELP"); + if (acl_smtp_etrn != NULL) Ustrcat(buffer, " ETRN"); + if (acl_smtp_expn != NULL) Ustrcat(buffer, " EXPN"); + if (acl_smtp_vrfy != NULL) Ustrcat(buffer, " VRFY"); + smtp_printf("214%s\r\n", buffer); + } + break; + + + case EOF_CMD: + incomplete_transaction_log(US"connection lost"); + smtp_printf("421 %s lost input connection\r\n", smtp_active_hostname); + + /* Don't log by default unless in the middle of a message, as some mailers + just drop the call rather than sending QUIT, and it clutters up the logs. + */ + + if (sender_address != NULL || recipients_count > 0) + log_write(L_lost_incoming_connection, + LOG_MAIN, + "unexpected %s while reading SMTP command from %s%s", + sender_host_unknown? "EOF" : "disconnection", + host_and_ident(FALSE), smtp_read_error); + + else log_write(L_smtp_connection, LOG_MAIN, "%s lost%s", + smtp_get_connection_info(), smtp_read_error); + + done = 1; + break; + + + case ETRN_CMD: + if (sender_address != NULL) + { + done = synprot_error(L_smtp_protocol_error, 503, NULL, + US"ETRN is not permitted inside a transaction"); + break; + } + + log_write(L_etrn, LOG_MAIN, "ETRN %s received from %s", smtp_data, + host_and_ident(FALSE)); + + rc = acl_check(ACL_WHERE_ETRN, smtp_data, acl_smtp_etrn, &user_msg, + &log_msg); + if (rc != OK) + { + done = smtp_handle_acl_fail(ACL_WHERE_ETRN, rc, user_msg, log_msg); + break; + } + + /* Compute the serialization key for this command. */ + + etrn_serialize_key = string_sprintf("etrn-%s\n", smtp_data); + + /* If a command has been specified for running as a result of ETRN, we + permit any argument to ETRN. If not, only the # standard form is permitted, + since that is strictly the only kind of ETRN that can be implemented + according to the RFC. */ + + if (smtp_etrn_command != NULL) + { + uschar *error; + BOOL rc; + etrn_command = smtp_etrn_command; + deliver_domain = smtp_data; + rc = transport_set_up_command(&argv, smtp_etrn_command, TRUE, 0, NULL, + US"ETRN processing", &error); + deliver_domain = NULL; + if (!rc) + { + log_write(0, LOG_MAIN|LOG_PANIC, "failed to set up ETRN command: %s", + error); + smtp_printf("458 Internal failure\r\n"); + break; + } + } + + /* Else set up to call Exim with the -R option. */ + + else + { + if (*smtp_data++ != '#') + { + done = synprot_error(L_smtp_syntax_error, 501, NULL, + US"argument must begin with #"); + break; + } + etrn_command = US"exim -R"; + argv = child_exec_exim(CEE_RETURN_ARGV, TRUE, NULL, TRUE, 2, US"-R", + smtp_data); + } + + /* If we are host-testing, don't actually do anything. */ + + if (host_checking) + { + HDEBUG(D_any) + { + debug_printf("ETRN command is: %s\n", etrn_command); + debug_printf("ETRN command execution skipped\n"); + } + smtp_printf("250 OK\r\n"); + break; + } + + + /* If ETRN queue runs are to be serialized, check the database to + ensure one isn't already running. */ + + if (smtp_etrn_serialize && !enq_start(etrn_serialize_key)) + { + smtp_printf("458 Already processing %s\r\n", smtp_data); + break; + } + + /* Fork a child process and run the command. We don't want to have to + wait for the process at any point, so set SIGCHLD to SIG_IGN before + forking. It should be set that way anyway for external incoming SMTP, + but we save and restore to be tidy. If serialization is required, we + actually run the command in yet another process, so we can wait for it + to complete and then remove the serialization lock. */ + + oldsignal = signal(SIGCHLD, SIG_IGN); + + if ((pid = fork()) == 0) + { + smtp_input = FALSE; /* This process is not associated with the */ + fclose(smtp_in); /* SMTP call any more. */ + fclose(smtp_out); + + signal(SIGCHLD, SIG_DFL); /* Want to catch child */ + + /* If not serializing, do the exec right away. Otherwise, fork down + into another process. */ + + if (!smtp_etrn_serialize || (pid = fork()) == 0) + { + DEBUG(D_exec) debug_print_argv(argv); + exim_nullstd(); /* Ensure std{in,out,err} exist */ + execv(CS argv[0], (char *const *)argv); + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "exec of \"%s\" (ETRN) failed: %s", + etrn_command, strerror(errno)); + _exit(EXIT_FAILURE); /* paranoia */ + } + + /* Obey this if smtp_serialize and the 2nd fork yielded non-zero. That + is, we are in the first subprocess, after forking again. All we can do + for a failing fork is to log it. Otherwise, wait for the 2nd process to + complete, before removing the serialization. */ + + if (pid < 0) + log_write(0, LOG_MAIN|LOG_PANIC, "2nd fork for serialized ETRN " + "failed: %s", strerror(errno)); + else + { + int status; + DEBUG(D_any) debug_printf("waiting for serialized ETRN process %d\n", + (int)pid); + (void)wait(&status); + DEBUG(D_any) debug_printf("serialized ETRN process %d ended\n", + (int)pid); + } + + enq_end(etrn_serialize_key); + _exit(EXIT_SUCCESS); + } + + /* Back in the top level SMTP process. Check that we started a subprocess + and restore the signal state. */ + + if (pid < 0) + { + log_write(0, LOG_MAIN|LOG_PANIC, "fork of process for ETRN failed: %s", + strerror(errno)); + smtp_printf("458 Unable to fork process\r\n"); + if (smtp_etrn_serialize) enq_end(etrn_serialize_key); + } + else smtp_printf("250 OK\r\n"); + + signal(SIGCHLD, oldsignal); + break; + + + case BADARG_CMD: + done = synprot_error(L_smtp_syntax_error, 501, NULL, + US"unexpected argument data"); + break; + + + /* This currently happens only for NULLs, but could be extended. */ + + case BADCHAR_CMD: + done = synprot_error(L_smtp_syntax_error, 0, NULL, /* Just logs */ + US"NULL character(s) present (shown as '?')"); + smtp_printf("501 NULL characters are not allowed in SMTP commands\r\n"); + break; + + + case BADSYN_CMD: + if (smtp_inend >= smtp_inbuffer + in_buffer_size) + smtp_inend = smtp_inbuffer + in_buffer_size - 1; + c = smtp_inend - smtp_inptr; + if (c > 150) c = 150; + smtp_inptr[c] = 0; + incomplete_transaction_log(US"sync failure"); + log_write(0, LOG_MAIN|LOG_REJECT, "SMTP protocol violation: " + "synchronization error " + "(next input sent too soon: pipelining was%s advertised): " + "rejected \"%s\" %s next input=\"%s\"", + pipelining_advertised? "" : " not", + cmd_buffer, host_and_ident(TRUE), + string_printing(smtp_inptr)); + smtp_printf("554 SMTP synchronization error\r\n"); + done = 1; /* Pretend eof - drops connection */ + break; + + + case TOO_MANY_NONMAIL_CMD: + incomplete_transaction_log(US"too many non-mail commands"); + log_write(0, LOG_MAIN|LOG_REJECT, "SMTP call from %s dropped: too many " + "nonmail commands (last was \"%.*s\")", host_and_ident(FALSE), + smtp_data - cmd_buffer, cmd_buffer); + smtp_printf("554 Too many nonmail commands\r\n"); + done = 1; /* Pretend eof - drops connection */ + break; + + + default: + if (unknown_command_count++ >= smtp_max_unknown_commands) + { + log_write(L_smtp_syntax_error, LOG_MAIN, + "SMTP syntax error in \"%s\" %s %s", + string_printing(cmd_buffer), host_and_ident(TRUE), + US"unrecognized command"); + incomplete_transaction_log(US"unrecognized command"); + smtp_printf("500 Too many unrecognized commands\r\n"); + done = 2; + log_write(0, LOG_MAIN|LOG_REJECT, "SMTP call from %s dropped: too many " + "unrecognized commands (last was \"%s\")", host_and_ident(FALSE), + cmd_buffer); + } + else + done = synprot_error(L_smtp_syntax_error, 500, NULL, + US"unrecognized command"); + break; + } + + /* This label is used by goto's inside loops that want to break out to + the end of the command-processing loop. */ + + COMMAND_LOOP: + last_was_rej_mail = was_rej_mail; /* Remember some last commands for */ + last_was_rcpt = was_rcpt; /* protocol error handling */ + continue; + } + +return done - 2; /* Convert yield values */ +} + +/* End of smtp_in.c */ diff --git a/src/src/smtp_out.c b/src/src/smtp_out.c new file mode 100644 index 000000000..fc90c2511 --- /dev/null +++ b/src/src/smtp_out.c @@ -0,0 +1,527 @@ +/* $Cambridge: exim/src/src/smtp_out.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* A number of functions for driving outgoing SMTP calls. */ + + +#include "exim.h" + + + +/************************************************* +* Find an outgoing interface * +*************************************************/ + +/* This function is called from the smtp transport and also from the callout +code in verify.c. Its job is to expand a string to get a list of interfaces, +and choose a suitable one (IPv4 or IPv6) for the outgoing address. + +Arguments: + istring string interface setting, may be NULL, meaning "any", in + which case the function does nothing + host_af AF_INET or AF_INET6 for the outgoing IP address + addr the mail address being handled (for setting errors) + changed if not NULL, set TRUE if expansion actually changed istring + interface point this to the interface + msg to add to any error message + +Returns: TRUE on success, FALSE on failure, with error message + set in addr and transport_return set to PANIC +*/ + +BOOL +smtp_get_interface(uschar *istring, int host_af, address_item *addr, + BOOL *changed, uschar **interface, uschar *msg) +{ +uschar *expint; +uschar *iface; +int sep = 0; + +if (istring == NULL) return TRUE; + +expint = expand_string(istring); +if (expint == NULL) + { + if (expand_string_forcedfail) return TRUE; + addr->transport_return = PANIC; + addr->message = string_sprintf("failed to expand \"interface\" " + "option for %s: %s", msg, expand_string_message); + return FALSE; + } + +if (changed != NULL) *changed = expint != istring; + +while (isspace(*expint)) expint++; +if (*expint == 0) return TRUE; + +while ((iface = string_nextinlist(&expint, &sep, big_buffer, + big_buffer_size)) != NULL) + { + if (string_is_ip_address(iface, NULL) == 0) + { + addr->transport_return = PANIC; + addr->message = string_sprintf("\"%s\" is not a valid IP " + "address for the \"interface\" option for %s", + iface, msg); + return FALSE; + } + + if (((Ustrchr(iface, ':') == NULL)? AF_INET:AF_INET6) == host_af) + break; + } + +if (iface != NULL) *interface = string_copy(iface); +return TRUE; +} + + + +/************************************************* +* Find an outgoing port * +*************************************************/ + +/* This function is called from the smtp transport and also from the callout +code in verify.c. Its job is to find a port number. Note that getservbyname() +produces the number in network byte order. + +Arguments: + rstring raw (unexpanded) string representation of the port + addr the mail address being handled (for setting errors) + port stick the port in here + msg for adding to error message + +Returns: TRUE on success, FALSE on failure, with error message set + in addr, and transport_return set to PANIC +*/ + +BOOL +smtp_get_port(uschar *rstring, address_item *addr, int *port, uschar *msg) +{ +uschar *pstring = expand_string(rstring); + +if (pstring == NULL) + { + addr->transport_return = PANIC; + addr->message = string_sprintf("failed to expand \"%s\" (\"port\" option) " + "for %s: %s", rstring, msg, expand_string_message); + return FALSE; + } + +if (isdigit(*pstring)) + { + uschar *end; + *port = Ustrtol(pstring, &end, 0); + if (end != pstring + Ustrlen(pstring)) + { + addr->transport_return = PANIC; + addr->message = string_sprintf("invalid port number for %s: %s", msg, + pstring); + return FALSE; + } + } + +else + { + struct servent *smtp_service = getservbyname(CS pstring, "tcp"); + if (smtp_service == NULL) + { + addr->transport_return = PANIC; + addr->message = string_sprintf("TCP port \"%s\" is not defined for %s", + pstring, msg); + return FALSE; + } + *port = ntohs(smtp_service->s_port); + } + +return TRUE; +} + + + + +/************************************************* +* Connect to remote host * +*************************************************/ + +/* Create a socket, and connect it to a remote host. IPv6 addresses are +detected by checking for a colon in the address. AF_INET6 is defined even on +non-IPv6 systems, to enable the code to be less messy. However, on such systems +host->address will always be an IPv4 address. + +The port field in the host item is used if it is set (usually router from SRV +records). In other cases, the default passed as an argument is used. + +Arguments: + host host item containing name and address (and sometimes port) + host_af AF_INET or AF_INET6 + port default, remote port to connect to, in host byte order for those + hosts whose port setting is PORT_NONE + interface outgoing interface address or NULL + timeout timeout value or 0 + keepalive TRUE to use keepalive + +Returns: connected socket number, or -1 with errno set +*/ + +int +smtp_connect(host_item *host, int host_af, int port, uschar *interface, + int timeout, BOOL keepalive) +{ +int on = 1; +int save_errno = 0; +int sock; + +if (host->port != PORT_NONE) + { + HDEBUG(D_transport|D_acl|D_v) + debug_printf("Transport port=%d replaced by host-specific port=%d\n", port, + host->port); + port = host->port; + } + +HDEBUG(D_transport|D_acl|D_v) + { + if (interface == NULL) + debug_printf("Connecting to %s [%s]:%d ... ",host->name,host->address,port); + else + debug_printf("Connecting to %s [%s]:%d from %s ... ", host->name, + host->address, port, interface); + } + +/* Create the socket */ + +if ((sock = ip_socket(SOCK_STREAM, host_af)) < 0) return -1; + +/* Set TCP_NODELAY; Exim does its own buffering. */ + +setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (uschar *)(&on), sizeof(on)); + +/* Bind to a specific interface if requested. Caller must ensure the interface +is the same type (IPv4 or IPv6) as the outgoing address. */ + +if (interface != NULL && ip_bind(sock, host_af, interface, 0) < 0) + { + save_errno = errno; + HDEBUG(D_transport|D_acl|D_v) + debug_printf("unable to bind outgoing SMTP call to %s: %s", interface, + strerror(errno)); + } + +/* Connect to the remote host, and add keepalive to the socket before returning +it, if requested. */ + +else if (ip_connect(sock, host_af, host->address, port, timeout) < 0) + save_errno = errno; + +/* Either bind() or connect() failed */ + +if (save_errno != 0) + { + HDEBUG(D_transport|D_acl|D_v) debug_printf("failed\n"); + close(sock); + errno = save_errno; + return -1; + } + +/* Both bind() and connect() succeeded */ + +else + { + HDEBUG(D_transport|D_acl|D_v) debug_printf("connected\n"); + if (keepalive) ip_keepalive(sock, host->address, TRUE); + return sock; + } +} + + +/************************************************* +* Flush outgoing command buffer * +*************************************************/ + +/* This function is called only from smtp_write_command() below. It flushes +the buffer of outgoing commands. There is more than one in the buffer only when +pipelining. + +Argument: + outblock the SMTP output block + +Returns: TRUE if OK, FALSE on error, with errno set +*/ + +static BOOL +flush_buffer(smtp_outblock *outblock) +{ +int rc; + +#ifdef SUPPORT_TLS +if (tls_active == outblock->sock) + rc = tls_write(outblock->buffer, outblock->ptr - outblock->buffer); +else +#endif + +rc = send(outblock->sock, outblock->buffer, outblock->ptr - outblock->buffer, 0); +if (rc <= 0) + { + HDEBUG(D_transport|D_acl) debug_printf("send failed: %s\n", strerror(errno)); + return FALSE; + } + +outblock->ptr = outblock->buffer; +outblock->cmd_count = 0; +return TRUE; +} + + + +/************************************************* +* Write SMTP command * +*************************************************/ + +/* The formatted command is left in big_buffer so that it can be reflected in +any error message. + +Arguments: + outblock contains buffer for pipelining, and socket + noflush if TRUE, save the command in the output buffer, for pipelining + format a format, starting with one of + of HELO, MAIL FROM, RCPT TO, DATA, ".", or QUIT. + ... data for the format + +Returns: 0 if command added to pipelining buffer, with nothing transmitted + +n if n commands transmitted (may still have buffered the new one) + -1 on error, with errno set +*/ + +int +smtp_write_command(smtp_outblock *outblock, BOOL noflush, char *format, ...) +{ +int count; +int rc = 0; +va_list ap; + +va_start(ap, format); +if (!string_vformat(big_buffer, big_buffer_size, CS format, ap)) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "overlong write_command in outgoing " + "SMTP"); +va_end(ap); +count = Ustrlen(big_buffer); + +if (count > outblock->buffersize - (outblock->ptr - outblock->buffer)) + { + rc = outblock->cmd_count; /* flush resets */ + if (!flush_buffer(outblock)) return -1; + } + +Ustrncpy(CS outblock->ptr, big_buffer, count); +outblock->ptr += count; +outblock->cmd_count++; +count -= 2; +big_buffer[count] = 0; /* remove \r\n for error message */ + +/* We want to hide the actual data sent in AUTH transactions from reflections +and logs. While authenticating, a flag is set in the outblock to enable this. +The AUTH command itself gets any data flattened. Other lines are flattened +completely. */ + +if (outblock->authenticating) + { + uschar *p = big_buffer; + if (Ustrncmp(big_buffer, "AUTH ", 5) == 0) + { + p += 5; + while (isspace(*p)) p++; + while (!isspace(*p)) p++; + while (isspace(*p)) p++; + } + while (*p != 0) *p++ = '*'; + } + +HDEBUG(D_transport|D_acl|D_v) debug_printf(" SMTP>> %s\n", big_buffer); + +if (!noflush) + { + rc += outblock->cmd_count; /* flush resets */ + if (!flush_buffer(outblock)) return -1; + } + +return rc; +} + + + +/************************************************* +* Read one line of SMTP response * +*************************************************/ + +/* This function reads one line of SMTP response from the server host. This may +not be a complete response - it could be just part of a multiline response. We +have to use a buffer for incoming packets, because when pipelining or using +LMTP, there may well be more than one response in a single packet. This +function is called only from the one that follows. + +Arguments: + inblock the SMTP input block (contains holding buffer, socket, etc.) + buffer where to put the line + size space available for the line + timeout the timeout to use when reading a packet + +Returns: length of a line that has been put in the buffer + -1 otherwise, with errno set +*/ + +static int +read_response_line(smtp_inblock *inblock, uschar *buffer, int size, int timeout) +{ +uschar *p = buffer; +uschar *ptr = inblock->ptr; +uschar *ptrend = inblock->ptrend; +int sock = inblock->sock; + +/* Loop for reading multiple packets or reading another packet after emptying +a previously-read one. */ + +for (;;) + { + int rc; + + /* If there is data in the input buffer left over from last time, copy + characters from it until the end of a line, at which point we can return, + having removed any whitespace (which will include CR) at the end of the line. + The rules for SMTP say that lines end in CRLF, but there are have been cases + of hosts using just LF, and other MTAs are reported to handle this, so we + just look for LF. If we run out of characters before the end of a line, + carry on to read the next incoming packet. */ + + while (ptr < ptrend) + { + int c = *ptr++; + if (c == '\n') + { + while (p > buffer && isspace(p[-1])) p--; + *p = 0; + inblock->ptr = ptr; + return p - buffer; + } + *p++ = c; + if (--size < 4) + { + *p = 0; /* Leave malformed line for error message */ + errno = ERRNO_SMTPFORMAT; + return -1; + } + } + + /* Need to read a new input packet. */ + + rc = ip_recv(sock, inblock->buffer, inblock->buffersize, timeout); + if (rc <= 0) break; + + /* Another block of data has been successfully read. Set up the pointers + and let the loop continue. */ + + ptrend = inblock->ptrend = inblock->buffer + rc; + ptr = inblock->buffer; + DEBUG(D_transport|D_acl) debug_printf("read response data: size=%d\n", rc); + } + +/* Get here if there has been some kind of recv() error; errno is set, but we +ensure that the result buffer is empty before returning. */ + +*buffer = 0; +return -1; +} + + + + + +/************************************************* +* Read SMTP response * +*************************************************/ + +/* This function reads an SMTP response with a timeout, and returns the +response in the given buffer, as a string. A multiline response will contain +newline characters between the lines. The function also analyzes the first +digit of the reply code and returns FALSE if it is not acceptable. FALSE is +also returned after a reading error. In this case buffer[0] will be zero, and +the error code will be in errno. + +Arguments: + inblock the SMTP input block (contains holding buffer, socket, etc.) + buffer where to put the response + size the size of the buffer + okdigit the expected first digit of the response + timeout the timeout to use + +Returns: TRUE if a valid, non-error response was received; else FALSE +*/ + +BOOL +smtp_read_response(smtp_inblock *inblock, uschar *buffer, int size, int okdigit, + int timeout) +{ +uschar *ptr = buffer; +int count; + +errno = 0; /* Ensure errno starts out zero */ + +/* This is a loop to read and concatentate the lines that make up a multi-line +response. */ + +for (;;) + { + if ((count = read_response_line(inblock, ptr, size, timeout)) < 0) + return FALSE; + + HDEBUG(D_transport|D_acl|D_v) + debug_printf(" %s %s\n", (ptr == buffer)? "SMTP<<" : " ", ptr); + + /* Check the format of the response: it must start with three digits; if + these are followed by a space or end of line, the response is complete. If + they are followed by '-' this is a multi-line response and we must look for + another line until the final line is reached. The only use made of multi-line + responses is to pass them back as error messages. We therefore just + concatenate them all within the buffer, which should be large enough to + accept any reasonable number of lines. */ + + if (count < 3 || + !isdigit(ptr[0]) || + !isdigit(ptr[1]) || + !isdigit(ptr[2]) || + (ptr[3] != '-' && ptr[3] != ' ' && ptr[3] != 0)) + { + errno = ERRNO_SMTPFORMAT; /* format error */ + return FALSE; + } + + /* If the line we have just read is a terminal line, line, we are done. + Otherwise more data has to be read. */ + + if (ptr[3] != '-') break; + + /* Move the reading pointer upwards in the buffer and insert \n between the + components of a multiline response. Space is left for this by read_response_ + line(). */ + + ptr += count; + *ptr++ = '\n'; + size -= count + 1; + } + +/* Return a value that depends on the SMTP return code. On some systems a +non-zero value of errno has been seen at this point, so ensure it is zero, +because the caller of this function looks at errno when FALSE is returned, to +distinguish between an unexpected return code and other errors such as +timeouts, lost connections, etc. */ + +errno = 0; +return buffer[0] == okdigit; +} + +/* End of smtp_out.c */ diff --git a/src/src/spool_in.c b/src/src/spool_in.c new file mode 100644 index 000000000..f43556bfa --- /dev/null +++ b/src/src/spool_in.c @@ -0,0 +1,684 @@ +/* $Cambridge: exim/src/src/spool_in.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Functions for reading spool files. When compiling for a utility (eximon), +not all are needed, and some functionality can be cut out. */ + + +#include "exim.h" + + + +#ifndef COMPILE_UTILITY +/************************************************* +* Open and lock data file * +*************************************************/ + +/* The data file is the one that is used for locking, because the header file +can get replaced during delivery because of header rewriting. The file has +to opened with write access so that we can get an exclusive lock, but in +fact it won't be written to. Just in case there's a major disaster (e.g. +overwriting some other file descriptor with the value of this one), open it +with append. + +Argument: the id of the message +Returns: TRUE if file successfully opened and locked + +Side effect: deliver_datafile is set to the fd of the open file. +*/ + +BOOL +spool_open_datafile(uschar *id) +{ +int i; +struct stat statbuf; +flock_t lock_data; +uschar spoolname[256]; + +/* If split_spool_directory is set, first look for the file in the appropriate +sub-directory of the input directory. If it is not found there, try the input +directory itself, to pick up leftovers from before the splitting. If split_ +spool_directory is not set, first look in the main input directory. If it is +not found there, try the split sub-directory, in case it is left over from a +splitting state. */ + +for (i = 0; i < 2; i++) + { + int save_errno; + message_subdir[0] = (split_spool_directory == (i == 0))? id[5] : 0; + sprintf(CS spoolname, "%s/input/%s/%s-D", spool_directory, message_subdir, id); + deliver_datafile = Uopen(spoolname, O_RDWR | O_APPEND, 0); + if (deliver_datafile >= 0) break; + save_errno = errno; + if (errno == ENOENT) + { + if (i == 0) continue; + if (!queue_running) + log_write(0, LOG_MAIN, "Spool file %s-D not found", id); + } + else log_write(0, LOG_MAIN, "Spool error for %s: %s", spoolname, + strerror(errno)); + errno = save_errno; + return FALSE; + } + +/* File is open and message_subdir is set. Set the close-on-exec flag, and lock +the file. We lock only the first line of the file (containing the message ID) +because this apparently is needed for running Exim under Cygwin. If the entire +file is locked in one process, a sub-process cannot access it, even when passed +an open file descriptor (at least, I think that's the Cygwin story). On real +Unix systems it doesn't make any difference as long as Exim is consistent in +what it locks. */ + +fcntl(deliver_datafile, F_SETFD, fcntl(deliver_datafile, F_GETFD) | + FD_CLOEXEC); + +lock_data.l_type = F_WRLCK; +lock_data.l_whence = SEEK_SET; +lock_data.l_start = 0; +lock_data.l_len = SPOOL_DATA_START_OFFSET; + +if (fcntl(deliver_datafile, F_SETLK, &lock_data) < 0) + { + log_write(L_skip_delivery, + LOG_MAIN, + "Spool file is locked (another process is handling this message)"); + close(deliver_datafile); + deliver_datafile = -1; + errno = 0; + return FALSE; + } + +/* Get the size of the data; don't include the leading filename line +in the count, but add one for the newline before the data. */ + +if (fstat(deliver_datafile, &statbuf) == 0) + { + message_body_size = statbuf.st_size - SPOOL_DATA_START_OFFSET; + message_size = message_body_size + 1; + } + +return TRUE; +} +#endif /* COMPILE_UTILITY */ + + + +/************************************************* +* Read non-recipients tree from spool file * +*************************************************/ + +/* The tree of non-recipients is written to the spool file in a form that +makes it easy to read back into a tree. The format is as follows: + + . Each node is preceded by two letter(Y/N) indicating whether it has left + or right children. There's one space after the two flags, before the name. + + . The left subtree (if any) then follows, then the right subtree (if any). + +This function is entered with the next input line in the buffer. Note we must +save the right flag before recursing with the same buffer. + +Once the tree is read, we re-construct the balance fields by scanning the tree. +I forgot to write them out originally, and the compatible fix is to do it this +way. This initial local recursing function does the necessary. + +Arguments: + node tree node + +Returns: maximum depth below the node, including the node itself +*/ + +static int +count_below(tree_node *node) +{ +int nleft, nright; +if (node == NULL) return 0; +nleft = count_below(node->left); +nright = count_below(node->right); +node->balance = (nleft > nright)? 1 : ((nright > nleft)? 2 : 0); +return 1 + ((nleft > nright)? nleft : nright); +} + +/* This is the real function... + +Arguments: + connect pointer to the root of the tree + f FILE to read data from + buffer contains next input line; further lines read into it + buffer_size size of the buffer + +Returns: FALSE on format error +*/ + +static BOOL +read_nonrecipients_tree(tree_node **connect, FILE *f, uschar *buffer, + int buffer_size) +{ +tree_node *node; +int n = Ustrlen(buffer); +BOOL right = buffer[1] == 'Y'; + +if (n < 5) return FALSE; /* malformed line */ +buffer[n-1] = 0; /* Remove \n */ +node = store_get(sizeof(tree_node) + n - 3); +*connect = node; +Ustrcpy(node->name, buffer + 3); +node->data.ptr = NULL; + +if (buffer[0] == 'Y') + { + if (Ufgets(buffer, buffer_size, f) == NULL || + !read_nonrecipients_tree(&node->left, f, buffer, buffer_size)) + return FALSE; + } +else node->left = NULL; + +if (right) + { + if (Ufgets(buffer, buffer_size, f) == NULL || + !read_nonrecipients_tree(&node->right, f, buffer, buffer_size)) + return FALSE; + } +else node->right = NULL; + +(void) count_below(*connect); +return TRUE; +} + + + + +/************************************************* +* Read spool header file * +*************************************************/ + +/* This function reads a spool header file and places the data into the +appropriate global variables. The header portion is always read, but header +structures are built only if read_headers is set true. It isn't, for example, +while generating -bp output. + +It may be possible for blocks of nulls (binary zeroes) to get written on the +end of a file if there is a system crash during writing. It was observed on an +earlier version of Exim that omitted to fsync() the files - this is thought to +have been the cause of that incident, but in any case, this code must be robust +against such an event, and if such a file is encountered, it must be treated as +malformed. + +Arguments: + name name of the header file, including the -H + read_headers TRUE if in-store header structures are to be built + subdir_set TRUE is message_subdir is already set + +Returns: spool_read_OK success + spool_read_notopen open failed + spool_read_enverror error in the envelope portion + spool_read_hdrdrror error in the header portion +*/ + +int +spool_read_header(uschar *name, BOOL read_headers, BOOL subdir_set) +{ +FILE *f = NULL; +int n; +int rcount = 0; +long int uid, gid; +BOOL inheader = FALSE; +uschar originator[64]; + +/* Reset all the global variables to their default values. However, there is +one exception. DO NOT change the default value of dont_deliver, because it may +be forced by an external setting. */ + +for (n = 0; n < ACL_C_MAX + ACL_M_MAX; n++) acl_var[n] = NULL; + +authenticated_id = NULL; +authenticated_sender = NULL; +allow_unqualified_recipient = FALSE; +allow_unqualified_sender = FALSE; +body_linecount = 0; +body_zerocount = 0; +deliver_firsttime = FALSE; +deliver_freeze = FALSE; +deliver_frozen_at = 0; +deliver_manual_thaw = FALSE; +/* dont_deliver must NOT be reset */ +header_list = header_last = NULL; +host_lookup_failed = FALSE; +interface_address = NULL; +interface_port = 0; +local_error_message = FALSE; +local_scan_data = NULL; +message_linecount = 0; +received_protocol = NULL; +received_count = 0; +recipients_list = NULL; +sender_address = NULL; +sender_fullhost = NULL; +sender_helo_name = NULL; +sender_host_address = NULL; +sender_host_name = NULL; +sender_host_port = 0; +sender_host_authenticated = NULL; +sender_ident = NULL; +sender_local = FALSE; +sender_set_untrusted = FALSE; +tree_nonrecipients = NULL; + +#ifdef SUPPORT_TLS +tls_certificate_verified = FALSE; +tls_cipher = NULL; +tls_peerdn = NULL; +#endif + +/* Generate the full name and open the file. If message_subdir is already +set, just look in the given directory. Otherwise, look in both the split +and unsplit directories, as for the data file above. */ + +for (n = 0; n < 2; n++) + { + if (!subdir_set) + message_subdir[0] = (split_spool_directory == (n == 0))? name[5] : 0; + sprintf(CS big_buffer, "%s/input/%s/%s", spool_directory, message_subdir, + name); + f = Ufopen(big_buffer, "rb"); + if (f != NULL) break; + if (n != 0 || subdir_set || errno != ENOENT) return spool_read_notopen; + } + +errno = 0; + +#ifndef COMPILE_UTILITY +DEBUG(D_deliver) debug_printf("reading spool file %s\n", name); +#endif /* COMPILE_UTILITY */ + +/* The first line of a spool file contains the message id followed by -H (i.e. +the file name), in order to make the file self-identifying. */ + +if (Ufgets(big_buffer, big_buffer_size, f) == NULL) goto SPOOL_READ_ERROR; +if (Ustrlen(big_buffer) != MESSAGE_ID_LENGTH + 3 || + Ustrncmp(big_buffer, name, MESSAGE_ID_LENGTH + 2) != 0) + goto SPOOL_FORMAT_ERROR; + +/* The next three lines in the header file are in a fixed format. The first +contains the login, uid, and gid of the user who caused the file to be written. +The second contains the mail address of the message's sender, enclosed in <>. +The third contains the time the message was received, and the number of warning +messages for delivery delays that have been sent. */ + +if (Ufgets(big_buffer, big_buffer_size, f) == NULL) goto SPOOL_READ_ERROR; + +if (sscanf(CS big_buffer, "%s %ld %ld", originator, &uid, &gid) != 3) + goto SPOOL_FORMAT_ERROR; +originator_login = string_copy(originator); +originator_uid = (uid_t)uid; +originator_gid = (gid_t)gid; + +if (Ufgets(big_buffer, big_buffer_size, f) == NULL) goto SPOOL_READ_ERROR; +n = Ustrlen(big_buffer); +if (n < 3 || big_buffer[0] != '<' || big_buffer[n-2] != '>') + goto SPOOL_FORMAT_ERROR; + +sender_address = store_get(n-2); +Ustrncpy(sender_address, big_buffer+1, n-3); +sender_address[n-3] = 0; + +if (Ufgets(big_buffer, big_buffer_size, f) == NULL) goto SPOOL_READ_ERROR; +if (sscanf(CS big_buffer, "%d %d", &received_time, &warning_count) != 2) + goto SPOOL_FORMAT_ERROR; + +message_age = time(NULL) - received_time; + +#ifndef COMPILE_UTILITY +DEBUG(D_deliver) debug_printf("user=%s uid=%ld gid=%ld sender=%s\n", + originator_login, (long int)originator_uid, (long int)originator_gid, + sender_address); +#endif /* COMPILE_UTILITY */ + +/* Now there may be a number of optional lines, each starting with "-". +If you add a new setting here, make sure you set the default above. */ + +for (;;) + { + if (Ufgets(big_buffer, big_buffer_size, f) == NULL) goto SPOOL_READ_ERROR; + if (big_buffer[0] != '-') break; + + big_buffer[Ustrlen(big_buffer) - 1] = 0; + if (Ustrncmp(big_buffer, "-acl ", 5) == 0) + { + int index, count; + if (sscanf(CS big_buffer + 5, "%d %d", &index, &count) != 2) + goto SPOOL_FORMAT_ERROR; + /* Ignore if index too big - might be if a later release with more + variables built this spool file. */ + if (index < ACL_C_MAX + ACL_M_MAX) + { + acl_var[index] = store_get(count + 1); + if (fread(acl_var[index], 1, count+1, f) < count) goto SPOOL_READ_ERROR; + acl_var[index][count] = 0; + } + } + else if (Ustrcmp(big_buffer, "-local") == 0) sender_local = TRUE; + else if (Ustrcmp(big_buffer, "-localerror") == 0) + local_error_message = TRUE; + else if (Ustrncmp(big_buffer, "-local_scan ", 12) == 0) + local_scan_data = string_copy(big_buffer + 12); + else if (Ustrcmp(big_buffer, "-host_lookup_failed") == 0) + host_lookup_failed = TRUE; + else if (Ustrncmp(big_buffer, "-body_linecount", 15) == 0) + body_linecount = Uatoi(big_buffer + 15); + else if (Ustrncmp(big_buffer, "-body_zerocount", 15) == 0) + body_zerocount = Uatoi(big_buffer + 15); + else if (Ustrncmp(big_buffer, "-frozen", 7) == 0) + { + deliver_freeze = TRUE; + deliver_frozen_at = Uatoi(big_buffer + 7); + } + else if (Ustrcmp(big_buffer, "-allow_unqualified_recipient") == 0) + allow_unqualified_recipient = TRUE; + else if (Ustrcmp(big_buffer, "-allow_unqualified_sender") == 0) + allow_unqualified_sender = TRUE; + else if (Ustrcmp(big_buffer, "-deliver_firsttime") == 0) + deliver_firsttime = TRUE; + else if (Ustrcmp(big_buffer, "-manual_thaw") == 0) + deliver_manual_thaw = TRUE; + else if (Ustrncmp(big_buffer, "-auth_id", 8) == 0) + authenticated_id = string_copy(big_buffer + 9); + else if (Ustrncmp(big_buffer, "-auth_sender", 12) == 0) + authenticated_sender = string_copy(big_buffer + 13); + else if (Ustrncmp(big_buffer, "-sender_set_untrusted", 21) == 0) + sender_set_untrusted = TRUE; + + #ifdef SUPPORT_TLS + else if (Ustrncmp(big_buffer, "-tls_certificate_verified", 25) == 0) + tls_certificate_verified = TRUE; + else if (Ustrncmp(big_buffer, "-tls_cipher", 11) == 0) + tls_cipher = string_copy(big_buffer + 12); + else if (Ustrncmp(big_buffer, "-tls_peerdn", 11) == 0) + tls_peerdn = string_copy(big_buffer + 12); + #endif + + /* We now record the port number after the address, separated by a + dot. For compatibility during upgrading, do nothing if there + isn't a value (it gets left at zero). */ + + else if (Ustrncmp(big_buffer, "-host_address", 13) == 0) + { + sender_host_port = host_extract_port(big_buffer + 14); + sender_host_address = string_copy(big_buffer + 14); + } + + else if (Ustrncmp(big_buffer, "-interface_address", 18) == 0) + { + interface_port = host_extract_port(big_buffer + 19); + interface_address = string_copy(big_buffer + 19); + } + + else if (Ustrncmp(big_buffer, "-host_auth", 10) == 0) + sender_host_authenticated = string_copy(big_buffer + 11); + else if (Ustrncmp(big_buffer, "-host_name", 10) == 0) + sender_host_name = string_copy(big_buffer + 11); + else if (Ustrncmp(big_buffer, "-helo_name", 10) == 0) + sender_helo_name = string_copy(big_buffer + 11); + else if (Ustrncmp(big_buffer, "-ident", 6) == 0) + sender_ident = string_copy(big_buffer + 7); + else if (Ustrncmp(big_buffer, "-received_protocol", 18) == 0) + received_protocol = string_copy(big_buffer + 19); + else if (Ustrncmp(big_buffer, "-N", 2) == 0) + dont_deliver = TRUE; + + /* To allow new versions of Exim that add additional flags to interwork + with older versions that do not understand them, just ignore any flagged + lines that we don't recognize. Otherwise it wouldn't be possible to back + off a new version that left new-style flags written on the spool. That's + why the following line is commented out. */ + + /* else goto SPOOL_FORMAT_ERROR; */ + } + +/* Build sender_fullhost if required */ + +#ifndef COMPILE_UTILITY +host_build_sender_fullhost(); +#endif /* COMPILE_UTILITY */ + +#ifndef COMPILE_UTILITY +DEBUG(D_deliver) + debug_printf("sender_local=%d ident=%s\n", sender_local, + (sender_ident == NULL)? US"unset" : sender_ident); +#endif /* COMPILE_UTILITY */ + +/* We now have the tree of addresses NOT to deliver to, or a line +containing "XX", indicating no tree. */ + +if (Ustrncmp(big_buffer, "XX\n", 3) != 0 && + !read_nonrecipients_tree(&tree_nonrecipients, f, big_buffer, big_buffer_size)) + goto SPOOL_FORMAT_ERROR; + +#ifndef COMPILE_UTILITY +DEBUG(D_deliver) + { + debug_printf("Non-recipients:\n"); + debug_print_tree(tree_nonrecipients); + } +#endif /* COMPILE_UTILITY */ + +/* After reading the tree, the next line has not yet been read into the +buffer. It contains the count of recipients which follow on separate lines. */ + +if (Ufgets(big_buffer, big_buffer_size, f) == NULL) goto SPOOL_READ_ERROR; +if (sscanf(CS big_buffer, "%d", &rcount) != 1) goto SPOOL_FORMAT_ERROR; + +#ifndef COMPILE_UTILITY +DEBUG(D_deliver) debug_printf("recipients_count=%d\n", rcount); +#endif /* COMPILE_UTILITY */ + +recipients_list_max = rcount; +recipients_list = store_get(rcount * sizeof(recipient_item)); + +for (recipients_count = 0; recipients_count < rcount; recipients_count++) + { + int nn; + int pno = -1; + uschar *errors_to = NULL; + uschar *p; + + if (Ufgets(big_buffer, big_buffer_size, f) == NULL) goto SPOOL_READ_ERROR; + nn = Ustrlen(big_buffer); + if (nn < 2) goto SPOOL_FORMAT_ERROR; + + /* Remove the newline; this terminates the address if there is no additional + data on the line. */ + + p = big_buffer + nn - 1; + *p-- = 0; + + /* Look back from the end of the line for digits and special terminators. + Since an address must end with a domain, we can tell that extra data is + present by the presence of the terminator, which is always some character + that cannot exist in a domain. (If I'd thought of the need for additional + data early on, I'd have put it at the start, with the address at the end. As + it is, we have to operate backwards. Addresses are permitted to contain + spaces, you see.) + + This code has to cope with various versions of this data that have evolved + over time. In all cases, the line might just contain an address, with no + additional data. Otherwise, the possibilities are as follows: + + Exim 3 type:
    ,, + + The second set of digits is the parent number for one_time addresses. The + other values were remnants of earlier experiments that were abandoned. + + Exim 4 first type:
    + + The digits are the parent number for one_time addresses. + + Exim 4 new type:
    # + + The type bits indicate what the contents of the data are. + + Bit 01 indicates that, reading from right to left, the data + ends with , where pno is + the parent number for one_time addresses, and len is the length + of the errors_to address (zero meaning none). + */ + + while (isdigit(*p)) p--; + + /* Handle Exim 3 spool files */ + + if (*p == ',') + { + int dummy; + while (isdigit(*(--p)) || *p == ','); + if (*p == ' ') + { + *p++ = 0; + sscanf(CS p, "%d,%d", &dummy, &pno); + } + } + + /* Handle early Exim 4 spool files */ + + else if (*p == ' ') + { + *p++ = 0; + sscanf(CS p, "%d", &pno); + } + + /* Handle current format Exim 4 spool files */ + + else if (*p == '#') + { + int flags; + sscanf(CS p+1, "%d", &flags); + + if ((flags & 0x01) != 0) /* one_time data exists */ + { + int len; + while (isdigit(*(--p)) || *p == ',' || *p == '-'); + sscanf(CS p+1, "%d,%d", &len, &pno); + *p = 0; + if (len > 0) + { + p -= len; + errors_to = string_copy(p); + } + } + + *(--p) = 0; /* Terminate address */ + } + + recipients_list[recipients_count].address = string_copy(big_buffer); + recipients_list[recipients_count].pno = pno; + recipients_list[recipients_count].errors_to = errors_to; + } + +/* The remainder of the spool header file contains the headers for the message, +separated off from the previous data by a blank line. Each header is preceded +by a count of its length and either a certain letter (for various identified +headers), space (for a miscellaneous live header) or an asterisk (for a header +that has been rewritten). Count the Received: headers. We read the headers +always, in order to check on the format of the file, but only create a header +list if requested to do so. */ + +inheader = TRUE; +if (Ufgets(big_buffer, big_buffer_size, f) == NULL) goto SPOOL_READ_ERROR; +if (big_buffer[0] != '\n') goto SPOOL_FORMAT_ERROR; + +while ((n = fgetc(f)) != EOF) + { + header_line *h; + uschar flag[4]; + int i; + + if (!isdigit(n)) goto SPOOL_FORMAT_ERROR; + ungetc(n, f); + fscanf(f, "%d%c ", &n, flag); + if (flag[0] != '*') message_size += n; /* Omit non-transmitted headers */ + + if (read_headers) + { + h = store_get(sizeof(header_line)); + h->next = NULL; + h->type = flag[0]; + h->slen = n; + h->text = store_get(n+1); + + if (h->type == htype_received) received_count++; + + if (header_list == NULL) header_list = h; + else header_last->next = h; + header_last = h; + + for (i = 0; i < n; i++) + { + int c = fgetc(f); + if (c == 0 || c == EOF) goto SPOOL_FORMAT_ERROR; + if (c == '\n' && h->type != htype_old) message_linecount++; + h->text[i] = c; + } + h->text[i] = 0; + } + + /* Not requiring header data, just skip through the bytes */ + + else for (i = 0; i < n; i++) + { + int c = fgetc(f); + if (c == 0 || c == EOF) goto SPOOL_FORMAT_ERROR; + } + } + +/* We have successfully read the data in the header file. Update the message +line count by adding the body linecount to the header linecount. Close the file +and give a positive response. */ + +#ifndef COMPILE_UTILITY +DEBUG(D_deliver) debug_printf("body_linecount=%d message_linecount=%d\n", + body_linecount, message_linecount); +#endif /* COMPILE_UTILITY */ + +message_linecount += body_linecount; + +fclose(f); +return spool_read_OK; + + +/* There was an error reading the spool or there was missing data, +or there was a format error. A "read error" with no errno means an +unexpected EOF, which we treat as a format error. */ + +SPOOL_READ_ERROR: +if (errno != 0) + { + n = errno; + + #ifndef COMPILE_UTILITY + DEBUG(D_any) debug_printf("Error while reading spool file %s\n", name); + #endif /* COMPILE_UTILITY */ + + fclose(f); + errno = n; + return inheader? spool_read_hdrerror : spool_read_enverror; + } + +SPOOL_FORMAT_ERROR: + +#ifndef COMPILE_UTILITY +DEBUG(D_any) debug_printf("Format error in spool file %s\n", name); +#endif /* COMPILE_UTILITY */ + +fclose(f); +errno = ERRNO_SPOOLFORMAT; +return inheader? spool_read_hdrerror : spool_read_enverror; +} + +/* End of spool_in.c */ diff --git a/src/src/spool_out.c b/src/src/spool_out.c new file mode 100644 index 000000000..af36f4169 --- /dev/null +++ b/src/src/spool_out.c @@ -0,0 +1,473 @@ +/* $Cambridge: exim/src/src/spool_out.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Functions for writing spool files, and moving them about. */ + + +#include "exim.h" + + + +/************************************************* +* Deal with header writing errors * +*************************************************/ + +/* This function is called immediately after errors in writing the spool, with +errno still set. It creates and error message, depending on the circumstances. +If errmsg is NULL, it logs the message and panic-dies. Otherwise errmsg is set +to point to the message, and -1 is returned. This function makes the code of +spool_write_header() a bit neater. + +Arguments: + where SW_RECEIVING, SW_DELIVERING, or SW_MODIFYING + errmsg where to put the message; NULL => panic-die + s text to add to log string + temp_name name of temp file to unlink + f FILE to close, if not NULL + +Returns: -1 if errmsg is not NULL; otherwise doesn't return +*/ + +static int +spool_write_error(int where, uschar **errmsg, uschar *s, uschar *temp_name, + FILE *f) +{ +uschar *msg = (where == SW_RECEIVING)? + string_sprintf("spool file %s error while receiving from %s: %s", s, + (sender_fullhost != NULL)? sender_fullhost : sender_ident, + strerror(errno)) + : + string_sprintf("spool file %s error while %s: %s", s, + (where == SW_DELIVERING)? "delivering" : "modifying", + strerror(errno)); + +if (temp_name != NULL) Uunlink(temp_name); +if (f != NULL) fclose(f); + +if (errmsg == NULL) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s", msg); +else + *errmsg = msg; + +return -1; +} + + + +/************************************************* +* Open file under temporary name * +*************************************************/ + +/* This is used for opening spool files under a temporary name, +with a single attempt at deleting if they already exist. + +Argument: temporary name for spool header file +Returns: file descriptor of open file, or < 0 on failure, with errno unchanged +*/ + +int +spool_open_temp(uschar *temp_name) +{ +int fd = Uopen(temp_name, O_RDWR|O_CREAT|O_EXCL, SPOOL_MODE); + +/* If the file already exists, something has gone wrong. This process may well +have previously created the file if it is delivering more than one address, but +it should have renamed it almost immediately. A file could, however, be left +around as a result of a system crash, and by coincidence this process might +have the same pid. We therefore have one go at unlinking it before giving up. +*/ + +if (fd < 0 && errno == EEXIST) + { + DEBUG(D_any) debug_printf("%s exists: unlinking\n", temp_name); + Uunlink(temp_name); + fd = Uopen(temp_name, O_RDWR|O_CREAT|O_EXCL, SPOOL_MODE); + } + +/* If the file has been opened, make sure the file's group is the Exim gid, and +double-check the mode because the group setting doesn't always get set +automatically. */ + +if (fd >= 0) + { + fchown(fd, exim_uid, exim_gid); + fchmod(fd, SPOOL_MODE); + } + +return fd; +} + + + +/************************************************* +* Write the header spool file * +*************************************************/ + +/* Returns the size of the file for success; zero for failure. The file is +written under a temporary name, and then renamed. It's done this way so that it +works with re-writing the file on message deferral as well as for the initial +write. Whenever this function is called, the data file for the message should +be open and locked, thus preventing any other exim process from working on this +message. + +Argument: + id the message id + where SW_RECEIVING, SW_DELIVERING, or SW_MODIFYING + errmsg where to put an error message; if NULL, panic-die on error + +Returns: the size of the header texts on success; + negative on writing failure, unless errmsg == NULL +*/ + +int +spool_write_header(uschar *id, int where, uschar **errmsg) +{ +int fd; +int i; +int size_correction; +FILE *f; +header_line *h; +struct stat statbuf; +uschar name[256]; +uschar temp_name[256]; + +sprintf(CS temp_name, "%s/input/%s/hdr.%d", spool_directory, message_subdir, + (int)getpid()); +fd = spool_open_temp(temp_name); +if (fd < 0) return spool_write_error(where, errmsg, US"open", NULL, NULL); +f = fdopen(fd, "wb"); +DEBUG(D_receive|D_deliver) debug_printf("Writing spool header file\n"); + +/* We now have an open file to which the header data is to be written. Start +with the file's leaf name, to make the file self-identifying. Continue with the +identity of the submitting user, followed by the sender's address. The sender's +address is enclosed in <> because it might be the null address. Then write the +received time and the number of warning messages that have been sent. */ + +fprintf(f, "%s-H\n", message_id); +fprintf(f, "%.63s %ld %ld\n", originator_login, (long int)originator_uid, + (long int)originator_gid); +fprintf(f, "<%s>\n", sender_address); +fprintf(f, "%d %d\n", received_time, warning_count); + +/* If there is information about a sending host, remember it. The HELO +data can be set for local SMTP as well as remote. */ + +if (sender_helo_name != NULL) + fprintf(f, "-helo_name %s\n", sender_helo_name); + +if (sender_host_address != NULL) + { + fprintf(f, "-host_address %s.%d\n", sender_host_address, sender_host_port); + if (sender_host_name != NULL) + fprintf(f, "-host_name %s\n", sender_host_name); + if (sender_host_authenticated != NULL) + fprintf(f, "-host_auth %s\n", sender_host_authenticated); + } + +/* Also about the interface a message came in on */ + +if (interface_address != NULL) + fprintf(f, "-interface_address %s.%d\n", interface_address, interface_port); + +/* Likewise for any ident information; for local messages this is +likely to be the same as originator_login, but will be different if +the originator was root, forcing a different ident. */ + +if (sender_ident != NULL) fprintf(f, "-ident %s\n", sender_ident); + +/* Ditto for the received protocol */ + +if (received_protocol != NULL) + fprintf(f, "-received_protocol %s\n", received_protocol); + +/* Preserve any ACL variables that are set. Because the values may contain +newlines, we use an explicit length. */ + +for (i = 0; i < ACL_C_MAX + ACL_M_MAX; i++) + { + if (acl_var[i] != NULL) + fprintf(f, "-acl %d %d\n%s\n", i, Ustrlen(acl_var[i]), acl_var[i]); + } + +/* Now any other data that needs to be remembered. */ + +fprintf(f, "-body_linecount %d\n", body_linecount); + +if (body_zerocount > 0) fprintf(f, "-body_zerocount %d\n", body_zerocount); + +if (authenticated_id != NULL) + fprintf(f, "-auth_id %s\n", authenticated_id); +if (authenticated_sender != NULL) + fprintf(f, "-auth_sender %s\n", authenticated_sender); + +if (allow_unqualified_recipient) fprintf(f, "-allow_unqualified_recipient\n"); +if (allow_unqualified_sender) fprintf(f, "-allow_unqualified_sender\n"); +if (deliver_firsttime) fprintf(f, "-deliver_firsttime\n"); +if (deliver_freeze) fprintf(f, "-frozen %d\n", deliver_frozen_at); +if (dont_deliver) fprintf(f, "-N\n"); +if (host_lookup_failed) fprintf(f, "-host_lookup_failed\n"); +if (sender_local) fprintf(f, "-local\n"); +if (local_error_message) fprintf(f, "-localerror\n"); +if (local_scan_data != NULL) fprintf(f, "-local_scan %s\n", local_scan_data); +if (deliver_manual_thaw) fprintf(f, "-manual_thaw\n"); +if (sender_set_untrusted) fprintf(f, "-sender_set_untrusted\n"); + +#ifdef SUPPORT_TLS +if (tls_certificate_verified) fprintf(f, "-tls_certificate_verified\n"); +if (tls_cipher != NULL) fprintf(f, "-tls_cipher %s\n", tls_cipher); +if (tls_peerdn != NULL) fprintf(f, "-tls_peerdn %s\n", tls_peerdn); +#endif + +/* To complete the envelope, write out the tree of non-recipients, followed by +the list of recipients. These won't be disjoint the first time, when no +checking has been done. If a recipient is a "one-time" alias, it is followed by +a space and its parent address number (pno). */ + +tree_write(tree_nonrecipients, f); +fprintf(f, "%d\n", recipients_count); +for (i = 0; i < recipients_count; i++) + { + recipient_item *r = recipients_list + i; + if (r->pno < 0 && r->errors_to == NULL) + fprintf(f, "%s\n", r->address); + else + { + uschar *errors_to = (r->errors_to == NULL)? US"" : r->errors_to; + fprintf(f, "%s %s %d,%d#1\n", r->address, errors_to, + Ustrlen(errors_to), r->pno); + } + } + +/* Put a blank line before the headers */ + +fprintf(f, "\n"); + +/* Save the size of the file so far so we can subtract it from the final length +to get the actual size of the headers. */ + +fflush(f); +fstat(fd, &statbuf); +size_correction = statbuf.st_size; + +/* Finally, write out the message's headers. To make it easier to read them +in again, precede each one with the count of its length. Make the count fixed +length to aid human eyes when debugging and arrange for it not be included in +the size. It is followed by a space for normal headers, a flagging letter for +various other headers, or an asterisk for old headers that have been rewritten. +These are saved as a record for debugging. Don't included them in the message's +size. */ + +for (h = header_list; h != NULL; h = h->next) + { + fprintf(f, "%03d%c %s", h->slen, h->type, h->text); + size_correction += 5; + if (h->type == '*') size_correction += h->slen; + } + +/* Flush and check for any errors while writing */ + +if (fflush(f) != 0 || ferror(f)) + return spool_write_error(where, errmsg, US"write", temp_name, f); + +/* Force the file's contents to be written to disk. Note that fflush() +just pushes it out of C, and fclose() doesn't guarantee to do the write +either. That's just the way Unix works... */ + +if (fsync(fileno(f)) < 0) + return spool_write_error(where, errmsg, US"sync", temp_name, f); + +/* Get the size of the file, and close it. */ + +fstat(fd, &statbuf); +if (fclose(f) != 0) + return spool_write_error(where, errmsg, US"close", temp_name, NULL); + +/* Rename the file to its correct name, thereby replacing any previous +incarnation. */ + +sprintf(CS name, "%s/input/%s/%s-H", spool_directory, message_subdir, id); + +if (Urename(temp_name, name) < 0) + return spool_write_error(where, errmsg, US"rename", temp_name, NULL); + +/* Linux (and maybe other OS?) does not automatically sync a directory after +an operation like rename. We therefore have to do it forcibly ourselves in +these cases, to make sure the file is actually accessible on disk, as opposed +to just the data being accessible from a file in lost+found. Linux also has +O_DIRECTORY, for opening a directory. + +However, it turns out that some file systems (some versions of NFS?) do not +support directory syncing. It seems safe enough to ignore EINVAL to cope with +these cases. One hack on top of another... but that's life. */ + +#ifdef NEED_SYNC_DIRECTORY + +sprintf(CS temp_name, "%s/input/%s/.", spool_directory, message_subdir); + +#ifndef O_DIRECTORY +#define O_DIRECTORY 0 +#endif + +if ((fd = Uopen(temp_name, O_RDONLY|O_DIRECTORY, 0)) < 0) + return spool_write_error(where, errmsg, US"directory open", name, NULL); + +if (fsync(fd) < 0 && errno != EINVAL) + return spool_write_error(where, errmsg, US"directory sync", name, NULL); + +if (close(fd) < 0) + return spool_write_error(where, errmsg, US"directory close", name, NULL); + +#endif /* NEED_SYNC_DIRECTORY */ + +/* Return the number of characters in the headers, which is the file size, less +the prelimary stuff, less the additional count fields on the headers. */ + +DEBUG(D_receive) debug_printf("Size of headers = %d\n", + (int)(statbuf.st_size - size_correction)); + +return statbuf.st_size - size_correction; +} + + +#ifdef SUPPORT_MOVE_FROZEN_MESSAGES + +/************************************************ +* Make a hard link * +************************************************/ + +/* Used by spool_move_message() below. Note re the use of sprintf(): the value +of spool_directory is checked to ensure that it is less than 200 characters at +start-up time. + +Arguments: + dir base directory name + subdir subdirectory name + id message id + suffix suffix to add to id + from source directory prefix + to destination directory prefix + noentok if TRUE, absence of file is not an error + +Returns: TRUE if all went well + FALSE, having panic logged if not +*/ + +static BOOL +make_link(uschar *dir, uschar *subdir, uschar *id, uschar *suffix, uschar *from, + uschar *to, BOOL noentok) +{ +uschar f[256], t[256]; +sprintf(CS f, "%s/%s%s/%s/%s%s", spool_directory, from, dir, subdir, id, suffix); +sprintf(CS t, "%s/%s%s/%s/%s%s", spool_directory, to, dir, subdir, id, suffix); +if (Ulink(f, t) < 0 && (!noentok || errno != ENOENT)) + { + log_write(0, LOG_MAIN|LOG_PANIC, "link(\"%s\", \"%s\") failed while moving " + "message: %s", f, t, strerror(errno)); + return FALSE; + } +return TRUE; +} + + + +/************************************************ +* Break a link * +************************************************/ + +/* Used by spool_move_message() below. Note re the use of sprintf(): the value +of spool_directory is checked to ensure that it is less than 200 characters at +start-up time. + +Arguments: + dir base directory name + subdir subdirectory name + id message id + suffix suffix to add to id + from source directory prefix + noentok if TRUE, absence of file is not an error + +Returns: TRUE if all went well + FALSE, having panic logged if not +*/ + +static BOOL +break_link(uschar *dir, uschar *subdir, uschar *id, uschar *suffix, uschar *from, + BOOL noentok) +{ +uschar f[256]; +sprintf(CS f, "%s/%s%s/%s/%s%s", spool_directory, from, dir, subdir, id, suffix); +if (Uunlink(f) < 0 && (!noentok || errno != ENOENT)) + { + log_write(0, LOG_MAIN|LOG_PANIC, "unlink(\"%s\") failed while moving " + "message: %s", f, strerror(errno)); + return FALSE; + } +return TRUE; +} + + + +/************************************************ +* Move message files * +************************************************/ + +/* Move the files for a message (-H, -D, and msglog) from one directory (or +hierarchy) to another. It is assume that there is no -J file in existence when +this is done. At present, this is used only when move_frozen_messages is set, +so compile it only when that support is configured. + +Arguments: + id the id of the message to be delivered + subdir the subdirectory name, or an empty string + from a prefix for "input" or "msglog" for where the message is now + to a prefix for "input" or "msglog" for where the message is to go + +Returns: TRUE if all is well + FALSE if not, with error logged in panic and main logs +*/ + +BOOL +spool_move_message(uschar *id, uschar *subdir, uschar *from, uschar *to) +{ +/* Create any output directories that do not exist. */ + +sprintf(CS big_buffer, "%sinput/%s", to, subdir); +(void)directory_make(spool_directory, big_buffer, INPUT_DIRECTORY_MODE, TRUE); +sprintf(CS big_buffer, "%smsglog/%s", to, subdir); +(void)directory_make(spool_directory, big_buffer, INPUT_DIRECTORY_MODE, TRUE); + +/* Move the message by first creating new hard links for all the files, and +then removing the old links. When moving messages onto the main spool, the -H +file should be set up last, because that's the one that tells Exim there is a +message to be delivered, so we create its new link last and remove its old link +first. Programs that look at the alternate directories should follow the same +rule of waiting for a -H file before doing anything. When moving messsages off +the mail spool, the -D file should be open and locked at the time, thus keeping +Exim's hands off. */ + +if (!make_link(US"msglog", subdir, id, US"", from, to, TRUE) || + !make_link(US"input", subdir, id, US"-D", from, to, FALSE) || + !make_link(US"input", subdir, id, US"-H", from, to, FALSE)) + return FALSE; + +if (!break_link(US"input", subdir, id, US"-H", from, FALSE) || + !break_link(US"input", subdir, id, US"-D", from, FALSE) || + !break_link(US"msglog", subdir, id, US"", from, TRUE)) + return FALSE; + +log_write(0, LOG_MAIN, "moved from %sinput, %smsglog to %sinput, %smsglog", + from, from, to, to); + +return TRUE; +} + +#endif + +/* End of spool_out.c */ diff --git a/src/src/store.c b/src/src/store.c new file mode 100644 index 000000000..738c4f6a6 --- /dev/null +++ b/src/src/store.c @@ -0,0 +1,554 @@ +/* $Cambridge: exim/src/src/store.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Exim gets and frees all its store through these functions. In the original +implementation there was a lot of mallocing and freeing of small bits of store. +The philosophy has now changed to a scheme which includes the concept of +"stacking pools" of store. For the short-lived processes, there isn't any real +need to do any garbage collection, but the stack concept allows quick resetting +in places where this seems sensible. + +Obviously the long-running processes (the daemon, the queue runner, and eximon) +must take care not to eat store. + +The following different types of store are recognized: + +. Long-lived, large blocks: This is implemented by retaining the original + malloc/free functions, and it used for permanent working buffers and for + getting blocks to cut up for the other types. + +. Long-lived, small blocks: This is used for blocks that have to survive until + the process exits. It is implemented as a stacking pool (POOL_PERM). This is + functionally the same as store_malloc(), except that the store can't be + freed, but I expect it to be more efficient for handling small blocks. + +. Short-lived, short blocks: Most of the dynamic store falls into this + category. It is implemented as a stacking pool (POOL_MAIN) which is reset + after accepting a message when multiple messages are received by a single + process. Resetting happens at some other times as well, usually fairly + locally after some specific processing that needs working store. + +. There is a separate pool (POOL_SEARCH) that is used only for lookup storage. + This means it can be freed when search_tidyup() is called to close down all + the lookup caching. +*/ + + +#include "exim.h" + + +/* We need to know how to align blocks of data for general use. I'm not sure +how to get an alignment factor in general. In the current world, a value of 8 +is probably right, and this is sizeof(double) on some systems and sizeof(void +*) on others, so take the larger of those. Since everything in this expression +is a constant, the compiler should optimize it to a simple constant wherever it +appears (I checked that gcc does do this). */ + +#define alignment \ + ((sizeof(void *) > sizeof(double))? sizeof(void *) : sizeof(double)) + +/* Size of block to get from malloc to carve up into smaller ones. This +must be a multiple of the alignment. We assume that 8192 is going to be +suitably aligned. */ + +#define STORE_BLOCK_SIZE 8192 + +/* store_reset() will not free the following block if the last used block has +less than this much left in it. */ + +#define STOREPOOL_MIN_SIZE 256 + +/* Structure describing the beginning of each big block. */ + +typedef struct storeblock { + struct storeblock *next; + size_t length; +} storeblock; + +/* Just in case we find ourselves on a system where the structure above has a +length that is not a multiple of the alignment, set up a macro for the padded +length. */ + +#define ALIGNED_SIZEOF_STOREBLOCK \ + (((sizeof(storeblock) + alignment - 1) / alignment) * alignment) + +/* Variables holding data for the local pools of store. The current pool number +is held in store_pool, which is global so that it can be changed from outside. +Setting the initial length values to -1 forces a malloc for the first call, +even if the length is zero (which is used for getting a point to reset to). */ + +int store_pool = POOL_PERM; + +static storeblock *chainbase[3] = { NULL, NULL, NULL }; +static storeblock *current_block[3] = { NULL, NULL, NULL }; +static void *next_yield[3] = { NULL, NULL, NULL }; +static int yield_length[3] = { -1, -1, -1 }; + +/* pool_malloc holds the amount of memory used by the store pools; this goes up +and down as store is reset or released. nonpool_malloc is the total got by +malloc from other calls; this doesn't go down because it is just freed by +pointer. */ + +static int pool_malloc = 0; +static int nonpool_malloc = 0; + +/* This variable is set by store_get() to its yield, and by store_reset() to +NULL. This enables string_cat() to optimize its store handling for very long +strings. That's why the variable is global. */ + +void *store_last_get[3] = { NULL, NULL, NULL }; + + + +/************************************************* +* Get a block from the current pool * +*************************************************/ + +/* Running out of store is a total disaster. This function is called via the +macro store_get(). It passes back a block of store within the current big +block, getting a new one if necessary. The address is saved in +store_last_was_get. + +Arguments: + size amount wanted + filename source file from which called + linenumber line number in source file. + +Returns: pointer to store (panic on malloc failure) +*/ + +void * +store_get_3(int size, const char *filename, int linenumber) +{ +/* Round up the size to a multiple of the alignment. Although this looks a +messy statement, because "alignment" is a constant expression, the compiler can +do a reasonable job of optimizing, especially if the value of "alignment" is a +power of two. I checked this with -O2, and gcc did very well, compiling it to 4 +instructions on a Sparc (alignment = 8). */ + +if (size % alignment != 0) size += alignment - (size % alignment); + +/* If there isn't room in the current block, get a new one. The minimum +size is STORE_BLOCK_SIZE, and we would expect this to be the norm, since +these functions are mostly called for small amounts of store. */ + +if (size > yield_length[store_pool]) + { + int length = (size <= STORE_BLOCK_SIZE)? STORE_BLOCK_SIZE : size; + int mlength = length + ALIGNED_SIZEOF_STOREBLOCK; + storeblock *newblock = NULL; + + /* Sometimes store_reset() may leave a block for us; check if we can use it */ + + if (current_block[store_pool] != NULL && + current_block[store_pool]->next != NULL) + { + newblock = current_block[store_pool]->next; + if (newblock->length < length) + { + /* Give up on this block, because it's too small */ + store_free(newblock); + newblock = NULL; + } + } + + /* If there was no free block, get a new one */ + + if (newblock == NULL) + { + pool_malloc += mlength; /* Used in pools */ + nonpool_malloc -= mlength; /* Exclude from overall total */ + newblock = store_malloc(mlength); + newblock->next = NULL; + newblock->length = length; + if (chainbase[store_pool] == NULL) chainbase[store_pool] = newblock; + else current_block[store_pool]->next = newblock; + } + + current_block[store_pool] = newblock; + yield_length[store_pool] = newblock->length; + next_yield[store_pool] = + (void *)((char *)current_block[store_pool] + ALIGNED_SIZEOF_STOREBLOCK); + } + +/* There's (now) enough room in the current block; the yield is the next +pointer. */ + +store_last_get[store_pool] = next_yield[store_pool]; + +/* Cut out the debugging stuff for utilities, but stop picky compilers from +giving warnings. */ + +#ifdef COMPILE_UTILITY +filename = filename; +linenumber = linenumber; +#else +DEBUG(D_memory) + { + if (running_in_test_harness) + debug_printf("---%d Get %5d\n", store_pool, size); + else + debug_printf("---%d Get %6p %5d %-14s %4d\n", store_pool, + store_last_get[store_pool], size, filename, linenumber); + } +#endif /* COMPILE_UTILITY */ + +/* Update next pointer and number of bytes left in the current block. */ + +next_yield[store_pool] = (void *)((char *)next_yield[store_pool] + size); +yield_length[store_pool] -= size; + +return store_last_get[store_pool]; +} + + + +/************************************************* +* Get a block from the PERM pool * +*************************************************/ + +/* This is just a convenience function, useful when just a single block is to +be obtained. + +Arguments: + size amount wanted + filename source file from which called + linenumber line number in source file. + +Returns: pointer to store (panic on malloc failure) +*/ + +void * +store_get_perm_3(int size, const char *filename, int linenumber) +{ +void *yield; +int old_pool = store_pool; +store_pool = POOL_PERM; +yield = store_get_3(size, filename, linenumber); +store_pool = old_pool; +return yield; +} + + + +/************************************************* +* Extend a block if it is at the top * +*************************************************/ + +/* While reading strings of unknown length, it is often the case that the +string is being read into the block at the top of the stack. If it needs to be +extended, it is more efficient just to extend the top block rather than +allocate a new block and then have to copy the data. This function is provided +for the use of string_cat(), but of course can be used elsewhere too. + +Arguments: + ptr pointer to store block + oldsize current size of the block, as requested by user + newsize new size required + filename source file from which called + linenumber line number in source file + +Returns: TRUE if the block is at the top of the stack and has been + extended; FALSE if it isn't at the top of the stack, or cannot + be extended +*/ + +BOOL +store_extend_3(void *ptr, int oldsize, int newsize, const char *filename, + int linenumber) +{ +int inc = newsize - oldsize; +int rounded_oldsize = oldsize; + +if (rounded_oldsize % alignment != 0) + rounded_oldsize += alignment - (rounded_oldsize % alignment); + +if ((char *)ptr + rounded_oldsize != (char *)(next_yield[store_pool]) || + inc > yield_length[store_pool] + rounded_oldsize - oldsize) + return FALSE; + +/* Cut out the debugging stuff for utilities, but stop picky compilers from +giving warnings. */ + +#ifdef COMPILE_UTILITY +filename = filename; +linenumber = linenumber; +#else +DEBUG(D_memory) + { + if (running_in_test_harness) + debug_printf("---%d Ext %5d\n", store_pool, newsize); + else + debug_printf("---%d Ext %6p %5d %-14s %4d\n", store_pool, ptr, newsize, + filename, linenumber); + } +#endif /* COMPILE_UTILITY */ + +if (newsize % alignment != 0) newsize += alignment - (newsize % alignment); +next_yield[store_pool] = (char *)ptr + newsize; +yield_length[store_pool] -= newsize - rounded_oldsize; +return TRUE; +} + + + + +/************************************************* +* Back up to a previous point on the stack * +*************************************************/ + +/* This function resets the next pointer, freeing any subsequent whole blocks +that are now unused. Normally it is given a pointer that was the yield of a +call to store_get, and is therefore aligned, but it may be given an offset +after such a pointer in order to release the end of a block and anything that +follows. + +Arguments: + ptr place to back up to + filename source file from which called + linenumber line number in source file + +Returns: nothing +*/ + +void +store_reset_3(void *ptr, const char *filename, int linenumber) +{ +storeblock *bb; +storeblock *b = current_block[store_pool]; +char *bc = (char *)b + ALIGNED_SIZEOF_STOREBLOCK; +int newlength; + +/* Last store operation was not a get */ + +store_last_get[store_pool] = NULL; + +/* See if the place is in the current block - as it often will be. Otherwise, +search for the block in which it lies. */ + +if ((char *)ptr < bc || (char *)ptr > bc + b->length) + { + for (b = chainbase[store_pool]; b != NULL; b = b->next) + { + bc = (char *)b + ALIGNED_SIZEOF_STOREBLOCK; + if ((char *)ptr >= bc && (char *)ptr <= bc + b->length) break; + } + if (b == NULL) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "internal error: store_reset(%d) " + "failed: pool=%d %-14s %4d", ptr, store_pool, filename, linenumber); + } + +/* Back up, rounding to the alignment if necessary. When testing, flatten +the released memory. */ + +newlength = bc + b->length - (char *)ptr; +#ifndef COMPILE_UTILITY +if (running_in_test_harness) memset(ptr, 0xF0, newlength); +#endif +yield_length[store_pool] = newlength - (newlength % alignment); +next_yield[store_pool] = (char *)ptr + (newlength % alignment); +current_block[store_pool] = b; + +/* Free any subsequent block. Do NOT free the first successor, if our +current block has less than 256 bytes left. This should prevent us from +flapping memory. However, keep this block only when it has the default size. */ + +if (yield_length[store_pool] < STOREPOOL_MIN_SIZE && + b->next != NULL && + b->next->length == STORE_BLOCK_SIZE) + b = b->next; + +bb = b->next; +b->next = NULL; + +while (bb != NULL) + { + b = bb; + bb = bb->next; + pool_malloc -= b->length + ALIGNED_SIZEOF_STOREBLOCK; + store_free_3(b, filename, linenumber); + } + +/* Cut out the debugging stuff for utilities, but stop picky compilers from +giving warnings. */ + +#ifdef COMPILE_UTILITY +filename = filename; +linenumber = linenumber; +#else +DEBUG(D_memory) + { + if (running_in_test_harness) + debug_printf("---%d Rst ** %d\n", store_pool, pool_malloc); + else + debug_printf("---%d Rst %6p ** %-14s %4d %d\n", store_pool, ptr, + filename, linenumber, pool_malloc); + } +#endif /* COMPILE_UTILITY */ +} + + + + + +/************************************************ +* Release store * +************************************************/ + +/* This function is specifically provided for use when reading very +long strings, e.g. header lines. When the string gets longer than a +complete block, it gets copied to a new block. It is helpful to free +the old block iff the previous copy of the string is at its start, +and therefore the only thing in it. Otherwise, for very long strings, +dead store can pile up somewhat disastrously. This function checks that +the pointer it is given is the first thing in a block, and if so, +releases that block. + +Arguments: + block block of store to consider + filename source file from which called + linenumber line number in source file + +Returns: nothing +*/ + +void +store_release_3(void *block, const char *filename, int linenumber) +{ +storeblock *b; + +/* It will never be the first block, so no need to check that. */ + +for (b = chainbase[store_pool]; b != NULL; b = b->next) + { + storeblock *bb = b->next; + if (bb != NULL && (char *)block == (char *)bb + ALIGNED_SIZEOF_STOREBLOCK) + { + b->next = bb->next; + pool_malloc -= bb->length + ALIGNED_SIZEOF_STOREBLOCK; + + /* Cut out the debugging stuff for utilities, but stop picky compilers + from giving warnings. */ + + #ifdef COMPILE_UTILITY + filename = filename; + linenumber = linenumber; + #else + DEBUG(D_memory) + { + if (running_in_test_harness) + debug_printf("-Release %d\n", pool_malloc); + else + debug_printf("-Release %6p %-20s %4d %d\n", (void *)bb, filename, + linenumber, pool_malloc); + } + if (running_in_test_harness) + memset(bb, 0xF0, bb->length+ALIGNED_SIZEOF_STOREBLOCK); + #endif /* COMPILE_UTILITY */ + + free(bb); + return; + } + } +} + + + + +/************************************************* +* Malloc store * +*************************************************/ + +/* Running out of store is a total disaster for exim. Some malloc functions +do not run happily on very small sizes, nor do they document this fact. This +function is called via the macro store_malloc(). + +Arguments: + size amount of store wanted + filename source file from which called + linenumber line number in source file + +Returns: pointer to gotten store (panic on failure) +*/ + +void * +store_malloc_3(int size, const char *filename, int linenumber) +{ +void *yield; + +if (size < 16) size = 16; +yield = malloc((size_t)size); + +if (yield == NULL) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "failed to malloc %d bytes of memory: " + "called from line %d of %s", size, linenumber, filename); + +nonpool_malloc += size; + +/* Cut out the debugging stuff for utilities, but stop picky compilers from +giving warnings. */ + +#ifdef COMPILE_UTILITY +filename = filename; +linenumber = linenumber; +#else + +/* If running in test harness, spend time making sure all the new store +is not filled with zeros so as to catch problems. */ + +if (running_in_test_harness) + { + memset(yield, 0xF0, (size_t)size); + DEBUG(D_memory) debug_printf("--Malloc %5d %d %d\n", size, pool_malloc, + nonpool_malloc); + } +else + { + DEBUG(D_memory) debug_printf("--Malloc %6p %5d %-14s %4d %d %d\n", yield, + size, filename, linenumber, pool_malloc, nonpool_malloc); + } +#endif /* COMPILE_UTILITY */ + +return yield; +} + + +/************************************************ +* Free store * +************************************************/ + +/* This function is called by the macro store_free(). + +Arguments: + block block of store to free + filename source file from which called + linenumber line number in source file + +Returns: nothing +*/ + +void +store_free_3(void *block, const char *filename, int linenumber) +{ +#ifdef COMPILE_UTILITY +filename = filename; +linenumber = linenumber; +#else +DEBUG(D_memory) + { + if (running_in_test_harness) + debug_printf("----Free\n"); + else + debug_printf("----Free %6p %-20s %4d\n", block, filename, linenumber); + } +#endif /* COMPILE_UTILITY */ +free(block); +} + +/* End of store.c */ diff --git a/src/src/store.h b/src/src/store.h new file mode 100644 index 000000000..bac31b2ed --- /dev/null +++ b/src/src/store.h @@ -0,0 +1,55 @@ +/* $Cambridge: exim/src/src/store.h,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Header for Exim's memory allocation functions */ + +#ifndef STORE_H +#define STORE_H + +/* Define symbols for identifying the store pools. */ + +enum { POOL_MAIN, POOL_PERM, POOL_SEARCH }; + +/* This variable (the one for the current pool) is set by store_get() to its +yield, and by store_reset() to NULL. This allows string_cat() to optimize its +store handling. */ + +extern void *store_last_get[3]; + +/* This variable contains the current store pool number. */ + +extern int store_pool; + +/* Macros for calling the memory allocation routines with +tracing information for debugging. */ + +#define store_extend(addr,old,new) \ + store_extend_3(addr, old, new, __FILE__, __LINE__) + +#define store_free(addr) store_free_3(addr, __FILE__, __LINE__) +#define store_get(size) store_get_3(size, __FILE__, __LINE__) +#define store_get_perm(size) store_get_perm_3(size, __FILE__, __LINE__) +#define store_malloc(size) store_malloc_3(size, __FILE__, __LINE__) +#define store_release(addr) store_release_3(addr, __FILE__, __LINE__) +#define store_reset(addr) store_reset_3(addr, __FILE__, __LINE__) + + +/* The real functions */ + +extern BOOL store_extend_3(void *, int, int, const char *, int); /* The */ +extern void store_free_3(void *, const char *, int); /* value of the */ +extern void *store_get_3(int, const char *, int); /* 2nd arg is */ +extern void *store_get_perm_3(int, const char *, int); /* __FILE__ in */ +extern void *store_malloc_3(int, const char *, int); /* every call, */ +extern void store_release_3(void *, const char *, int); /* so give its */ +extern void store_reset_3(void *, const char *, int); /* correct type */ + +#endif /* STORE_H */ + +/* End of store.h */ diff --git a/src/src/string.c b/src/src/string.c new file mode 100644 index 000000000..62678b145 --- /dev/null +++ b/src/src/string.c @@ -0,0 +1,1502 @@ +/* $Cambridge: exim/src/src/string.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Miscellaneous string-handling functions. Some are not required for +utilities and tests, and are cut out by the COMPILE_UTILITY macro. */ + + +#include "exim.h" + + +#ifndef COMPILE_UTILITY +/************************************************* +* Test for IP address * +*************************************************/ + +/* This used just to be a regular expression, but with IPv6 things are a bit +more complicated. If the address contains a colon, it is assumed to be a v6 +address (assuming HAVE_IPV6 is set). If a mask is permitted and one is present, +and maskptr is not NULL, its offset is placed there. + +Arguments: + s a string + maskptr NULL if no mask is permitted to follow + otherwise, points to an int where the offset of '/' is placed + +Returns: 0 if the string is not a textual representation of an IP address + 4 if it is an IPv4 address + 6 if it is an IPv6 address +*/ + +int +string_is_ip_address(uschar *s, int *maskptr) +{ +int i; +int yield = 4; + +/* If an optional mask is permitted, check for it. If found, pass back the +offset. */ + +if (maskptr != NULL) + { + uschar *ss = s + Ustrlen(s); + *maskptr = 0; + if (s != ss && isdigit(*(--ss))) + { + while (ss > s && isdigit(ss[-1])) ss--; + if (ss > s && *(--ss) == '/') *maskptr = ss - s; + } + } + +/* A colon anywhere in the string => IPv6 address */ + +if (Ustrchr(s, ':') != NULL) + { + BOOL had_double_colon = FALSE; + BOOL v4end = FALSE; + int count = 0; + + yield = 6; + + /* An IPv6 address must start with hex digit or double colon. A single + colon is invalid. */ + + if (*s == ':' && *(++s) != ':') return 0; + + /* Now read up to 8 components consisting of up to 4 hex digits each. There + may be one and only one appearance of double colon, which implies any number + of binary zero bits. The number of preceding components is held in count. */ + + for (count = 0; count < 8; count++) + { + /* If the end of the string is reached before reading 8 components, the + address is valid provided a double colon has been read. This also applies + if we hit the / that introduces a mask or the % that introduces the + interface specifier (scope id) of a link-local address. */ + + if (*s == 0 || *s == '%' || *s == '/') return had_double_colon? yield : 0; + + /* If a component starts with an additional colon, we have hit a double + colon. This is permitted to appear once only, and counts as at least + one component. The final component may be of this form. */ + + if (*s == ':') + { + if (had_double_colon) return 0; + had_double_colon = TRUE; + s++; + continue; + } + + /* If the remainder of the string contains a dot but no colons, we + can expect a trailing IPv4 address. This is valid if either there has + been no double-colon and this is the 7th component (with the IPv4 address + being the 7th & 8th components), OR if there has been a double-colon + and fewer than 6 components. */ + + if (Ustrchr(s, ':') == NULL && Ustrchr(s, '.') != NULL) + { + if ((!had_double_colon && count != 6) || + (had_double_colon && count > 6)) return 0; + v4end = TRUE; + yield = 6; + break; + } + + /* Check for at least one and not more than 4 hex digits for this + component. */ + + if (!isxdigit(*s++)) return 0; + if (isxdigit(*s) && isxdigit(*(++s)) && isxdigit(*(++s))) s++; + + /* If the component is terminated by colon and there is more to + follow, skip over the colon. If there is no more to follow the address is + invalid. */ + + if (*s == ':' && *(++s) == 0) return 0; + } + + /* If about to handle a trailing IPv4 address, drop through. Otherwise + all is well if we are at the end of the string or at the mask or at a percent + sign, which introduces the interface specifier (scope id) of a link local + address. */ + + if (!v4end) return (*s == 0 || *s == '%' || *s == '/')? yield : 0; + } + +/* Test for IPv4 address, which may be the tail-end of an IPv6 address. */ + +for (i = 0; i < 4; i++) + { + if (i != 0 && *s++ != '.') return 0; + if (!isdigit(*s++)) return 0; + if (isdigit(*s) && isdigit(*(++s))) s++; + } + +return (*s == 0 || *s == '/')? yield : 0; +} +#endif /* COMPILE_UTILITY */ + + +/************************************************* +* Format message size * +*************************************************/ + +/* Convert a message size in bytes to printing form, rounding +according to the magnitude of the number. A value of zero causes +a string of spaces to be returned. + +Arguments: + size the message size in bytes + buffer where to put the answer + +Returns: pointer to the buffer + a string of exactly 5 characters is normally returned +*/ + +uschar * +string_format_size(int size, uschar *buffer) +{ +if (size == 0) Ustrcpy(CS buffer, " "); +else if (size < 1024) sprintf(CS buffer, "%5d", size); +else if (size < 10*1024) + sprintf(CS buffer, "%4.1fK", (double)size / 1024.0); +else if (size < 1024*1024) + sprintf(CS buffer, "%4dK", (size + 512)/1024); +else if (size < 10*1024*1024) + sprintf(CS buffer, "%4.1fM", (double)size / (1024.0 * 1024.0)); +else + sprintf(CS buffer, "%4dM", (size + 512 * 1024)/(1024*1024)); +return buffer; +} + + + +#ifndef COMPILE_UTILITY +/************************************************* +* Convert a number to base 62 format * +*************************************************/ + +/* Convert a long integer into an ASCII base 62 string. For Cygwin the value of +BASE_62 is actually 36. Always return exactly 6 characters plus zero, in a +static area. + +Argument: a long integer +Returns: pointer to base 62 string +*/ + +uschar * +string_base62(unsigned long int value) +{ +static uschar yield[7]; +uschar *p = yield + sizeof(yield) - 1; +*p = 0; +while (p > yield) + { + *(--p) = base62_chars[value % BASE_62]; + value /= BASE_62; + } +return yield; +} +#endif /* COMPILE_UTILITY */ + + + +#ifndef COMPILE_UTILITY +/************************************************* +* Interpret escape sequence * +*************************************************/ + +/* This function is called from several places where escape sequences are to be +interpreted in strings. + +Arguments: + pp points a pointer to the initiating "\" in the string; + the pointer gets updated to point to the final character +Returns: the value of the character escape +*/ + +int +string_interpret_escape(uschar **pp) +{ +int ch; +uschar *p = *pp; +ch = *(++p); +if (isdigit(ch) && ch != '8' && ch != '9') + { + ch -= '0'; + if (isdigit(p[1]) && p[1] != '8' && p[1] != '9') + { + ch = ch * 8 + *(++p) - '0'; + if (isdigit(p[1]) && p[1] != '8' && p[1] != '9') + ch = ch * 8 + *(++p) - '0'; + } + } +else switch(ch) + { + case 'n': ch = '\n'; break; + case 'r': ch = '\r'; break; + case 't': ch = '\t'; break; + case 'x': + ch = 0; + if (isxdigit(p[1])) + { + ch = ch * 16 + + Ustrchr(hex_digits, tolower(*(++p))) - hex_digits; + if (isxdigit(p[1])) ch = ch * 16 + + Ustrchr(hex_digits, tolower(*(++p))) - hex_digits; + } + break; + } +*pp = p; +return ch; +} +#endif /* COMPILE_UTILITY */ + + + +#ifndef COMPILE_UTILITY +/************************************************* +* Ensure string is printable * +*************************************************/ + +/* This function is called for critical strings. It checks for any +non-printing characters, and if any are found, it makes a new copy +of the string with suitable escape sequences. It is most often called by the +macro string_printing(), which sets allow_tab TRUE. + +Arguments: + s the input string + allow_tab TRUE to allow tab as a printing character + +Returns: string with non-printers encoded as printing sequences +*/ + +uschar * +string_printing2(uschar *s, BOOL allow_tab) +{ +int nonprintcount = 0; +int length = 0; +uschar *t = s; +uschar *ss, *tt; + +while (*t != 0) + { + int c = *t++; + if (!mac_isprint(c) || (!allow_tab && c == '\t')) nonprintcount++; + length++; + } + +if (nonprintcount == 0) return s; + +/* Get a new block of store guaranteed big enough to hold the +expanded string. */ + +ss = store_get(length + nonprintcount * 4 + 1); + +/* Copy everying, escaping non printers. */ + +t = s; +tt = ss; + +while (*t != 0) + { + int c = *t; + if (mac_isprint(c) && (allow_tab || c != '\t')) *tt++ = *t++; else + { + *tt++ = '\\'; + switch (*t) + { + case '\n': *tt++ = 'n'; break; + case '\r': *tt++ = 'r'; break; + case '\b': *tt++ = 'b'; break; + case '\v': *tt++ = 'v'; break; + case '\f': *tt++ = 'f'; break; + case '\t': *tt++ = 't'; break; + default: sprintf(CS tt, "%03o", *t); tt += 3; break; + } + t++; + } + } +*tt = 0; +return ss; +} +#endif /* COMPILE_UTILITY */ + + + + +/************************************************* +* Copy and save string * +*************************************************/ + +/* This function assumes that memcpy() is faster than strcpy(). + +Argument: string to copy +Returns: copy of string in new store +*/ + +uschar * +string_copy(uschar *s) +{ +int len = Ustrlen(s) + 1; +uschar *ss = store_get(len); +memcpy(ss, s, len); +return ss; +} + + + +/************************************************* +* Copy and save string in malloc'd store * +*************************************************/ + +/* This function assumes that memcpy() is faster than strcpy(). + +Argument: string to copy +Returns: copy of string in new store +*/ + +uschar * +string_copy_malloc(uschar *s) +{ +int len = Ustrlen(s) + 1; +uschar *ss = store_malloc(len); +memcpy(ss, s, len); +return ss; +} + + + +/************************************************* +* Copy, lowercase and save string * +*************************************************/ + +/* +Argument: string to copy +Returns: copy of string in new store, with letters lowercased +*/ + +uschar * +string_copylc(uschar *s) +{ +uschar *ss = store_get(Ustrlen(s) + 1); +uschar *p = ss; +while (*s != 0) *p++ = tolower(*s++); +*p = 0; +return ss; +} + + + +/************************************************* +* Copy and save string, given length * +*************************************************/ + +/* It is assumed the data contains no zeros. A zero is added +onto the end. + +Arguments: + s string to copy + n number of characters + +Returns: copy of string in new store +*/ + +uschar * +string_copyn(uschar *s, int n) +{ +uschar *ss = store_get(n + 1); +Ustrncpy(ss, s, n); +ss[n] = 0; +return ss; +} + + +/************************************************* +* Copy, lowercase, and save string, given length * +*************************************************/ + +/* It is assumed the data contains no zeros. A zero is added +onto the end. + +Arguments: + s string to copy + n number of characters + +Returns: copy of string in new store, with letters lowercased +*/ + +uschar * +string_copynlc(uschar *s, int n) +{ +uschar *ss = store_get(n + 1); +uschar *p = ss; +while (n-- > 0) *p++ = tolower(*s++); +*p = 0; +return ss; +} + + + +/************************************************* +* Copy returned DNS domain name, de-escaping * +*************************************************/ + +/* If a domain name contains top-bit characters, some resolvers return +the fully qualified name with those characters turned into escapes. The +convention is a backslash followed by _decimal_ digits. We convert these +back into the original binary values. This will be relevant when +allow_utf8_domains is set true and UTF-8 characters are used in domain +names. Backslash can also be used to escape other characters, though we +shouldn't come across them in domain names. + +Argument: the domain name string +Returns: copy of string in new store, de-escaped +*/ + +uschar * +string_copy_dnsdomain(uschar *s) +{ +uschar *yield; +uschar *ss = yield = store_get(Ustrlen(s) + 1); + +while (*s != 0) + { + if (*s != '\\') + { + *ss++ = *s++; + } + else if (isdigit(s[1])) + { + *ss++ = (s[1] - '0')*100 + (s[2] - '0')*10 + s[3] - '0'; + s += 4; + } + else if (*(++s) != 0) + { + *ss++ = *s++; + } + } + +*ss = 0; +return yield; +} + + +#ifndef COMPILE_UTILITY +/************************************************* +* Copy space-terminated or quoted string * +*************************************************/ + +/* This function copies from a string until its end, or until whitespace is +encountered, unless the string begins with a double quote, in which case the +terminating quote is sought, and escaping within the string is done. The length +of a de-quoted string can be no longer than the original, since escaping always +turns n characters into 1 character. + +Argument: pointer to the pointer to the first character, which gets updated +Returns: the new string +*/ + +uschar * +string_dequote(uschar **sptr) +{ +uschar *s = *sptr; +uschar *t, *yield; + +/* First find the end of the string */ + +if (*s != '\"') + { + while (*s != 0 && !isspace(*s)) s++; + } +else + { + s++; + while (*s != 0 && *s != '\"') + { + if (*s == '\\') (void)string_interpret_escape(&s); + s++; + } + if (*s != 0) s++; + } + +/* Get enough store to copy into */ + +t = yield = store_get(s - *sptr + 1); +s = *sptr; + +/* Do the copy */ + +if (*s != '\"') + { + while (*s != 0 && !isspace(*s)) *t++ = *s++; + } +else + { + s++; + while (*s != 0 && *s != '\"') + { + if (*s == '\\') *t++ = string_interpret_escape(&s); + else *t++ = *s; + s++; + } + if (*s != 0) s++; + } + +/* Update the pointer and return the terminated copy */ + +*sptr = s; +*t = 0; +return yield; +} +#endif /* COMPILE_UTILITY */ + + + +/************************************************* +* Format a string and save it * +*************************************************/ + +/* The formatting is done by string_format, which checks the length of +everything. + +Arguments: + format a printf() format - deliberately char * rather than uschar * + because it will most usually be a literal string + ... arguments for format + +Returns: pointer to fresh piece of store containing sprintf'ed string +*/ + +uschar * +string_sprintf(char *format, ...) +{ +va_list ap; +uschar buffer[STRING_SPRINTF_BUFFER_SIZE]; +va_start(ap, format); +if (!string_vformat(buffer, sizeof(buffer), format, ap)) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, + "string_sprintf expansion was longer than %d", sizeof(buffer)); +va_end(ap); +return string_copy(buffer); +} + + + +/************************************************* +* Case-independent strncmp() function * +*************************************************/ + +/* +Arguments: + s first string + t second string + n number of characters to compare + +Returns: < 0, = 0, or > 0, according to the comparison +*/ + +int +strncmpic(uschar *s, uschar *t, int n) +{ +while (n--) + { + int c = tolower(*s++) - tolower(*t++); + if (c) return c; + } +return 0; +} + + +/************************************************* +* Case-independent strcmp() function * +*************************************************/ + +/* +Arguments: + s first string + t second string + +Returns: < 0, = 0, or > 0, according to the comparison +*/ + +int +strcmpic(uschar *s, uschar *t) +{ +while (*s != 0) + { + int c = tolower(*s++) - tolower(*t++); + if (c != 0) return c; + } +return *t; +} + + +/************************************************* +* Case-independent strstr() function * +*************************************************/ + +/* The third argument specifies whether whitespace is required +to follow the matched string. + +Arguments: + s string to search + t substring to search for + space_follows if TRUE, match only if whitespace follows + +Returns: pointer to substring in string, or NULL if not found +*/ + +uschar * +strstric(uschar *s, uschar *t, BOOL space_follows) +{ +uschar *p = t; +uschar *yield = NULL; +int cl = tolower(*p); +int cu = toupper(*p); + +while (*s) + { + if (*s == cl || *s == cu) + { + if (yield == NULL) yield = s; + if (*(++p) == 0) + { + if (!space_follows || s[1] == ' ' || s[1] == '\n' ) return yield; + yield = NULL; + p = t; + } + cl = tolower(*p); + cu = toupper(*p); + s++; + } + else if (yield != NULL) + { + yield = NULL; + p = t; + cl = tolower(*p); + cu = toupper(*p); + } + else s++; + } +return NULL; +} + + + +#ifndef COMPILE_UTILITY +/************************************************* +* Get next string from separated list * +*************************************************/ + +/* Leading and trailing space is removed from each item. The separator in the +list is controlled by the int pointed to by the separator argument as follows: + + If its value is > 0 it is used as the delimiter. + (If its value is actually > UCHAR_MAX there is only one item in the list. + This is used for some cases when called via functions that sometimes + plough through lists, and sometimes are given single items.) + If its value is <= 0, the string is inspected for a leading 0 && isspace(buffer[p-1])) p--; + buffer[p] = 0; + } + +/* Handle the case when a buffer is not provided. */ + +else + { + /* We know that *s != 0 at this point. However, it might be pointing to a + separator, which could indicate an empty string, or could be doubled to + indicate a separator character as data at the start of a string. */ + + if (*s == sep) + { + s++; + if (*s != sep) buffer = string_copy(US""); + } + + if (buffer == NULL) + { + int size = 0; + int ptr = 0; + uschar *ss; + for (;;) + { + for (ss = s + 1; *ss != 0 && *ss != sep; ss++); + buffer = string_cat(buffer, &size, &ptr, s, ss-s); + s = ss; + if (*s == 0 || *(++s) != sep) break; + } + while (ptr > 0 && isspace(buffer[ptr-1])) ptr--; + buffer[ptr] = 0; + } + } + +/* Update the current pointer and return the new string */ + +*listptr = s; +return buffer; +} +#endif /* COMPILE_UTILITY */ + + + +#ifndef COMPILE_UTILITY +/************************************************* +* Add chars to string * +*************************************************/ + +/* This function is used when building up strings of unknown length. Room is +always left for a terminating zero to be added to the string that is being +built. This function does not require the string that is being added to be NUL +terminated, because the number of characters to add is given explicitly. It is +sometimes called to extract parts of other strings. + +Arguments: + string points to the start of the string that is being built, or NULL + if this is a new string that has no contents yet + size points to a variable that holds the current capacity of the memory + block (updated if changed) + ptr points to a variable that holds the offset at which to add + characters, updated to the new offset + s points to characters to add + count count of characters to add; must not exceed the length of s, if s + is a C string + +If string is given as NULL, *size and *ptr should both be zero. + +Returns: pointer to the start of the string, changed if copied for expansion. + Note that a NUL is not added, though space is left for one. This is + because string_cat() is often called multiple times to build up a + string - there's no point adding the NUL till the end. +*/ + +uschar * +string_cat(uschar *string, int *size, int *ptr, const uschar *s, int count) +{ +int p = *ptr; + +if (p + count >= *size) + { + int oldsize = *size; + + /* Mostly, string_cat() is used to build small strings of a few hundred + characters at most. There are times, however, when the strings are very much + longer (for example, a lookup that returns a vast number of alias addresses). + To try to keep things reasonable, we use increments whose size depends on the + existing length of the string. */ + + int inc = (oldsize < 4096)? 100 : 1024; + while (*size <= p + count) *size += inc; + + /* New string */ + + if (string == NULL) string = store_get(*size); + + /* Try to extend an existing allocation. If the result of calling + store_extend() is false, either there isn't room in the current memory block, + or this string is not the top item on the dynamic store stack. We then have + to get a new chunk of store and copy the old string. When building large + strings, it is helpful to call store_release() on the old string, to release + memory blocks that have become empty. (The block will be freed if the string + is at its start.) However, we can do this only if we know that the old string + was the last item on the dynamic memory stack. This is the case if it matches + store_last_get. */ + + else if (!store_extend(string, oldsize, *size)) + { + BOOL release_ok = store_last_get[store_pool] == string; + uschar *newstring = store_get(*size); + memcpy(newstring, string, p); + if (release_ok) store_release(string); + string = newstring; + } + } + +/* Because we always specify the exact number of characters to copy, we can +use memcpy(), which is likely to be more efficient than strncopy() because the +latter has to check for zero bytes. */ + +memcpy(string + p, s, count); +*ptr = p + count; +return string; +} +#endif /* COMPILE_UTILITY */ + + + +#ifndef COMPILE_UTILITY +/************************************************* +* Append strings to another string * +*************************************************/ + +/* This function can be used to build a string from many other strings. +It calls string_cat() to do the dirty work. + +Arguments: + string points to the start of the string that is being built, or NULL + if this is a new string that has no contents yet + size points to a variable that holds the current capacity of the memory + block (updated if changed) + ptr points to a variable that holds the offset at which to add + characters, updated to the new offset + count the number of strings to append + ... "count" uschar* arguments, which must be valid zero-terminated + C strings + +Returns: pointer to the start of the string, changed if copied for expansion. + The string is not zero-terminated - see string_cat() above. +*/ + +uschar * +string_append(uschar *string, int *size, int *ptr, int count, ...) +{ +va_list ap; +int i; + +va_start(ap, count); +for (i = 0; i < count; i++) + { + uschar *t = va_arg(ap, uschar *); + string = string_cat(string, size, ptr, t, Ustrlen(t)); + } +va_end(ap); + +return string; +} +#endif + + + +/************************************************* +* Format a string with length checks * +*************************************************/ + +/* This function is used to format a string with checking of the length of the +output for all conversions. It protects Exim from absent-mindedness when +calling functions like debug_printf and string_sprintf, and elsewhere. There +are two different entry points to what is actually the same function, depending +on whether the variable length list of data arguments are given explicitly or +as a va_list item. + +The formats are the usual printf() ones, with some omissions (never used) and +two additions for strings: %S forces lower case, %#s or %#S prints nothing for +a NULL string. Without the # "NULL" is printed (useful in debugging). There is +also the addition of %D, which inserts the date in the form used for +datestamped log files. + +Arguments: + buffer a buffer in which to put the formatted string + buflen the length of the buffer + format the format string - deliberately char * and not uschar * + ... or ap variable list of supplementary arguments + +Returns: TRUE if the result fitted in the buffer +*/ + +BOOL +string_format(uschar *buffer, int buflen, char *format, ...) +{ +BOOL yield; +va_list ap; +va_start(ap, format); +yield = string_vformat(buffer, buflen, format, ap); +va_end(ap); +return yield; +} + + +BOOL +string_vformat(uschar *buffer, int buflen, char *format, va_list ap) +{ +BOOL yield = TRUE; +int width, precision; +char *fp = format; /* Deliberately not unsigned */ +uschar *p = buffer; +uschar *last = buffer + buflen - 1; + +string_datestamp_offset = -1; /* Datestamp not inserted */ + +/* Scan the format and handle the insertions */ + +while (*fp != 0) + { + int *nptr; + int slen; + char *null = "NULL"; /* ) These variables */ + char *item_start, *s; /* ) are deliberately */ + char newformat[16]; /* ) not unsigned */ + + /* Non-% characters just get copied verbatim */ + + if (*fp != '%') + { + if (p >= last) { yield = FALSE; break; } + *p++ = (uschar)*fp++; + continue; + } + + /* Deal with % characters. Pick off the width and precision, for checking + strings, skipping over the flag and modifier characters. */ + + item_start = fp; + width = precision = -1; + + if (strchr("-+ #0", *(++fp)) != NULL) + { + if (*fp == '#') null = ""; + fp++; + } + + if (isdigit((uschar)*fp)) + { + width = *fp++ - '0'; + while (isdigit((uschar)*fp)) width = width * 10 + *fp++ - '0'; + } + else if (*fp == '*') + { + width = va_arg(ap, int); + fp++; + } + + if (*fp == '.') + { + if (*(++fp) == '*') + { + precision = va_arg(ap, int); + fp++; + } + else + { + precision = 0; + while (isdigit((uschar)*fp)) + precision = precision*10 + *fp++ - '0'; + } + } + + if (strchr("hlL", *fp) != NULL) fp++; + + /* Handle each specific format type. */ + + switch (*fp++) + { + case 'n': + nptr = va_arg(ap, int *); + *nptr = p - buffer; + break; + + case 'd': + case 'o': + case 'u': + case 'x': + case 'X': + if (p >= last - 12) { yield = FALSE; goto END_FORMAT; } + strncpy(newformat, item_start, fp - item_start); + newformat[fp - item_start] = 0; + sprintf(CS p, newformat, va_arg(ap, int)); + while (*p) p++; + break; + + case 'p': + if (p >= last - 24) { yield = FALSE; goto END_FORMAT; } + strncpy(newformat, item_start, fp - item_start); + newformat[fp - item_start] = 0; + sprintf(CS p, newformat, va_arg(ap, void *)); + while (*p) p++; + break; + + /* %f format is inherently insecure if the numbers that it may be + handed are unknown (e.g. 1e300). However, in Exim, the only use of %f + is for printing load averages, and these are actually stored as integers + (load average * 1000) so the size of the numbers is constrained. */ + + case 'f': + case 'e': + case 'E': + case 'g': + case 'G': + if (precision < 0) precision = 6; + if (p >= last - precision - 8) { yield = FALSE; goto END_FORMAT; } + strncpy(newformat, item_start, fp - item_start); + newformat[fp-item_start] = 0; + sprintf(CS p, newformat, va_arg(ap, double)); + while (*p) p++; + break; + + /* String types */ + + case '%': + if (p >= last) { yield = FALSE; goto END_FORMAT; } + *p++ = '%'; + break; + + case 'c': + if (p >= last) { yield = FALSE; goto END_FORMAT; } + *p++ = va_arg(ap, int); + break; + + case 'D': /* Insert datestamp for log file names */ + s = CS tod_stamp(tod_log_datestamp); + string_datestamp_offset = p - buffer; /* Passed back via global */ + goto INSERT_STRING; + + case 's': + case 'S': /* Forces *lower* case */ + s = va_arg(ap, char *); + + INSERT_STRING: /* Come to from %D above */ + if (s == NULL) s = null; + slen = Ustrlen(s); + + /* If the width is specified, check that there is a precision + set; if not, set it to the width to prevent overruns of long + strings. */ + + if (width >= 0) + { + if (precision < 0) precision = width; + } + + /* If a width is not specified and the precision is specified, set + the width to the precision, or the string length if shorted. */ + + else if (precision >= 0) + { + width = (precision < slen)? precision : slen; + } + + /* If neither are specified, set them both to the string length. */ + + else width = precision = slen; + + /* Check string space, and add the string to the buffer if ok. If + not OK, add part of the string (debugging uses this to show as + much as possible). */ + + if (p >= last - width) + { + yield = FALSE; + width = precision = last - p - 1; + } + sprintf(CS p, "%*.*s", width, precision, s); + if (fp[-1] == 'S') + while (*p) { *p = tolower(*p); p++; } + else + while (*p) p++; + if (!yield) goto END_FORMAT; + break; + + /* Some things are never used in Exim; also catches junk. */ + + default: + strncpy(newformat, item_start, fp - item_start); + newformat[fp-item_start] = 0; + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "string_format: unsupported type " + "in \"%s\" in \"%s\"", newformat, format); + break; + } + } + +/* Ensure string is complete; return TRUE if got to the end of the format */ + +END_FORMAT: + +*p = 0; +return yield; +} + + + +#ifndef COMPILE_UTILITY +/************************************************* +* Generate an "open failed" message * +*************************************************/ + +/* This function creates a message after failure to open a file. It includes a +string supplied as data, adds the strerror() text, and if the failure was +"Permission denied", reads and includes the euid and egid. + +Arguments: + eno the value of errno after the failure + format a text format string - deliberately not uschar * + ... arguments for the format string + +Returns: a message, in dynamic store +*/ + +uschar * +string_open_failed(int eno, char *format, ...) +{ +va_list ap; +uschar buffer[1024]; + +Ustrcpy(buffer, "failed to open "); +va_start(ap, format); + +/* Use the checked formatting routine to ensure that the buffer +does not overflow. It should not, since this is called only for internally +specified messages. If it does, the message just gets truncated, and there +doesn't seem much we can do about that. */ + +(void)string_vformat(buffer+15, sizeof(buffer) - 15, format, ap); + +return (eno == EACCES)? + string_sprintf("%s: %s (euid=%ld egid=%ld)", buffer, strerror(eno), + (long int)geteuid(), (long int)getegid()) : + string_sprintf("%s: %s", buffer, strerror(eno)); +} +#endif /* COMPILE_UTILITY */ + + + +#ifndef COMPILE_UTILITY +/************************************************* +* Generate local prt for logging * +*************************************************/ + +/* This function is a subroutine for use in string_log_address() below. + +Arguments: + addr the address being logged + yield the current dynamic buffer pointer + sizeptr points to current size + ptrptr points to current insert pointer + +Returns: the new value of the buffer pointer +*/ + +static uschar * +string_get_localpart(address_item *addr, uschar *yield, int *sizeptr, + int *ptrptr) +{ +if (testflag(addr, af_include_affixes) && addr->prefix != NULL) + yield = string_cat(yield, sizeptr, ptrptr, addr->prefix, + Ustrlen(addr->prefix)); +yield = string_cat(yield, sizeptr, ptrptr, addr->local_part, + Ustrlen(addr->local_part)); +if (testflag(addr, af_include_affixes) && addr->suffix != NULL) + yield = string_cat(yield, sizeptr, ptrptr, addr->suffix, + Ustrlen(addr->suffix)); +return yield; +} + + +/************************************************* +* Generate log address list * +*************************************************/ + +/* This function generates a list consisting of an address and its parents, for +use in logging lines. For saved onetime aliased addresses, the onetime parent +field is used. If the address was delivered by a transport with rcpt_include_ +affixes set, the af_include_affixes bit will be set in the address. In that +case, we include the affixes here too. + +Arguments: + addr bottom (ultimate) address + all_parents if TRUE, include all parents + success TRUE for successful delivery + +Returns: a string in dynamic store +*/ + +uschar * +string_log_address(address_item *addr, BOOL all_parents, BOOL success) +{ +int size = 64; +int ptr = 0; +BOOL add_topaddr = TRUE; +uschar *yield = store_get(size); +address_item *topaddr; + +/* Find the ultimate parent */ + +for (topaddr = addr; topaddr->parent != NULL; topaddr = topaddr->parent); + +/* We start with just the local part for pipe, file, and reply deliveries, and +for successful local deliveries from routers that have the log_as_local flag +set. File deliveries from filters can be specified as non-absolute paths in +cases where the transport is goin to complete the path. If there is an error +before this happens (expansion failure) the local part will not be updated, and +so won't necessarily look like a path. Add extra text for this case. */ + +if (testflag(addr, af_pfr) || + (success && + addr->router != NULL && addr->router->log_as_local && + addr->transport != NULL && addr->transport->info->local)) + { + if (testflag(addr, af_file) && addr->local_part[0] != '/') + yield = string_cat(yield, &size, &ptr, CUS"save ", 5); + yield = string_get_localpart(addr, yield, &size, &ptr); + } + +/* Other deliveries start with the full address. It we have split it into local +part and domain, use those fields. Some early failures can happen before the +splitting is done; in those cases use the original field. */ + +else + { + if (addr->local_part != NULL) + { + yield = string_get_localpart(addr, yield, &size, &ptr); + yield = string_cat(yield, &size, &ptr, US"@", 1); + yield = string_cat(yield, &size, &ptr, addr->domain, + Ustrlen(addr->domain) ); + } + else + { + yield = string_cat(yield, &size, &ptr, addr->address, Ustrlen(addr->address)); + } + yield[ptr] = 0; + + /* If the address we are going to print is the same as the top address, + and all parents are not being included, don't add on the top address. First + of all, do a caseless comparison; if this succeeds, do a caseful comparison + on the local parts. */ + + if (strcmpic(yield, topaddr->address) == 0 && + Ustrncmp(yield, topaddr->address, Ustrchr(yield, '@') - yield) == 0 && + addr->onetime_parent == NULL && + (!all_parents || addr->parent == NULL || addr->parent == topaddr)) + add_topaddr = FALSE; + } + +/* If all parents are requested, or this is a local pipe/file/reply, and +there is at least one intermediate parent, show it in brackets, and continue +with all of them if all are wanted. */ + +if ((all_parents || testflag(addr, af_pfr)) && + addr->parent != NULL && + addr->parent != topaddr) + { + uschar *s = US" ("; + address_item *addr2; + for (addr2 = addr->parent; addr2 != topaddr; addr2 = addr2->parent) + { + yield = string_cat(yield, &size, &ptr, s, 2); + yield = string_cat(yield, &size, &ptr, addr2->address, Ustrlen(addr2->address)); + if (!all_parents) break; + s = US", "; + } + yield = string_cat(yield, &size, &ptr, US")", 1); + } + +/* Add the top address if it is required */ + +if (add_topaddr) + { + yield = string_cat(yield, &size, &ptr, US" <", 2); + + if (addr->onetime_parent == NULL) + yield = string_cat(yield, &size, &ptr, topaddr->address, + Ustrlen(topaddr->address)); + else + yield = string_cat(yield, &size, &ptr, addr->onetime_parent, + Ustrlen(addr->onetime_parent)); + + yield = string_cat(yield, &size, &ptr, US">", 1); + } + +yield[ptr] = 0; /* string_cat() leaves space */ +return yield; +} +#endif /* COMPILE_UTILITY */ + + + + + +/************************************************* +************************************************** +* Stand-alone test program * +************************************************** +*************************************************/ + +#ifdef STAND_ALONE +int main(void) +{ +uschar buffer[256]; + +printf("Testing is_ip_address\n"); + +while (fgets(CS buffer, sizeof(buffer), stdin) != NULL) + { + int offset; + buffer[Ustrlen(buffer) - 1] = 0; + printf("%d\n", string_is_ip_address(buffer, NULL)); + printf("%d %d %s\n", string_is_ip_address(buffer, &offset), offset, buffer); + } + +printf("Testing string_nextinlist\n"); + +while (fgets(CS buffer, sizeof(buffer), stdin) != NULL) + { + uschar *list = buffer; + uschar *lp1, *lp2; + uschar item[256]; + int sep1 = 0; + int sep2 = 0; + + if (*list == '<') + { + sep1 = sep2 = list[1]; + list += 2; + } + + lp1 = lp2 = list; + for (;;) + { + uschar *item1 = string_nextinlist(&lp1, &sep1, item, sizeof(item)); + uschar *item2 = string_nextinlist(&lp2, &sep2, NULL, 0); + + if (item1 == NULL && item2 == NULL) break; + if (item == NULL || item2 == NULL || Ustrcmp(item1, item2) != 0) + { + printf("***ERROR\nitem1=\"%s\"\nitem2=\"%s\"\n", + (item1 == NULL)? "NULL" : CS item1, + (item2 == NULL)? "NULL" : CS item2); + break; + } + else printf(" \"%s\"\n", CS item1); + } + } + +/* This is a horrible lash-up, but it serves its purpose. */ + +printf("Testing string_format\n"); + +while (fgets(CS buffer, sizeof(buffer), stdin) != NULL) + { + void *args[3]; + double dargs[3]; + int dflag = 0; + int n = 0; + int count; + int countset = 0; + uschar format[256]; + uschar outbuf[256]; + uschar *s; + buffer[Ustrlen(buffer) - 1] = 0; + + s = Ustrchr(buffer, ','); + if (s == NULL) s = buffer + Ustrlen(buffer); + + Ustrncpy(format, buffer, s - buffer); + format[s-buffer] = 0; + + if (*s == ',') s++; + + while (*s != 0) + { + uschar *ss = s; + s = Ustrchr(ss, ','); + if (s == NULL) s = ss + Ustrlen(ss); + + if (isdigit(*ss)) + { + Ustrncpy(outbuf, ss, s-ss); + if (Ustrchr(outbuf, '.') != NULL) + { + dflag = 1; + dargs[n++] = Ustrtod(outbuf, NULL); + } + else + { + args[n++] = (void *)Uatoi(outbuf); + } + } + + else if (Ustrcmp(ss, "*") == 0) + { + args[n++] = (void *)(&count); + countset = 1; + } + + else + { + uschar *sss = malloc(s - ss + 1); + Ustrncpy(sss, ss, s-ss); + args[n++] = sss; + } + + if (*s == ',') s++; + } + + if (!dflag) printf("%s\n", string_format(outbuf, sizeof(outbuf), CS format, + args[0], args[1], args[2])? "True" : "False"); + + else printf("%s\n", string_format(outbuf, sizeof(outbuf), CS format, + dargs[0], dargs[1], dargs[2])? "True" : "False"); + + printf("%s\n", CS outbuf); + if (countset) printf("count=%d\n", count); + } + +return 0; +} +#endif + +/* End of string.c */ diff --git a/src/src/structs.h b/src/src/structs.h new file mode 100644 index 000000000..453582766 --- /dev/null +++ b/src/src/structs.h @@ -0,0 +1,776 @@ +/* $Cambridge: exim/src/src/structs.h,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + + +/* Definitions of various structures. In addition, those that are visible for +the compilation of local_scan() are defined in local_scan.h. These are + + header_line + optionlist + recipient_item + +For those declared here, we have to pre-declare some because of mutually +recursive definitions in the auths, routers, and transports blocks. */ + +struct address_item; +struct auth_info; +struct driver_info; +struct director_info; +struct smtp_inblock; +struct smtp_outblock; +struct transport_info; +struct router_info; + +/* Structure for remembering macros for the configuration file */ + +typedef struct macro_item { + struct macro_item *next; + BOOL command_line; + uschar *replacement; + uschar name[1]; +} macro_item; + +/* Structure for bit tables for debugging and logging */ + +typedef struct bit_table { + uschar *name; + unsigned int bit; +} bit_table; + +/* Block for holding a uid and gid, possibly unset, and an initgroups flag. */ + +typedef struct ugid_block { + uid_t uid; + gid_t gid; + BOOL uid_set; + BOOL gid_set; + BOOL initgroups; +} ugid_block; + +/* Structure for holding information about a host for use mainly by routers, +but also used when checking lists of hosts and when transporting. Looking up +host addresses is done using this structure. */ + +typedef struct host_item { + struct host_item *next; + uschar *name; /* Host name */ + uschar *address; /* IP address in text form */ + int port; /* port value in host order (if SRV lookup) */ + int mx; /* MX value if found via MX records */ + int sort_key; /* MX*1000 plus random "fraction" */ + int status; /* Usable, unusable, or unknown */ + int why; /* Why host is unusable */ + int last_try; /* Time of last try if known */ + BOOL update_waiting; /* Turned off if wait db not to be updated */ +} host_item; + +/* Chain of rewrite rules, read from the rewrite config, or parsed from the +rewrite_headers field of a transport. */ + +typedef struct rewrite_rule { + struct rewrite_rule *next; + int flags; + uschar *key; + uschar *replacement; +} rewrite_rule; + +/* This structure is used to pass back configuration data from the smtp +transport to the outside world. It is used during callback processing. If ever +another remote transport were implemented, it could use the same structure. */ + +typedef struct transport_feedback { + uschar *interface; + uschar *port; + uschar *protocol; + uschar *hosts; + BOOL hosts_override; + BOOL hosts_randomize; + BOOL gethostbyname; + BOOL qualify_single; + BOOL search_parents; +} transport_feedback; + +/* Routers, transports, and authenticators have similar data blocks. Each +driver that is compiled into the code is represented by a xxx_info block; the +active drivers are represented by a chain of xxx_instance blocks. To make it +possible to use the same code for reading the configuration files for all +three, the layout of the start of the blocks is kept the same, and represented +by the generic structures driver_info and driver_instance. */ + +typedef struct driver_instance { + struct driver_instance *next; + uschar *name; /* Instance name */ + struct driver_info *info; /* Points to info for this driver */ + void *options_block; /* Pointer to private options */ + uschar *driver_name; /* All start with this generic option */ +} driver_instance; + +typedef struct driver_info { + uschar *driver_name; /* Name of driver */ + optionlist *options; /* Table of private options names */ + int *options_count; /* -> Number of entries in table */ + void *options_block; /* Points to default private block */ + int options_len; /* Length of same in bytes */ + void (*init)( /* Initialization entry point */ + struct driver_instance *); +} driver_info; + + +/* Structure for holding information about the configured transports. Some +of the generally accessible options are set from the configuration file; others +are set by transport initialization, since they can only be set for certain +transports. They need to be generally accessible, however, as they are used by +the main transport code. */ + +typedef struct transport_instance { + struct transport_instance *next; + uschar *name; /* Instance name */ + struct transport_info *info; /* Info for this driver */ + void *options_block; /* Pointer to private options */ + uschar *driver_name; /* Must be first */ + int (*setup)( /* Setup entry point */ + struct transport_instance *, + struct address_item *, + struct transport_feedback *, /* For passing back config data */ + uschar **); /* For an error message */ + /**************************************/ + int batch_max; /* ) */ + uschar *batch_id; /* ) */ + uschar *home_dir; /* ) Used only for local transports */ + uschar *current_dir; /* ) */ + /**************************************/ + BOOL multi_domain; /* ) */ + BOOL overrides_hosts; /* ) Used only for remote transports */ + int max_addresses; /* ) */ + int connection_max_messages;/* ) */ + /**************************************/ + BOOL deliver_as_creator; /* Used only by pipe at present */ + BOOL disable_logging; /* For very weird requirements */ + BOOL initgroups; /* Initialize groups when setting uid */ + BOOL uid_set; /* uid is set */ + BOOL gid_set; /* gid is set */ + uid_t uid; + gid_t gid; + uschar *expand_uid; /* Variable uid */ + uschar *expand_gid; /* Variable gid */ + uschar *warn_message; /* Used only by appendfile at present */ + uschar *shadow; /* Name of shadow transport */ + uschar *shadow_condition; /* Condition for running it */ + uschar *filter_command; /* For on-the-fly-filtering */ + uschar *add_headers; /* Add these headers */ + uschar *remove_headers; /* Remove these headers */ + uschar *return_path; /* Overriding (rewriting) return path */ + uschar *debug_string; /* Debugging output */ + uschar *message_size_limit; /* Biggest message this transport handles */ + uschar *headers_rewrite; /* Rules for rewriting headers */ + rewrite_rule *rewrite_rules; /* Parsed rewriting rules */ + int rewrite_existflags; /* Bits showing which headers are rewritten */ + int filter_timeout; /* For transport filter timing */ + BOOL body_only; /* Deliver only the body */ + BOOL delivery_date_add; /* Add Delivery-Date header */ + BOOL envelope_to_add; /* Add Envelope-To header */ + BOOL headers_only; /* Deliver only the headers */ + BOOL rcpt_include_affixes; /* TRUE to retain affixes in RCPT commands */ + BOOL return_path_add; /* Add Return-Path header */ + BOOL return_output; /* TRUE if output should always be returned */ + BOOL return_fail_output; /* ditto, but only on failure */ + BOOL log_output; /* Similarly for logging */ + BOOL log_fail_output; + BOOL log_defer_output; + BOOL retry_use_local_part; /* Defaults true for local, false for remote */ +} transport_instance; + + +/* Structure for holding information about a type of transport. The first six +fields must match driver_info above. */ + +typedef struct transport_info { + uschar *driver_name; /* Driver name */ + optionlist *options; /* Table of private options names */ + int *options_count; /* -> Number of entries in table */ + void *options_block; /* Points to default private block */ + int options_len; /* Length of same in bytes */ + void (*init)( /* Initialization function */ + struct transport_instance *); +/****/ + BOOL (*code)( /* Main entry point */ + transport_instance *, + struct address_item *); + void (*tidyup)( /* Tidyup function */ + struct transport_instance *); + void (*closedown)( /* For closing down a passed channel */ + struct transport_instance *); + BOOL local; /* TRUE for local transports */ +} transport_info; + + + +/* Structure for holding information about the configured routers. */ + +typedef struct router_instance { + struct router_instance *next; + uschar *name; + struct router_info *info; + void *options_block; /* Pointer to private options */ + uschar *driver_name; /* Must be first */ + + uschar *address_data; /* Arbitrary data */ + uschar *cannot_route_message; /* Used when routing fails */ + uschar *condition; /* General condition */ + uschar *current_directory; /* For use during delivery */ + uschar *debug_string; /* Debugging output */ + uschar *domains; /* Specific domains */ + uschar *errors_to; /* Errors address */ + uschar *expand_gid; /* Expanded gid string */ + uschar *expand_uid; /* Expanded uid string */ + uschar *expand_more; /* Expanded more string */ + uschar *expand_unseen; /* Expanded unseen string */ + uschar *extra_headers; /* Additional headers */ + uschar *fallback_hosts; /* For remote transports (text list) */ + uschar *home_directory; /* For use during delivery */ + uschar *ignore_target_hosts; /* Target hosts to ignore */ + uschar *local_parts; /* Specific local parts */ + uschar *pass_router_name; /* Router for passed address */ + uschar *prefix; /* Address prefix */ + uschar *redirect_router_name; /* Router for generated address */ + uschar *remove_headers; /* Removed headers */ + uschar *require_files; /* File checks before router is run */ + uschar *router_home_directory; /* For use while routing */ + uschar *self; /* Text option for handling self reference */ + uschar *senders; /* Specific senders */ + uschar *suffix; /* Address suffix */ + uschar *translate_ip_address; /* IP address translation fudgery */ + uschar *transport_name; /* Transport name */ + + BOOL address_test; /* Use this router when testing addresses */ + BOOL expn; /* Use this router when processing EXPN */ + BOOL caseful_local_part; /* TRUE => don't lowercase */ + BOOL check_local_user; /* TRUE => check local user */ + BOOL disable_logging; /* For very weird requirements */ + BOOL fail_verify_recipient; /* Fail verify if recipient match this router */ + BOOL fail_verify_sender; /* Fail verify if sender match this router */ + BOOL gid_set; /* Flag to indicate gid is set */ + BOOL initgroups; /* TRUE if initgroups is required */ + BOOL log_as_local; /* TRUE logs as a local delivery */ + BOOL more; /* If FALSE, do no more if this one fails */ + BOOL pass_on_timeout; /* Treat timeout DEFERs as fails */ + BOOL prefix_optional; /* Just what it says */ + BOOL repeat_use; /* If FALSE, skip if ancestor used it */ + BOOL retry_use_local_part; /* Just what it says */ + BOOL same_domain_copy_routing; /* TRUE => copy routing for same domain */ + BOOL self_rewrite; /* TRUE to rewrite headers if making local */ + BOOL suffix_optional; /* As it says */ + BOOL verify_only; /* Skip this router if not verifying */ + BOOL verify_recipient; /* Use this router when verifying a recipient*/ + BOOL verify_sender; /* Use this router when verifying a sender */ + BOOL uid_set; /* Flag to indicate uid is set */ + BOOL unseen; /* If TRUE carry on, even after success */ + + int self_code; /* Encoded version of "self" */ + uid_t uid; /* Fixed uid value */ + gid_t gid; /* Fixed gid value */ + + host_item *fallback_hostlist; /* For remote transport (block chain) */ + transport_instance *transport; /* Transport block (when found) */ + struct router_instance *pass_router; /* Actual router for passed address */ + struct router_instance *redirect_router; /* Actual router for generated address */ +} router_instance; + + +/* Structure for holding information about a type of router. The first six +fields must match driver_info above. */ + +typedef struct router_info { + uschar *driver_name; + optionlist *options; /* Table of private options names */ + int *options_count; /* -> Number of entries in table */ + void *options_block; /* Points to default private block */ + int options_len; /* Length of same in bytes */ + void (*init)( /* Initialization function */ + struct router_instance *); +/****/ + int (*code)( /* Main entry point */ + router_instance *, + struct address_item *, + struct passwd *, + BOOL, + struct address_item **, + struct address_item **, + struct address_item **, + struct address_item **); + void (*tidyup)( /* Tidyup function */ + struct router_instance *); + int ri_flags; /* Descriptive flags */ +} router_info; + + +/* Structure for holding information about a lookup type. */ + +typedef struct lookup_info { + uschar *name; /* e.g. "lsearch" */ + int type; /* query/singlekey/abs-file */ + void *(*open)( /* open function */ + uschar *, /* file name for those that have one */ + uschar **); /* for error message */ + BOOL (*check)( /* file checking function */ + void *, /* handle */ + uschar *, /* file name */ + int, /* modemask for file checking */ + uid_t *, /* owners for file checking */ + gid_t *, /* owngroups for file checking */ + uschar **); /* for error messages */ + int (*find)( /* find function */ + void *, /* handle */ + uschar *, /* file name or NULL */ + uschar *, /* key or query */ + int, /* length of key or query */ + uschar **, /* for returning answer */ + uschar **, /* for error message */ + BOOL *); /* to request cache cleanup */ + void (*close)( /* close function */ + void *); /* handle */ + void (*tidy)(void); /* tidy function */ + uschar *(*quote)( /* quoting function */ + uschar *, /* string to quote */ + uschar *); /* additional data from quote name */ +} lookup_info; + + +/* Structure for holding information about the configured authentication +mechanisms */ + +typedef struct auth_instance { + struct auth_instance *next; + uschar *name; /* Exim instance name */ + struct auth_info *info; /* Pointer to driver info block */ + void *options_block; /* Pointer to private options */ + uschar *driver_name; /* Must be first */ + uschar *advertise_condition; /* Are we going to advertise this?*/ + uschar *public_name; /* Advertised name */ + uschar *set_id; /* String to set as authenticated id */ + uschar *mail_auth_condition; /* Condition for AUTH on MAIL command */ + uschar *server_debug_string; /* Debugging output */ + BOOL client; /* TRUE if client option(s) set */ + BOOL server; /* TRUE if server options(s) set */ + BOOL advertised; /* Set TRUE when advertised */ +} auth_instance; + + +/* Structure for holding information about an authentication mechanism. The +first six fields must match driver_info above. */ + +typedef struct auth_info { + uschar *driver_name; /* e.g. "condition" */ + optionlist *options; /* Table of private options names */ + int *options_count; /* -> Number of entries in table */ + void *options_block; /* Points to default private block */ + int options_len; /* Length of same in bytes */ + void (*init)( /* initialization function */ + struct auth_instance *); +/****/ + int (*servercode)( /* server function */ + auth_instance *, /* the instance data */ + uschar *); /* rest of AUTH command */ + int (*clientcode)( /* client function */ + struct auth_instance *, + struct smtp_inblock *, /* socket and input buffer */ + struct smtp_outblock *, /* socket and output buffer */ + int, /* command timeout */ + uschar *, /* buffer for reading response */ + int); /* sizeof buffer */ +} auth_info; + + +/* Structure for holding a single IP address and port; used for the chain of +addresses and ports for the local host. Make the char string large enough to +hold an IPv6 address. */ + +typedef struct ip_address_item { + struct ip_address_item *next; + int port; + BOOL v6_include_v4; /* Used in the daemon */ + uschar address[46]; +} ip_address_item; + +/* Structure for chaining together arbitrary strings. */ + +typedef struct string_item { + struct string_item *next; + uschar *text; +} string_item; + +/* Information about a soft delivery failure, for use when calculating +retry information. It's separate from the address block, because there +can be a chain of them for SMTP deliveries where multiple IP addresses +can be tried. */ + +typedef struct retry_item { + struct retry_item *next; /* for chaining */ + uschar *key; /* string identifying host/address/message */ + int basic_errno; /* error code for this destination */ + int more_errno; /* additional error information */ + uschar *message; /* local error message */ + int flags; /* see below */ +} retry_item; + +/* Retry data flags */ + +#define rf_delete 0x0001 /* retry info is to be deleted */ +#define rf_host 0x0002 /* retry info is for a remote host */ +#define rf_message 0x0004 /* retry info is for a host+message */ + +/* Information about a constructed message that is to be sent using the +autoreply transport. This is pointed to from the address block. */ + +typedef struct reply_item { + uschar *from; /* ) */ + uschar *reply_to; /* ) */ + uschar *to; /* ) */ + uschar *cc; /* ) specific header fields */ + uschar *bcc; /* ) */ + uschar *subject; /* ) */ + uschar *headers; /* misc other headers, concatenated */ + uschar *text; /* text string body */ + uschar *file; /* file body */ + BOOL file_expand; /* expand the body */ + int expand_forbid; /* expansion lockout flags */ + uschar *logfile; /* file to keep a log in */ + uschar *oncelog; /* file to keep records in for once only */ + time_t once_repeat; /* time to repeat "once only" */ + BOOL return_message; /* send back the original message */ +} reply_item; + + +/* The address_item structure contains many fields which are used at various +times while delivering a message. Some are used only for remote deliveries; +some only for local. A particular set of fields is copied whenever a child +address is created. For convenience, we keep those fields in a separate +sub-structure so they can be copied in one go. This also means I won't forget +to edit the various copying places when new to-be-copied fields are added. */ + +typedef struct address_item_propagated { + uschar *address_data; /* arbitrary data to keep with the address */ + uschar *domain_data; /* from "domains" lookup */ + uschar *localpart_data; /* from "local_parts" lookup */ + uschar *errors_address; /* where to send errors (NULL => sender) */ + header_line *extra_headers; /* additional headers */ + uschar *remove_headers; /* list of those to remove */ +} address_item_propagated; + +/* Bits for the flags field below */ + +#define af_allow_file 0x00000001 /* allow file in generated address */ +#define af_allow_pipe 0x00000002 /* allow pipe in generated address */ +#define af_allow_reply 0x00000004 /* allow autoreply in generated address */ +#define af_dr_retry_exists 0x00000008 /* router retry record exists */ +#define af_expand_pipe 0x00000010 /* expand pipe arguments */ +#define af_file 0x00000020 /* file delivery; always with pfr */ +#define af_gid_set 0x00000040 /* gid field is set */ +#define af_home_expanded 0x00000080 /* home_dir is already expanded */ +#define af_ignore_error 0x00000100 /* ignore delivery error */ +#define af_initgroups 0x00000200 /* use initgroups() for local transporting */ +#define af_local_host_removed 0x00000400 /* local host was backup */ +#define af_lt_retry_exists 0x00000800 /* local transport retry exists */ +#define af_pfr 0x00001000 /* pipe or file or reply delivery */ +#define af_retry_skipped 0x00002000 /* true if retry caused some skipping */ +#define af_retry_timedout 0x00004000 /* true if retry timed out */ +#define af_uid_set 0x00008000 /* uid field is set */ +#define af_hide_child 0x00010000 /* hide child in bounce/defer msgs */ +#define af_sverify_told 0x00020000 /* sender verify failure notified */ +#define af_verify_pmfail 0x00040000 /* verify failure was postmaster callout */ +#define af_verify_nsfail 0x00080000 /* verify failure was null sender callout */ +#define af_homonym 0x00100000 /* an ancestor has same address */ +#define af_verify_routed 0x00200000 /* for cached sender verify: routed OK */ +#define af_verify_callout 0x00400000 /* for cached sender verify: callout was specified */ +#define af_include_affixes 0x00800000 /* delivered with affixes in RCPT */ +#define af_cert_verified 0x01000000 /* delivered with verified TLS cert */ + +/* These flags must be propagated when a child is created */ + +#define af_propagate (af_ignore_error) + +/* The main address structure. Note that fields that are to be copied to +generated addresses should be put in the address_item_propagated structure (see +above) rather than directly into the address_item structure. */ + +typedef struct address_item { + struct address_item *next; /* for chaining addresses */ + struct address_item *parent; /* parent address */ + struct address_item *first; /* points to first after group delivery */ + struct address_item *dupof; /* points to address this is a duplicate of */ + + router_instance *start_router; /* generated address starts here */ + router_instance *router; /* the router that routed */ + transport_instance *transport; /* the transport to use */ + + host_item *host_list; /* host data for the transport */ + host_item *host_used; /* host that took delivery or failed hard */ + host_item *fallback_hosts; /* to try if delivery defers */ + + reply_item *reply; /* data for autoreply */ + retry_item *retries; /* chain of retry information */ + + uschar *address; /* address being delivered or routed */ + uschar *unique; /* used for disambiguating */ + uschar *cc_local_part; /* caseful local part */ + uschar *lc_local_part; /* lowercased local part */ + uschar *local_part; /* points to cc or lc version */ + uschar *prefix; /* stripped prefix of local part */ + uschar *suffix; /* stripped suffix of local part */ + uschar *domain; /* working domain (lower cased) */ + + uschar *address_retry_key; /* retry key including full address */ + uschar *domain_retry_key; /* retry key for domain only */ + + uschar *current_dir; /* current directory for transporting */ + uschar *home_dir; /* home directory for transporting */ + uschar *message; /* error message */ + uschar *user_message; /* error message that can be sent over SMTP + or quoted in bounce message */ + uschar *onetime_parent; /* saved original parent for onetime */ + uschar **pipe_expandn; /* numeric expansions for pipe from filter */ + uschar *return_filename; /* name of return file */ + uschar *self_hostname; /* after self=pass */ + uschar *shadow_message; /* info about shadow transporting */ + + #ifdef SUPPORT_TLS + uschar *cipher; /* Cipher used for transport */ + uschar *peerdn; /* DN of server's certificate */ + #endif + + uid_t uid; /* uid for transporting */ + gid_t gid; /* gid for transporting */ + + unsigned int flags; /* a row of bits, defined above */ + unsigned int domain_cache[(MAX_NAMED_LIST * 2)/32]; + unsigned int localpart_cache[(MAX_NAMED_LIST * 2)/32]; + int mode; /* mode for local transporting to a file */ + int more_errno; /* additional error information */ + /* (may need to hold a timestamp) */ + + short int basic_errno; /* status after failure */ + short int child_count; /* number of child addresses */ + short int return_file; /* fileno of return data file */ + short int special_action; /* ( used when when deferred or failed */ + /* ( also */ + /* ( contains = or - when successful SMTP delivered */ + /* ( also */ + /* ( contains verify rc in sender verify cache */ + short int transport_return; /* result of delivery attempt */ + address_item_propagated p; /* fields that are propagated to children */ +} address_item; + +/* The table of header names consists of items of this type */ + +typedef struct { + uschar *name; + int len; + BOOL allow_resent; + int htype; +} header_name; + +/* Chain of information about errors (e.g. bad addresses) */ + +typedef struct error_block { + struct error_block *next; + uschar *text1; + uschar *text2; +} error_block; + +/* Chain of file names when processing the queue */ + +typedef struct queue_filename { + struct queue_filename *next; + uschar dir_uschar; + uschar text[1]; +} queue_filename; + +/* Chain of items of retry information, read from the retry config. */ + +typedef struct retry_rule { + struct retry_rule *next; + int rule; + int timeout; + int p1; + int p2; +} retry_rule; + +typedef struct retry_config { + struct retry_config *next; + uschar *pattern; + int basic_errno; + int more_errno; + uschar *senders; + retry_rule *rules; +} retry_config; + +/* Structure for each node in a tree, of which there are various kinds */ + +typedef struct tree_node { + struct tree_node *left; /* pointer to left child */ + struct tree_node *right; /* pointer to right child */ + union + { + void *ptr; /* pointer to data */ + int val; /* or integer data */ + } data; + uschar balance; /* balancing factor */ + uschar name[1]; /* node name - variable length */ +} tree_node; + +/* Structure for holding the handle and the cached last lookup for searches. +This block is pointed to by the tree entry for the file. The file can get +closed if too many are opened at once. There is a LRU chain for deciding which +to close. */ + +typedef struct search_cache { + void *handle; /* lookup handle, or NULL if closed */ + int search_type; /* search type */ + tree_node *up; /* LRU up pointer */ + tree_node *down; /* LRU down pointer */ + tree_node *item_cache; /* tree of cached results */ +} search_cache; + +/* Structure for holding a partially decoded DNS record; the name has been +uncompressed, but the data pointer is into the raw data. */ + +typedef struct { + uschar name[DNS_MAXNAME]; /* domain name */ + int type; /* record type */ + int size; /* size of data */ + uschar *data; /* pointer to data */ +} dns_record; + +/* Structure for holding the result of a DNS query. */ + +typedef struct { + int answerlen; /* length of the answer */ + uschar answer[MAXPACKET]; /* the answer itself */ +} dns_answer; + +/* Structure for holding the intermediate data while scanning a DNS answer +block. */ + +typedef struct { + int rrcount; /* count of RRs in the answer */ + uschar *aptr; /* pointer in the answer while scanning */ + dns_record srr; /* data from current record in scan */ +} dns_scan; + +/* Structure for holding a chain of IP addresses that are extracted from +an A, AAAA, or A6 record. For the first two, there is only ever one address, +but the chaining feature of A6 allows for several addresses to be realized from +a single initial A6 record. The structure defines the address field of length +1. In use, a suitable sized block is obtained to hold the complete textual +address. */ + +typedef struct dns_address { + struct dns_address *next; + uschar address[1]; +} dns_address; + +/* Structure used for holding intermediate data during MD5 computations. */ + +typedef struct md5 { + unsigned int length; + unsigned int abcd[4]; + } +md5; + +/* Structure used for holding intermediate data during SHA-1 computations. */ + +typedef struct sha1 { + unsigned int H[5]; + unsigned int length; + } +sha1; + +/* Structure used to hold incoming packets of SMTP responses for a specific +socket. The packets which may contain multiple lines (and in some cases, +multiple responses). */ + +typedef struct smtp_inblock { + int sock; /* the socket */ + int buffersize; /* the size of the buffer */ + uschar *ptr; /* current position in the buffer */ + uschar *ptrend; /* end of data in the buffer */ + uschar *buffer; /* the buffer itself */ +} smtp_inblock; + +/* Structure used to hold buffered outgoing packets of SMTP commands for a +specific socket. The packets which may contain multiple lines when pipelining +is in use. */ + +typedef struct smtp_outblock { + int sock; /* the socket */ + int cmd_count; /* count of buffered commands */ + int buffersize; /* the size of the buffer */ + BOOL authenticating; /* TRUE when authenticating */ + uschar *ptr; /* current position in the buffer */ + uschar *buffer; /* the buffer itself */ +} smtp_outblock; + +/* Structure to hold information about the source of redirection information */ + +typedef struct redirect_block { + uschar *string; /* file name or string */ + uid_t *owners; /* allowed file owners */ + gid_t *owngroups; /* allowed file groups */ + struct passwd *pw; /* possible owner if not NULL */ + int modemask; /* forbidden bits */ + BOOL isfile; /* TRUE if string is a file name */ + BOOL check_owner; /* TRUE, FALSE, or TRUE_UNSET */ + BOOL check_group; /* TRUE, FALSE, or TRUE_UNSET */ +} redirect_block; + +/* Structure for passing arguments to check_host() */ + +typedef struct check_host_block { + uschar *host_name; + uschar *host_address; + uschar *host_ipv4; + BOOL negative; +} check_host_block; + +/* Structure for remembering lookup data when caching the result of +a lookup in a named list. */ + +typedef struct namedlist_cacheblock { + struct namedlist_cacheblock *next; + uschar *key; + uschar *data; +} namedlist_cacheblock; + +/* Structure for holding data for an entry in a named list */ + +typedef struct namedlist_block { + uschar *string; /* the list string */ + namedlist_cacheblock *cache_data; /* cached domain_data or localpart_data */ + int number; /* the number of the list for caching */ +} namedlist_block; + +/* Structures for Access Control Lists */ + +typedef struct acl_condition_block { + struct acl_condition_block *next; + uschar *arg; + int type; + union { + BOOL negated; + int varnumber; + } u; +} acl_condition_block; + +typedef struct acl_block { + struct acl_block *next; + acl_condition_block *condition; + int verb; +} acl_block; + +/* End of structs.h */ diff --git a/src/src/tls-gnu.c b/src/src/tls-gnu.c new file mode 100644 index 000000000..14a11ed56 --- /dev/null +++ b/src/src/tls-gnu.c @@ -0,0 +1,1212 @@ +/* $Cambridge: exim/src/src/tls-gnu.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* This module provides TLS (aka SSL) support for Exim using the GnuTLS +library. It is #included into tls.c when that library is used. The code herein +is based on a patch that was contributed by Nikos Mavroyanopoulos. + +No cryptographic code is included in Exim. All this module does is to call +functions from the GnuTLS library. */ + + +/* Heading stuff for GnuTLS */ + +#include +#include + + +#define UNKNOWN_NAME "unknown" +#define DH_BITS 768 +#define RSA_BITS 512 + +/* Values for verify_requirment and initialized */ + +enum { VERIFY_NONE, VERIFY_OPTIONAL, VERIFY_REQUIRED }; +enum { INITIALIZED_NOT, INITIALIZED_SERVER, INITIALIZED_CLIENT }; + +/* Local static variables for GNUTLS */ + +static BOOL initialized = INITIALIZED_NOT; +static host_item *client_host; + +static gnutls_rsa_params rsa_params = NULL; +static gnutls_dh_params dh_params = NULL; + +static gnutls_certificate_server_credentials x509_cred = NULL; +static gnutls_session tls_session = NULL; + +static char ssl_errstring[256]; + +static int ssl_session_timeout = 200; +static int verify_requirement; + +/* Priorities for TLS algorithms to use. At present, only the cipher priority +vector can be altered. */ + +static const int protocol_priority[16] = { GNUTLS_TLS1, GNUTLS_SSL3, 0 }; + +static const int kx_priority[16] = { + GNUTLS_KX_RSA, + GNUTLS_KX_DHE_DSS, + GNUTLS_KX_DHE_RSA, + GNUTLS_KX_RSA_EXPORT, + 0 }; + +static int default_cipher_priority[16] = { + GNUTLS_CIPHER_ARCFOUR_128, + GNUTLS_CIPHER_AES_128_CBC, + GNUTLS_CIPHER_3DES_CBC, + GNUTLS_CIPHER_ARCFOUR_40, + 0 }; + +static int cipher_priority[16]; + +static const int mac_priority[16] = { + GNUTLS_MAC_SHA, + GNUTLS_MAC_MD5, + 0 }; + +static const int comp_priority[16] = { GNUTLS_COMP_NULL, 0 }; +static const int cert_type_priority[16] = { GNUTLS_CRT_X509, 0 }; + +/* Tables of cipher names and equivalent numbers */ + +typedef struct pri_item { + uschar *name; + int *values; +} pri_item; + +static int arcfour_128_codes[] = { GNUTLS_CIPHER_ARCFOUR_128, 0 }; +static int arcfour_40_codes[] = { GNUTLS_CIPHER_ARCFOUR_40, 0 }; +static int arcfour_codes[] = { GNUTLS_CIPHER_ARCFOUR_128, + GNUTLS_CIPHER_ARCFOUR_40, 0 }; +static int aes_256_codes[] = { GNUTLS_CIPHER_AES_256_CBC, 0 }; +static int aes_128_codes[] = { GNUTLS_CIPHER_AES_128_CBC, 0 }; +static int aes_codes[] = { GNUTLS_CIPHER_AES_256_CBC, + GNUTLS_CIPHER_AES_128_CBC, 0 }; +static int des3_codes[] = { GNUTLS_CIPHER_3DES_CBC, 0 }; + +static pri_item cipher_index[] = { + { US"ARCFOUR_128", arcfour_128_codes }, + { US"ARCFOUR_40", arcfour_40_codes }, + { US"ARCFOUR", arcfour_codes }, + { US"AES_256", aes_256_codes }, + { US"AES_128", aes_128_codes }, + { US"AES", aes_codes }, + { US"3DES", des3_codes } +}; + + + +/************************************************* +* Handle TLS error * +*************************************************/ + +/* Called from lots of places when errors occur before actually starting to do +the TLS handshake, that is, while the session is still in clear. Always returns +DEFER for a server and FAIL for a client so that most calls can use "return +tls_error(...)" to do this processing and then give an appropriate return. A +single function is used for both server and client, because it is called from +some shared functions. + +Argument: + prefix text to include in the logged error + host NULL if setting up a server; + the connected host if setting up a client + err a GnuTLS error number, or 0 if local error + +Returns: OK/DEFER/FAIL +*/ + +static int +tls_error(uschar *prefix, host_item *host, int err) +{ +uschar *errtext = US""; +if (err != 0) errtext = string_sprintf(": %s", gnutls_strerror(err)); +if (host == NULL) + { + log_write(0, LOG_MAIN, "TLS error on connection from %s (%s)%s", + (sender_fullhost != NULL)? sender_fullhost : US "local process", + prefix, errtext); + return DEFER; + } +else + { + log_write(0, LOG_MAIN, "TLS error on connection to %s [%s] (%s)%s", + host->name, host->address, prefix, errtext); + return FAIL; + } +} + + + +/************************************************* +* Verify certificate * +*************************************************/ + +/* Called after a successful handshake, when certificate verification is +required or optional, for both server and client. + +Arguments: + session GNUTLS session + error where to put text giving a reason for failure + +Returns: TRUE/FALSE +*/ + +static BOOL +verify_certificate(gnutls_session session, uschar **error) +{ +int verify; +uschar *dn_string = US""; +const gnutls_datum *cert; +unsigned int cert_size = 0; + +*error = NULL; + +/* Get the peer's certificate. If it sent one, extract it's DN, and then +attempt to verify the certificate. If no certificate is supplied, verification +is forced to fail. */ + +cert = gnutls_certificate_get_peers(session, &cert_size); +if (cert != NULL) + { + uschar buff[1024]; + gnutls_x509_crt gcert; + + gnutls_x509_crt_init(&gcert); + dn_string = US"unknown"; + + if (gnutls_x509_crt_import(gcert, cert, GNUTLS_X509_FMT_DER) == 0) + { + size_t bufsize = sizeof(buff); + if (gnutls_x509_crt_get_dn(gcert, CS buff, &bufsize) >= 0) + dn_string = string_copy_malloc(buff); + } + + verify = gnutls_certificate_verify_peers(session); + } +else + { + DEBUG(D_tls) debug_printf("no peer certificate supplied\n"); + verify = GNUTLS_CERT_INVALID; + *error = US"not supplied"; + } + +/* Handle the result of verification. INVALID seems to be set as well +as REVOKED, but leave the test for both. */ + +if ((verify & (GNUTLS_CERT_INVALID|GNUTLS_CERT_REVOKED)) != 0) + { + tls_certificate_verified = FALSE; + if (*error == NULL) *error = ((verify & GNUTLS_CERT_REVOKED) != 0)? + US"revoked" : US"invalid"; + if (verify_requirement == VERIFY_REQUIRED) + { + DEBUG(D_tls) debug_printf("TLS certificate verification failed (%s): " + "peerdn=%s\n", *error, dn_string); + gnutls_alert_send(session, GNUTLS_AL_FATAL, GNUTLS_A_BAD_CERTIFICATE); + return FALSE; /* reject */ + } + DEBUG(D_tls) debug_printf("TLS certificate verify failure (%s) overridden " + "(host in tls_try_verify_hosts): peerdn=%s\n", *error, dn_string); + } +else + { + tls_certificate_verified = TRUE; + DEBUG(D_tls) debug_printf("TLS certificate verified: peerdn=%s\n", + dn_string); + } + +tls_peerdn = dn_string; +return TRUE; /* accept */ +} + + + + +/************************************************* +* Write/read datum to/from file * +*************************************************/ + +/* These functions are used for saving and restoring the RSA and D-H parameters +for use by all Exim processes. Data that is read is placed in malloc'd store +because that's what happens for newly generated data. + +Arguments: + fd the file descriptor + d points to the datum + +returns: FALSE on error (errno set) +*/ + +static BOOL +write_datum(int fd, gnutls_datum *d) +{ +if (write(fd, &(d->size), sizeof(d->size)) != sizeof(d->size)) return FALSE; +if (write(fd, d->data, d->size) != d->size) return FALSE; +return TRUE; +} + + +static BOOL +read_datum(int fd, gnutls_datum *d) +{ +if (read(fd, &(d->size), sizeof(d->size)) != sizeof(d->size)) return FALSE; +d->data = malloc(d->size); +if (d->data == NULL) return FALSE; +if (read(fd, d->data, d->size) != d->size) return FALSE; +return TRUE; +} + + + +/************************************************* +* Setup up RSA and DH parameters * +*************************************************/ + +/* Generating the RSA and D-H parameters takes a long time. They only need to +be re-generated every so often, depending on security policy. What we do is to +keep these parameters in a file in the spool directory. If the file does not +exist, we generate them. This means that it is easy to cause a regeneration. + +The new file is written as a temporary file and renamed, so that an incomplete +file is never present. If two processes both compute some new parameters, you +waste a bit of effort, but it doesn't seem worth messing around with locking to +prevent this. + +Argument: + host NULL for server, server for client (for error handling) + +Returns: OK/DEFER/FAIL +*/ + +static int +init_rsa_dh(host_item *host) +{ +int fd, ret; +gnutls_datum m, e, d, p, q, u, prime, generator; +uschar filename[200]; + +/* Initialize the data structures for holding the parameters */ + +ret = gnutls_rsa_params_init(&rsa_params); +if (ret < 0) return tls_error(US"init rsa_params", host, ret); + +ret = gnutls_dh_params_init(&dh_params); +if (ret < 0) return tls_error(US"init dh_params", host, ret); + +/* Set up the name of the cache file */ + +if (!string_format(filename, sizeof(filename), "%s/gnutls-params", + spool_directory)) + return tls_error(US"overlong filename", host, 0); + +/* Open the cache file for reading. If this fails because of a non-existent +file, compute a new set of parameters, write them to a temporary file, and then +rename that file as the cache file. Other opening errors are bad. */ + +fd = Uopen(filename, O_RDONLY, 0); +if (fd < 0) + { + unsigned int rsa_bits = RSA_BITS; + unsigned int dh_bits = DH_BITS; + uschar tempfilename[sizeof(filename) + 10]; + + if (errno != ENOENT) + return tls_error(string_open_failed(errno, "%s for reading", filename), + host, 0); + + DEBUG(D_tls) debug_printf("generating %d bit RSA key...\n", RSA_BITS); + ret = gnutls_rsa_params_generate2(rsa_params, RSA_BITS); + if (ret < 0) return tls_error(US"RSA key generation", host, ret); + + DEBUG(D_tls) debug_printf("generating %d bit Diffie-Hellman key...\n", + DH_BITS); + ret = gnutls_dh_params_generate2(dh_params, DH_BITS); + if (ret < 0) return tls_error(US"D-H key generation", host, ret); + + /* Write the parameters to a file in the spool directory so that we + can use them from other Exim processes. */ + + sprintf(CS tempfilename, "%s-%d", filename, (int)getpid()); + fd = Uopen(tempfilename, O_WRONLY|O_CREAT, 0400); + if (fd < 0) + return tls_error(string_open_failed(errno, "%s for writing", filename), + host, 0); + (void)fchown(fd, exim_uid, exim_gid); /* Probably not necessary */ + + ret = gnutls_rsa_params_export_raw(rsa_params, &m, &e, &d, &p, &q, &u, + &rsa_bits); + if (ret < 0) return tls_error(US"RSA params export", host, ret); + + ret = gnutls_dh_params_export_raw(dh_params, &prime, &generator, &dh_bits); + if (ret < 0) return tls_error(US"DH params export", host, ret); + + if (!write_datum(fd, &m) || + !write_datum(fd, &e) || + !write_datum(fd, &d) || + !write_datum(fd, &p) || + !write_datum(fd, &q) || + !write_datum(fd, &u) || + !write_datum(fd, &prime) || + !write_datum(fd, &generator)) + return tls_error(US"TLS cache write failed", host, 0); + + (void)close(fd); + + if (rename(CS tempfilename, CS filename) < 0) + return tls_error(string_sprintf("failed to rename %s as %s: %s", + tempfilename, filename, strerror(errno)), host, 0); + + DEBUG(D_tls) debug_printf("wrote RSA and D-H parameters to file\n"); + } + +/* File opened for reading; get the data */ + +else + { + if (!read_datum(fd, &m) || + !read_datum(fd, &e) || + !read_datum(fd, &d) || + !read_datum(fd, &p) || + !read_datum(fd, &q) || + !read_datum(fd, &u) || + !read_datum(fd, &prime) || + !read_datum(fd, &generator)) + return tls_error(US"TLS cache read failed", host, 0); + + (void)close(fd); + + ret = gnutls_rsa_params_import_raw(rsa_params, &m, &e, &d, &p, &q, &u); + if (ret < 0) return tls_error(US"RSA params import", host, ret); + + ret = gnutls_dh_params_import_raw(dh_params, &prime, &generator); + if (ret < 0) return tls_error(US"DH params import", host, ret); + + DEBUG(D_tls) debug_printf("read RSA and D-H parameters from file\n"); + } + +DEBUG(D_tls) debug_printf("initialized RSA and D-H parameters\n"); +return OK; +} + + + + +/************************************************* +* Initialize for GnuTLS * +*************************************************/ + +/* Called from both server and client code. In the case of a server, errors +before actual TLS negotiation return DEFER. + +Arguments: + host connected host, if client; NULL if server + certificate certificate file + privatekey private key file + cas CA certs file + crl CRL file + +Returns: OK/DEFER/FAIL +*/ + +static int +tls_init(host_item *host, uschar *certificate, uschar *privatekey, uschar *cas, + uschar *crl) +{ +int rc; +uschar *cert_expanded, *key_expanded, *cas_expanded, *crl_expanded; + +initialized = (host == NULL)? INITIALIZED_SERVER : INITIALIZED_CLIENT; + +rc = gnutls_global_init(); +if (rc < 0) return tls_error(US"tls-init", host, rc); + +/* Create RSA and D-H parameters, or read them from the cache file. This +function does its own SMTP error messaging. */ + +rc = init_rsa_dh(host); +if (rc != OK) return rc; + +/* Create the credentials structure */ + +rc = gnutls_certificate_allocate_credentials(&x509_cred); +if (rc < 0) return tls_error(US"certificate_allocate_credentials", host, rc); + +/* This stuff must be done for each session, because different certificates +may be required for different sessions. */ + +if (!expand_check(certificate, US"tls_certificate", &cert_expanded)) + return DEFER; + +if (privatekey != NULL) + { + if (!expand_check(privatekey, US"tls_privatekey", &key_expanded)) + return DEFER; + } +else key_expanded = cert_expanded; + +/* Set the certificate and private keys */ + +if (cert_expanded != NULL) + { + DEBUG(D_tls) debug_printf("certificate file = %s\nkey file = %s\n", + cert_expanded, key_expanded); + rc = gnutls_certificate_set_x509_key_file(x509_cred, CS cert_expanded, + CS key_expanded, GNUTLS_X509_FMT_PEM); + if (rc < 0) return tls_error(US"cert/key setup", host, rc); + } + +/* A certificate is mandatory in a server, but not in a client */ + +else + { + if (host == NULL) + return tls_error(US"no TLS server certificate is specified", host, 0); + DEBUG(D_tls) debug_printf("no TLS client certificate is specified\n"); + } + +/* Set the trusted CAs file if one is provided, and then add the CRL if one is +provided. Experiment shows that, if the certificate file is empty, an unhelpful +error message is provided. However, if we just refrain from setting anything up +in that case, certificate verification fails, which seems to be the correct +behaviour. */ + +if (cas != NULL) + { + struct stat statbuf; + + if (!expand_check(cas, US"tls_verify_certificates", &cas_expanded)) + return DEFER; + + if (stat(CS cas_expanded, &statbuf) < 0) + { + log_write(0, LOG_MAIN|LOG_PANIC, "could not stat %s " + "(tls_verify_certificates): %s", cas_expanded, strerror(errno)); + return DEFER; + } + + DEBUG(D_tls) debug_printf("verify certificates = %s size=%d\n", + cas_expanded, (int)statbuf.st_size); + + /* If the cert file is empty, there's no point in loading the CRL file. */ + + if (statbuf.st_size > 0) + { + rc = gnutls_certificate_set_x509_trust_file(x509_cred, CS cas_expanded, + GNUTLS_X509_FMT_PEM); + if (rc < 0) return tls_error(US"setup_certs", host, rc); + + if (crl != NULL && *crl != 0) + { + if (!expand_check(crl, US"tls_crl", &crl_expanded)) + return DEFER; + DEBUG(D_tls) debug_printf("loading CRL file = %s\n", crl_expanded); + rc = gnutls_certificate_set_x509_crl_file(x509_cred, CS crl_expanded, + GNUTLS_X509_FMT_PEM); + if (rc < 0) return tls_error(US"CRL setup", host, rc); + } + } + } + +/* Associate the parameters with the x509 credentials structure. */ + +gnutls_certificate_set_dh_params(x509_cred, dh_params); +gnutls_certificate_set_rsa_params(x509_cred, rsa_params); + +DEBUG(D_tls) debug_printf("initialized certificate stuff\n"); +return OK; +} + + + + +/************************************************* +* Remove ciphers from priority list * +*************************************************/ + +/* Cautiously written so that it will remove duplicates if present. + +Arguments: + list a zero-terminated list + remove_list a zero-terminated list to be removed + +Returns: nothing +*/ + +static void +remove_ciphers(int *list, int *remove_list) +{ +for (; *remove_list != 0; remove_list++) + { + int *p = list; + while (*p != 0) + { + if (*p == *remove_list) + { + int *pp = p; + do { pp[0] = pp[1]; pp++; } while (*pp != 0); + } + else p++; + } + } +} + + + +/************************************************* +* Add ciphers to priority list * +*************************************************/ + +/* Cautiously written to check the list size + +Arguments: + list a zero-terminated list + list_max maximum offset in the list + add_list a zero-terminated list to be added + +Returns: TRUE if OK; FALSE if list overflows +*/ + +static BOOL +add_ciphers(int *list, int list_max, int *add_list) +{ +int next = 0; +while (list[next] != 0) next++; +while (*add_list != 0) + { + if (next >= list_max) return FALSE; + list[next++] = *add_list++; + } +list[next] = 0; +return TRUE; +} + + + +/************************************************* +* Initialize a single GNUTLS session * +*************************************************/ + +/* Set the algorithm, the db backend, whether to request certificates etc. + +TLS in Exim was first implemented using OpenSSL. This has a function to which +you pass a list of cipher suites that are permitted/not permitted. GnuTLS works +differently. It operates using priority lists for the different components of +cipher suites. + +For compatibility of configuration, we scan a list of cipher suites and set +priorities therefrom. However, at the moment, we pay attention only to the bulk +cipher. + +Arguments: + side one of GNUTLS_SERVER, GNUTLS_CLIENT + expciphers expanded ciphers list + +Returns: a gnutls_session, or NULL if there is a problem +*/ + +static gnutls_session +tls_session_init(int side, uschar *expciphers) +{ +gnutls_session session; + +gnutls_init(&session, side); + +/* Handle the list of permitted ciphers */ + +memcpy(cipher_priority, default_cipher_priority, sizeof(cipher_priority)); + +if (expciphers != NULL) + { + int sep = 0; + BOOL first = TRUE; + uschar *cipher; + + /* The names OpenSSL uses are of the form DES-CBC3-SHA, using hyphen + separators. GnuTLS uses underscore separators. So that I can use either form + in my tests, and also for general convenience, we turn hyphens into + underscores before scanning the list. */ + + uschar *s = expciphers; + while (*s != 0) { if (*s == '-') *s = '_'; s++; } + + while ((cipher = string_nextinlist(&expciphers, &sep, big_buffer, + big_buffer_size)) != NULL) + { + int i; + BOOL exclude = cipher[0] == '!'; + if (first && !exclude) cipher_priority[0] = 0; + first = FALSE; + + for (i = 0; i < sizeof(cipher_index)/sizeof(pri_item); i++) + { + uschar *ss = strstric(cipher, cipher_index[i].name, FALSE); + if (ss != NULL) + { + uschar *endss = ss + Ustrlen(cipher_index[i].name); + if ((ss == cipher || !isalnum(ss[-1])) && !isalnum(*endss)) + { + if (exclude) + remove_ciphers(cipher_priority, cipher_index[i].values); + else + { + if (!add_ciphers(cipher_priority, + sizeof(cipher_priority)/sizeof(pri_item), + cipher_index[i].values)) + { + log_write(0, LOG_MAIN|LOG_PANIC, "GnuTLS init failed: cipher " + "priority table overflow"); + gnutls_deinit(session); + return NULL; + } + } + } + } + } + } + + DEBUG(D_tls) + { + int *ptr = cipher_priority; + debug_printf("adjusted cipher priorities:"); + while (*ptr != 0) debug_printf(" %d", *ptr++); + debug_printf("\n"); + } + } + +/* Define the various priorities */ + +gnutls_cipher_set_priority(session, cipher_priority); +gnutls_compression_set_priority(session, comp_priority); +gnutls_kx_set_priority(session, kx_priority); +gnutls_protocol_set_priority(session, protocol_priority); +gnutls_mac_set_priority(session, mac_priority); + +gnutls_cred_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred); + +gnutls_dh_set_prime_bits(session, DH_BITS); + +/* Request or demand a certificate of the peer, as configured. This will +happen only in a server. */ + +if (verify_requirement != VERIFY_NONE) + gnutls_certificate_server_set_request(session, + (verify_requirement == VERIFY_OPTIONAL)? + GNUTLS_CERT_REQUEST : GNUTLS_CERT_REQUIRE); + +gnutls_db_set_cache_expiration(session, ssl_session_timeout); + +DEBUG(D_tls) debug_printf("initialized GnuTLS session\n"); +return session; +} + + + +/************************************************* +* Get name of cipher in use * +*************************************************/ + +/* The answer is left in a static buffer, and tls_cipher is set to point +to it. + +Argument: pointer to a GnuTLS session +Returns: nothing +*/ + +static void +construct_cipher_name(gnutls_session session) +{ +static uschar cipherbuf[256]; +uschar *ver; +int bits, c, kx, mac; + +ver = string_copy( + US gnutls_protocol_get_name(gnutls_protocol_get_version(session))); +if (Ustrncmp(ver, "TLS ", 4) == 0) ver[3] = '-'; /* Don't want space */ + +c = gnutls_cipher_get(session); +bits = gnutls_cipher_get_key_size(c); + +mac = gnutls_mac_get(session); +kx = gnutls_kx_get(session); + +string_format(cipherbuf, sizeof(cipherbuf), "%s:%s:%u", ver, + gnutls_cipher_suite_get_name(kx, c, mac), bits); +tls_cipher = cipherbuf; + +DEBUG(D_tls) debug_printf("cipher: %s\n", cipherbuf); +} + + + +/************************************************* +* Start a TLS session in a server * +*************************************************/ + +/* This is called when Exim is running as a server, after having received +the STARTTLS command. It must respond to that command, and then negotiate +a TLS session. + +Arguments: + require_ciphers list of allowed ciphers + +Returns: OK on success + DEFER for errors before the start of the negotiation + FAIL for errors during the negotation; the server can't + continue running. +*/ + +int +tls_server_start(uschar *require_ciphers) +{ +int rc; +uschar *error; +uschar *expciphers = NULL; + +/* Check for previous activation */ + +if (tls_active >= 0) + { + log_write(0, LOG_MAIN, "STARTTLS received in already encrypted " + "connection from %s", + (sender_fullhost != NULL)? sender_fullhost : US"local process"); + smtp_printf("554 Already in TLS\r\n"); + return FAIL; + } + +/* Initialize the library. If it fails, it will already have logged the error +and sent an SMTP response. */ + +DEBUG(D_tls) debug_printf("initializing GnuTLS as a server\n"); + +rc = tls_init(NULL, tls_certificate, tls_privatekey, tls_verify_certificates, + tls_crl); +if (rc != OK) return rc; + +if (!expand_check(require_ciphers, US"tls_require_ciphers", &expciphers)) + return FAIL; + +/* If this is a host for which certificate verification is mandatory or +optional, set up appropriately. */ + +tls_certificate_verified = FALSE; +verify_requirement = VERIFY_NONE; + +if (verify_check_host(&tls_verify_hosts) == OK) + verify_requirement = VERIFY_REQUIRED; +else if (verify_check_host(&tls_try_verify_hosts) == OK) + verify_requirement = VERIFY_OPTIONAL; + +/* Prepare for new connection */ + +tls_session = tls_session_init(GNUTLS_SERVER, expciphers); +if (tls_session == NULL) + return tls_error(US"tls_session_init", NULL, GNUTLS_E_MEMORY_ERROR); + +/* Set context and tell client to go ahead, except in the case of TLS startup +on connection, where outputting anything now upsets the clients and tends to +make them disconnect. We need to have an explicit fflush() here, to force out +the response. Other smtp_printf() calls do not need it, because in non-TLS +mode, the fflush() happens when smtp_getc() is called. */ + +if (!tls_on_connect) + { + smtp_printf("220 TLS go ahead\r\n"); + fflush(smtp_out); + } + +/* Now negotiate the TLS session. We put our own timer on it, since it seems +that the GnuTLS library doesn't. */ + +gnutls_transport_set_ptr(tls_session, (gnutls_transport_ptr)fileno(smtp_out)); + +sigalrm_seen = FALSE; +if (smtp_receive_timeout > 0) alarm(smtp_receive_timeout); +rc = gnutls_handshake(tls_session); +alarm(0); + +if (rc < 0) + { + if (sigalrm_seen) + Ustrcpy(ssl_errstring, "timed out"); + else + Ustrcpy(ssl_errstring, gnutls_strerror(rc)); + log_write(0, LOG_MAIN, + "TLS error on connection from %s (gnutls_handshake): %s", + (sender_fullhost != NULL)? sender_fullhost : US"local process", + ssl_errstring); + + /* It seems that, except in the case of a timeout, we have to close the + connection right here; otherwise if the other end is running OpenSSL it hangs + until the server times out. */ + + if (!sigalrm_seen) + { + fclose(smtp_out); + fclose(smtp_in); + } + + return FAIL; + } + +DEBUG(D_tls) debug_printf("gnutls_handshake was successful\n"); + +if (verify_requirement != VERIFY_NONE && + !verify_certificate(tls_session, &error)) + { + log_write(0, LOG_MAIN, + "TLS error on connection from %s: certificate verification failed (%s)", + (sender_fullhost != NULL)? sender_fullhost : US"local process", error); + return FAIL; + } + +construct_cipher_name(tls_session); + +/* TLS has been set up. Adjust the input functions to read via TLS, +and initialize appropriately. */ + +ssl_xfer_buffer = store_malloc(ssl_xfer_buffer_size); +ssl_xfer_buffer_lwm = ssl_xfer_buffer_hwm = 0; +ssl_xfer_eof = ssl_xfer_error = 0; + +receive_getc = tls_getc; +receive_ungetc = tls_ungetc; +receive_feof = tls_feof; +receive_ferror = tls_ferror; + +tls_active = fileno(smtp_out); + +return OK; +} + + + + +/************************************************* +* Start a TLS session in a client * +*************************************************/ + +/* Called from the smtp transport after STARTTLS has been accepted. + +Arguments: + fd the fd of the connection + host connected host (for messages) + addr + dhparam DH parameter file + certificate certificate file + privatekey private key file + verify_certs file for certificate verify + verify_crl CRL for verify + require_ciphers list of allowed ciphers + timeout startup timeout + +Returns: OK/DEFER/FAIL (because using common functions), + but for a client, DEFER and FAIL have the same meaning +*/ + +int +tls_client_start(int fd, host_item *host, address_item *addr, uschar *dhparam, + uschar *certificate, uschar *privatekey, uschar *verify_certs, + uschar *verify_crl, uschar *require_ciphers, int timeout) +{ +const gnutls_datum *server_certs; +uschar *expciphers = NULL; +uschar *error; +unsigned int server_certs_size; +int rc; + +DEBUG(D_tls) debug_printf("initializing GnuTLS as a client\n"); + +client_host = host; +verify_requirement = (verify_certs == NULL)? VERIFY_NONE : VERIFY_REQUIRED; +rc = tls_init(host, certificate, privatekey, verify_certs, verify_crl); +if (rc != OK) return rc; + +if (!expand_check(require_ciphers, US"tls_require_ciphers", &expciphers)) + return FAIL; + +tls_session = tls_session_init(GNUTLS_CLIENT, expciphers); +if (tls_session == NULL) + return tls_error(US "tls_session_init", host, GNUTLS_E_MEMORY_ERROR); + +gnutls_transport_set_ptr(tls_session, (gnutls_transport_ptr)fd); + +/* There doesn't seem to be a built-in timeout on connection. */ + +sigalrm_seen = FALSE; +alarm(timeout); +rc = gnutls_handshake(tls_session); +alarm(0); + +if (rc < 0) + { + if (sigalrm_seen) + { + log_write(0, LOG_MAIN, "TLS error on connection to %s [%s]: " + "gnutls_handshake timed out", host->name, host->address); + return FAIL; + } + else return tls_error(US "gnutls_handshake", host, rc); + } + +server_certs = gnutls_certificate_get_peers(tls_session, &server_certs_size); + +if (server_certs != NULL) + { + uschar buff[1024]; + gnutls_x509_crt gcert; + + gnutls_x509_crt_init(&gcert); + tls_peerdn = US"unknown"; + + if (gnutls_x509_crt_import(gcert, server_certs, GNUTLS_X509_FMT_DER) == 0) + { + size_t bufsize = sizeof(buff); + if (gnutls_x509_crt_get_dn(gcert, CS buff, &bufsize) >= 0) + tls_peerdn = string_copy_malloc(buff); + } + } + +/* Should we also verify the hostname here? */ + +if (verify_requirement != VERIFY_NONE && + !verify_certificate(tls_session, &error)) + { + log_write(0, LOG_MAIN, + "TLS error on connection to %s [%s]: certificate verification failed (%s)", + host->name, host->address, error); + return FAIL; + } + +construct_cipher_name(tls_session); /* Sets tls_cipher */ +tls_active = fd; +return OK; +} + + + +/************************************************* +* Deal with logging errors during I/O * +*************************************************/ + +/* We have to get the identity of the peer from saved data. + +Argument: + ec the GnuTLS error code, or 0 if it's a local error + when text identifying read or write + text local error text when ec is 0 + +Returns: nothing +*/ + +static void +record_io_error(int ec, uschar *when, uschar *text) +{ +uschar *additional = US""; + +if (ec == GNUTLS_E_FATAL_ALERT_RECEIVED) + additional = string_sprintf(": %s", + gnutls_alert_get_name(gnutls_alert_get(tls_session))); + +if (initialized == INITIALIZED_SERVER) + log_write(0, LOG_MAIN, "TLS %s error on connection from %s: %s%s", when, + (sender_fullhost != NULL)? sender_fullhost : US "local process", + (ec == 0)? text : US gnutls_strerror(ec), additional); + +else + log_write(0, LOG_MAIN, "TLS %s error on connection to %s [%s]: %s%s", when, + client_host->name, client_host->address, + (ec == 0)? text : US gnutls_strerror(ec), additional); +} + + + +/************************************************* +* TLS version of getc * +*************************************************/ + +/* This gets the next byte from the TLS input buffer. If the buffer is empty, +it refills the buffer via the GnuTLS reading function. + +Arguments: none +Returns: the next character or EOF +*/ + +int +tls_getc(void) +{ +if (ssl_xfer_buffer_lwm >= ssl_xfer_buffer_hwm) + { + int inbytes; + + DEBUG(D_tls) debug_printf("Calling gnutls_record_recv(%lx, %lx, %u)\n", + (long) tls_session, (long) ssl_xfer_buffer, ssl_xfer_buffer_size); + + if (smtp_receive_timeout > 0) alarm(smtp_receive_timeout); + inbytes = gnutls_record_recv(tls_session, CS ssl_xfer_buffer, + ssl_xfer_buffer_size); + alarm(0); + + /* A zero-byte return appears to mean that the TLS session has been + closed down, not that the socket itself has been closed down. Revert to + non-TLS handling. */ + + if (inbytes == 0) + { + DEBUG(D_tls) debug_printf("Got TLS_EOF\n"); + + receive_getc = smtp_getc; + receive_ungetc = smtp_ungetc; + receive_feof = smtp_feof; + receive_ferror = smtp_ferror; + + gnutls_deinit(tls_session); + tls_session = NULL; + tls_active = -1; + tls_cipher = NULL; + tls_peerdn = NULL; + + return smtp_getc(); + } + + /* Handle genuine errors */ + + else if (inbytes < 0) + { + record_io_error(inbytes, US"recv", NULL); + ssl_xfer_error = 1; + return EOF; + } + + ssl_xfer_buffer_hwm = inbytes; + ssl_xfer_buffer_lwm = 0; + } + + +/* Something in the buffer; return next uschar */ + +return ssl_xfer_buffer[ssl_xfer_buffer_lwm++]; +} + + + +/************************************************* +* Read bytes from TLS channel * +*************************************************/ + +/* +Arguments: + buff buffer of data + len size of buffer + +Returns: the number of bytes read + -1 after a failed read +*/ + +int +tls_read(uschar *buff, size_t len) +{ +int inbytes; + +DEBUG(D_tls) debug_printf("Calling gnutls_record_recv(%lx, %lx, %u)\n", + (long) tls_session, (long) buff, len); + +inbytes = gnutls_record_recv(tls_session, CS buff, len); +if (inbytes > 0) return inbytes; +if (inbytes == 0) + { + DEBUG(D_tls) debug_printf("Got TLS_EOF\n"); + } +else record_io_error(inbytes, US"recv", NULL); + +return -1; +} + + + +/************************************************* +* Write bytes down TLS channel * +*************************************************/ + +/* +Arguments: + buff buffer of data + len number of bytes + +Returns: the number of bytes after a successful write, + -1 after a failed write +*/ + +int +tls_write(const uschar *buff, size_t len) +{ +int outbytes; +int left = len; + +DEBUG(D_tls) debug_printf("tls_do_write(%lx, %d)\n", (long) buff, left); +while (left > 0) + { + DEBUG(D_tls) debug_printf("gnutls_record_send(SSL, %lx, %d)\n", (long)buff, + left); + outbytes = gnutls_record_send(tls_session, CS buff, left); + + DEBUG(D_tls) debug_printf("outbytes=%d\n", outbytes); + if (outbytes < 0) + { + record_io_error(outbytes, US"send", NULL); + return -1; + } + if (outbytes == 0) + { + record_io_error(0, US"send", US"TLS channel closed on write"); + return -1; + } + + left -= outbytes; + buff += outbytes; + } + +return len; +} + + + +/************************************************* +* Close down a TLS session * +*************************************************/ + +/* This is also called from within a delivery subprocess forked from the +daemon, to shut down the TLS library, without actually doing a shutdown (which +would tamper with the TLS session in the parent process). + +Arguments: TRUE if gnutls_bye is to be called +Returns: nothing +*/ + +void +tls_close(BOOL shutdown) +{ +if (tls_active < 0) return; /* TLS was not active */ + +if (shutdown) + { + DEBUG(D_tls) debug_printf("tls_close(): shutting down TLS\n"); + gnutls_bye(tls_session, GNUTLS_SHUT_WR); + } + +gnutls_deinit(tls_session); +tls_session = NULL; +gnutls_global_deinit(); + +tls_active = -1; +} + +/* End of tls-gnu.c */ diff --git a/src/src/tls-openssl.c b/src/src/tls-openssl.c new file mode 100644 index 000000000..18165e306 --- /dev/null +++ b/src/src/tls-openssl.c @@ -0,0 +1,1009 @@ +/* $Cambridge: exim/src/src/tls-openssl.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* This module provides the TLS (aka SSL) support for Exim using the OpenSSL +library. It is #included into the tls.c file when that library is used. The +code herein is based on a patch that was originally contributed by Steve +Haslam. It was adapted from stunnel, a GPL program by Michal Trojnara. + +No cryptographic code is included in Exim. All this module does is to call +functions from the OpenSSL library. */ + + +/* Heading stuff */ + +#include +#include +#include +#include + +/* Structure for collecting random data for seeding. */ + +typedef struct randstuff { + time_t t; + pid_t p; +} randstuff; + +/* Local static variables */ + +static BOOL verify_callback_called = FALSE; +static const uschar *sid_ctx = US"exim"; + +static SSL_CTX *ctx = NULL; +static SSL *ssl = NULL; + +static char ssl_errstring[256]; + +static int ssl_session_timeout = 200; +static BOOL verify_optional = FALSE; + + + + + +/************************************************* +* Handle TLS error * +*************************************************/ + +/* Called from lots of places when errors occur before actually starting to do +the TLS handshake, that is, while the session is still in clear. Always returns +DEFER for a server and FAIL for a client so that most calls can use "return +tls_error(...)" to do this processing and then give an appropriate return. A +single function is used for both server and client, because it is called from +some shared functions. + +Argument: + prefix text to include in the logged error + host NULL if setting up a server; + the connected host if setting up a client + +Returns: OK/DEFER/FAIL +*/ + +static int +tls_error(uschar *prefix, host_item *host) +{ +ERR_error_string(ERR_get_error(), ssl_errstring); +if (host == NULL) + { + log_write(0, LOG_MAIN, "TLS error on connection from %s (%s): %s", + (sender_fullhost != NULL)? sender_fullhost : US"local process", + prefix, ssl_errstring); + return DEFER; + } +else + { + log_write(0, LOG_MAIN, "TLS error on connection to %s [%s] (%s): %s", + host->name, host->address, prefix, ssl_errstring); + return FAIL; + } +} + + + +/************************************************* +* Callback to generate RSA key * +*************************************************/ + +/* +Arguments: + s SSL connection + export not used + keylength keylength + +Returns: pointer to generated key +*/ + +static RSA * +rsa_callback(SSL *s, int export, int keylength) +{ +RSA *rsa_key; +export = export; /* Shut picky compilers up */ +DEBUG(D_tls) debug_printf("Generating %d bit RSA key...\n", keylength); +rsa_key = RSA_generate_key(keylength, RSA_F4, NULL, NULL); +if (rsa_key == NULL) + { + ERR_error_string(ERR_get_error(), ssl_errstring); + log_write(0, LOG_MAIN|LOG_PANIC, "TLS error (RSA_generate_key): %s", + ssl_errstring); + return NULL; + } +return rsa_key; +} + + + + +/************************************************* +* Callback for verification * +*************************************************/ + +/* The SSL library does certificate verification if set up to do so. This +callback has the current yes/no state is in "state". If verification succeeded, +we set up the tls_peerdn string. If verification failed, what happens depends +on whether the client is required to present a verifiable certificate or not. + +If verification is optional, we change the state to yes, but still log the +verification error. For some reason (it really would help to have proper +documentation of OpenSSL), this callback function then gets called again, this +time with state = 1. In fact, that's useful, because we can set up the peerdn +value, but we must take care not to set the private verified flag on the second +time through. + +Note: this function is not called if the client fails to present a certificate +when asked. We get here only if a certificate has been received. Handling of +optional verification for this case is done when requesting SSL to verify, by +setting SSL_VERIFY_FAIL_IF_NO_PEER_CERT in the non-optional case. + +Arguments: + state current yes/no state as 1/0 + x509ctx certificate information. + +Returns: 1 if verified, 0 if not +*/ + +static int +verify_callback(int state, X509_STORE_CTX *x509ctx) +{ +static uschar txt[256]; + +X509_NAME_oneline(X509_get_subject_name(x509ctx->current_cert), + CS txt, sizeof(txt)); + +if (state == 0) + { + log_write(0, LOG_MAIN, "SSL verify error: depth=%d error=%s cert=%s", + x509ctx->error_depth, + X509_verify_cert_error_string(x509ctx->error), + txt); + tls_certificate_verified = FALSE; + verify_callback_called = TRUE; + if (!verify_optional) return 0; /* reject */ + DEBUG(D_tls) debug_printf("SSL verify failure overridden (host in " + "tls_try_verify_hosts)\n"); + return 1; /* accept */ + } + +if (x509ctx->error_depth != 0) + { + DEBUG(D_tls) debug_printf("SSL verify ok: depth=%d cert=%s\n", + x509ctx->error_depth, txt); + } +else + { + DEBUG(D_tls) debug_printf("SSL%s peer: %s\n", + verify_callback_called? "" : " authenticated", txt); + tls_peerdn = txt; + } + + +debug_printf("+++verify_callback_called=%d\n", verify_callback_called); + +if (!verify_callback_called) tls_certificate_verified = TRUE; +verify_callback_called = TRUE; + +return 1; /* accept */ +} + + + +/************************************************* +* Information callback * +*************************************************/ + +/* The SSL library functions call this from time to time to indicate what they +are doing. We copy the string to the debugging output when the level is high +enough. + +Arguments: + s the SSL connection + where + ret + +Returns: nothing +*/ + +static void +info_callback(SSL *s, int where, int ret) +{ +where = where; +ret = ret; +DEBUG(D_tls) debug_printf("SSL info: %s\n", SSL_state_string_long(s)); +} + + + +/************************************************* +* Initialize for DH * +*************************************************/ + +/* If dhparam is set, expand it, and load up the parameters for DH encryption. + +Arguments: + dhparam DH parameter file + +Returns: TRUE if OK (nothing to set up, or setup worked) +*/ + +static BOOL +init_dh(uschar *dhparam) +{ +BOOL yield = TRUE; +BIO *bio; +DH *dh; +uschar *dhexpanded; + +if (!expand_check(dhparam, US"tls_dhparam", &dhexpanded)) + return FALSE; + +if (dhexpanded == NULL) return TRUE; + +if ((bio = BIO_new_file(CS dhexpanded, "r")) == NULL) + { + log_write(0, LOG_MAIN, "DH: could not read %s: %s", dhexpanded, + strerror(errno)); + yield = FALSE; + } +else + { + if ((dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL)) == NULL) + { + log_write(0, LOG_MAIN, "DH: could not load params from %s", + dhexpanded); + yield = FALSE; + } + else + { + SSL_CTX_set_tmp_dh(ctx, dh); + DEBUG(D_tls) + debug_printf("Diffie-Hellman initialized from %s with %d-bit key\n", + dhexpanded, 8*DH_size(dh)); + DH_free(dh); + } + BIO_free(bio); + } + +return yield; +} + + + + +/************************************************* +* Initialize for TLS * +*************************************************/ + +/* Called from both server and client code, to do preliminary initialization of +the library. + +Arguments: + host connected host, if client; NULL if server + dhparam DH parameter file + certificate certificate file + privatekey private key + addr address if client; NULL if server (for some randomness) + +Returns: OK/DEFER/FAIL +*/ + +static int +tls_init(host_item *host, uschar *dhparam, uschar *certificate, uschar *privatekey, + address_item *addr) +{ +SSL_load_error_strings(); /* basic set up */ +OpenSSL_add_ssl_algorithms(); + +/* Create a context */ + +ctx = SSL_CTX_new((host == NULL)? + SSLv23_server_method() : SSLv23_client_method()); + +if (ctx == NULL) return tls_error(US"SSL_CTX_new", host); + +/* It turns out that we need to seed the random number generator this early in +order to get the full complement of ciphers to work. It took me roughly a day +of work to discover this by experiment. + +On systems that have /dev/urandom, SSL may automatically seed itself from +there. Otherwise, we have to make something up as best we can. Double check +afterwards. */ + +if (!RAND_status()) + { + randstuff r; + r.t = time(NULL); + r.p = getpid(); + + RAND_seed((uschar *)(&r), sizeof(r)); + RAND_seed((uschar *)big_buffer, big_buffer_size); + if (addr != NULL) RAND_seed((uschar *)addr, sizeof(addr)); + + if (!RAND_status()) + { + if (host == NULL) + { + log_write(0, LOG_MAIN, "TLS error on connection from %s: " + "unable to seed random number generator", + (sender_fullhost != NULL)? sender_fullhost : US"local process"); + return DEFER; + } + else + { + log_write(0, LOG_MAIN, "TLS error on connection to %s [%s]: " + "unable to seed random number generator", + host->name, host->address); + return FAIL; + } + } + } + +/* Set up the information callback, which outputs if debugging is at a suitable +level. */ + +if (!(SSL_CTX_set_info_callback(ctx, (void (*)())info_callback))) + return tls_error(US"SSL_CTX_set_info_callback", host); + +/* The following patch was supplied by Robert Roselius */ + +#if OPENSSL_VERSION_NUMBER > 0x00906040L +/* Enable client-bug workaround. + Versions of OpenSSL as of 0.9.6d include a "CBC countermeasure" feature, + which causes problems with some clients (such as the Certicom SSL Plus + library used by Eudora). This option, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS, + disables the coutermeasure allowing Eudora to connect. + Some poppers and MTAs use SSL_OP_ALL, which enables all such bug + workarounds. */ +/* XXX (Silently?) ignore failure here? XXX*/ + +if (!(SSL_CTX_set_options(ctx, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))) + return tls_error(US"SSL_CTX_set_option", host); +#endif + +/* Initialize with DH parameters if supplied */ + +if (!init_dh(dhparam)) return DEFER; + +/* Set up certificate and key */ + +if (certificate != NULL) + { + uschar *expanded; + if (!expand_check(certificate, US"tls_certificate", &expanded)) + return DEFER; + + if (expanded != NULL) + { + DEBUG(D_tls) debug_printf("tls_certificate file %s\n", expanded); + if (!SSL_CTX_use_certificate_chain_file(ctx, CS expanded)) + return tls_error(US"SSL_CTX_use_certificate_chain_file", host); + } + + if (privatekey != NULL && + !expand_check(privatekey, US"tls_privatekey", &expanded)) + return DEFER; + + if (expanded != NULL) + { + DEBUG(D_tls) debug_printf("tls_privatekey file %s\n", expanded); + if (!SSL_CTX_use_PrivateKey_file(ctx, CS expanded, SSL_FILETYPE_PEM)) + return tls_error(US"SSL_CTX_use_PrivateKey_file", host); + } + } + +/* Set up the RSA callback */ + +SSL_CTX_set_tmp_rsa_callback(ctx, rsa_callback); + +/* Finally, set the timeout, and we are done */ + +SSL_CTX_set_timeout(ctx, ssl_session_timeout); +DEBUG(D_tls) debug_printf("Initialized TLS\n"); +return OK; +} + + + + +/************************************************* +* Get name of cipher in use * +*************************************************/ + +/* The answer is left in a static buffer, and tls_cipher is set to point +to it. + +Argument: pointer to an SSL structure for the connection +Returns: nothing +*/ + +static void +construct_cipher_name(SSL *ssl) +{ +static uschar cipherbuf[256]; +SSL_CIPHER *c; +uschar *ver; +int bits; + +switch (ssl->session->ssl_version) + { + case SSL2_VERSION: + ver = US"SSLv2"; + break; + + case SSL3_VERSION: + ver = US"SSLv3"; + break; + + case TLS1_VERSION: + ver = US"TLSv1"; + break; + + default: + ver = US"UNKNOWN"; + } + +c = SSL_get_current_cipher(ssl); +SSL_CIPHER_get_bits(c, &bits); + +string_format(cipherbuf, sizeof(cipherbuf), "%s:%s:%u", ver, + SSL_CIPHER_get_name(c), bits); +tls_cipher = cipherbuf; + +DEBUG(D_tls) debug_printf("Cipher: %s\n", cipherbuf); +} + + + + + +/************************************************* +* Set up for verifying certificates * +*************************************************/ + +/* Called by both client and server startup + +Arguments: + certs certs file or NULL + crl CRL file or NULL + host NULL in a server; the remote host in a client + optional TRUE if called from a server for a host in tls_try_verify_hosts; + otherwise passed as FALSE + +Returns: OK/DEFER/FAIL +*/ + +static int +setup_certs(uschar *certs, uschar *crl, host_item *host, BOOL optional) +{ +uschar *expcerts, *expcrl; + +if (!expand_check(certs, US"tls_verify_certificates", &expcerts)) + return DEFER; + +if (expcerts != NULL) + { + struct stat statbuf; + if (!SSL_CTX_set_default_verify_paths(ctx)) + return tls_error(US"SSL_CTX_set_default_verify_paths", host); + + if (Ustat(expcerts, &statbuf) < 0) + { + log_write(0, LOG_MAIN|LOG_PANIC, + "failed to stat %s for certificates", expcerts); + return DEFER; + } + else + { + uschar *file, *dir; + if ((statbuf.st_mode & S_IFMT) == S_IFDIR) + { file = NULL; dir = expcerts; } + else + { file = expcerts; dir = NULL; } + + /* If a certificate file is empty, the next function fails with an + unhelpful error message. If we skip it, we get the correct behaviour (no + certificates are recognized, but the error message is still misleading (it + says no certificate was supplied.) But this is better. */ + + if ((file == NULL || statbuf.st_size > 0) && + !SSL_CTX_load_verify_locations(ctx, CS file, CS dir)) + return tls_error(US"SSL_CTX_load_verify_locations", host); + + if (file != NULL) + { + SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(CS file)); + } + } + + /* Handle a certificate revocation list. */ + + #if OPENSSL_VERSION_NUMBER > 0x00907000L + + if (!expand_check(crl, US"tls_crl", &expcrl)) return DEFER; + if (expcrl != NULL && *expcrl != 0) + { + BIO *crl_bio; + X509_CRL *crl_x509; + X509_STORE *cvstore; + + cvstore = SSL_CTX_get_cert_store(ctx); /* cert validation store */ + + crl_bio = BIO_new(BIO_s_file_internal()); + if (crl_bio != NULL) + { + if (BIO_read_filename(crl_bio, expcrl)) + { + crl_x509 = PEM_read_bio_X509_CRL(crl_bio, NULL, NULL, NULL); + BIO_free(crl_bio); + X509_STORE_add_crl(cvstore, crl_x509); + X509_CRL_free(crl_x509); + X509_STORE_set_flags(cvstore, + X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); + } + else + { + BIO_free(crl_bio); + return tls_error(US"BIO_read_filename", host); + } + } + else return tls_error(US"BIO_new", host); + } + + #endif /* OPENSSL_VERSION_NUMBER > 0x00907000L */ + + /* If verification is optional, don't fail if no certificate */ + + SSL_CTX_set_verify(ctx, + SSL_VERIFY_PEER | (optional? 0 : SSL_VERIFY_FAIL_IF_NO_PEER_CERT), + verify_callback); + } + +return OK; +} + + + +/************************************************* +* Start a TLS session in a server * +*************************************************/ + +/* This is called when Exim is running as a server, after having received +the STARTTLS command. It must respond to that command, and then negotiate +a TLS session. + +Arguments: + require_ciphers allowed ciphers + +Returns: OK on success + DEFER for errors before the start of the negotiation + FAIL for errors during the negotation; the server can't + continue running. +*/ + +int +tls_server_start(uschar *require_ciphers) +{ +int rc; +uschar *expciphers; + +/* Check for previous activation */ + +if (tls_active >= 0) + { + log_write(0, LOG_MAIN, "STARTTLS received in already encrypted " + "connection from %s", + (sender_fullhost != NULL)? sender_fullhost : US"local process"); + smtp_printf("554 Already in TLS\r\n"); + return FAIL; + } + +/* Initialize the SSL library. If it fails, it will already have logged +the error. */ + +rc = tls_init(NULL, tls_dhparam, tls_certificate, tls_privatekey, NULL); +if (rc != OK) return rc; + +if (!expand_check(require_ciphers, US"tls_require_ciphers", &expciphers)) + return FAIL; + +/* In OpenSSL, cipher components are separated by hyphens. In GnuTLS, they +are separated by underscores. So that I can use either form in my tests, and +also for general convenience, we turn underscores into hyphens here. */ + +if (expciphers != NULL) + { + uschar *s = expciphers; + while (*s != 0) { if (*s == '_') *s = '-'; s++; } + DEBUG(D_tls) debug_printf("required ciphers: %s\n", expciphers); + if (!SSL_CTX_set_cipher_list(ctx, CS expciphers)) + return tls_error(US"SSL_CTX_set_cipher_list", NULL); + } + +/* If this is a host for which certificate verification is mandatory or +optional, set up appropriately. */ + +tls_certificate_verified = FALSE; +verify_callback_called = FALSE; + +if (verify_check_host(&tls_verify_hosts) == OK) + { + rc = setup_certs(tls_verify_certificates, tls_crl, NULL, FALSE); + if (rc != OK) return rc; + verify_optional = FALSE; + } +else if (verify_check_host(&tls_try_verify_hosts) == OK) + { + rc = setup_certs(tls_verify_certificates, tls_crl, NULL, TRUE); + if (rc != OK) return rc; + verify_optional = TRUE; + } + +/* Prepare for new connection */ + +if ((ssl = SSL_new(ctx)) == NULL) return tls_error(US"SSL_new", NULL); +SSL_clear(ssl); + +/* Set context and tell client to go ahead, except in the case of TLS startup +on connection, where outputting anything now upsets the clients and tends to +make them disconnect. We need to have an explicit fflush() here, to force out +the response. Other smtp_printf() calls do not need it, because in non-TLS +mode, the fflush() happens when smtp_getc() is called. */ + +SSL_set_session_id_context(ssl, sid_ctx, Ustrlen(sid_ctx)); +if (!tls_on_connect) + { + smtp_printf("220 TLS go ahead\r\n"); + fflush(smtp_out); + } + +/* Now negotiate the TLS session. We put our own timer on it, since it seems +that the OpenSSL library doesn't. */ + +SSL_set_fd(ssl, fileno(smtp_out)); +SSL_set_accept_state(ssl); + +DEBUG(D_tls) debug_printf("Calling SSL_accept\n"); + +sigalrm_seen = FALSE; +if (smtp_receive_timeout > 0) alarm(smtp_receive_timeout); +rc = SSL_accept(ssl); +alarm(0); + +if (rc <= 0) + { + if (sigalrm_seen) Ustrcpy(ssl_errstring, "timed out"); + else ERR_error_string(ERR_get_error(), ssl_errstring); + log_write(0, LOG_MAIN, "TLS error on connection from %s (SSL_accept): %s", + (sender_fullhost != NULL)? sender_fullhost : US"local process", + ssl_errstring); + return FAIL; + } + +DEBUG(D_tls) debug_printf("SSL_accept was successful\n"); + +/* TLS has been set up. Adjust the input functions to read via TLS, +and initialize things. */ + +construct_cipher_name(ssl); + +DEBUG(D_tls) + { + uschar buf[2048]; + if (SSL_get_shared_ciphers(ssl, CS buf, sizeof(buf)) != NULL) + debug_printf("Shared ciphers: %s\n", buf); + } + + +ssl_xfer_buffer = store_malloc(ssl_xfer_buffer_size); +ssl_xfer_buffer_lwm = ssl_xfer_buffer_hwm = 0; +ssl_xfer_eof = ssl_xfer_error = 0; + +receive_getc = tls_getc; +receive_ungetc = tls_ungetc; +receive_feof = tls_feof; +receive_ferror = tls_ferror; + +tls_active = fileno(smtp_out); +return OK; +} + + + + + +/************************************************* +* Start a TLS session in a client * +*************************************************/ + +/* Called from the smtp transport after STARTTLS has been accepted. + +Argument: + fd the fd of the connection + host connected host (for messages) + dhparam DH parameter file + certificate certificate file + privatekey private key file + verify_certs file for certificate verify + crl file containing CRL + require_ciphers list of allowed ciphers + +Returns: OK on success + FAIL otherwise - note that tls_error() will not give DEFER + because this is not a server +*/ + +int +tls_client_start(int fd, host_item *host, address_item *addr, uschar *dhparam, + uschar *certificate, uschar *privatekey, uschar *verify_certs, uschar *crl, + uschar *require_ciphers, int timeout) +{ +static uschar txt[256]; +uschar *expciphers; +X509* server_cert; +int rc; + +rc = tls_init(host, dhparam, certificate, privatekey, addr); +if (rc != OK) return rc; + +tls_certificate_verified = FALSE; +verify_callback_called = FALSE; + +if (!expand_check(require_ciphers, US"tls_require_ciphers", &expciphers)) + return FAIL; + +/* In OpenSSL, cipher components are separated by hyphens. In GnuTLS, they +are separated by underscores. So that I can use either form in my tests, and +also for general convenience, we turn underscores into hyphens here. */ + +if (expciphers != NULL) + { + uschar *s = expciphers; + while (*s != 0) { if (*s == '_') *s = '-'; s++; } + DEBUG(D_tls) debug_printf("required ciphers: %s\n", expciphers); + if (!SSL_CTX_set_cipher_list(ctx, CS expciphers)) + return tls_error(US"SSL_CTX_set_cipher_list", host); + } + +rc = setup_certs(verify_certs, crl, host, FALSE); +if (rc != OK) return rc; + +if ((ssl = SSL_new(ctx)) == NULL) return tls_error(US"SSL_new", host); +SSL_set_session_id_context(ssl, sid_ctx, Ustrlen(sid_ctx)); +SSL_set_fd(ssl, fd); +SSL_set_connect_state(ssl); + +/* There doesn't seem to be a built-in timeout on connection. */ + +DEBUG(D_tls) debug_printf("Calling SSL_connect\n"); +sigalrm_seen = FALSE; +alarm(timeout); +rc = SSL_connect(ssl); +alarm(0); + +if (rc <= 0) + { + if (sigalrm_seen) + { + log_write(0, LOG_MAIN, "TLS error on connection to %s [%s]: " + "SSL_connect timed out", host->name, host->address); + return FAIL; + } + else return tls_error(US"SSL_connect", host); + } + +DEBUG(D_tls) debug_printf("SSL_connect succeeded\n"); + +server_cert = SSL_get_peer_certificate (ssl); +tls_peerdn = US X509_NAME_oneline(X509_get_subject_name(server_cert), + CS txt, sizeof(txt)); +tls_peerdn = txt; + +construct_cipher_name(ssl); /* Sets tls_cipher */ + +tls_active = fd; +return OK; +} + + + + + +/************************************************* +* TLS version of getc * +*************************************************/ + +/* This gets the next byte from the TLS input buffer. If the buffer is empty, +it refills the buffer via the SSL reading function. + +Arguments: none +Returns: the next character or EOF +*/ + +int +tls_getc(void) +{ +if (ssl_xfer_buffer_lwm >= ssl_xfer_buffer_hwm) + { + int error; + int inbytes; + + DEBUG(D_tls) debug_printf("Calling SSL_read(%lx, %lx, %u)\n", (long)ssl, + (long)ssl_xfer_buffer, ssl_xfer_buffer_size); + + if (smtp_receive_timeout > 0) alarm(smtp_receive_timeout); + inbytes = SSL_read(ssl, CS ssl_xfer_buffer, ssl_xfer_buffer_size); + error = SSL_get_error(ssl, inbytes); + alarm(0); + + /* SSL_ERROR_ZERO_RETURN appears to mean that the SSL session has been + closed down, not that the socket itself has been closed down. Revert to + non-SSL handling. */ + + if (error == SSL_ERROR_ZERO_RETURN) + { + DEBUG(D_tls) debug_printf("Got SSL_ERROR_ZERO_RETURN\n"); + + receive_getc = smtp_getc; + receive_ungetc = smtp_ungetc; + receive_feof = smtp_feof; + receive_ferror = smtp_ferror; + + SSL_free(ssl); + ssl = NULL; + tls_active = -1; + tls_cipher = NULL; + tls_peerdn = NULL; + + return smtp_getc(); + } + + /* Handle genuine errors */ + + else if (error != SSL_ERROR_NONE) + { + DEBUG(D_tls) debug_printf("Got SSL error %d\n", error); + ssl_xfer_error = 1; + return EOF; + } + + ssl_xfer_buffer_hwm = inbytes; + ssl_xfer_buffer_lwm = 0; + } + +/* Something in the buffer; return next uschar */ + +return ssl_xfer_buffer[ssl_xfer_buffer_lwm++]; +} + + + +/************************************************* +* Read bytes from TLS channel * +*************************************************/ + +/* +Arguments: + buff buffer of data + len size of buffer + +Returns: the number of bytes read + -1 after a failed read +*/ + +int +tls_read(uschar *buff, size_t len) +{ +int inbytes; +int error; + +DEBUG(D_tls) debug_printf("Calling SSL_read(%lx, %lx, %u)\n", (long)ssl, + (long)buff, (unsigned int)len); + +inbytes = SSL_read(ssl, CS buff, len); +error = SSL_get_error(ssl, inbytes); + +if (error == SSL_ERROR_ZERO_RETURN) + { + DEBUG(D_tls) debug_printf("Got SSL_ERROR_ZERO_RETURN\n"); + return -1; + } +else if (error != SSL_ERROR_NONE) + { + return -1; + } + +return inbytes; +} + + + + + +/************************************************* +* Write bytes down TLS channel * +*************************************************/ + +/* +Arguments: + buff buffer of data + len number of bytes + +Returns: the number of bytes after a successful write, + -1 after a failed write +*/ + +int +tls_write(const uschar *buff, size_t len) +{ +int outbytes; +int error; +int left = len; + +DEBUG(D_tls) debug_printf("tls_do_write(%lx, %d)\n", (long)buff, left); +while (left > 0) + { + DEBUG(D_tls) debug_printf("SSL_write(SSL, %lx, %d)\n", (long)buff, left); + outbytes = SSL_write(ssl, CS buff, left); + error = SSL_get_error(ssl, outbytes); + DEBUG(D_tls) debug_printf("outbytes=%d error=%d\n", outbytes, error); + switch (error) + { + case SSL_ERROR_SSL: + ERR_error_string(ERR_get_error(), ssl_errstring); + log_write(0, LOG_MAIN, "TLS error (SSL_write): %s", ssl_errstring); + return -1; + + case SSL_ERROR_NONE: + left -= outbytes; + buff += outbytes; + break; + + case SSL_ERROR_ZERO_RETURN: + log_write(0, LOG_MAIN, "SSL channel closed on write"); + return -1; + + default: + log_write(0, LOG_MAIN, "SSL_write error %d", error); + return -1; + } + } +return len; +} + + + +/************************************************* +* Close down a TLS session * +*************************************************/ + +/* This is also called from within a delivery subprocess forked from the +daemon, to shut down the TLS library, without actually doing a shutdown (which +would tamper with the SSL session in the parent process). + +Arguments: TRUE if SSL_shutdown is to be called +Returns: nothing +*/ + +void +tls_close(BOOL shutdown) +{ +if (tls_active < 0) return; /* TLS was not active */ + +if (shutdown) + { + DEBUG(D_tls) debug_printf("tls_close(): shutting down SSL\n"); + SSL_shutdown(ssl); + } + +SSL_free(ssl); +ssl = NULL; + +tls_active = -1; +} + +/* End of tls-openssl.c */ diff --git a/src/src/tls.c b/src/src/tls.c new file mode 100644 index 000000000..04d8eaba4 --- /dev/null +++ b/src/src/tls.c @@ -0,0 +1,152 @@ +/* $Cambridge: exim/src/src/tls.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* This module provides TLS (aka SSL) support for Exim. The code for OpenSSL is +based on a patch that was originally contributed by Steve Haslam. It was +adapted from stunnel, a GPL program by Michal Trojnara. The code for GNU TLS is +based on a patch contributed by Nikos Mavroyanopoulos. Because these packages +are so very different, the functions for each are kept in separate files. The +relevant file is #included as required, after any any common functions. + +No cryptographic code is included in Exim. All this module does is to call +functions from the OpenSSL or GNU TLS libraries. */ + + +#include "exim.h" + +/* This module is compiled only when it is specifically requested in the +build-time configuration. However, some compilers don't like compiling empty +modules, so keep them happy with a dummy when skipping the rest. Make it +reference itself to stop picky compilers complaining that it is unused, and put +in a dummy argument to stop even pickier compilers complaining about infinite +loops. */ + +#ifndef SUPPORT_TLS +static void dummy(int x) { dummy(x-1); } +#else + +/* Static variables that are used for buffering data by both sets of +functions and the common functions below. */ + + +static uschar *ssl_xfer_buffer = NULL; +static int ssl_xfer_buffer_size = 4096; +static int ssl_xfer_buffer_lwm = 0; +static int ssl_xfer_buffer_hwm = 0; +static int ssl_xfer_eof = 0; +static int ssl_xfer_error = 0; + + + +/************************************************* +* Expand string; give error on failure * +*************************************************/ + +/* If expansion is forced to fail, set the result NULL and return TRUE. +Other failures return FALSE. For a server, an SMTP response is given. + +Arguments: + s the string to expand; if NULL just return TRUE + name name of string being expanded (for error) + result where to put the result + +Returns: TRUE if OK; result may still be NULL after forced failure +*/ + +static BOOL +expand_check(uschar *s, uschar *name, uschar **result) +{ +if (s == NULL) *result = NULL; else + { + *result = expand_string(s); + if (*result == NULL && !expand_string_forcedfail) + { + log_write(0, LOG_MAIN|LOG_PANIC, "expansion of %s failed: %s", name, + expand_string_message); + return FALSE; + } + } +return TRUE; +} + + +/************************************************* +* Many functions are package-specific * +*************************************************/ + +#ifdef USE_GNUTLS +#include "tls-gnu.c" +#else +#include "tls-openssl.c" +#endif + + + +/************************************************* +* TLS version of ungetc * +*************************************************/ + +/* Puts a character back in the input buffer. Only ever +called once. + +Arguments: + ch the character + +Returns: the character +*/ + +int +tls_ungetc(int ch) +{ +ssl_xfer_buffer[--ssl_xfer_buffer_lwm] = ch; +return ch; +} + + + +/************************************************* +* TLS version of feof * +*************************************************/ + +/* Tests for a previous EOF + +Arguments: none +Returns: non-zero if the eof flag is set +*/ + +int +tls_feof(void) +{ +return ssl_xfer_eof; +} + + + +/************************************************* +* TLS version of ferror * +*************************************************/ + +/* Tests for a previous read error, and returns with errno +restored to what it was when the error was detected. + +>>>>> Hmm. Errno not handled yet. Where do we get it from? >>>>> + +Arguments: none +Returns: non-zero if the error flag is set +*/ + +int +tls_ferror(void) +{ +return ssl_xfer_error; +} + +#endif /* SUPPORT_TLS */ + +/* End of tls.c */ diff --git a/src/src/tod.c b/src/src/tod.c new file mode 100644 index 000000000..406ea40b7 --- /dev/null +++ b/src/src/tod.c @@ -0,0 +1,191 @@ +/* $Cambridge: exim/src/src/tod.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* A function for returning the time of day in various formats */ + + +#include "exim.h" + +/* #define TESTING_LOG_DATESTAMP */ + + +static uschar timebuf[sizeof("www, dd-mmm-yyyy hh:mm:ss +zzzz")]; + + +/************************************************* +* Return timestamp * +*************************************************/ + +/* The log timestamp format is dd-mmm-yy so as to be non-confusing on both +sides of the Atlantic. We calculate an explicit numerical offset from GMT for +the full datestamp and BSD inbox datestamp. Note that on some systems +localtime() and gmtime() re-use the same store, so we must save the local time +values before calling gmtime(). If timestamps_utc is set, don't use +localtime(); all times are then in UTC (with offset +0000). + +There are also some contortions to get the day of the month without +a leading zero for the full stamp, since Ustrftime() doesn't provide this +option. + +Argument: type of timestamp required: + tod_bsdin BSD inbox format + tod_epoch Unix epoch format + tod_full full date and time + tod_log log file data line format, + with zone if log_timezone is TRUE + tod_log_bare always without zone + tod_log_datestamp for log file names when datestamped + tod_log_zone always with zone + tod_mbx MBX inbox format + tod_zone just the timezone offset + tod_zulu time in 8601 zulu format + +Returns: pointer to fixed buffer containing the timestamp +*/ + +uschar * +tod_stamp(int type) +{ +time_t now = time(NULL); +struct tm *t; + +/* Vary log type according to timezone requirement */ + +if (type == tod_log) type = log_timezone? tod_log_zone : tod_log_bare; + +/* Styles that don't need local time */ + +else if (type == tod_epoch) + { + (void) sprintf(CS timebuf, "%d", (int)now); /* Unix epoch format */ + return timebuf; + } + +else if (type == tod_zulu) + { + t = gmtime(&now); + (void) sprintf(CS timebuf, "%04d%02d%02d%02d%02d%02dZ", + 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, + t->tm_sec); + return timebuf; + } + +/* Convert to local time or UTC */ + +t = timestamps_utc? gmtime(&now) : localtime(&now); + +switch(type) + { + case tod_log_bare: /* Format used in logging without timezone */ + (void) sprintf(CS timebuf, "%04d-%02d-%02d %02d:%02d:%02d", + 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday, + t->tm_hour, t->tm_min, t->tm_sec); + break; + + /* Format used as suffix of log file name when 'log_datestamp' is active. For + testing purposes, it changes the file every second. */ + + case tod_log_datestamp: + #ifdef TESTING_LOG_DATESTAMP + (void) sprintf(CS timebuf, "%04d%02d%02d%02d%02d", + 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min); + #else + (void) sprintf(CS timebuf, "%04d%02d%02d", + 1900 + t->tm_year, 1 + t->tm_mon, t->tm_mday); + #endif + break; + + /* Format used in BSD inbox separator lines. Sort-of documented in RFC 976 + ("UUCP Mail Interchange Format Standard") but only by example, not by + explicit definition. The examples show no timezone offsets, and some MUAs + appear to be sensitive to this, so Exim has been changed to remove the + timezone offsets that originally appeared. */ + + case tod_bsdin: + { + int len = Ustrftime(timebuf, sizeof(timebuf), "%a %b %d %H:%M:%S", t); + Ustrftime(timebuf + len, sizeof(timebuf) - len, " %Y", t); + } + break; + + /* Other types require the GMT offset to be calculated, or just set up in the + case of UTC timestamping. We need to take a copy of the local time first. */ + + default: + { + int diff_hour, diff_min; + struct tm local; + memcpy(&local, t, sizeof(struct tm)); + + if (timestamps_utc) + { + diff_hour = diff_min = 0; + } + else + { + struct tm *gmt = gmtime(&now); + diff_min = 60*(local.tm_hour - gmt->tm_hour) + local.tm_min - gmt->tm_min; + if (local.tm_year != gmt->tm_year) + diff_min += (local.tm_year > gmt->tm_year)? 1440 : -1440; + else if (local.tm_yday != gmt->tm_yday) + diff_min += (local.tm_yday > gmt->tm_yday)? 1440 : -1440; + diff_hour = diff_min/60; + diff_min = abs(diff_min - diff_hour*60); + } + + switch(type) + { + case tod_log_zone: /* Format used in logging with timezone */ + (void) sprintf(CS timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+03d%02d", + 1900 + local.tm_year, 1 + local.tm_mon, local.tm_mday, + local.tm_hour, local.tm_min, local.tm_sec, + diff_hour, diff_min); + break; + + case tod_zone: /* Just the timezone offset */ + (void) sprintf(CS timebuf, "%+03d%02d", diff_hour, diff_min); + break; + + /* tod_mbx: format used in MBX mailboxes - subtly different to tod_full */ + + #ifdef SUPPORT_MBX + case tod_mbx: + { + int len; + (void) sprintf(CS timebuf, "%02d-", local.tm_mday); + len = Ustrlen(timebuf); + len += Ustrftime(timebuf + len, sizeof(timebuf) - len, "%b-%Y %H:%M:%S", + &local); + (void) sprintf(CS timebuf + len, " %+03d%02d", diff_hour, diff_min); + } + break; + #endif + + /* tod_full: format used in Received: headers (use as default just in case + called with a junk type value) */ + + default: + { + int len = Ustrftime(timebuf, sizeof(timebuf), "%a, ", &local); + (void) sprintf(CS timebuf + len, "%02d ", local.tm_mday); + len += Ustrlen(timebuf + len); + len += Ustrftime(timebuf + len, sizeof(timebuf) - len, "%b %Y %H:%M:%S", + &local); + (void) sprintf(CS timebuf + len, " %+03d%02d", diff_hour, diff_min); + } + break; + } + } + break; + } + +return timebuf; +} + +/* End of tod.c */ diff --git a/src/src/transport-filter.src b/src/src/transport-filter.src new file mode 100644 index 000000000..3a82c7639 --- /dev/null +++ b/src/src/transport-filter.src @@ -0,0 +1,84 @@ +#! PERL_COMMAND -w +# $Cambridge: exim/src/src/transport-filter.src,v 1.1 2004/10/07 10:39:01 ph10 Exp $ + +# This is a Perl script to demonstrate the possibilities of on-the-fly +# delivery filtering in Exim. It is presented with a message on its standard +# input, and must copy it to the standard output, transforming it as it +# pleases, but of course it must keep to the syntax of RFC 822 for the headers. + +# The filter is run before any SMTP-specific processing, such as turning +# \n into \r\n and escaping lines beginning with a dot. +# +# Philip Hazel, May 1997 +############################################################################# + + +# If the filter is called with any arguments, insert them into the message +# as X-Arg headers, just to verify what they are. + +for ($ac = 0; $ac < @ARGV; $ac++) + { + printf("X-Arg%d: %s\n", $ac, $ARGV[$ac]); + } + +# Now read the header portion of the message; this is easy to do in Perl + +$/ = ""; # set paragraph mode +chomp($headers = ); # read a paragraph, remove trailing newlines +$/ = "\n"; # unset paragraph mode + +# Splitting up a sequence of unique headers is easy to do in Perl, but a +# message may contain duplicate headers of various kinds. It is better +# to extract the headers one wants from the whole paragraph, do any appropriate +# munging, and then put them back (unless removing them altogether). Messing +# with "Received:" headers is not in any case to be encouraged. + +# As a demonstration, we extract the "From:" header, add a textual comment +# to it, and put it back. + +($pre, $from, $post) = + $headers =~ /^(|(?:.|\n)+\n) (?# Stuff preceding the From header, + which is either null, or any number + of characters, including \n, ending + with \n.) + From:[\s\t]* (?# Header name, with optional space or tab.) + ((?:.|\n)*?) (?# Header body, which contains any chars, + including \n, but we want to make it as + short as possible so as not to include + following headers by mistake.) + (|\n\S(?:.|\n)*)$ (?# Header terminates at end or at \n followed + by a non-whitespace character and + remaining headers.) + /ix; # case independent, regular expression, + # use extended features (ignore whitespace) + +# Only do something if there was a From: header, of course. It has been +# extracted without the final \n, which is on the front of the $post +# variable. + +if ($pre) + { + $headers = $pre . "From: $from (this is an added comment)" . $post; + } + +# Add a new header to the end of the headers; remember that the final +# \n isn't there. + +$headers .= "\nX-Comment: Message munged"; + +# Write out the processed headers, plus a blank line to separate them from +# the body. + +printf(STDOUT "%s\n\n", $headers); + +# As a demonstration of munging the body of a message, reverse all the +# characters in each line. + +while () + { + chomp; + $_ = reverse($_); + printf(STDOUT "%s\n", $_); + } + +# End diff --git a/src/src/transport.c b/src/src/transport.c new file mode 100644 index 000000000..855bedd91 --- /dev/null +++ b/src/src/transport.c @@ -0,0 +1,1798 @@ +/* $Cambridge: exim/src/src/transport.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* General functions concerned with transportation, and generic options for all +transports. */ + + +#include "exim.h" + + +/* Structure for keeping list of addresses that have been added to +Envelope-To:, in order to avoid duplication. */ + +struct aci { + struct aci *next; + address_item *ptr; + }; + + +/* Static data for write_chunk() */ + +static uschar *chunk_ptr; /* chunk pointer */ +static uschar *nl_check; /* string to look for at line start */ +static int nl_check_length; /* length of same */ +static uschar *nl_escape; /* string to insert */ +static int nl_escape_length; /* length of same */ +static int nl_partial_match; /* length matched at chunk end */ + + +/* Generic options for transports, all of which live inside transport_instance +data blocks and which therefore have the opt_public flag set. Note that there +are other options living inside this structure which can be set only from +certain transports. */ + +optionlist optionlist_transports[] = { + { "*expand_group", opt_stringptr|opt_hidden|opt_public, + (void *)offsetof(transport_instance, expand_gid) }, + { "*expand_user", opt_stringptr|opt_hidden|opt_public, + (void *)offsetof(transport_instance, expand_uid) }, + { "*headers_rewrite_flags", opt_int|opt_public|opt_hidden, + (void *)offsetof(transport_instance, rewrite_existflags) }, + { "*headers_rewrite_rules", opt_void|opt_public|opt_hidden, + (void *)offsetof(transport_instance, rewrite_rules) }, + { "*set_group", opt_bool|opt_hidden|opt_public, + (void *)offsetof(transport_instance, gid_set) }, + { "*set_user", opt_bool|opt_hidden|opt_public, + (void *)offsetof(transport_instance, uid_set) }, + { "body_only", opt_bool|opt_public, + (void *)offsetof(transport_instance, body_only) }, + { "current_directory", opt_stringptr|opt_public, + (void *)offsetof(transport_instance, current_dir) }, + { "debug_print", opt_stringptr | opt_public, + (void *)offsetof(transport_instance, debug_string) }, + { "delivery_date_add", opt_bool|opt_public, + (void *)(offsetof(transport_instance, delivery_date_add)) }, + { "disable_logging", opt_bool|opt_public, + (void *)(offsetof(transport_instance, disable_logging)) }, + { "driver", opt_stringptr|opt_public, + (void *)offsetof(transport_instance, driver_name) }, + { "envelope_to_add", opt_bool|opt_public, + (void *)(offsetof(transport_instance, envelope_to_add)) }, + { "group", opt_expand_gid|opt_public, + (void *)offsetof(transport_instance, gid) }, + { "headers_add", opt_stringptr|opt_public, + (void *)offsetof(transport_instance, add_headers) }, + { "headers_only", opt_bool|opt_public, + (void *)offsetof(transport_instance, headers_only) }, + { "headers_remove", opt_stringptr|opt_public, + (void *)offsetof(transport_instance, remove_headers) }, + { "headers_rewrite", opt_rewrite|opt_public, + (void *)offsetof(transport_instance, headers_rewrite) }, + { "home_directory", opt_stringptr|opt_public, + (void *)offsetof(transport_instance, home_dir) }, + { "initgroups", opt_bool|opt_public, + (void *)offsetof(transport_instance, initgroups) }, + { "message_size_limit", opt_stringptr|opt_public, + (void *)offsetof(transport_instance, message_size_limit) }, + { "rcpt_include_affixes", opt_bool|opt_public, + (void *)offsetof(transport_instance, rcpt_include_affixes) }, + { "retry_use_local_part", opt_bool|opt_public, + (void *)offsetof(transport_instance, retry_use_local_part) }, + { "return_path", opt_stringptr|opt_public, + (void *)(offsetof(transport_instance, return_path)) }, + { "return_path_add", opt_bool|opt_public, + (void *)(offsetof(transport_instance, return_path_add)) }, + { "shadow_condition", opt_stringptr|opt_public, + (void *)offsetof(transport_instance, shadow_condition) }, + { "shadow_transport", opt_stringptr|opt_public, + (void *)offsetof(transport_instance, shadow) }, + { "transport_filter", opt_stringptr|opt_public, + (void *)offsetof(transport_instance, filter_command) }, + { "transport_filter_timeout", opt_time|opt_public, + (void *)offsetof(transport_instance, filter_timeout) }, + { "user", opt_expand_uid|opt_public, + (void *)offsetof(transport_instance, uid) } +}; + +int optionlist_transports_size = + sizeof(optionlist_transports)/sizeof(optionlist); + + +/************************************************* +* Initialize transport list * +*************************************************/ + +/* Read the transports section of the configuration file, and set up a chain of +transport instances according to its contents. Each transport has generic +options and may also have its own private options. This function is only ever +called when transports == NULL. We use generic code in readconf to do most of +the work. */ + +void +transport_init(void) +{ +transport_instance *t; + +readconf_driver_init(US"transport", + (driver_instance **)(&transports), /* chain anchor */ + (driver_info *)transports_available, /* available drivers */ + sizeof(transport_info), /* size of info block */ + &transport_defaults, /* default values for generic options */ + sizeof(transport_instance), /* size of instance block */ + optionlist_transports, /* generic options */ + optionlist_transports_size); + +/* Now scan the configured transports and check inconsistencies. A shadow +transport is permitted only for local transports. */ + +for (t = transports; t != NULL; t = t->next) + { + if (!t->info->local) + { + if (t->shadow != NULL) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG, + "shadow transport not allowed on non-local transport %s", t->name); + } + + if (t->body_only && t->headers_only) + log_write(0, LOG_PANIC_DIE|LOG_CONFIG, + "%s transport: body_only and headers_only are mutually exclusive", + t->name); + } +} + + + +/************************************************* +* Write block of data * +*************************************************/ + +/* Subroutine called by write_chunk() and at the end of the message actually +to write a data block. Also called directly by some transports to write +additional data to the file descriptor (e.g. prefix, suffix). + +If a transport wants data transfers to be timed, it sets a non-zero value in +transport_write_timeout. A non-zero transport_write_timeout causes a timer to +be set for each block of data written from here. If time runs out, then write() +fails and provokes an error return. The caller can then inspect sigalrm_seen to +check for a timeout. + +On some systems, if a quota is exceeded during the write, the yield is the +number of bytes written rather than an immediate error code. This also happens +on some systems in other cases, for example a pipe that goes away because the +other end's process terminates (Linux). On other systems, (e.g. Solaris 2) you +get the error codes the first time. + +The write() function is also interruptible; the Solaris 2.6 man page says: + + If write() is interrupted by a signal before it writes any + data, it will return -1 with errno set to EINTR. + + If write() is interrupted by a signal after it successfully + writes some data, it will return the number of bytes written. + +To handle these cases, we want to restart the write() to output the remainder +of the data after a non-negative return from write(), except after a timeout. +In the error cases (EDQUOT, EPIPE) no bytes get written the second time, and a +proper error then occurs. In principle, after an interruption, the second +write() could suffer the same fate, but we do not want to continue for +evermore, so stick a maximum repetition count on the loop to act as a +longstop. + +Arguments: + fd file descriptor to write to + block block of bytes to write + len number of bytes to write + +Returns: TRUE on success, FALSE on failure (with errno preserved); + transport_count is incremented by the number of bytes written +*/ + +BOOL +transport_write_block(int fd, uschar *block, int len) +{ +int i, rc, save_errno; + +for (i = 0; i < 100; i++) + { + DEBUG(D_transport) + debug_printf("writing data block fd=%d size=%d timeout=%d\n", + fd, len, transport_write_timeout); + if (transport_write_timeout > 0) alarm(transport_write_timeout); + + #ifdef SUPPORT_TLS + if (tls_active == fd) rc = tls_write(block, len); else + #endif + + rc = write(fd, block, len); + save_errno = errno; + + /* Cancel the alarm and deal with a timeout */ + + if (transport_write_timeout > 0) + { + alarm(0); + if (sigalrm_seen) + { + errno = ETIMEDOUT; + return FALSE; + } + } + + /* Hopefully, the most common case is success, so test that first. */ + + if (rc == len) { transport_count += len; return TRUE; } + + /* A non-negative return code is an incomplete write. Try again. */ + + if (rc >= 0) + { + len -= rc; + block += rc; + transport_count += rc; + DEBUG(D_transport) debug_printf("write incomplete (%d)\n", rc); + continue; + } + + /* A negative return code with an EINTR error is another form of + incomplete write, zero bytes having been written */ + + if (save_errno == EINTR) + { + DEBUG(D_transport) + debug_printf("write interrupted before anything written\n"); + continue; + } + + /* A response of EAGAIN from write() is likely only in the case of writing + to a FIFO that is not swallowing the data as fast as Exim is writing it. */ + + if (save_errno == EAGAIN) + { + DEBUG(D_transport) + debug_printf("write temporarily locked out, waiting 1 sec\n"); + sleep(1); + continue; + } + + /* Otherwise there's been an error */ + + DEBUG(D_transport) debug_printf("writing error %d: %s\n", save_errno, + strerror(save_errno)); + errno = save_errno; + return FALSE; + } + +/* We've tried and tried and tried but still failed */ + +errno = ERRNO_WRITEINCOMPLETE; +return FALSE; +} + + + + +/************************************************* +* Write formatted string * +*************************************************/ + +/* This is called by various transports. It is a convenience function. + +Arguments: + fd file descriptor + format string format + ... arguments for format + +Returns: the yield of transport_write_block() +*/ + +BOOL +transport_write_string(int fd, char *format, ...) +{ +va_list ap; +va_start(ap, format); +if (!string_vformat(big_buffer, big_buffer_size, format, ap)) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "overlong formatted string in transport"); +va_end(ap); +return transport_write_block(fd, big_buffer, Ustrlen(big_buffer)); +} + + + + +/************************************************* +* Write character chunk * +*************************************************/ + +/* Subroutine used by transport_write_message() to scan character chunks for +newlines and act appropriately. The object is to minimise the number of writes. +The output byte stream is buffered up in deliver_out_buffer, which is written +only when it gets full, thus minimizing write operations and TCP packets. + +Static data is used to handle the case when the last character of the previous +chunk was NL, or matched part of the data that has to be escaped. + +Arguments: + fd file descript to write to + chunk pointer to data to write + len length of data to write + usr_crlf TRUE if CR LF is wanted at the end of each line + +In addition, the static nl_xxx variables must be set as required. + +Returns: TRUE on success, FALSE on failure (with errno preserved) +*/ + +static BOOL +write_chunk(int fd, uschar *chunk, int len, BOOL use_crlf) +{ +uschar *start = chunk; +uschar *end = chunk + len; +register uschar *ptr; +int mlen = DELIVER_OUT_BUFFER_SIZE - nl_escape_length - 2; + +/* The assumption is made that the check string will never stretch over move +than one chunk since the only time there are partial matches is when copying +the body in large buffers. There is always enough room in the buffer for an +escape string, since the loop below ensures this for each character it +processes, and it won't have stuck in the escape string if it left a partial +match. */ + +if (nl_partial_match >= 0) + { + if (nl_check_length > 0 && len >= nl_check_length && + Ustrncmp(start, nl_check + nl_partial_match, + nl_check_length - nl_partial_match) == 0) + { + Ustrncpy(chunk_ptr, nl_escape, nl_escape_length); + chunk_ptr += nl_escape_length; + start += nl_check_length - nl_partial_match; + } + + /* The partial match was a false one. Insert the characters carried over + from the previous chunk. */ + + else if (nl_partial_match > 0) + { + Ustrncpy(chunk_ptr, nl_check, nl_partial_match); + chunk_ptr += nl_partial_match; + } + + nl_partial_match = -1; + } + +/* Now process the characters in the chunk. Whenever we hit a newline we check +for possible escaping. The code for the non-NL route should be as fast as +possible. */ + +for (ptr = start; ptr < end; ptr++) + { + register int ch; + + /* Flush the buffer if it has reached the threshold - we want to leave enough + room for the next uschar, plus a possible extra CR for an LF, plus the escape + string. */ + + if (chunk_ptr - deliver_out_buffer > mlen) + { + if (!transport_write_block(fd, deliver_out_buffer, + chunk_ptr - deliver_out_buffer)) + return FALSE; + chunk_ptr = deliver_out_buffer; + } + + if ((ch = *ptr) == '\n') + { + int left = end - ptr - 1; /* count of chars left after NL */ + + /* Insert CR before NL if required */ + + if (use_crlf) *chunk_ptr++ = '\r'; + *chunk_ptr++ = '\n'; + + /* The check_string test (formerly "from hack") replaces the specific + string at the start of a line with an escape string (e.g. "From " becomes + ">From " or "." becomes "..". It is a case-sensitive test. The length + check above ensures there is always enough room to insert this string. */ + + if (nl_check_length > 0) + { + if (left >= nl_check_length && + Ustrncmp(ptr+1, nl_check, nl_check_length) == 0) + { + Ustrncpy(chunk_ptr, nl_escape, nl_escape_length); + chunk_ptr += nl_escape_length; + ptr += nl_check_length; + } + + /* Handle the case when there isn't enough left to match the whole + check string, but there may be a partial match. We remember how many + characters matched, and finish processing this chunk. */ + + else if (left <= 0) nl_partial_match = 0; + + else if (Ustrncmp(ptr+1, nl_check, left) == 0) + { + nl_partial_match = left; + ptr = end; + } + } + } + + /* Not a NL character */ + + else *chunk_ptr++ = ch; + } + +return TRUE; +} + + + + +/************************************************* +* Generate address for RCPT TO * +*************************************************/ + +/* This function puts together an address for RCPT to, using the caseful +version of the local part and the caseful version of the domain. If there is no +prefix or suffix, or if affixes are to be retained, we can just use the +original address. Otherwise, if there is a prefix but no suffix we can use a +pointer into the original address. If there is a suffix, however, we have to +build a new string. + +Arguments: + addr the address item + include_affixes TRUE if affixes are to be included + +Returns: a string +*/ + +uschar * +transport_rcpt_address(address_item *addr, BOOL include_affixes) +{ +uschar *at; +int plen, slen; + +if (include_affixes) + { + setflag(addr, af_include_affixes); /* Affects logged => line */ + return addr->address; + } + +if (addr->suffix == NULL) + { + if (addr->prefix == NULL) return addr->address; + return addr->address + Ustrlen(addr->prefix); + } + +at = Ustrrchr(addr->address, '@'); +plen = (addr->prefix == NULL)? 0 : Ustrlen(addr->prefix); +slen = Ustrlen(addr->suffix); + +return string_sprintf("%.*s@%s", (at - addr->address - plen - slen), + addr->address + plen, at + 1); +} + + +/************************************************* +* Output Envelope-To: address & scan duplicates * +*************************************************/ + +/* This function is called from internal_transport_write_message() below, when +generating an Envelope-To: header line. It checks for duplicates of the given +address and its ancestors. When one is found, this function calls itself +recursively, to output the envelope address of the duplicate. + +We want to avoid duplication in the list, which can arise for example when +A->B,C and then both B and C alias to D. This can also happen when there are +unseen drivers in use. So a list of addresses that have been output is kept in +the plist variable. + +It is also possible to have loops in the address ancestry/duplication graph, +for example if there are two top level addresses A and B and we have A->B,C and +B->A. To break the loop, we use a list of processed addresses in the dlist +variable. + +After handling duplication, this function outputs the progenitor of the given +address. + +Arguments: + p the address we are interested in + pplist address of anchor of the list of addresses not to output + pdlist address of anchor of the list of processed addresses + first TRUE if this is the first address; set it FALSE afterwards + fd the file descriptor to write to + use_crlf to be passed on to write_chunk() + +Returns: FALSE if writing failed +*/ + +static BOOL +write_env_to(address_item *p, struct aci **pplist, struct aci **pdlist, + BOOL *first, int fd, BOOL use_crlf) +{ +address_item *pp; +struct aci *ppp; + +/* Do nothing if we have already handled this address. If not, remember it +so that we don't handle it again. */ + +for (ppp = *pdlist; ppp != NULL; ppp = ppp->next) + { if (p == ppp->ptr) return TRUE; } + +ppp = store_get(sizeof(struct aci)); +ppp->next = *pdlist; +*pdlist = ppp; +ppp->ptr = p; + +/* Now scan up the ancestry, checking for duplicates at each generation. */ + +for (pp = p;; pp = pp->parent) + { + address_item *dup; + for (dup = addr_duplicate; dup != NULL; dup = dup->next) + { + if (dup->dupof != pp) continue; /* Not a dup of our address */ + if (!write_env_to(dup, pplist, pdlist, first, fd, use_crlf)) return FALSE; + } + if (pp->parent == NULL) break; + } + +/* Check to see if we have already output the progenitor. */ + +for (ppp = *pplist; ppp != NULL; ppp = ppp->next) + { if (pp == ppp->ptr) break; } +if (ppp != NULL) return TRUE; + +/* Remember what we have output, and output it. */ + +ppp = store_get(sizeof(struct aci)); +ppp->next = *pplist; +*pplist = ppp; +ppp->ptr = pp; + +if (!(*first) && !write_chunk(fd, US",\n ", 3, use_crlf)) return FALSE; +*first = FALSE; +return write_chunk(fd, pp->address, Ustrlen(pp->address), use_crlf); +} + + + + +/************************************************* +* Write the message * +*************************************************/ + +/* This function writes the message to the given file descriptor. The headers +are in the in-store data structure, and the rest of the message is in the open +file descriptor deliver_datafile. Make sure we start it at the beginning. + +. If add_return_path is TRUE, a "return-path:" header is added to the message, + containing the envelope sender's address. + +. If add_envelope_to is TRUE, a "envelope-to:" header is added to the message, + giving the top-level envelope address that caused this delivery to happen. + +. If add_delivery_date is TRUE, a "delivery-date:" header is added to the + message. It gives the time and date that delivery took place. + +. If check_string is not null, the start of each line is checked for that + string. If it is found, it is replaced by escape_string. This used to be + the "from hack" for files, and "smtp_dots" for escaping SMTP dots. + +. If use_crlf is true, newlines are turned into CRLF (SMTP output). + +The yield is TRUE if all went well, and FALSE if not. Exit *immediately* after +any writing or reading error, leaving the code in errno intact. Error exits +can include timeouts for certain transports, which are requested by setting +transport_write_timeout non-zero. + +Arguments: + addr (chain of) addresses (for extra headers), or NULL; + only the first address is used + fd file descriptor to write the message to + options bit-wise options: + add_return_path if TRUE, add a "return-path" header + add_envelope_to if TRUE, add a "envelope-to" header + add_delivery_date if TRUE, add a "delivery-date" header + use_crlf if TRUE, turn NL into CR LF + end_dot if TRUE, send a terminating "." line at the end + no_headers if TRUE, omit the headers + no_body if TRUE, omit the body + size_limit if > 0, this is a limit to the size of message written; + it is used when returning messages to their senders, + and is approximate rather than exact, owing to chunk + buffering + add_headers a string containing one or more headers to add; it is + expanded, and must be in correct RFC 822 format as + it is transmitted verbatim; NULL => no additions, + and so does empty string or forced expansion fail + remove_headers a colon-separated list of headers to remove, or NULL + check_string a string to check for at the start of lines, or NULL + escape_string a string to insert in front of any check string + rewrite_rules chain of header rewriting rules + rewrite_existflags flags for the rewriting rules + +Returns: TRUE on success; FALSE (with errno) on failure. + In addition, the global variable transport_count + is incremented by the number of bytes written. +*/ + +static BOOL +internal_transport_write_message(address_item *addr, int fd, int options, + int size_limit, uschar *add_headers, uschar *remove_headers, uschar *check_string, + uschar *escape_string, rewrite_rule *rewrite_rules, int rewrite_existflags) +{ +int written = 0; +int len; +header_line *h; +BOOL use_crlf = (options & topt_use_crlf) != 0; + +/* Initialize pointer in output buffer. */ + +chunk_ptr = deliver_out_buffer; + +/* Set up the data for start-of-line data checking and escaping */ + +nl_partial_match = -1; +if (check_string != NULL && escape_string != NULL) + { + nl_check = check_string; + nl_check_length = Ustrlen(nl_check); + nl_escape = escape_string; + nl_escape_length = Ustrlen(nl_escape); + } +else nl_check_length = nl_escape_length = 0; + +/* Whether the escaping mechanism is applied to headers or not is controlled by +an option (set for SMTP, not otherwise). Negate the length if not wanted till +after the headers. */ + +if ((options & topt_escape_headers) == 0) nl_check_length = -nl_check_length; + +/* Write the headers if required, including any that have to be added. If there +are header rewriting rules, apply them. */ + +if ((options & topt_no_headers) == 0) + { + /* Add return-path: if requested. */ + + if ((options & topt_add_return_path) != 0) + { + uschar buffer[ADDRESS_MAXLENGTH + 20]; + sprintf(CS buffer, "Return-path: <%.*s>\n", ADDRESS_MAXLENGTH, + return_path); + if (!write_chunk(fd, buffer, Ustrlen(buffer), use_crlf)) return FALSE; + } + + /* Add envelope-to: if requested */ + + if ((options & topt_add_envelope_to) != 0) + { + BOOL first = TRUE; + address_item *p; + struct aci *plist = NULL; + struct aci *dlist = NULL; + void *reset_point = store_get(0); + + if (!write_chunk(fd, US"Envelope-to: ", 13, use_crlf)) return FALSE; + + /* Pick up from all the addresses. The plist and dlist variables are + anchors for lists of addresses already handled; they have to be defined at + this level becuase write_env_to() calls itself recursively. */ + + for (p = addr; p != NULL; p = p->next) + { + if (!write_env_to(p, &plist, &dlist, &first, fd, use_crlf)) return FALSE; + } + + /* Add a final newline and reset the store used for tracking duplicates */ + + if (!write_chunk(fd, US"\n", 1, use_crlf)) return FALSE; + store_reset(reset_point); + } + + /* Add delivery-date: if requested. */ + + if ((options & topt_add_delivery_date) != 0) + { + uschar buffer[100]; + sprintf(CS buffer, "Delivery-date: %s\n", tod_stamp(tod_full)); + if (!write_chunk(fd, buffer, Ustrlen(buffer), use_crlf)) return FALSE; + } + + /* Then the message's headers. Don't write any that are flagged as "old"; + that means they were rewritten, or are a record of envelope rewriting, or + were removed (e.g. Bcc). If remove_headers is not null, skip any headers that + match any entries therein. Then check addr->p.remove_headers too, provided that + addr is not NULL. */ + + if (remove_headers != NULL) + { + uschar *s = expand_string(remove_headers); + if (s == NULL && !expand_string_forcedfail) + { + errno = ERRNO_CHHEADER_FAIL; + return FALSE; + } + remove_headers = s; + } + + for (h = header_list; h != NULL; h = h->next) + { + int i; + uschar *list = NULL; + BOOL include_header; + + if (h->type == htype_old) continue; + + include_header = TRUE; + list = remove_headers; + + for (i = 0; i < 2; i++) /* For remove_headers && addr->p.remove_headers */ + { + if (list != NULL) + { + int sep = ':'; /* This is specified as a colon-separated list */ + uschar *s, *ss; + uschar buffer[128]; + while ((s = string_nextinlist(&list, &sep, buffer, sizeof(buffer))) + != NULL) + { + int len = Ustrlen(s); + if (strncmpic(h->text, s, len) != 0) continue; + ss = h->text + len; + while (*ss == ' ' || *ss == '\t') ss++; + if (*ss == ':') break; + } + if (s != NULL) { include_header = FALSE; break; } + } + if (addr != NULL) list = addr->p.remove_headers; + } + + /* If this header is to be output, try to rewrite it if there are rewriting + rules. */ + + if (include_header) + { + if (rewrite_rules != NULL) + { + void *reset_point = store_get(0); + header_line *hh = + rewrite_header(h, NULL, NULL, rewrite_rules, rewrite_existflags, + FALSE); + if (hh != NULL) + { + if (!write_chunk(fd, hh->text, hh->slen, use_crlf)) return FALSE; + store_reset(reset_point); + continue; /* With the next header line */ + } + } + + /* Either no rewriting rules, or it didn't get rewritten */ + + if (!write_chunk(fd, h->text, h->slen, use_crlf)) return FALSE; + } + + /* Header removed */ + + else + { + DEBUG(D_transport) debug_printf("removed header line:\n%s---\n", + h->text); + } + } + + /* Add on any address-specific headers. If there are multiple addresses, + they will all have the same headers in order to be batched. The headers + are chained in reverse order of adding (so several addresses from the + same alias might share some of them) but we want to output them in the + opposite order. This is a bit tedious, but there shouldn't be very many + of them. We just walk the list twice, reversing the pointers each time, + but on the second time, write out the items. */ + + if (addr != NULL) + { + int i; + header_line *hprev = addr->p.extra_headers; + header_line *hnext; + for (i = 0; i < 2; i++) + { + for (h = hprev, hprev = NULL; h != NULL; h = hnext) + { + hnext = h->next; + h->next = hprev; + hprev = h; + if (i == 1) + { + if (!write_chunk(fd, h->text, h->slen, use_crlf)) return FALSE; + DEBUG(D_transport) + debug_printf("added header line(s):\n%s---\n", h->text); + } + } + } + } + + /* If a string containing additional headers exists, expand it and write + out the result. This is done last so that if it (deliberately or accidentally) + isn't in header format, it won't mess up any other headers. An empty string + or a forced expansion failure are noops. */ + + if (add_headers != NULL) + { + uschar *s = expand_string(add_headers); + if (s == NULL) + { + if (!expand_string_forcedfail) + { + errno = ERRNO_CHHEADER_FAIL; + return FALSE; + } + } + else + { + int len = Ustrlen(s); + if (len > 0) + { + if (!write_chunk(fd, s, len, use_crlf)) return FALSE; + if (s[len-1] != '\n' && !write_chunk(fd, US"\n", 1, use_crlf)) + return FALSE; + DEBUG(D_transport) + debug_printf("added header line(s):\n%s---\n", s); + } + } + } + + /* Separate headers from body with a blank line */ + + if (!write_chunk(fd, US"\n", 1, use_crlf)) return FALSE; + } + +/* If the body is required, ensure that the data for check strings (formerly +the "from hack") is enabled by negating the length if necessary. (It will be +negative in cases where it isn't to apply to the headers). Then ensure the body +is positioned at the start of its file (following the message id), then write +it, applying the size limit if required. */ + +if ((options & topt_no_body) == 0) + { + nl_check_length = abs(nl_check_length); + nl_partial_match = 0; + lseek(deliver_datafile, SPOOL_DATA_START_OFFSET, SEEK_SET); + while ((len = read(deliver_datafile, deliver_in_buffer, + DELIVER_IN_BUFFER_SIZE)) > 0) + { + if (!write_chunk(fd, deliver_in_buffer, len, use_crlf)) return FALSE; + if (size_limit > 0) + { + written += len; + if (written > size_limit) + { + len = 0; /* Pretend EOF */ + break; + } + } + } + + /* Finished with the check string */ + + nl_check_length = nl_escape_length = 0; + + /* A read error on the body will have left len == -1 and errno set. */ + + if (len != 0) return FALSE; + + /* If requested, add a terminating "." line (SMTP output). */ + + if ((options & topt_end_dot) != 0 && !write_chunk(fd, US".\n", 2, use_crlf)) + return FALSE; + } + +/* Write out any remaining data in the buffer before returning. */ + +return (len = chunk_ptr - deliver_out_buffer) <= 0 || + transport_write_block(fd, deliver_out_buffer, len); +} + + + + +/************************************************* +* External interface to write the message * +*************************************************/ + +/* If there is no filtering required, call the internal function above to do +the real work, passing over all the arguments from this function. Otherwise, +set up a filtering process, fork another process to call the internal function +to write to the filter, and in this process just suck from the filter and write +down the given fd. At the end, tidy up the pipes and the processes. + +Arguments: as for internal_transport_write_message() above + +Returns: TRUE on success; FALSE (with errno) for any failure + transport_count is incremented by the number of bytes written +*/ + +BOOL +transport_write_message(address_item *addr, int fd, int options, + int size_limit, uschar *add_headers, uschar *remove_headers, + uschar *check_string, uschar *escape_string, rewrite_rule *rewrite_rules, + int rewrite_existflags) +{ +BOOL use_crlf; +BOOL last_filter_was_NL = TRUE; +int rc, len, yield, fd_read, fd_write, save_errno; +int pfd[2]; +pid_t filter_pid, write_pid; + +/* If there is no filter command set up, call the internal function that does +the actual work, passing it the incoming fd, and return its result. */ + +if (transport_filter_argv == NULL) + return internal_transport_write_message(addr, fd, options, size_limit, + add_headers, remove_headers, check_string, escape_string, + rewrite_rules, rewrite_existflags); + +/* Otherwise the message must be written to a filter process and read back +before being written to the incoming fd. First set up the special processing to +be done during the copying. */ + +use_crlf = (options & topt_use_crlf) != 0; +nl_partial_match = -1; + +if (check_string != NULL && escape_string != NULL) + { + nl_check = check_string; + nl_check_length = Ustrlen(nl_check); + nl_escape = escape_string; + nl_escape_length = Ustrlen(nl_escape); + } +else nl_check_length = nl_escape_length = 0; + +/* Start up a subprocess to run the command. Ensure that our main fd will +be closed when the subprocess execs, but remove the flag afterwards. +(Otherwise, if this is a TCP/IP socket, it can't get passed on to another +process to deliver another message.) We get back stdin/stdout file descriptors. +If the process creation failed, give an error return. */ + +fd_read = -1; +fd_write = -1; +save_errno = 0; +yield = FALSE; +write_pid = (pid_t)(-1); + +fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); +filter_pid = child_open(transport_filter_argv, NULL, 077, &fd_write, &fd_read, + FALSE); +fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) & ~FD_CLOEXEC); +if (filter_pid < 0) goto TIDY_UP; /* errno set */ + +DEBUG(D_transport) + debug_printf("process %d running as transport filter: write=%d read=%d\n", + (int)filter_pid, fd_write, fd_read); + +/* Fork subprocess to write the message to the filter, and return the result +via a(nother) pipe. While writing to the filter, we do not do the CRLF, +smtp dots, or check string processing. */ + +if (pipe(pfd) != 0) goto TIDY_UP; /* errno set */ +if ((write_pid = fork()) == 0) + { + BOOL rc; + close(fd_read); + close(pfd[pipe_read]); + nl_check_length = nl_escape_length = 0; + rc = internal_transport_write_message(addr, fd_write, + (options & ~(topt_use_crlf | topt_end_dot)), + size_limit, add_headers, remove_headers, NULL, NULL, + rewrite_rules, rewrite_existflags); + save_errno = errno; + write(pfd[pipe_write], (void *)&rc, sizeof(BOOL)); + write(pfd[pipe_write], (void *)&save_errno, sizeof(int)); + write(pfd[pipe_write], (void *)&(addr->more_errno), sizeof(int)); + _exit(0); + } +save_errno = errno; + +/* Parent process: close our copy of the writing subprocess' pipes. */ + +close(pfd[pipe_write]); +close(fd_write); +fd_write = -1; + +/* Writing process creation failed */ + +if (write_pid < 0) + { + errno = save_errno; /* restore */ + goto TIDY_UP; + } + +/* When testing, let the subprocess get going */ + +if (running_in_test_harness) millisleep(250); + +DEBUG(D_transport) + debug_printf("process %d writing to transport filter\n", (int)write_pid); + +/* Copy the message from the filter to the output fd. A read error leaves len +== -1 and errno set. We need to apply a timeout to the read, to cope with +the case when the filter gets stuck, but it can be quite a long one. The +default is 5m, but this is now configurable. */ + +DEBUG(D_transport) debug_printf("copying from the filter\n"); + +/* Copy the output of the filter, remembering if the last character was NL. If +no data is returned, that counts as "ended with NL" (default setting of the +variable is TRUE). */ + +chunk_ptr = deliver_out_buffer; + +for (;;) + { + sigalrm_seen = FALSE; + alarm(transport_filter_timeout); + len = read(fd_read, deliver_in_buffer, DELIVER_IN_BUFFER_SIZE); + alarm(0); + if (sigalrm_seen) + { + errno = ETIMEDOUT; + goto TIDY_UP; + } + + /* If the read was successful, write the block down the original fd, + remembering whether it ends in \n or not. */ + + if (len > 0) + { + if (!write_chunk(fd, deliver_in_buffer, len, use_crlf)) goto TIDY_UP; + last_filter_was_NL = (deliver_in_buffer[len-1] == '\n'); + } + + /* Otherwise, break the loop. If we have hit EOF, set yield = TRUE. */ + + else + { + if (len == 0) yield = TRUE; + break; + } + } + +/* Tidying up code. If yield = FALSE there has been an error and errno is set +to something. Ensure the pipes are all closed and the processes are removed. If +there has been an error, kill the processes before waiting for them, just to be +sure. Also apply a paranoia timeout. */ + +TIDY_UP: +save_errno = errno; + +close(fd_read); +if (fd_write > 0) close(fd_write); + +if (!yield) + { + if (filter_pid > 0) kill(filter_pid, SIGKILL); + if (write_pid > 0) kill(write_pid, SIGKILL); + } + +/* Wait for the filter process to complete. */ + +DEBUG(D_transport) debug_printf("waiting for filter process\n"); +if (filter_pid > 0 && (rc = child_close(filter_pid, 30)) != 0 && yield) + { + yield = FALSE; + save_errno = ERRNO_FILTER_FAIL; + addr->more_errno = rc; + DEBUG(D_transport) debug_printf("filter process returned %d\n", rc); + } + +/* Wait for the writing process to complete. If it ends successfully, +read the results from its pipe. */ + +DEBUG(D_transport) debug_printf("waiting for writing process\n"); +if (write_pid > 0) + { + if ((rc = child_close(write_pid, 30)) == 0) + { + BOOL ok; + read(pfd[pipe_read], (void *)&ok, sizeof(BOOL)); + if (!ok) + { + read(pfd[pipe_read], (void *)&save_errno, sizeof(int)); + read(pfd[pipe_read], (void *)&(addr->more_errno), sizeof(int)); + yield = FALSE; + } + } + else if (yield) + { + yield = FALSE; + save_errno = ERRNO_FILTER_FAIL; + addr->more_errno = rc; + DEBUG(D_transport) debug_printf("writing process returned %d\n", rc); + } + } +close(pfd[pipe_read]); + +/* If there have been no problems we can now add the terminating "." if this is +SMTP output, turning off escaping beforehand. If the last character from the +filter was not NL, insert a NL to make the SMTP protocol work. */ + +if (yield) + { + nl_check_length = nl_escape_length = 0; + if ((options & topt_end_dot) != 0 && (last_filter_was_NL? + !write_chunk(fd, US".\n", 2, use_crlf) : + !write_chunk(fd, US"\n.\n", 3, use_crlf))) + { + yield = FALSE; + } + + /* Write out any remaining data in the buffer. */ + + else + { + yield = (len = chunk_ptr - deliver_out_buffer) <= 0 || + transport_write_block(fd, deliver_out_buffer, len); + } + } +else errno = save_errno; /* From some earlier error */ + +DEBUG(D_transport) + { + debug_printf("end of filtering transport writing: yield=%d\n", yield); + if (!yield) + debug_printf("errno=%d more_errno=%d\n", errno, addr->more_errno); + } + +return yield; +} + + + + + +/************************************************* +* Update waiting database * +*************************************************/ + +/* This is called when an address is deferred by remote transports that are +capable of sending more than one message over one connection. A database is +maintained for each transport, keeping track of which messages are waiting for +which hosts. The transport can then consult this when eventually a successful +delivery happens, and if it finds that another message is waiting for the same +host, it can fire up a new process to deal with it using the same connection. + +The database records are keyed by host name. They can get full if there are +lots of messages waiting, and so there is a continuation mechanism for them. + +Each record contains a list of message ids, packed end to end without any +zeros. Each one is MESSAGE_ID_LENGTH bytes long. The count field says how many +in this record, and the sequence field says if there are any other records for +this host. If the sequence field is 0, there are none. If it is 1, then another +record with the name :0 exists; if it is 2, then two other records +with sequence numbers 0 and 1 exist, and so on. + +Currently, an exhaustive search of all continuation records has to be done to +determine whether to add a message id to a given record. This shouldn't be +too bad except in extreme cases. I can't figure out a *simple* way of doing +better. + +Old records should eventually get swept up by the exim_tidydb utility. + +Arguments: + hostlist list of hosts that this message could be sent to; + the update_waiting flag is set if a host is to be noted + tpname name of the transport + +Returns: nothing +*/ + +void +transport_update_waiting(host_item *hostlist, uschar *tpname) +{ +uschar buffer[256]; +uschar *prevname = US""; +host_item *host; +open_db dbblock; +open_db *dbm_file; + +/* Open the database for this transport */ + +sprintf(CS buffer, "wait-%.200s", tpname); +dbm_file = dbfn_open(buffer, O_RDWR, &dbblock, TRUE); +if (dbm_file == NULL) return; + +/* Scan the list of hosts for which this message is waiting, and ensure +that the message id is in each host record for those that have the +update_waiting flag set. */ + +for (host = hostlist; host!= NULL; host = host->next) + { + BOOL already = FALSE; + dbdata_wait *host_record; + uschar *s; + int i, host_length; + + /* Skip if the update_waiting flag is not set. */ + + if (!host->update_waiting) continue; + + /* Skip if this is the same host as we just processed; otherwise remember + the name for next time. */ + + if (Ustrcmp(prevname, host->name) == 0) continue; + prevname = host->name; + + /* Look up the host record; if there isn't one, make an empty one. */ + + host_record = dbfn_read(dbm_file, host->name); + if (host_record == NULL) + { + host_record = store_get(sizeof(dbdata_wait) + MESSAGE_ID_LENGTH); + host_record->count = host_record->sequence = 0; + } + + /* Compute the current length */ + + host_length = host_record->count * MESSAGE_ID_LENGTH; + + /* Search the record to see if the current message is already in it. */ + + for (s = host_record->text; s < host_record->text + host_length; + s += MESSAGE_ID_LENGTH) + { + if (Ustrncmp(s, message_id, MESSAGE_ID_LENGTH) == 0) + { already = TRUE; break; } + } + + /* If we haven't found this message in the main record, search any + continuation records that exist. */ + + for (i = host_record->sequence - 1; i >= 0 && !already; i--) + { + dbdata_wait *cont; + sprintf(CS buffer, "%.200s:%d", host->name, i); + cont = dbfn_read(dbm_file, buffer); + if (cont != NULL) + { + int clen = cont->count * MESSAGE_ID_LENGTH; + for (s = cont->text; s < cont->text + clen; s += MESSAGE_ID_LENGTH) + { + if (Ustrncmp(s, message_id, MESSAGE_ID_LENGTH) == 0) + { already = TRUE; break; } + } + } + } + + /* If this message is already in a record, no need to update. */ + + if (already) continue; + + + /* If this record is full, write it out with a new name constructed + from the sequence number, increase the sequence number, and empty + the record. */ + + if (host_record->count >= WAIT_NAME_MAX) + { + sprintf(CS buffer, "%.200s:%d", host->name, host_record->sequence); + dbfn_write(dbm_file, buffer, host_record, sizeof(dbdata_wait) + host_length); + host_record->sequence++; + host_record->count = 0; + host_length = 0; + } + + /* If this record is not full, increase the size of the record to + allow for one new message id. */ + + else + { + dbdata_wait *newr = + store_get(sizeof(dbdata_wait) + host_length + MESSAGE_ID_LENGTH); + memcpy(newr, host_record, sizeof(dbdata_wait) + host_length); + host_record = newr; + } + + /* Now add the new name on the end */ + + memcpy(host_record->text + host_length, message_id, MESSAGE_ID_LENGTH); + host_record->count++; + host_length += MESSAGE_ID_LENGTH; + + /* Update the database */ + + dbfn_write(dbm_file, host->name, host_record, sizeof(dbdata_wait) + host_length); + } + +/* All now done */ + +dbfn_close(dbm_file); +} + + + + +/************************************************* +* Test for waiting messages * +*************************************************/ + +/* This function is called by a remote transport which uses the previous +function to remember which messages are waiting for which remote hosts. It's +called after a successful delivery and its job is to check whether there is +another message waiting for the same host. However, it doesn't do this if the +current continue sequence is greater than the maximum supplied as an argument, +or greater than the global connection_max_messages, which, if set, overrides. + +Arguments: + transport_name name of the transport + hostname name of the host + local_message_max maximum number of messages down one connection + as set by the caller transport + new_message_id set to the message id of a waiting message + more set TRUE if there are yet more messages waiting + +Returns: TRUE if new_message_id set; FALSE otherwise +*/ + +BOOL +transport_check_waiting(uschar *transport_name, uschar *hostname, + int local_message_max, uschar *new_message_id, BOOL *more) +{ +dbdata_wait *host_record; +int host_length, path_len; +open_db dbblock; +open_db *dbm_file; +uschar buffer[256]; + +*more = FALSE; + +DEBUG(D_transport) + { + debug_printf("transport_check_waiting entered\n"); + debug_printf(" sequence=%d local_max=%d global_max=%d\n", + continue_sequence, local_message_max, connection_max_messages); + } + +/* Do nothing if we have hit the maximum number that can be send down one +connection. */ + +if (connection_max_messages >= 0) local_message_max = connection_max_messages; +if (local_message_max > 0 && continue_sequence >= local_message_max) + { + DEBUG(D_transport) + debug_printf("max messages for one connection reached: returning\n"); + return FALSE; + } + +/* Open the waiting information database. */ + +sprintf(CS buffer, "wait-%.200s", transport_name); +dbm_file = dbfn_open(buffer, O_RDWR, &dbblock, TRUE); +if (dbm_file == NULL) return FALSE; + +/* See if there is a record for this host; if not, there's nothing to do. */ + +host_record = dbfn_read(dbm_file, hostname); +if (host_record == NULL) + { + dbfn_close(dbm_file); + DEBUG(D_transport) debug_printf("no messages waiting for %s\n", hostname); + return FALSE; + } + +/* If the data in the record looks corrupt, just log something and +don't try to use it. */ + +if (host_record->count > WAIT_NAME_MAX) + { + dbfn_close(dbm_file); + log_write(0, LOG_MAIN|LOG_PANIC, "smtp-wait database entry for %s has bad " + "count=%d (max=%d)", hostname, host_record->count, WAIT_NAME_MAX); + return FALSE; + } + +/* Scan the message ids in the record from the end towards the beginning, +until one is found for which a spool file actually exists. If the record gets +emptied, delete it and continue with any continuation records that may exist. +*/ + +host_length = host_record->count * MESSAGE_ID_LENGTH; + +/* Loop to handle continuation host records in the database */ + +for (;;) + { + BOOL found = FALSE; + + sprintf(CS buffer, "%s/input/", spool_directory); + path_len = Ustrlen(buffer); + + for (host_length -= MESSAGE_ID_LENGTH; host_length >= 0; + host_length -= MESSAGE_ID_LENGTH) + { + struct stat statbuf; + Ustrncpy(new_message_id, host_record->text + host_length, + MESSAGE_ID_LENGTH); + new_message_id[MESSAGE_ID_LENGTH] = 0; + + if (split_spool_directory) + sprintf(CS(buffer + path_len), "%c/%s-D", new_message_id[5], new_message_id); + else + sprintf(CS(buffer + path_len), "%s-D", new_message_id); + + /* The listed message may be the one we are currently processing. If + so, we want to remove it from the list without doing anything else. + If not, do a stat to see if it is an existing message. If it is, break + the loop to handle it. No need to bother about locks; as this is all + "hint" processing, it won't matter if it doesn't exist by the time exim + actually tries to deliver it. */ + + if (Ustrcmp(new_message_id, message_id) != 0 && + Ustat(buffer, &statbuf) == 0) + { + found = TRUE; + break; + } + } + + /* If we have removed all the message ids from the record delete the record. + If there is a continuation record, fetch it and remove it from the file, + as it will be rewritten as the main record. Repeat in the case of an + empty continuation. */ + + while (host_length <= 0) + { + int i; + dbdata_wait *newr = NULL; + + /* Search for a continuation */ + + for (i = host_record->sequence - 1; i >= 0 && newr == NULL; i--) + { + sprintf(CS buffer, "%.200s:%d", hostname, i); + newr = dbfn_read(dbm_file, buffer); + } + + /* If no continuation, delete the current and break the loop */ + + if (newr == NULL) + { + dbfn_delete(dbm_file, hostname); + break; + } + + /* Else replace the current with the continuation */ + + dbfn_delete(dbm_file, buffer); + host_record = newr; + host_length = host_record->count * MESSAGE_ID_LENGTH; + } + + /* If we found an existing message, break the continuation loop. */ + + if (found) break; + + /* If host_length <= 0 we have emptied a record and not found a good message, + and there are no continuation records. Otherwise there is a continuation + record to process. */ + + if (host_length <= 0) + { + dbfn_close(dbm_file); + DEBUG(D_transport) debug_printf("waiting messages already delivered\n"); + return FALSE; + } + } + +/* Control gets here when an existing message has been encountered; its +id is in new_message_id, and host_length is the revised length of the +host record. If it is zero, the record has been removed. Update the +record if required, close the database, and return TRUE. */ + +if (host_length > 0) + { + host_record->count = host_length/MESSAGE_ID_LENGTH; + dbfn_write(dbm_file, hostname, host_record, (int)sizeof(dbdata_wait) + host_length); + *more = TRUE; + } + +dbfn_close(dbm_file); +return TRUE; +} + + + +/************************************************* +* Deliver waiting message down same socket * +*************************************************/ + +/* Fork a new exim process to deliver the message, and do a re-exec, both to +get a clean delivery process, and to regain root privilege in cases where it +has been given away. + +Arguments: + transport_name to pass to the new process + hostname ditto + hostaddress ditto + id the new message to process + socket_fd the connected socket + +Returns: FALSE if fork fails; TRUE otherwise +*/ + +BOOL +transport_pass_socket(uschar *transport_name, uschar *hostname, + uschar *hostaddress, uschar *id, int socket_fd) +{ +pid_t pid; +int status; + +DEBUG(D_transport) debug_printf("transport_pass_socket entered\n"); + +if ((pid = fork()) == 0) + { + int i = 16; + uschar **argv; + + /* Disconnect entirely from the parent process. If we are running in the + test harness, wait for a bit to allow the previous process time to finish, + write the log, etc., so that the output is always in the same order for + automatic comparison. */ + + if ((pid = fork()) != 0) _exit(EXIT_SUCCESS); + if (running_in_test_harness) millisleep(500); + + /* Set up the calling arguments; use the standard function for the basics, + but we have a number of extras that may be added. */ + + argv = child_exec_exim(CEE_RETURN_ARGV, TRUE, &i, FALSE, 0); + + if (smtp_authenticated) argv[i++] = US"-MCA"; + + #ifdef SUPPORT_TLS + if (tls_offered) argv[i++] = US"-MCT"; + #endif + + if (smtp_use_size) argv[i++] = US"-MCS"; + if (smtp_use_pipelining) argv[i++] = US"-MCP"; + + if (queue_run_pid != (pid_t)0) + { + argv[i++] = US"-MCQ"; + argv[i++] = string_sprintf("%d", queue_run_pid); + argv[i++] = string_sprintf("%d", queue_run_pipe); + } + + argv[i++] = US"-MC"; + argv[i++] = transport_name; + argv[i++] = hostname; + argv[i++] = hostaddress; + argv[i++] = string_sprintf("%d", continue_sequence + 1); + argv[i++] = id; + argv[i++] = NULL; + + /* Arrange for the channel to be on stdin. */ + + if (socket_fd != 0) + { + dup2(socket_fd, 0); + close(socket_fd); + } + + DEBUG(D_exec) debug_print_argv(argv); + exim_nullstd(); /* Ensure std{out,err} exist */ + execv(CS argv[0], (char *const *)argv); + + DEBUG(D_any) debug_printf("execv failed: %s\n", strerror(errno)); + _exit(errno); /* Note: must be _exit(), NOT exit() */ + } + +/* If the process creation succeeded, wait for the first-level child, which +immediately exits, leaving the second level process entirely disconnected from +this one. */ + +if (pid > 0) + { + int rc; + while ((rc = wait(&status)) != pid && (rc >= 0 || errno != ECHILD)); + DEBUG(D_transport) debug_printf("transport_pass_socket succeeded\n"); + return TRUE; + } +else + { + DEBUG(D_transport) debug_printf("transport_pass_socket failed to fork: %s\n", + strerror(errno)); + return FALSE; + } +} + + + +/************************************************* +* Set up direct (non-shell) command * +*************************************************/ + +/* This function is called when a command line is to be parsed and executed +directly, without the use of /bin/sh. It is called by the pipe transport, +the queryprogram router, and also from the main delivery code when setting up a +transport filter process. The code for ETRN also makes use of this; in that +case, no addresses are passed. + +Arguments: + argvptr pointer to anchor for argv vector + cmd points to the command string + expand_arguments true if expansion is to occur + expand_failed error value to set if expansion fails; not relevant if + addr == NULL + addr chain of addresses, or NULL + etext text for use in error messages + errptr where to put error message if addr is NULL; + otherwise it is put in the first address + +Returns: TRUE if all went well; otherwise an error will be + set in the first address and FALSE returned +*/ + +BOOL +transport_set_up_command(uschar ***argvptr, uschar *cmd, BOOL expand_arguments, + int expand_failed, address_item *addr, uschar *etext, uschar **errptr) +{ +address_item *ad; +uschar **argv; +uschar *s, *ss; +int address_count = 0; +int argcount = 0; +int i, max_args; + +/* Get store in which to build an argument list. Count the number of addresses +supplied, and allow for that many arguments, plus an additional 60, which +should be enough for anybody. Multiple addresses happen only when the local +delivery batch option is set. */ + +for (ad = addr; ad != NULL; ad = ad->next) address_count++; +max_args = address_count + 60; +*argvptr = argv = store_get((max_args+1)*sizeof(uschar *)); + +/* Split the command up into arguments terminated by white space. Lose +trailing space at the start and end. Double-quoted arguments can contain \\ and +\" escapes and so can be handled by the standard function; single-quoted +arguments are verbatim. Copy each argument into a new string. */ + +s = cmd; +while (isspace(*s)) s++; + +while (*s != 0 && argcount < max_args) + { + if (*s == '\'') + { + ss = s + 1; + while (*ss != 0 && *ss != '\'') ss++; + argv[argcount++] = ss = store_get(ss - s++); + while (*s != 0 && *s != '\'') *ss++ = *s++; + if (*s != 0) s++; + *ss++ = 0; + } + else argv[argcount++] = string_dequote(&s); + while (isspace(*s)) s++; + } + +argv[argcount] = (uschar *)0; + +/* If *s != 0 we have run out of argument slots. */ + +if (*s != 0) + { + uschar *msg = string_sprintf("Too many arguments in command \"%s\" in " + "%s", cmd, etext); + if (addr != NULL) + { + addr->transport_return = FAIL; + addr->message = msg; + } + else *errptr = msg; + return FALSE; + } + +/* Expand each individual argument if required. Expansion happens for pipes set +up in filter files and with directly-supplied commands. It does not happen if +the pipe comes from a traditional .forward file. A failing expansion is a big +disaster if the command came from Exim's configuration; if it came from a user +it is just a normal failure. The expand_failed value is used as the error value +to cater for these two cases. + +An argument consisting just of the text "$pipe_addresses" is treated specially. +It is not passed to the general expansion function. Instead, it is replaced by +a number of arguments, one for each address. This avoids problems with shell +metacharacters and spaces in addresses. + +If the parent of the top address has an original part of "system-filter", this +pipe was set up by the system filter, and we can permit the expansion of +$recipients. */ + +DEBUG(D_transport) + { + debug_printf("direct command:\n"); + for (i = 0; argv[i] != (uschar *)0; i++) + debug_printf(" argv[%d] = %s\n", i, string_printing(argv[i])); + } + +if (expand_arguments) + { + BOOL allow_dollar_recipients = addr != NULL && + addr->parent != NULL && + Ustrcmp(addr->parent->address, "system-filter") == 0; + + for (i = 0; argv[i] != (uschar *)0; i++) + { + + /* Handle special fudge for passing an address list */ + + if (addr != NULL && + (Ustrcmp(argv[i], "$pipe_addresses") == 0 || + Ustrcmp(argv[i], "${pipe_addresses}") == 0)) + { + int additional; + + if (argcount + address_count - 1 > max_args) + { + addr->transport_return = FAIL; + addr->message = string_sprintf("Too many arguments to command \"%s\" " + "in %s", cmd, etext); + return FALSE; + } + + additional = address_count - 1; + if (additional > 0) + memmove(argv + i + 1 + additional, argv + i + 1, + (argcount - i)*sizeof(uschar *)); + + for (ad = addr; ad != NULL; ad = ad->next) argv[i++] = ad->address; + i--; + } + + /* Handle normal expansion string */ + + else + { + uschar *expanded_arg; + enable_dollar_recipients = allow_dollar_recipients; + expanded_arg = expand_string(argv[i]); + enable_dollar_recipients = FALSE; + + if (expanded_arg == NULL) + { + uschar *msg = string_sprintf("Expansion of \"%s\" " + "from command \"%s\" in %s failed: %s", + argv[i], cmd, etext, expand_string_message); + if (addr != NULL) + { + addr->transport_return = expand_failed; + addr->message = msg; + } + else *errptr = msg; + return FALSE; + } + argv[i] = expanded_arg; + } + } + + DEBUG(D_transport) + { + debug_printf("direct command after expansion:\n"); + for (i = 0; argv[i] != (uschar *)0; i++) + debug_printf(" argv[%d] = %s\n", i, string_printing(argv[i])); + } + } + +return TRUE; +} + +/* End of transport.c */ diff --git a/src/src/tree.c b/src/src/tree.c new file mode 100644 index 000000000..12592c100 --- /dev/null +++ b/src/src/tree.c @@ -0,0 +1,345 @@ +/* $Cambridge: exim/src/src/tree.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Functions for maintaining binary balanced trees and some associated +functions as well. */ + + +#include "exim.h" + + + + +/************************************************* +* Add entry to non-recipients tree * +*************************************************/ + +/* Duplicates are just discarded. + +Arguments: + s string to add + +Returns: nothing +*/ + +void +tree_add_nonrecipient(uschar *s) +{ +tree_node *node = store_get(sizeof(tree_node) + Ustrlen(s)); +Ustrcpy(node->name, s); +node->data.ptr = NULL; +if (!tree_insertnode(&tree_nonrecipients, node)) store_reset(node); +} + + + +/************************************************* +* Add entry to duplicates tree * +*************************************************/ + +/* Duplicates are just discarded. + +Argument: + s string to add + addr the address is is a duplicate of + +Returns: nothing +*/ + +void +tree_add_duplicate(uschar *s, address_item *addr) +{ +tree_node *node = store_get(sizeof(tree_node) + Ustrlen(s)); +Ustrcpy(node->name, s); +node->data.ptr = addr; +if (!tree_insertnode(&tree_duplicates, node)) store_reset(node); +} + + + +/************************************************* +* Add entry to unusable addresses tree * +*************************************************/ + +/* Duplicates are simply discarded. + +Argument: the host item +Returns: nothing +*/ + +void +tree_add_unusable(host_item *h) +{ +tree_node *node; +uschar s[256]; +sprintf(CS s, "T:%.200s:%s", h->name, h->address); +node = store_get(sizeof(tree_node) + Ustrlen(s)); +Ustrcpy(node->name, s); +node->data.val = h->why; +if (h->status == hstatus_unusable_expired) node->data.val += 256; +if (!tree_insertnode(&tree_unusable, node)) store_reset(node); +} + + + +/************************************************* +* Write a tree in re-readable form * +*************************************************/ + +/* This function writes out a tree in a form in which it can +easily be re-read. It is used for writing out the non-recipients +tree onto the spool, for retrieval at the next retry time. + +The format is as follows: + + . If the tree is empty, write one line containing XX. + + . Otherwise, each node is written, preceded by two letters + (Y/N) indicating whether it has left or right children. + + . The left subtree (if any) then follows, then the right subtree. + +First, there's an internal recursive subroutine. + +Arguments: + p current node + f FILE to write to + +Returns: nothing +*/ + +static void +write_tree(tree_node *p, FILE *f) +{ +fprintf(f, "%c%c %s\n", + (p->left == NULL)? 'N':'Y', (p->right == NULL)? 'N':'Y', p->name); +if (p->left != NULL) write_tree(p->left, f); +if (p->right != NULL) write_tree(p->right, f); +} + +/* This is the top-level function, with the same arguments. */ + +void +tree_write(tree_node *p, FILE *f) +{ +if (p == NULL) + { + fprintf(f, "XX\n"); + return; + } +write_tree(p, f); +} + + + + + +/*********************************************************** +* Binary Balanced Tree Management Routines * +***********************************************************/ + +/* This set of routines maintains a balanced binary tree using +the algorithm given in Knuth Vol 3 page 455. + +The routines make use of uschar * pointers as byte pointers, +so as to be able to do arithmetic on them, since ANSI Standard +C does not permit additions and subtractions on void pointers. */ + + +/************************************************* +* Flags and Parameters * +*************************************************/ + +#define tree_lbal 1 /* left subtree is longer */ +#define tree_rbal 2 /* right subtree is longer */ +#define tree_bmask 3 /* mask for flipping bits */ + + +/************************************************* +* Insert a new node into a tree * +*************************************************/ + +/* The node->name field must (obviously) be set, but the other +fields need not be initialized. + +Arguments: + treebase pointer to the root of the tree + node the note to insert, with name field set + +Returns: TRUE if node inserted; FALSE if not (duplicate) +*/ + +int +tree_insertnode(tree_node **treebase, tree_node *node) +{ +tree_node *p = *treebase; +tree_node **q, *r, *s, **t; +int a; + +node->left = node->right = NULL; +node->balance = 0; + +/* Deal with an empty tree */ + +if (p == NULL) + { + *treebase = node; + return TRUE; + } + +/* The tree is not empty. While finding the insertion point, +q points to the pointer to p, and t points to the pointer to +the potential re-balancing point. */ + +q = treebase; +t = q; + +/* Loop to search tree for place to insert new node */ + +for (;;) + { + int c = Ustrcmp(node->name, p->name); + if (c == 0) return FALSE; /* Duplicate node encountered */ + + /* Deal with climbing down the tree, exiting from the loop + when we reach a leaf. */ + + q = (c > 0)? &(p->right) : &(p->left); + p = *q; + if (p == NULL) break; + + /* Save the address of the pointer to the last node en route + which has a non-zero balance factor. */ + + if (p->balance != 0) t = q; + } + +/* When the above loop completes, q points to the pointer to NULL; +that is the place at which the new node must be inserted. */ + +*q = node; + +/* Set up s as the potential re-balancing point, and r as the +next node after it along the route. */ + +s = *t; +r = (Ustrcmp(node->name, s->name) > 0)? s->right : s->left; + +/* Adjust balance factors along the route from s to node. */ + +p = r; + +while (p != node) + { + if (Ustrcmp(node->name, p->name) < 0) + { + p->balance = tree_lbal; + p = p->left; + } + else + { + p->balance = tree_rbal; + p = p->right; + } + } + +/* Now the World-Famous Balancing Act */ + +a = (Ustrcmp(node->name, s->name) < 0)? tree_lbal : tree_rbal; + +if (s->balance == 0) s->balance = (uschar)a; /* The tree has grown higher */ + else if (s->balance != (uschar)a) s->balance = 0; /* It's become more balanced */ +else /* It's got out of balance */ + { + /* Perform a single rotation */ + + if (r->balance == (uschar)a) + { + p = r; + if (a == tree_rbal) + { + s->right = r->left; + r->left = s; + } + else + { + s->left = r->right; + r->right = s; + } + s->balance = 0; + r->balance = 0; + } + + /* Perform a double rotation There was an occasion when the balancing + factors were screwed up by a bug in the code that reads a tree from + the spool. In case this ever happens again, check for changing p to NULL + and don't do it. It is better to have an unbalanced tree than a crash. */ + + else + { + if (a == tree_rbal) + { + if (r->left == NULL) return TRUE; /* Bail out if tree corrupt */ + p = r->left; + r->left = p->right; + p->right = r; + s->right = p->left; + p->left = s; + } + else + { + if (r->right == NULL) return TRUE; /* Bail out if tree corrupt */ + p = r->right; + r->right = p->left; + p->left = r; + s->left = p->right; + p->right = s; + } + + s->balance = (p->balance == (uschar)a)? (uschar)(a^tree_bmask) : 0; + r->balance = (p->balance == (uschar)(a^tree_bmask))? (uschar)a : 0; + p->balance = 0; + } + + /* Finishing touch */ + + *t = p; + } + +return TRUE; /* Successful insertion */ +} + + + +/************************************************* +* Search tree for node by name * +*************************************************/ + +/* +Arguments: + p root of tree + name key to search for + +Returns: pointer to node, or NULL if not found +*/ + +tree_node * +tree_search(tree_node *p, uschar *name) +{ +while (p != NULL) + { + int c = Ustrcmp(name, p->name); + if (c == 0) return p; + p = (c < 0)? p->left : p->right; + } +return NULL; +} + + +/* End of tree.c */ diff --git a/src/src/verify.c b/src/src/verify.c new file mode 100644 index 000000000..bae410e67 --- /dev/null +++ b/src/src/verify.c @@ -0,0 +1,2508 @@ +/* $Cambridge: exim/src/src/verify.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Functions concerned with verifying things. The original code for callout +caching was contributed by Kevin Fleming (but I hacked it around a bit). */ + + +#include "exim.h" + + +/* Structure for caching DNSBL lookups */ + +typedef struct dnsbl_cache_block { + dns_address *rhs; + uschar *text; + int rc; + BOOL text_set; +} dnsbl_cache_block; + + +/* Anchor for DNSBL cache */ + +static tree_node *dnsbl_cache = NULL; + + + +/************************************************* +* Retrieve a callout cache record * +*************************************************/ + +/* If a record exists, check whether it has expired. + +Arguments: + dbm_file an open hints file + key the record key + type "address" or "domain" + positive_expire expire time for positive records + negative_expire expire time for negative records + +Returns: the cache record if a non-expired one exists, else NULL +*/ + +static dbdata_callout_cache * +get_callout_cache_record(open_db *dbm_file, uschar *key, uschar *type, + int positive_expire, int negative_expire) +{ +BOOL negative; +int length, expire; +time_t now; +dbdata_callout_cache *cache_record; + +cache_record = dbfn_read_with_length(dbm_file, key, &length); + +if (cache_record == NULL) + { + HDEBUG(D_verify) debug_printf("callout cache: no %s record found\n", type); + return NULL; + } + +/* We treat a record as "negative" if its result field is not positive, or if +it is a domain record and the postmaster field is negative. */ + +negative = cache_record->result != ccache_accept || + (type[0] == 'd' && cache_record->postmaster_result == ccache_reject); +expire = negative? negative_expire : positive_expire; +now = time(NULL); + +if (now - cache_record->time_stamp > expire) + { + HDEBUG(D_verify) debug_printf("callout cache: %s record expired\n", type); + return NULL; + } + +/* If this is a non-reject domain record, check for the obsolete format version +that doesn't have the postmaster and random timestamps, by looking at the +length. If so, copy it to a new-style block, replicating the record's +timestamp. Then check the additional timestamps. (There's no point wasting +effort if connections are rejected.) */ + +if (type[0] == 'd' && cache_record->result != ccache_reject) + { + if (length == sizeof(dbdata_callout_cache_obs)) + { + dbdata_callout_cache *new = store_get(sizeof(dbdata_callout_cache)); + memcpy(new, cache_record, length); + new->postmaster_stamp = new->random_stamp = new->time_stamp; + cache_record = new; + } + + if (now - cache_record->postmaster_stamp > expire) + cache_record->postmaster_result = ccache_unknown; + + if (now - cache_record->random_stamp > expire) + cache_record->random_result = ccache_unknown; + } + +HDEBUG(D_verify) debug_printf("callout cache: found %s record\n", type); +return cache_record; +} + + + +/************************************************* +* Do callout verification for an address * +*************************************************/ + +/* This function is called from verify_address() when the address has routed to +a host list, and a callout has been requested. Callouts are expensive; that is +why a cache is used to improve the efficiency. + +Arguments: + addr the address that's been routed + host_list the list of hosts to try + tf the transport feedback block + + ifstring "interface" option from transport, or NULL + portstring "port" option from transport, or NULL + protocolstring "protocol" option from transport, or NULL + callout the per-command callout timeout + callout_overall the overall callout timeout (if < 0; use 4*callout) + options the verification options - these bits are used: + vopt_is_recipient => this is a recipient address + vopt_callout_no_cache => don't use callout cache + vopt_callout_random => do the "random" thing + vopt_callout_recipsender => use real sender for recipient + vopt_callout_recippmaster => use postmaster for recipient + se_mailfrom MAIL FROM address for sender verify; NULL => "" + pm_mailfrom if non-NULL, do the postmaster check with this sender + +Returns: OK/FAIL/DEFER +*/ + +static int +do_callout(address_item *addr, host_item *host_list, transport_feedback *tf, + int callout, int callout_overall, int options, uschar *se_mailfrom, + uschar *pm_mailfrom) +{ +BOOL is_recipient = (options & vopt_is_recipient) != 0; +BOOL callout_no_cache = (options & vopt_callout_no_cache) != 0; +BOOL callout_random = (options & vopt_callout_random) != 0; + +int yield = OK; +BOOL done = FALSE; +uschar *address_key; +uschar *from_address; +uschar *random_local_part = NULL; +open_db dbblock; +open_db *dbm_file = NULL; +dbdata_callout_cache new_domain_record; +dbdata_callout_cache_address new_address_record; +host_item *host; +time_t callout_start_time; + +new_domain_record.result = ccache_unknown; +new_domain_record.postmaster_result = ccache_unknown; +new_domain_record.random_result = ccache_unknown; + +memset(&new_address_record, 0, sizeof(new_address_record)); + +/* For a recipient callout, the key used for the address cache record must +include the sender address if we are using the real sender in the callout, +because that may influence the result of the callout. */ + +address_key = addr->address; +from_address = US""; + +if (is_recipient) + { + if ((options & vopt_callout_recipsender) != 0) + { + address_key = string_sprintf("%s/<%s>", addr->address, sender_address); + from_address = sender_address; + } + else if ((options & vopt_callout_recippmaster) != 0) + { + address_key = string_sprintf("%s/", addr->address, + qualify_domain_sender); + from_address = string_sprintf("postmaster@%s", qualify_domain_sender); + } + } + +/* For a sender callout, we must adjust the key if the mailfrom address is not +empty. */ + +else + { + from_address = (se_mailfrom == NULL)? US"" : se_mailfrom; + if (from_address[0] != 0) + address_key = string_sprintf("%s/<%s>", addr->address, from_address); + } + +/* Open the callout cache database, it it exists, for reading only at this +stage, unless caching has been disabled. */ + +if (callout_no_cache) + { + HDEBUG(D_verify) debug_printf("callout cache: disabled by no_cache\n"); + } +else if ((dbm_file = dbfn_open(US"callout", O_RDWR, &dbblock, FALSE)) == NULL) + { + HDEBUG(D_verify) debug_printf("callout cache: not available\n"); + } + +/* If a cache database is available see if we can avoid the need to do an +actual callout by making use of previously-obtained data. */ + +if (dbm_file != NULL) + { + dbdata_callout_cache_address *cache_address_record; + dbdata_callout_cache *cache_record = get_callout_cache_record(dbm_file, + addr->domain, US"domain", + callout_cache_domain_positive_expire, + callout_cache_domain_negative_expire); + + /* If an unexpired cache record was found for this domain, see if the callout + process can be short-circuited. */ + + if (cache_record != NULL) + { + /* If an early command (up to and including MAIL FROM:<>) was rejected, + there is no point carrying on. The callout fails. */ + + if (cache_record->result == ccache_reject) + { + setflag(addr, af_verify_nsfail); + HDEBUG(D_verify) + debug_printf("callout cache: domain gave initial rejection, or " + "does not accept HELO or MAIL FROM:<>\n"); + setflag(addr, af_verify_nsfail); + addr->user_message = US"(result of an earlier callout reused)."; + yield = FAIL; + goto END_CALLOUT; + } + + /* If a previous check on a "random" local part was accepted, we assume + that the server does not do any checking on local parts. There is therefore + no point in doing the callout, because it will always be successful. If a + random check previously failed, arrange not to do it again, but preserve + the data in the new record. If a random check is required but hasn't been + done, skip the remaining cache processing. */ + + if (callout_random) switch(cache_record->random_result) + { + case ccache_accept: + HDEBUG(D_verify) + debug_printf("callout cache: domain accepts random addresses\n"); + goto END_CALLOUT; /* Default yield is OK */ + + case ccache_reject: + HDEBUG(D_verify) + debug_printf("callout cache: domain rejects random addresses\n"); + callout_random = FALSE; + new_domain_record.random_result = ccache_reject; + new_domain_record.random_stamp = cache_record->random_stamp; + break; + + default: + HDEBUG(D_verify) + debug_printf("callout cache: need to check random address handling " + "(not cached or cache expired)\n"); + goto END_CACHE; + } + + /* If a postmaster check is requested, but there was a previous failure, + there is again no point in carrying on. If a postmaster check is required, + but has not been done before, we are going to have to do a callout, so skip + remaining cache processing. */ + + if (pm_mailfrom != NULL) + { + if (cache_record->postmaster_result == ccache_reject) + { + setflag(addr, af_verify_pmfail); + HDEBUG(D_verify) + debug_printf("callout cache: domain does not accept " + "RCPT TO:\n"); + yield = FAIL; + setflag(addr, af_verify_pmfail); + addr->user_message = US"(result of earlier verification reused)."; + goto END_CALLOUT; + } + if (cache_record->postmaster_result == ccache_unknown) + { + HDEBUG(D_verify) + debug_printf("callout cache: need to check RCPT " + "TO: (not cached or cache expired)\n"); + goto END_CACHE; + } + + /* If cache says OK, set pm_mailfrom NULL to prevent a redundant + postmaster check if the address itself has to be checked. Also ensure + that the value in the cache record is preserved (with its old timestamp). + */ + + HDEBUG(D_verify) debug_printf("callout cache: domain accepts RCPT " + "TO:\n"); + pm_mailfrom = NULL; + new_domain_record.postmaster_result = ccache_accept; + new_domain_record.postmaster_stamp = cache_record->postmaster_stamp; + } + } + + /* We can't give a result based on information about the domain. See if there + is an unexpired cache record for this specific address (combined with the + sender address if we are doing a recipient callout with a non-empty sender). + */ + + cache_address_record = (dbdata_callout_cache_address *) + get_callout_cache_record(dbm_file, + address_key, US"address", + callout_cache_positive_expire, + callout_cache_negative_expire); + + if (cache_address_record != NULL) + { + if (cache_address_record->result == ccache_accept) + { + HDEBUG(D_verify) + debug_printf("callout cache: address record is positive\n"); + } + else + { + HDEBUG(D_verify) + debug_printf("callout cache: address record is negative\n"); + addr->user_message = US"Previous (cached) callout verification failure"; + yield = FAIL; + } + goto END_CALLOUT; + } + + /* Close the cache database while we actually do the callout for real. */ + + END_CACHE: + dbfn_close(dbm_file); + dbm_file = NULL; + } + +/* The information wasn't available in the cache, so we have to do a real +callout and save the result in the cache for next time, unless no_cache is set, +or unless we have a previously cached negative random result. If we are to test +with a random local part, ensure that such a local part is available. If not, +log the fact, but carry on without randomming. */ + +if (callout_random && callout_random_local_part != NULL) + { + random_local_part = expand_string(callout_random_local_part); + if (random_local_part == NULL) + log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand " + "callout_random_local_part: %s", expand_string_message); + } + +/* Default the overall callout timeout if not set, and record the time we are +starting so that we can enforce it. */ + +if (callout_overall < 0) callout_overall = 4 * callout; +callout_start_time = time(NULL); + +/* Now make connections to the hosts and do real callouts. The list of hosts +is passed in as an argument. */ + +for (host = host_list; host != NULL && !done; host = host->next) + { + smtp_inblock inblock; + smtp_outblock outblock; + int host_af; + int port = 25; + uschar *helo = US"HELO"; + uschar *interface = NULL; /* Outgoing interface to use; NULL => any */ + uschar inbuffer[4096]; + uschar outbuffer[1024]; + uschar responsebuffer[4096]; + + clearflag(addr, af_verify_pmfail); /* postmaster callout flag */ + clearflag(addr, af_verify_nsfail); /* null sender callout flag */ + + /* Skip this host if we don't have an IP address for it. */ + + if (host->address == NULL) + { + DEBUG(D_verify) debug_printf("no IP address for host name %s: skipping\n", + host->name); + continue; + } + + /* Check the overall callout timeout */ + + if (time(NULL) - callout_start_time >= callout_overall) + { + HDEBUG(D_verify) debug_printf("overall timeout for callout exceeded\n"); + break; + } + + /* Set IPv4 or IPv6 */ + + host_af = (Ustrchr(host->address, ':') == NULL)? AF_INET:AF_INET6; + + /* Expand and interpret the interface and port strings. This has to + be delayed till now, because they may expand differently for different + hosts. If there's a failure, log it, but carry on with the defaults. */ + + deliver_host = host->name; + deliver_host_address = host->address; + if (!smtp_get_interface(tf->interface, host_af, addr, NULL, &interface, + US"callout") || + !smtp_get_port(tf->port, addr, &port, US"callout")) + log_write(0, LOG_MAIN|LOG_PANIC, "<%s>: %s", addr->address, + addr->message); + deliver_host = deliver_host_address = NULL; + + /* Set HELO string according to the protocol */ + + if (Ustrcmp(tf->protocol, "lmtp") == 0) helo = US"LHLO"; + + HDEBUG(D_verify) debug_printf("interface=%s port=%d\n", interface, port); + + /* Set up the buffer for reading SMTP response packets. */ + + inblock.buffer = inbuffer; + inblock.buffersize = sizeof(inbuffer); + inblock.ptr = inbuffer; + inblock.ptrend = inbuffer; + + /* Set up the buffer for holding SMTP commands while pipelining */ + + outblock.buffer = outbuffer; + outblock.buffersize = sizeof(outbuffer); + outblock.ptr = outbuffer; + outblock.cmd_count = 0; + outblock.authenticating = FALSE; + + /* Connect to the host; on failure, just loop for the next one, but we + set the error for the last one. */ + + inblock.sock = outblock.sock = + smtp_connect(host, host_af, port, interface, callout, TRUE); + if (inblock.sock < 0) + { + addr->message = string_sprintf("could not connect to %s [%s]: %s", + host->name, host->address, strerror(errno)); + continue; + } + + /* Wait for initial response, and then run the initial SMTP commands. The + smtp_write_command() function leaves its command in big_buffer. This is + used in error responses. Initialize it in case the connection is + rejected. */ + + Ustrcpy(big_buffer, "initial connection"); + + done = + smtp_read_response(&inblock, responsebuffer, sizeof(responsebuffer), + '2', callout) && + + smtp_write_command(&outblock, FALSE, "%s %s\r\n", helo, + smtp_active_hostname) >= 0 && + smtp_read_response(&inblock, responsebuffer, sizeof(responsebuffer), + '2', callout) && + + smtp_write_command(&outblock, FALSE, "MAIL FROM:<%s>\r\n", + from_address) >= 0 && + smtp_read_response(&inblock, responsebuffer, sizeof(responsebuffer), + '2', callout); + + /* If the host gave an initial error, or does not accept HELO or MAIL + FROM:<>, arrange to cache this information, but don't record anything for an + I/O error or a defer. Do not cache rejections when a non-empty sender has + been used, because that blocks the whole domain for all senders. */ + + if (!done) + { + if (errno == 0 && responsebuffer[0] == '5') + { + setflag(addr, af_verify_nsfail); + if (from_address[0] == 0) new_domain_record.result = ccache_reject; + } + } + + /* Otherwise, proceed to check a "random" address (if required), then the + given address, and the postmaster address (if required). Between each check, + issue RSET, because some servers accept only one recipient after MAIL + FROM:<>. */ + + else + { + new_domain_record.result = ccache_accept; + + /* Do the random local part check first */ + + if (random_local_part != NULL) + { + uschar randombuffer[1024]; + BOOL random_ok = + smtp_write_command(&outblock, FALSE, + "RCPT TO:<%.1000s@%.1000s>\r\n", random_local_part, + addr->domain) >= 0 && + smtp_read_response(&inblock, randombuffer, + sizeof(randombuffer), '2', callout); + + /* Remember when we last did a random test */ + + new_domain_record.random_stamp = time(NULL); + + /* If accepted, we aren't going to do any further tests below. */ + + if (random_ok) + { + new_domain_record.random_result = ccache_accept; + } + + /* Otherwise, cache a real negative response, and get back to the right + state to send RCPT. Unless there's some problem such as a dropped + connection, we expect to succeed, because the commands succeeded above. */ + + else if (errno == 0) + { + if (randombuffer[0] == '5') + new_domain_record.random_result = ccache_reject; + + done = + smtp_write_command(&outblock, FALSE, "RSET\r\n") >= 0 && + smtp_read_response(&inblock, responsebuffer, sizeof(responsebuffer), + '2', callout) && + + smtp_write_command(&outblock, FALSE, "MAIL FROM:<>\r\n") >= 0 && + smtp_read_response(&inblock, responsebuffer, sizeof(responsebuffer), + '2', callout); + } + else done = FALSE; /* Some timeout/connection problem */ + } /* Random check */ + + /* If the host is accepting all local parts, as determined by the "random" + check, we don't need to waste time doing any further checking. */ + + if (new_domain_record.random_result != ccache_accept && done) + { + done = + smtp_write_command(&outblock, FALSE, "RCPT TO:<%.1000s>\r\n", + addr->address) >= 0 && + smtp_read_response(&inblock, responsebuffer, sizeof(responsebuffer), + '2', callout); + + if (done) + new_address_record.result = ccache_accept; + else if (errno == 0 && responsebuffer[0] == '5') + new_address_record.result = ccache_reject; + + /* Do postmaster check if requested */ + + if (done && pm_mailfrom != NULL) + { + done = + smtp_write_command(&outblock, FALSE, "RSET\r\n") >= 0 && + smtp_read_response(&inblock, responsebuffer, + sizeof(responsebuffer), '2', callout) && + + smtp_write_command(&outblock, FALSE, + "MAIL FROM:<%s>\r\n", pm_mailfrom) >= 0 && + smtp_read_response(&inblock, responsebuffer, + sizeof(responsebuffer), '2', callout) && + + smtp_write_command(&outblock, FALSE, + "RCPT TO:\r\n", addr->domain) >= 0 && + smtp_read_response(&inblock, responsebuffer, + sizeof(responsebuffer), '2', callout); + + new_domain_record.postmaster_stamp = time(NULL); + + if (done) + new_domain_record.postmaster_result = ccache_accept; + else if (errno == 0 && responsebuffer[0] == '5') + { + setflag(addr, af_verify_pmfail); + new_domain_record.postmaster_result = ccache_reject; + } + } + } /* Random not accepted */ + } /* MAIL FROM:<> accepted */ + + /* For any failure of the main check, other than a negative response, we just + close the connection and carry on. We can identify a negative response by the + fact that errno is zero. For I/O errors it will be non-zero + + Set up different error texts for logging and for sending back to the caller + as an SMTP response. Log in all cases, using a one-line format. For sender + callouts, give a full response to the caller, but for recipient callouts, + don't give the IP address because this may be an internal host whose identity + is not to be widely broadcast. */ + + if (!done) + { + if (errno == ETIMEDOUT) + { + HDEBUG(D_verify) debug_printf("SMTP timeout\n"); + } + else if (errno == 0) + { + if (*responsebuffer == 0) Ustrcpy(responsebuffer, US"connection dropped"); + + addr->message = + string_sprintf("response to \"%s\" from %s [%s] was: %s", + big_buffer, host->name, host->address, + string_printing(responsebuffer)); + + addr->user_message = is_recipient? + string_sprintf("Callout verification failed:\n%s", responsebuffer) + : + string_sprintf("Called: %s\nSent: %s\nResponse: %s", + host->address, big_buffer, responsebuffer); + + /* Hard rejection ends the process */ + + if (responsebuffer[0] == '5') /* Address rejected */ + { + yield = FAIL; + done = TRUE; + } + } + } + + /* End the SMTP conversation and close the connection. */ + + (void)smtp_write_command(&outblock, FALSE, "QUIT\r\n"); + close(inblock.sock); + } /* Loop through all hosts, while !done */ + +/* If we get here with done == TRUE, a successful callout happened, and yield +will be set OK or FAIL according to the response to the RCPT command. +Otherwise, we looped through the hosts but couldn't complete the business. +However, there may be domain-specific information to cache in both cases. + +The value of the result field in the new_domain record is ccache_unknown if +there was an error before or with MAIL FROM:<>, and errno was not zero, +implying some kind of I/O error. We don't want to write the cache in that case. +Otherwise the value is ccache_accept or ccache_reject. */ + +if (!callout_no_cache && new_domain_record.result != ccache_unknown) + { + if ((dbm_file = dbfn_open(US"callout", O_RDWR|O_CREAT, &dbblock, FALSE)) + == NULL) + { + HDEBUG(D_verify) debug_printf("callout cache: not available\n"); + } + else + { + (void)dbfn_write(dbm_file, addr->domain, &new_domain_record, + (int)sizeof(dbdata_callout_cache)); + HDEBUG(D_verify) debug_printf("wrote callout cache domain record:\n" + " result=%d postmaster=%d random=%d\n", + new_domain_record.result, + new_domain_record.postmaster_result, + new_domain_record.random_result); + } + } + +/* If a definite result was obtained for the callout, cache it unless caching +is disabled. */ + +if (done) + { + if (!callout_no_cache && new_address_record.result != ccache_unknown) + { + if (dbm_file == NULL) + dbm_file = dbfn_open(US"callout", O_RDWR|O_CREAT, &dbblock, FALSE); + if (dbm_file == NULL) + { + HDEBUG(D_verify) debug_printf("no callout cache available\n"); + } + else + { + (void)dbfn_write(dbm_file, address_key, &new_address_record, + (int)sizeof(dbdata_callout_cache_address)); + HDEBUG(D_verify) debug_printf("wrote %s callout cache address record\n", + (new_address_record.result == ccache_accept)? "positive" : "negative"); + } + } + } /* done */ + +/* Failure to connect to any host, or any response other than 2xx or 5xx is a +temporary error. If there was only one host, and a response was received, leave +it alone if supplying details. Otherwise, give a generic response. */ + +else /* !done */ + { + uschar *dullmsg = string_sprintf("Could not complete %s verify callout", + is_recipient? "recipient" : "sender"); + yield = DEFER; + + if (host_list->next != NULL || addr->message == NULL) addr->message = dullmsg; + + addr->user_message = (!smtp_return_error_details)? dullmsg : + string_sprintf("%s for <%s>.\n" + "The mail server(s) for the domain may be temporarily unreachable, or\n" + "they may be permanently unreachable from this server. In the latter case,\n%s", + dullmsg, addr->address, + is_recipient? + "the address will never be accepted." + : + "you need to change the address or create an MX record for its domain\n" + "if it is supposed to be generally accessible from the Internet.\n" + "Talk to your mail administrator for details."); + + /* Force a specific error code */ + + addr->basic_errno = ERRNO_CALLOUTDEFER; + } + +/* Come here from within the cache-reading code on fast-track exit. */ + +END_CALLOUT: +if (dbm_file != NULL) dbfn_close(dbm_file); +return yield; +} + + + +/************************************************* +* Copy error to toplevel address * +*************************************************/ + +/* This function is used when a verify fails or defers, to ensure that the +failure or defer information is in the original toplevel address. This applies +when an address is redirected to a single new address, and the failure or +deferral happens to the child address. + +Arguments: + vaddr the verify address item + addr the final address item + yield FAIL or DEFER + +Returns: the value of YIELD +*/ + +static int +copy_error(address_item *vaddr, address_item *addr, int yield) +{ +if (addr != vaddr) + { + vaddr->message = addr->message; + vaddr->user_message = addr->user_message; + vaddr->basic_errno = addr->basic_errno; + vaddr->more_errno = addr->more_errno; + } +return yield; +} + + + + +/************************************************* +* Verify an email address * +*************************************************/ + +/* This function is used both for verification (-bv and at other times) and +address testing (-bt), which is indicated by address_test_mode being set. + +Arguments: + vaddr contains the address to verify; the next field in this block + must be NULL + f if not NULL, write the result to this file + options various option bits: + vopt_fake_sender => this sender verify is not for the real + sender (it was verify=sender=xxxx or an address from a + header line) - rewriting must not change sender_address + vopt_is_recipient => this is a recipient address, otherwise + it's a sender address - this affects qualification and + rewriting and messages from callouts + vopt_qualify => qualify an unqualified address; else error + vopt_expn => called from SMTP EXPN command + + These ones are used by do_callout() -- the options variable + is passed to it. + + vopt_callout_no_cache => don't use callout cache + vopt_callout_random => do the "random" thing + vopt_callout_recipsender => use real sender for recipient + vopt_callout_recippmaster => use postmaster for recipient + + callout if > 0, specifies that callout is required, and gives timeout + for individual connections and commands + callout_overall if > 0, gives overall timeout for the callout function; + if < 0, a default is used (see do_callout()) + se_mailfrom when callout is requested to verify a sender, use this + in MAIL FROM; NULL => "" + pm_mailfrom when callout is requested, if non-NULL, do the postmaster + thing and use this as the sender address (may be "") + + routed if not NULL, set TRUE if routing succeeded, so we can + distinguish between routing failed and callout failed + +Returns: OK address verified + FAIL address failed to verify + DEFER can't tell at present +*/ + +int +verify_address(address_item *vaddr, FILE *f, int options, int callout, + int callout_overall, uschar *se_mailfrom, uschar *pm_mailfrom, BOOL *routed) +{ +BOOL allok = TRUE; +BOOL full_info = (f == NULL)? FALSE : (debug_selector != 0); +BOOL is_recipient = (options & vopt_is_recipient) != 0; +BOOL expn = (options & vopt_expn) != 0; + +int i; +int yield = OK; +int verify_type = expn? v_expn : + address_test_mode? v_none : + is_recipient? v_recipient : v_sender; +address_item *addr_list; +address_item *addr_new = NULL; +address_item *addr_remote = NULL; +address_item *addr_local = NULL; +address_item *addr_succeed = NULL; +uschar *ko_prefix, *cr; +uschar *address = vaddr->address; +uschar *save_sender; +uschar null_sender[] = { 0 }; /* Ensure writeable memory */ + +/* Set up a prefix and suffix for error message which allow us to use the same +output statements both in EXPN mode (where an SMTP response is needed) and when +debugging with an output file. */ + +if (expn) + { + ko_prefix = US"553 "; + cr = US"\r"; + } +else ko_prefix = cr = US""; + +/* Add qualify domain if permitted; otherwise an unqualified address fails. */ + +if (parse_find_at(address) == NULL) + { + if ((options & vopt_qualify) == 0) + { + if (f != NULL) + fprintf(f, "%sA domain is required for \"%s\"%s\n", ko_prefix, address, + cr); + return FAIL; + } + address = rewrite_address_qualify(address, is_recipient); + } + +DEBUG(D_verify) + { + debug_printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"); + debug_printf("%s %s\n", address_test_mode? "Testing" : "Verifying", address); + } + +/* Rewrite and report on it. Clear the domain and local part caches - these +may have been set by domains and local part tests during an ACL. */ + +if (global_rewrite_rules != NULL) + { + uschar *old = address; + address = rewrite_address(address, is_recipient, FALSE, + global_rewrite_rules, rewrite_existflags); + if (address != old) + { + for (i = 0; i < (MAX_NAMED_LIST * 2)/32; i++) vaddr->localpart_cache[i] = 0; + for (i = 0; i < (MAX_NAMED_LIST * 2)/32; i++) vaddr->domain_cache[i] = 0; + if (f != NULL && !expn) fprintf(f, "Address rewritten as: %s\n", address); + } + } + +/* If this is the real sender address, we must update sender_address at +this point, because it may be referred to in the routers. */ + +if ((options & (vopt_fake_sender|vopt_is_recipient)) == 0) + sender_address = address; + +/* If the address was rewritten to <> no verification can be done, and we have +to return OK. This rewriting is permitted only for sender addresses; for other +addresses, such rewriting fails. */ + +if (address[0] == 0) return OK; + +/* Save a copy of the sender address for re-instating if we change it to <> +while verifying a sender address (a nice bit of self-reference there). */ + +save_sender = sender_address; + +/* Update the address structure with the possibly qualified and rewritten +address. Set it up as the starting address on the chain of new addresses. */ + +vaddr->address = address; +addr_new = vaddr; + +/* We need a loop, because an address can generate new addresses. We must also +cope with generated pipes and files at the top level. (See also the code and +comment in deliver.c.) However, it is usually the case that the router for +user's .forward files has its verify flag turned off. + +If an address generates more than one child, the loop is used only when +full_info is set, and this can only be set locally. Remote enquiries just get +information about the top level address, not anything that it generated. */ + +while (addr_new != NULL) + { + int rc; + address_item *addr = addr_new; + + addr_new = addr->next; + addr->next = NULL; + + DEBUG(D_verify) + { + debug_printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"); + debug_printf("Considering %s\n", addr->address); + } + + /* Handle generated pipe, file or reply addresses. We don't get these + when handling EXPN, as it does only one level of expansion. */ + + if (testflag(addr, af_pfr)) + { + allok = FALSE; + if (f != NULL) + { + BOOL allow; + + if (addr->address[0] == '>') + { + allow = testflag(addr, af_allow_reply); + fprintf(f, "%s -> mail %s", addr->parent->address, addr->address + 1); + } + else + { + allow = (addr->address[0] == '|')? + testflag(addr, af_allow_pipe) : testflag(addr, af_allow_file); + fprintf(f, "%s -> %s", addr->parent->address, addr->address); + } + + if (addr->basic_errno == ERRNO_BADTRANSPORT) + fprintf(f, "\n*** Error in setting up pipe, file, or autoreply:\n" + "%s\n", addr->message); + else if (allow) + fprintf(f, "\n transport = %s\n", addr->transport->name); + else + fprintf(f, " *** forbidden ***\n"); + } + continue; + } + + /* Just in case some router parameter refers to it. */ + + return_path = (addr->p.errors_address != NULL)? + addr->p.errors_address : sender_address; + + /* Split the address into domain and local part, handling the %-hack if + necessary, and then route it. While routing a sender address, set + $sender_address to <> because that is what it will be if we were trying to + send a bounce to the sender. */ + + if (routed != NULL) *routed = FALSE; + if ((rc = deliver_split_address(addr)) == OK) + { + if (!is_recipient) sender_address = null_sender; + rc = route_address(addr, &addr_local, &addr_remote, &addr_new, + &addr_succeed, verify_type); + sender_address = save_sender; /* Put back the real sender */ + } + + /* If routing an address succeeded, set the flag that remembers, for use when + an ACL cached a sender verify (in case a callout fails). Then if routing set + up a list of hosts or the transport has a host list, and the callout option + is set, and we aren't in a host checking run, do the callout verification, + and set another flag that notes that a callout happened. */ + + if (rc == OK) + { + if (routed != NULL) *routed = TRUE; + if (callout > 0) + { + host_item *host_list = addr->host_list; + + /* Default, if no remote transport, to NULL for the interface (=> any), + "smtp" for the port, and "smtp" for the protocol. */ + + transport_feedback tf = { NULL, US"smtp", US"smtp", NULL, FALSE, FALSE }; + + /* If verification yielded a remote transport, we want to use that + transport's options, so as to mimic what would happen if we were really + sending a message to this address. */ + + if (addr->transport != NULL && !addr->transport->info->local) + { + (void)(addr->transport->setup)(addr->transport, addr, &tf, NULL); + + /* If the transport has hosts and the router does not, or if the + transport is configured to override the router's hosts, we must build a + host list of the transport's hosts, and find the IP addresses */ + + if (tf.hosts != NULL && (host_list == NULL || tf.hosts_override)) + { + uschar *s; + + host_list = NULL; /* Ignore the router's hosts */ + + deliver_domain = addr->domain; + deliver_localpart = addr->local_part; + s = expand_string(tf.hosts); + deliver_domain = deliver_localpart = NULL; + + if (s == NULL) + { + log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand list of hosts " + "\"%s\" in %s transport for callout: %s", tf.hosts, + addr->transport->name, expand_string_message); + } + else + { + uschar *canonical_name; + host_item *host; + host_build_hostlist(&host_list, s, tf.hosts_randomize); + + /* Just ignore failures to find a host address. If we don't manage + to find any addresses, the callout will defer. */ + + for (host = host_list; host != NULL; host = host->next) + { + if (tf.gethostbyname || string_is_ip_address(host->name, NULL)) + (void)host_find_byname(host, NULL, &canonical_name, TRUE); + else + { + int flags = HOST_FIND_BY_A; + if (tf.qualify_single) flags |= HOST_FIND_QUALIFY_SINGLE; + if (tf.search_parents) flags |= HOST_FIND_SEARCH_PARENTS; + (void)host_find_bydns(host, NULL, flags, NULL, NULL, NULL, + &canonical_name, NULL); + } + } + } + } + } + + /* Can only do a callout if we have at least one host! */ + + if (host_list != NULL) + { + HDEBUG(D_verify) debug_printf("Attempting full verification using callout\n"); + if (host_checking && !host_checking_callout) + { + HDEBUG(D_verify) + debug_printf("... callout omitted by default when host testing\n" + "(Use -bhc if you want the callouts to happen.)\n"); + } + else + { + rc = do_callout(addr, host_list, &tf, callout, callout_overall, + options, se_mailfrom, pm_mailfrom); + } + } + else + { + HDEBUG(D_verify) debug_printf("Cannot do callout: neither router nor " + "transport provided a host list\n"); + } + } + } + + /* A router may return REROUTED if it has set up a child address as a result + of a change of domain name (typically from widening). In this case we always + want to continue to verify the new child. */ + + if (rc == REROUTED) continue; + + /* Handle hard failures */ + + if (rc == FAIL) + { + allok = FALSE; + if (f != NULL) + { + fprintf(f, "%s%s %s", ko_prefix, address, + address_test_mode? "is undeliverable" : "failed to verify"); + if (!expn && admin_user) + { + if (addr->basic_errno > 0) + fprintf(f, ": %s", strerror(addr->basic_errno)); + if (addr->message != NULL) + fprintf(f, ":\n %s", addr->message); + } + fprintf(f, "%s\n", cr); + } + + if (!full_info) return copy_error(vaddr, addr, FAIL); + else yield = FAIL; + } + + /* Soft failure */ + + else if (rc == DEFER) + { + allok = FALSE; + if (f != NULL) + { + fprintf(f, "%s%s cannot be resolved at this time", ko_prefix, address); + if (!expn && admin_user) + { + if (addr->basic_errno > 0) + fprintf(f, ":\n %s", strerror(addr->basic_errno)); + if (addr->message != NULL) + fprintf(f, ":\n %s", addr->message); + else if (addr->basic_errno <= 0) + fprintf(f, ":\n unknown error"); + } + + fprintf(f, "%s\n", cr); + } + if (!full_info) return copy_error(vaddr, addr, DEFER); + else if (yield == OK) yield = DEFER; + } + + /* If we are handling EXPN, we do not want to continue to route beyond + the top level. */ + + else if (expn) + { + uschar *ok_prefix = US"250-"; + if (addr_new == NULL) + { + if (addr_local == NULL && addr_remote == NULL) + fprintf(f, "250 mail to <%s> is discarded\r\n", address); + else + fprintf(f, "250 <%s>\r\n", address); + } + else while (addr_new != NULL) + { + address_item *addr2 = addr_new; + addr_new = addr2->next; + if (addr_new == NULL) ok_prefix = US"250 "; + fprintf(f, "%s<%s>\r\n", ok_prefix, addr2->address); + } + return OK; + } + + /* Successful routing other than EXPN. */ + + else + { + /* Handle successful routing when short info wanted. Otherwise continue for + other (generated) addresses. Short info is the operational case. Full info + can be requested only when debug_selector != 0 and a file is supplied. + + There is a conflict between the use of aliasing as an alternate email + address, and as a sort of mailing list. If an alias turns the incoming + address into just one address (e.g. J.Caesar->jc44) you may well want to + carry on verifying the generated address to ensure it is valid when + checking incoming mail. If aliasing generates multiple addresses, you + probably don't want to do this. Exim therefore treats the generation of + just a single new address as a special case, and continues on to verify the + generated address. */ + + if (!full_info && /* Stop if short info wanted AND */ + (addr_new == NULL || /* No new address OR */ + addr_new->next != NULL || /* More than one new address OR */ + testflag(addr_new, af_pfr))) /* New address is pfr */ + { + if (f != NULL) fprintf(f, "%s %s\n", address, + address_test_mode? "is deliverable" : "verified"); + + /* If we have carried on to verify a child address, we want the value + of $address_data to be that of the child */ + + vaddr->p.address_data = addr->p.address_data; + return OK; + } + } + } /* Loop for generated addresses */ + +/* Display the full results of the successful routing, including any generated +addresses. Control gets here only when full_info is set, which requires f not +to be NULL, and this occurs only when a top-level verify is called with the +debugging switch on. + +If there are no local and no remote addresses, and there were no pipes, files, +or autoreplies, and there were no errors or deferments, the message is to be +discarded, usually because of the use of :blackhole: in an alias file. */ + +if (allok && addr_local == NULL && addr_remote == NULL) + fprintf(f, "mail to %s is discarded\n", address); + +else for (addr_list = addr_local, i = 0; i < 2; addr_list = addr_remote, i++) + { + while (addr_list != NULL) + { + address_item *addr = addr_list; + address_item *p = addr->parent; + addr_list = addr->next; + + fprintf(f, "%s", CS addr->address); + while (p != NULL) + { + fprintf(f, "\n <-- %s", p->address); + p = p->parent; + } + fprintf(f, "\n "); + + /* Show router, and transport */ + + fprintf(f, "router = %s, ", addr->router->name); + fprintf(f, "transport = %s\n", (addr->transport == NULL)? US"unset" : + addr->transport->name); + + /* Show any hosts that are set up by a router unless the transport + is going to override them; fiddle a bit to get a nice format. */ + + if (addr->host_list != NULL && addr->transport != NULL && + !addr->transport->overrides_hosts) + { + host_item *h; + int maxlen = 0; + int maxaddlen = 0; + for (h = addr->host_list; h != NULL; h = h->next) + { + int len = Ustrlen(h->name); + if (len > maxlen) maxlen = len; + len = (h->address != NULL)? Ustrlen(h->address) : 7; + if (len > maxaddlen) maxaddlen = len; + } + for (h = addr->host_list; h != NULL; h = h->next) + { + int len = Ustrlen(h->name); + fprintf(f, " host %s ", h->name); + while (len++ < maxlen) fprintf(f, " "); + if (h->address != NULL) + { + fprintf(f, "[%s] ", h->address); + len = Ustrlen(h->address); + } + else if (!addr->transport->info->local) /* Omit [unknown] for local */ + { + fprintf(f, "[unknown] "); + len = 7; + } + else len = -3; + while (len++ < maxaddlen) fprintf(f," "); + if (h->mx >= 0) fprintf(f, "MX=%d", h->mx); + if (h->port != PORT_NONE) fprintf(f, " port=%d", h->port); + if (h->status == hstatus_unusable) fprintf(f, " ** unusable **"); + fprintf(f, "\n"); + } + } + } + } + +return yield; /* Will be DEFER or FAIL if any one address has */ +} + + + + +/************************************************* +* Check headers for syntax errors * +*************************************************/ + +/* This function checks those header lines that contain addresses, and verifies +that all the addresses therein are syntactially correct. + +Arguments: + msgptr where to put an error message + +Returns: OK + FAIL +*/ + +int +verify_check_headers(uschar **msgptr) +{ +header_line *h; +uschar *colon, *s; + +for (h = header_list; h != NULL; h = h->next) + { + if (h->type != htype_from && + h->type != htype_reply_to && + h->type != htype_sender && + h->type != htype_to && + h->type != htype_cc && + h->type != htype_bcc) + continue; + + colon = Ustrchr(h->text, ':'); + s = colon + 1; + while (isspace(*s)) s++; + + parse_allow_group = TRUE; /* Allow group syntax */ + + /* Loop for multiple addresses in the header */ + + while (*s != 0) + { + uschar *ss = parse_find_address_end(s, FALSE); + uschar *recipient, *errmess; + int terminator = *ss; + int start, end, domain; + + /* Temporarily terminate the string at this point, and extract the + operative address within. */ + + *ss = 0; + recipient = parse_extract_address(s,&errmess,&start,&end,&domain,FALSE); + *ss = terminator; + + /* Permit an unqualified address only if the message is local, or if the + sending host is configured to be permitted to send them. */ + + if (recipient != NULL && domain == 0) + { + if (h->type == htype_from || h->type == htype_sender) + { + if (!allow_unqualified_sender) recipient = NULL; + } + else + { + if (!allow_unqualified_recipient) recipient = NULL; + } + if (recipient == NULL) errmess = US"unqualified address not permitted"; + } + + /* It's an error if no address could be extracted, except for the special + case of an empty address. */ + + if (recipient == NULL && Ustrcmp(errmess, "empty address") != 0) + { + uschar *verb = US"is"; + uschar *t = ss; + int len; + + /* Arrange not to include any white space at the end in the + error message. */ + + while (t > s && isspace(t[-1])) t--; + + /* Add the address which failed to the error message, since in a + header with very many addresses it is sometimes hard to spot + which one is at fault. However, limit the amount of address to + quote - cases have been seen where, for example, a missing double + quote in a humungous To: header creates an "address" that is longer + than string_sprintf can handle. */ + + len = t - s; + if (len > 1024) + { + len = 1024; + verb = US"begins"; + } + + *msgptr = string_printing( + string_sprintf("%s: failing address in \"%.*s\" header %s: %.*s", + errmess, colon - h->text, h->text, verb, len, s)); + + return FAIL; + } + + /* Advance to the next address */ + + s = ss + (terminator? 1:0); + while (isspace(*s)) s++; + } /* Next address */ + } /* Next header */ + +return OK; +} + + + + +/************************************************* +* Find if verified sender * +*************************************************/ + +/* Usually, just a single address is verified as the sender of the message. +However, Exim can be made to verify other addresses as well (often related in +some way), and this is useful in some environments. There may therefore be a +chain of such addresses that have previously been tested. This function finds +whether a given address is on the chain. + +Arguments: the address to be verified +Returns: pointer to an address item, or NULL +*/ + +address_item * +verify_checked_sender(uschar *sender) +{ +address_item *addr; +for (addr = sender_verified_list; addr != NULL; addr = addr->next) + if (Ustrcmp(sender, addr->address) == 0) break; +return addr; +} + + + + + +/************************************************* +* Get valid header address * +*************************************************/ + +/* Scan the originator headers of the message, looking for an address that +verifies successfully. RFC 822 says: + + o The "Sender" field mailbox should be sent notices of + any problems in transport or delivery of the original + messages. If there is no "Sender" field, then the + "From" field mailbox should be used. + + o If the "Reply-To" field exists, then the reply should + go to the addresses indicated in that field and not to + the address(es) indicated in the "From" field. + +So we check a Sender field if there is one, else a Reply_to field, else a From +field. As some strange messages may have more than one of these fields, +especially if they are resent- fields, check all of them if there is more than +one. + +Arguments: + user_msgptr points to where to put a user error message + log_msgptr points to where to put a log error message + callout timeout for callout check (passed to verify_address()) + callout_overall overall callout timeout (ditto) + se_mailfrom mailfrom for verify; NULL => "" + pm_mailfrom sender for pm callout check (passed to verify_address()) + options callout options (passed to verify_address()) + +If log_msgptr is set to something without setting user_msgptr, the caller +normally uses log_msgptr for both things. + +Returns: result of the verification attempt: OK, FAIL, or DEFER; + FAIL is given if no appropriate headers are found +*/ + +int +verify_check_header_address(uschar **user_msgptr, uschar **log_msgptr, + int callout, int callout_overall, uschar *se_mailfrom, uschar *pm_mailfrom, + int options) +{ +static int header_types[] = { htype_sender, htype_reply_to, htype_from }; +int yield = FAIL; +int i; + +for (i = 0; i < 3; i++) + { + header_line *h; + for (h = header_list; h != NULL; h = h->next) + { + int terminator, new_ok; + uschar *s, *ss, *endname; + + if (h->type != header_types[i]) continue; + s = endname = Ustrchr(h->text, ':') + 1; + + while (*s != 0) + { + address_item *vaddr; + + while (isspace(*s) || *s == ',') s++; + if (*s == 0) break; /* End of header */ + + ss = parse_find_address_end(s, FALSE); + + /* The terminator is a comma or end of header, but there may be white + space preceding it (including newline for the last address). Move back + past any white space so we can check against any cached envelope sender + address verifications. */ + + while (isspace(ss[-1])) ss--; + terminator = *ss; + *ss = 0; + + HDEBUG(D_verify) debug_printf("verifying %.*s header address %s\n", + (int)(endname - h->text), h->text, s); + + /* See if we have already verified this address as an envelope sender, + and if so, use the previous answer. */ + + vaddr = verify_checked_sender(s); + + if (vaddr != NULL && /* Previously checked */ + (callout <= 0 || /* No callout needed; OR */ + vaddr->special_action > 256)) /* Callout was done */ + { + new_ok = vaddr->special_action & 255; + HDEBUG(D_verify) debug_printf("previously checked as envelope sender\n"); + *ss = terminator; /* Restore shortened string */ + } + + /* Otherwise we run the verification now. We must restore the shortened + string before running the verification, so the headers are correct, in + case there is any rewriting. */ + + else + { + int start, end, domain; + uschar *address = parse_extract_address(s, log_msgptr, &start, + &end, &domain, FALSE); + + *ss = terminator; + + /* If verification failed because of a syntax error, fail this + function, and ensure that the failing address gets added to the error + message. */ + + if (address == NULL) + { + new_ok = FAIL; + if (*log_msgptr != NULL) + { + while (ss > s && isspace(ss[-1])) ss--; + *log_msgptr = string_sprintf("syntax error in '%.*s' header when " + "scanning for sender: %s in \"%.*s\"", + endname - h->text, h->text, *log_msgptr, ss - s, s); + return FAIL; + } + } + + /* Else go ahead with the sender verification. But is isn't *the* + sender of the message, so set vopt_fake_sender to stop sender_address + being replaced after rewriting or qualification. */ + + else + { + vaddr = deliver_make_addr(address, FALSE); + new_ok = verify_address(vaddr, NULL, options | vopt_fake_sender, + callout, callout_overall, se_mailfrom, pm_mailfrom, NULL); + } + } + + /* We now have the result, either newly found, or cached. If we are + giving out error details, set a specific user error. This means that the + last of these will be returned to the user if all three fail. We do not + set a log message - the generic one below will be used. */ + + if (new_ok != OK && smtp_return_error_details) + { + *user_msgptr = string_sprintf("Rejected after DATA: " + "could not verify \"%.*s\" header address\n%s: %s", + endname - h->text, h->text, vaddr->address, vaddr->message); + } + + /* Success or defer */ + + if (new_ok == OK) return OK; + if (new_ok == DEFER) yield = DEFER; + + /* Move on to any more addresses in the header */ + + s = ss; + } + } + } + +if (yield == FAIL && *log_msgptr == NULL) + *log_msgptr = US"there is no valid sender in any header line"; + +if (yield == DEFER && *log_msgptr == NULL) + *log_msgptr = US"all attempts to verify a sender in a header line deferred"; + +return yield; +} + + + + +/************************************************* +* Get RFC 1413 identification * +*************************************************/ + +/* Attempt to get an id from the sending machine via the RFC 1413 protocol. If +the timeout is set to zero, then the query is not done. There may also be lists +of hosts and nets which are exempt. To guard against malefactors sending +non-printing characters which could, for example, disrupt a message's headers, +make sure the string consists of printing characters only. + +Argument: + port the port to connect to; usually this is IDENT_PORT (113), but when + running in the test harness with -bh a different value is used. + +Returns: nothing + +Side effect: any received ident value is put in sender_ident (NULL otherwise) +*/ + +void +verify_get_ident(int port) +{ +int sock, host_af, qlen; +int received_sender_port, received_interface_port, n; +uschar *p; +uschar buffer[2048]; + +/* Default is no ident. Check whether we want to do an ident check for this +host. */ + +sender_ident = NULL; +if (rfc1413_query_timeout <= 0 || verify_check_host(&rfc1413_hosts) != OK) + return; + +DEBUG(D_ident) debug_printf("doing ident callback\n"); + +/* Set up a connection to the ident port of the remote host. Bind the local end +to the incoming interface address. If the sender host address is an IPv6 +address, the incoming interface address will also be IPv6. */ + +host_af = (Ustrchr(sender_host_address, ':') == NULL)? AF_INET : AF_INET6; +sock = ip_socket(SOCK_STREAM, host_af); +if (sock < 0) return; + +if (ip_bind(sock, host_af, interface_address, 0) < 0) + { + DEBUG(D_ident) debug_printf("bind socket for ident failed: %s\n", + strerror(errno)); + goto END_OFF; + } + +if (ip_connect(sock, host_af, sender_host_address, port, rfc1413_query_timeout) + < 0) + { + if (errno == ETIMEDOUT && (log_extra_selector & LX_ident_timeout) != 0) + { + log_write(0, LOG_MAIN, "ident connection to %s timed out", + sender_host_address); + } + else + { + DEBUG(D_ident) debug_printf("ident connection to %s failed: %s\n", + sender_host_address, strerror(errno)); + } + goto END_OFF; + } + +/* Construct and send the query. */ + +sprintf(CS buffer, "%d , %d\r\n", sender_host_port, interface_port); +qlen = Ustrlen(buffer); +if (send(sock, buffer, qlen, 0) < 0) + { + DEBUG(D_ident) debug_printf("ident send failed: %s\n", strerror(errno)); + goto END_OFF; + } + +/* Read a response line. We put it into the rest of the buffer, using several +recv() calls if necessary. */ + +p = buffer + qlen; + +for (;;) + { + uschar *pp; + int count; + int size = sizeof(buffer) - (p - buffer); + + if (size <= 0) goto END_OFF; /* Buffer filled without seeing \n. */ + count = ip_recv(sock, p, size, rfc1413_query_timeout); + if (count <= 0) goto END_OFF; /* Read error or EOF */ + + /* Scan what we just read, to see if we have reached the terminating \r\n. Be + generous, and accept a plain \n terminator as well. The only illegal + character is 0. */ + + for (pp = p; pp < p + count; pp++) + { + if (*pp == 0) goto END_OFF; /* Zero octet not allowed */ + if (*pp == '\n') + { + if (pp[-1] == '\r') pp--; + *pp = 0; + goto GOT_DATA; /* Break out of both loops */ + } + } + + /* Reached the end of the data without finding \n. Let the loop continue to + read some more, if there is room. */ + + p = pp; + } + +GOT_DATA: + +/* We have received a line of data. Check it carefully. It must start with the +same two port numbers that we sent, followed by data as defined by the RFC. For +example, + + 12345 , 25 : USERID : UNIX :root + +However, the amount of white space may be different to what we sent. In the +"osname" field there may be several sub-fields, comma separated. The data we +actually want to save follows the third colon. Some systems put leading spaces +in it - we discard those. */ + +if (sscanf(CS buffer + qlen, "%d , %d%n", &received_sender_port, + &received_interface_port, &n) != 2 || + received_sender_port != sender_host_port || + received_interface_port != interface_port) + goto END_OFF; + +p = buffer + qlen + n; +while(isspace(*p)) p++; +if (*p++ != ':') goto END_OFF; +while(isspace(*p)) p++; +if (Ustrncmp(p, "USERID", 6) != 0) goto END_OFF; +p += 6; +while(isspace(*p)) p++; +if (*p++ != ':') goto END_OFF; +while (*p != 0 && *p != ':') p++; +if (*p++ == 0) goto END_OFF; +while(isspace(*p)) p++; +if (*p == 0) goto END_OFF; + +/* The rest of the line is the data we want. We turn it into printing +characters when we save it, so that it cannot mess up the format of any logging +or Received: lines into which it gets inserted. We keep a maximum of 127 +characters. */ + +sender_ident = string_printing(string_copyn(p, 127)); +DEBUG(D_ident) debug_printf("sender_ident = %s\n", sender_ident); + +END_OFF: +close(sock); +return; +} + + + + +/************************************************* +* Match host to a single host-list item * +*************************************************/ + +/* This function compares a host (name or address) against a single item +from a host list. The host name gets looked up if it is needed and is not +already known. The function is called from verify_check_this_host() via +match_check_list(), which is why most of its arguments are in a single block. + +Arguments: + arg the argument block (see below) + ss the host-list item + valueptr where to pass back looked up data, or NULL + error for error message when returning ERROR + +The block contains: + host_name the host name or NULL, implying use sender_host_name and + sender_host_aliases, looking them up if required + host_address the host address + host_ipv4 the IPv4 address taken from an IPv6 one + +Returns: OK matched + FAIL did not match + DEFER lookup deferred + ERROR failed to find the host name or IP address + unknown lookup type specified +*/ + +static int +check_host(void *arg, uschar *ss, uschar **valueptr, uschar **error) +{ +check_host_block *cb = (check_host_block *)arg; +int maskoffset; +BOOL isquery = FALSE; +uschar *semicolon, *t; +uschar **aliases; + +/* Optimize for the special case when the pattern is "*". */ + +if (*ss == '*' && ss[1] == 0) return OK; + +/* If the pattern is empty, it matches only in the case when there is no host - +this can occur in ACL checking for SMTP input using the -bs option. In this +situation, the host address is the empty string. */ + +if (cb->host_address[0] == 0) return (*ss == 0)? OK : FAIL; +if (*ss == 0) return FAIL; + +/* If the pattern is precisely "@" then match against the primary host name; +if it's "@[]" match against the local host's IP addresses. */ + +if (*ss == '@') + { + if (ss[1] == 0) ss = primary_hostname; + else if (Ustrcmp(ss, "@[]") == 0) + { + ip_address_item *ip; + for (ip = host_find_interfaces(); ip != NULL; ip = ip->next) + if (Ustrcmp(ip->address, cb->host_address) == 0) return OK; + return FAIL; + } + } + +/* If the pattern is an IP address, optionally followed by a bitmask count, do +a (possibly masked) comparision with the current IP address. */ + +if (string_is_ip_address(ss, &maskoffset)) + return (host_is_in_net(cb->host_address, ss, maskoffset)? OK : FAIL); + +/* If the item is of the form net[n]-lookup; then it is a lookup on +a masked IP network, in textual form. The net- stuff really only applies to +single-key lookups where the key is implicit. For query-style lookups the key +is specified in the query. From release 4.30, the use of net- for query style +is no longer needed, but we retain it for backward compatibility. */ + +if (Ustrncmp(ss, "net", 3) == 0 && (semicolon = Ustrchr(ss, ';')) != NULL) + { + int mlen = 0; + for (t = ss + 3; isdigit(*t); t++) mlen = mlen * 10 + *t - '0'; + if (*t++ == '-') + { + int insize; + int search_type; + int incoming[4]; + void *handle; + uschar *filename, *key, *result; + uschar buffer[64]; + + /* If no mask was supplied, set a negative value */ + + if (mlen == 0 && t == ss+4) mlen = -1; + + /* Find the search type */ + + search_type = search_findtype(t, semicolon - t); + + if (search_type < 0) log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s", + search_error_message); + + /* Adjust parameters for the type of lookup. For a query-style + lookup, there is no file name, and the "key" is just the query. For + a single-key lookup, the key is the current IP address, masked + appropriately, and reconverted to text form, with the mask appended. */ + + if (mac_islookup(search_type, lookup_querystyle)) + { + filename = NULL; + key = semicolon + 1; + } + else + { + insize = host_aton(cb->host_address, incoming); + host_mask(insize, incoming, mlen); + (void)host_nmtoa(insize, incoming, mlen, buffer); + key = buffer; + filename = semicolon + 1; + } + + /* Now do the actual lookup; note that there is no search_close() because + of the caching arrangements. */ + + handle = search_open(filename, search_type, 0, NULL, NULL); + if (handle == NULL) log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s", + search_error_message); + result = search_find(handle, filename, key, -1, NULL, 0, 0, NULL); + if (valueptr != NULL) *valueptr = result; + return (result != NULL)? OK : search_find_defer? DEFER: FAIL; + } + } + +/* The pattern is not an IP address or network reference of any kind. That is, +it is a host name pattern. Check the characters of the pattern to see if they +comprise only letters, digits, full stops, and hyphens (the constituents of +domain names). Allow underscores, as they are all too commonly found. Sigh. +Also, if allow_utf8_domains is set, allow top-bit characters. */ + +for (t = ss; *t != 0; t++) + if (!isalnum(*t) && *t != '.' && *t != '-' && *t != '_' && + (!allow_utf8_domains || *t < 128)) break; + +/* If the pattern is a complete domain name, with no fancy characters, look up +its IP address and match against that. Note that a multi-homed host will add +items to the chain. */ + +if (*t == 0) + { + int rc; + host_item h; + h.next = NULL; + h.name = ss; + h.address = NULL; + h.mx = MX_NONE; + rc = host_find_byname(&h, NULL, NULL, FALSE); + if (rc == HOST_FOUND || rc == HOST_FOUND_LOCAL) + { + host_item *hh; + for (hh = &h; hh != NULL; hh = hh->next) + { + if (Ustrcmp(hh->address, (Ustrchr(hh->address, ':') == NULL)? + cb->host_ipv4 : cb->host_address) == 0) + return OK; + } + return FAIL; + } + if (rc == HOST_FIND_AGAIN) return DEFER; + *error = string_sprintf("failed to find IP address for %s", ss); + return ERROR; + } + +/* Almost all subsequent comparisons require the host name, and can be done +using the general string matching function. When this function is called for +outgoing hosts, the name is always given explicitly. If it is NULL, it means we +must use sender_host_name and its aliases, looking them up if necessary. */ + +if (cb->host_name != NULL) /* Explicit host name given */ + return match_check_string(cb->host_name, ss, -1, TRUE, TRUE, TRUE, + valueptr); + +/* Host name not given; in principle we need the sender host name and its +aliases. However, for query-style lookups, we do not need the name if the +query does not contain $sender_host_name. From release 4.23, a reference to +$sender_host_name causes it to be looked up, so we don't need to do the lookup +on spec. */ + +if ((semicolon = Ustrchr(ss, ';')) != NULL) + { + uschar *affix; + int partial, affixlen, starflags, id; + + *semicolon = 0; + id = search_findtype_partial(ss, &partial, &affix, &affixlen, &starflags); + *semicolon=';'; + + if (id < 0) /* Unknown lookup type */ + { + log_write(0, LOG_MAIN|LOG_PANIC, "%s in host list item \"%s\"", + search_error_message, ss); + return DEFER; + } + isquery = mac_islookup(id, lookup_querystyle); + } + +if (isquery) + { + switch(match_check_string(US"", ss, -1, TRUE, TRUE, TRUE, valueptr)) + { + case OK: return OK; + case DEFER: return DEFER; + default: return FAIL; + } + } + +/* Not a query-style lookup; must ensure the host name is present, and then we +do a check on the name and all its aliases. */ + +if (sender_host_name == NULL) + { + HDEBUG(D_host_lookup) + debug_printf("sender host name required, to match against %s\n", ss); + if (host_lookup_failed || host_name_lookup() != OK) + { + *error = string_sprintf("failed to find host name for %s", + sender_host_address);; + return ERROR; + } + host_build_sender_fullhost(); + } + +/* Match on the sender host name, using the general matching function */ + +switch(match_check_string(sender_host_name, ss, -1, TRUE, TRUE, TRUE, + valueptr)) + { + case OK: return OK; + case DEFER: return DEFER; + } + +/* If there are aliases, try matching on them. */ + +aliases = sender_host_aliases; +while (*aliases != NULL) + { + switch(match_check_string(*aliases++, ss, -1, TRUE, TRUE, TRUE, valueptr)) + { + case OK: return OK; + case DEFER: return DEFER; + } + } +return FAIL; +} + + + + +/************************************************* +* Check a specific host matches a host list * +*************************************************/ + +/* This function is passed a host list containing items in a number of +different formats and the identity of a host. Its job is to determine whether +the given host is in the set of hosts defined by the list. The host name is +passed as a pointer so that it can be looked up if needed and not already +known. This is commonly the case when called from verify_check_host() to check +an incoming connection. When called from elsewhere the host name should usually +be set. + +This function is now just a front end to match_check_list(), which runs common +code for scanning a list. We pass it the check_host() function to perform a +single test. + +Arguments: + listptr pointer to the host list + cache_bits pointer to cache for named lists, or NULL + host_name the host name or NULL, implying use sender_host_name and + sender_host_aliases, looking them up if required + host_address the IP address + valueptr if not NULL, data from a lookup is passed back here + +Returns: OK if the host is in the defined set + FAIL if the host is not in the defined set, + DEFER if a data lookup deferred (not a host lookup) + +If the host name was needed in order to make a comparison, and could not be +determined from the IP address, the result is FAIL unless the item +"+allow_unknown" was met earlier in the list, in which case OK is returned. */ + +int +verify_check_this_host(uschar **listptr, unsigned int *cache_bits, + uschar *host_name, uschar *host_address, uschar **valueptr) +{ +unsigned int *local_cache_bits = cache_bits; +check_host_block cb; +cb.host_name = host_name; +cb.host_address = host_address; + +if (valueptr != NULL) *valueptr = NULL; + +/* If the host address starts off ::ffff: it is an IPv6 address in +IPv4-compatible mode. Find the IPv4 part for checking against IPv4 +addresses. */ + +cb.host_ipv4 = (Ustrncmp(host_address, "::ffff:", 7) == 0)? + host_address + 7 : host_address; + +return match_check_list(listptr, 0, &hostlist_anchor, &local_cache_bits, + check_host, &cb, MCL_HOST, + (host_address == sender_host_address)? US"host" : host_address, valueptr); +} + + + + +/************************************************* +* Check the remote host matches a list * +*************************************************/ + +/* This is a front end to verify_check_this_host(), created because checking +the remote host is a common occurrence. With luck, a good compiler will spot +the tail recursion and optimize it. If there's no host address, this is +command-line SMTP input - check against an empty string for the address. + +Arguments: + listptr pointer to the host list + +Returns: the yield of verify_check_this_host(), + i.e. OK, FAIL, or DEFER +*/ + +int +verify_check_host(uschar **listptr) +{ +return verify_check_this_host(listptr, sender_host_cache, NULL, + (sender_host_address == NULL)? US"" : sender_host_address, NULL); +} + + + + + +/************************************************* +* Invert an IP address for a DNS black list * +*************************************************/ + +/* +Arguments: + buffer where to put the answer + address the address to invert +*/ + +static void +invert_address(uschar *buffer, uschar *address) +{ +int bin[4]; +uschar *bptr = buffer; + +/* If this is an IPv4 address mapped into IPv6 format, adjust the pointer +to the IPv4 part only. */ + +if (Ustrncmp(address, "::ffff:", 7) == 0) address += 7; + +/* Handle IPv4 address: when HAVE_IPV6 is false, the result of host_aton() is +always 1. */ + +if (host_aton(address, bin) == 1) + { + int i; + int x = bin[0]; + for (i = 0; i < 4; i++) + { + sprintf(CS bptr, "%d.", x & 255); + while (*bptr) bptr++; + x >>= 8; + } + } + +/* Handle IPv6 address. Actually, as far as I know, there are no IPv6 addresses +in any DNS black lists, and the format in which they will be looked up is +unknown. This is just a guess. */ + +#if HAVE_IPV6 +else + { + int i, j; + for (j = 3; j >= 0; j--) + { + int x = bin[j]; + for (i = 0; i < 8; i++) + { + sprintf(CS bptr, "%x.", x & 15); + while (*bptr) bptr++; + x >>= 4; + } + } + } +#endif +} + + + +/************************************************* +* Check host against DNS black lists * +*************************************************/ + +/* This function runs checks against a list of DNS black lists, until one +matches. Each item on the list can be of the form + + domain=ip-address/key + +The domain is the right-most domain that is used for the query, for example, +blackholes.mail-abuse.org. If the IP address is present, there is a match only +if the DNS lookup returns a matching IP address. Several addresses may be +given, comma-separated, for example: x.y.z=127.0.0.1,127.0.0.2. + +If no key is given, what is looked up in the domain is the inverted IP address +of the current client host. If a key is given, it is used to construct the +domain for the lookup. For example, + + dsn.rfc-ignorant.org/$sender_address_domain + +After finding a match in the DNS, the domain is placed in $dnslist_domain, and +then we check for a TXT record for an error message, and if found, save its +value in $dnslist_text. We also cache everything in a tree, to optimize +multiple lookups. + +Note: an address for testing RBL is 192.203.178.39 +Note: an address for testing DUL is 192.203.178.4 +Note: a domain for testing RFCI is example.tld.dsn.rfc-ignorant.org + +Arguments: + listptr the domain/address/data list + +Returns: OK successful lookup (i.e. the address is on the list), or + lookup deferred after +include_unknown + FAIL name not found, or no data found for the given type, or + lookup deferred after +exclude_unknown (default) + DEFER lookup failure, if +defer_unknown was set +*/ + +int +verify_check_dnsbl(uschar **listptr) +{ +int sep = 0; +int defer_return = FAIL; +int old_pool = store_pool; +BOOL invert_result = FALSE; +uschar *list = *listptr; +uschar *domain; +uschar *s; +uschar buffer[1024]; +uschar query[256]; /* DNS domain max length */ +uschar revadd[128]; /* Long enough for IPv6 address */ + +/* Indicate that the inverted IP address is not yet set up */ + +revadd[0] = 0; + +/* Loop through all the domains supplied, until something matches */ + +while ((domain = string_nextinlist(&list, &sep, buffer, sizeof(buffer))) != NULL) + { + BOOL frc; + BOOL bitmask = FALSE; + dns_answer dnsa; + dns_scan dnss; + uschar *iplist; + uschar *key; + tree_node *t; + dnsbl_cache_block *cb; + + HDEBUG(D_dnsbl) debug_printf("DNS list check: %s\n", domain); + + /* Deal with special values that change the behaviour on defer */ + + if (domain[0] == '+') + { + if (strcmpic(domain, US"+include_unknown") == 0) defer_return = OK; + else if (strcmpic(domain, US"+exclude_unknown") == 0) defer_return = FAIL; + else if (strcmpic(domain, US"+defer_unknown") == 0) defer_return = DEFER; + else + log_write(0, LOG_MAIN|LOG_PANIC, "unknown item in dnslist (ignored): %s", + domain); + continue; + } + + /* See if there's explicit data to be looked up */ + + key = Ustrchr(domain, '/'); + if (key != NULL) *key++ = 0; + + /* See if there's a list of addresses supplied after the domain name. This is + introduced by an = or a & character; if preceded by ! we invert the result. + */ + + iplist = Ustrchr(domain, '='); + if (iplist == NULL) + { + bitmask = TRUE; + iplist = Ustrchr(domain, '&'); + } + + if (iplist != NULL) + { + if (iplist > domain && iplist[-1] == '!') + { + invert_result = TRUE; + iplist[-1] = 0; + } + *iplist++ = 0; + } + + /* Check that what we have left is a sensible domain name. There is no reason + why these domains should in fact use the same syntax as hosts and email + domains, but in practice they seem to. However, there is little point in + actually causing an error here, because that would no doubt hold up incoming + mail. Instead, I'll just log it. */ + + for (s = domain; *s != 0; s++) + { + if (!isalnum(*s) && *s != '-' && *s != '.') + { + log_write(0, LOG_MAIN, "dnslists domain \"%s\" contains " + "strange characters - is this right?", domain); + break; + } + } + + /* Construct the query by adding the domain onto either the sending host + address, or the given key string. */ + + if (key == NULL) + { + if (sender_host_address == NULL) return FAIL; /* can never match */ + if (revadd[0] == 0) invert_address(revadd, sender_host_address); + frc = string_format(query, sizeof(query), "%s%s", revadd, domain); + } + else + { + frc = string_format(query, sizeof(query), "%s.%s", key, domain); + } + + if (!frc) + { + log_write(0, LOG_MAIN|LOG_PANIC, "dnslist query is too long " + "(ignored): %s...", query); + continue; + } + + /* Look for this query in the cache. */ + + t = tree_search(dnsbl_cache, query); + + /* If not cached from a previous lookup, we must do a DNS lookup, and + cache the result in permanent memory. */ + + if (t == NULL) + { + store_pool = POOL_PERM; + + /* In case this is the first time the DNS resolver is being used. */ + + dns_init(FALSE, FALSE); + + /* Set up a tree entry to cache the lookup */ + + t = store_get(sizeof(tree_node) + Ustrlen(query)); + Ustrcpy(t->name, query); + t->data.ptr = cb = store_get(sizeof(dnsbl_cache_block)); + (void)tree_insertnode(&dnsbl_cache, t); + + /* Do the DNS loopup . */ + + HDEBUG(D_dnsbl) debug_printf("new DNS lookup for %s\n", query); + cb->rc = dns_basic_lookup(&dnsa, query, T_A); + cb->text_set = FALSE; + cb->text = NULL; + cb->rhs = NULL; + + /* If the lookup succeeded, cache the RHS address. The code allows for + more than one address - this was for complete generality and the possible + use of A6 records. However, A6 records have been reduced to experimental + status (August 2001) and may die out. So they may never get used at all, + let alone in dnsbl records. However, leave the code here, just in case. + + Quite apart from one A6 RR generating multiple addresses, there are DNS + lists that return more than one A record, so we must handle multiple + addresses generated in that way as well. */ + + if (cb->rc == DNS_SUCCEED) + { + dns_record *rr; + dns_address **addrp = &(cb->rhs); + for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS); + rr != NULL; + rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT)) + { + if (rr->type == T_A) + { + dns_address *da = dns_address_from_rr(&dnsa, rr); + if (da != NULL) + { + *addrp = da; + while (da->next != NULL) da = da->next; + addrp = &(da->next); + } + } + } + + /* If we didn't find any A records, change the return code. This can + happen when there is a CNAME record but there are no A records for what + it points to. */ + + if (cb->rhs == NULL) cb->rc = DNS_NODATA; + } + + store_pool = old_pool; + } + + /* Previous lookup was cached */ + + else + { + HDEBUG(D_dnsbl) debug_printf("using result of previous DNS lookup\n"); + cb = t->data.ptr; + } + + /* We now have the result of the DNS lookup, either newly done, or cached + from a previous call. If the lookup succeeded, check against the address + list if there is one. This may be a positive equality list (introduced by + "="), a negative equality list (introduced by "!="), a positive bitmask + list (introduced by "&"), or a negative bitmask list (introduced by "!&").*/ + + if (cb->rc == DNS_SUCCEED) + { + dns_address *da = NULL; + uschar *addlist = cb->rhs->address; + + /* For A and AAAA records, there may be multiple addresses from multiple + records. For A6 records (currently not expected to be used) there may be + multiple addresses from a single record. */ + + for (da = cb->rhs->next; da != NULL; da = da->next) + addlist = string_sprintf("%s, %s", addlist, da->address); + + HDEBUG(D_dnsbl) debug_printf("DNS lookup for %s succeeded (yielding %s)\n", + query, addlist); + + /* Address list check; this can be either for equality, or via a bitmask. + In the latter case, all the bits must match. */ + + if (iplist != NULL) + { + int ipsep = ','; + uschar ip[46]; + uschar *ptr = iplist; + + while (string_nextinlist(&ptr, &ipsep, ip, sizeof(ip)) != NULL) + { + /* Handle exact matching */ + if (!bitmask) + { + for (da = cb->rhs; da != NULL; da = da->next) + { + if (Ustrcmp(CS da->address, ip) == 0) break; + } + } + /* Handle bitmask matching */ + else + { + int address[4]; + int mask = 0; + + /* At present, all known DNS blocking lists use A records, with + IPv4 addresses on the RHS encoding the information they return. I + wonder if this will linger on as the last vestige of IPv4 when IPv6 + is ubiquitous? Anyway, for now we use paranoia code to completely + ignore IPv6 addresses. The default mask is 0, which always matches. + We change this only for IPv4 addresses in the list. */ + + if (host_aton(ip, address) == 1) mask = address[0]; + + /* Scan the returned addresses, skipping any that are IPv6 */ + + for (da = cb->rhs; da != NULL; da = da->next) + { + if (host_aton(da->address, address) != 1) continue; + if ((address[0] & mask) == mask) break; + } + } + + /* Break out if a match has been found */ + + if (da != NULL) break; + } + + /* If either + + (a) No IP address in a positive list matched, or + (b) An IP address in a negative list did match + + then behave as if the DNSBL lookup had not succeeded, i.e. the host is + not on the list. */ + + if (invert_result != (da == NULL)) + { + HDEBUG(D_dnsbl) + { + debug_printf("=> but we are not accepting this block class because\n"); + debug_printf("=> there was %s match for %c%s\n", + invert_result? "an exclude":"no", bitmask? '&' : '=', iplist); + } + continue; /* With next DNSBL domain */ + } + } + + /* Either there was no IP list, or the record matched. Look up a TXT record + if it hasn't previously been done. */ + + if (!cb->text_set) + { + cb->text_set = TRUE; + if (dns_basic_lookup(&dnsa, query, T_TXT) == DNS_SUCCEED) + { + dns_record *rr; + for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS); + rr != NULL; + rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT)) + if (rr->type == T_TXT) break; + if (rr != NULL) + { + int len = (rr->data)[0]; + if (len > 511) len = 127; + store_pool = POOL_PERM; + cb->text = string_sprintf("%.*s", len, (const uschar *)(rr->data+1)); + store_pool = old_pool; + } + } + } + + HDEBUG(D_dnsbl) + { + debug_printf("=> that means %s is listed at %s\n", + (key == NULL)? sender_host_address : key, domain); + } + + dnslist_domain = string_copy(domain); + dnslist_value = addlist; + dnslist_text = cb->text; + return OK; + } + + /* There was a problem with the DNS lookup */ + + if (cb->rc != DNS_NOMATCH && cb->rc != DNS_NODATA) + { + log_write(L_dnslist_defer, LOG_MAIN, + "DNS list lookup defer (probably timeout) for %s: %s", query, + (defer_return == OK)? US"assumed in list" : + (defer_return == FAIL)? US"assumed not in list" : + US"returned DEFER"); + return defer_return; + } + + /* No entry was found in the DNS; continue for next domain */ + + HDEBUG(D_dnsbl) + { + debug_printf("DNS lookup for %s failed\n", query); + debug_printf("=> that means %s is not listed at %s\n", + (key == NULL)? sender_host_address : key, domain); + } + } /* Continue with next domain */ + +return FAIL; +} + +/* End of verify.c */ diff --git a/src/src/version.c b/src/src/version.c new file mode 100644 index 000000000..16554ad6e --- /dev/null +++ b/src/src/version.c @@ -0,0 +1,59 @@ +/* $Cambridge: exim/src/src/version.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */ + +/************************************************* +* Exim - an Internet mail transport agent * +*************************************************/ + +/* Copyright (c) University of Cambridge 1995 - 2004 */ +/* See the file NOTICE for conditions of use and distribution. */ + +/* Function for setting up the version string. */ + +#include "exim.h" + + +#define THIS_VERSION "4.43" + + +/* The header file cnumber.h contains a single line containing the +compilation number, making it easy to have it updated automatically. +Hence the fudgery below to get the number turned into a string, since +we can't use #include inside a macro argument list */ + +void +version_init(void) +{ +static uschar cnumber_buffer[24]; +static uschar date_buffer[32]; + +uschar today[20]; +uschar *version_cnumber_format; + +int cnumber = +#include "cnumber.h" +; + +/* The odd magic after each of these is so they can be easily found +for automatic patching to standard values when running regression tests. +The reason that version_cnumber_format isn't just written inline in the +sprintf() call is the gcc -Wall warns about a \0 in a format string. */ + +version_cnumber = cnumber_buffer; +version_cnumber_format = US"%d\0<>"; +sprintf(CS version_cnumber, CS version_cnumber_format, cnumber); +version_string = US THIS_VERSION "\0<>"; + +Ustrcpy(today, __DATE__); +if (today[4] == ' ') today[4] = '0'; +today[3] = today[6] = '-'; + +version_date = date_buffer; +version_date[0] = 0; +Ustrncat(version_date, today+4, 3); +Ustrncat(version_date, today, 4); +Ustrncat(version_date, today+7, 4); +Ustrcat(version_date, " "); +Ustrcat(version_date, __TIME__); +} + +/* End of version.c */ diff --git a/src/util/cramtest.pl b/src/util/cramtest.pl new file mode 100755 index 000000000..87b3eb451 --- /dev/null +++ b/src/util/cramtest.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl +# $Cambridge: exim/src/util/cramtest.pl,v 1.1 2004/10/07 10:39:03 ph10 Exp $ + +# This script is contributed by Vadim Vygonets to aid in debugging CRAM-MD5 +# authentication. + +# A patch was contributed by Jon Warbrick to upgrade it to use the Digest::MD5 +# module instead of the deprecated MD5 module. + +# The script prompts for three data values: a user name, a password, and the +# challenge as sent out by an SMTP server. The challenge is a base-64 string. +# It should be copied (cut-and-pasted) literally as the third data item. The +# output of the program is the base-64 string that is to be returned as the +# response to the challenge. Using the example in RFC 2195: +# +# User: tim +# Password: tanstaaftanstaaf +# Challenge: PDE4OTYuNjk3MTcwOTUyQHBvc3RvZmZpY2UucmVzdG9uLm1jaS5uZXQ+ +# dGltIGI5MTNhNjAyYzdlZGE3YTQ5NWI0ZTZlNzMzNGQzODkw +# +# The last line is what you you would send back to the server. + + +# Copyright (c) 2002 +# Vadim Vygonets . All rights reserved. +# Public domain is OK with me. + +use MIME::Base64; +use DIGEST::MD5; + +print "User: "; +chop($user = <>); +print "Password: "; +chop($passwd = <>); +print "Challenge: "; +chop($chal = <>); +$chal =~ s/^334 //; + +$context = new Digest::MD5; +if (length($passwd) > 64) { + $context->add($passwd); + $passwd = $context->digest(); + $context->reset(); +} + +@passwd = unpack("C*", pack("a64", $passwd)); +for ($i = 0; $i < 64; $i++) { + $pass_ipad[$i] = $passwd[$i] ^ 0x36; + $pass_opad[$i] = $passwd[$i] ^ 0x5C; +} +$context->add(pack("C64", @pass_ipad), decode_base64($chal)); +$digest = $context->digest(); +$context->reset(); +$context->add(pack("C64", @pass_opad), $digest); +$digest = $context->digest(); + +print encode_base64($user . " " . unpack("H*", $digest)); + +# End diff --git a/src/util/logargs.sh b/src/util/logargs.sh new file mode 100755 index 000000000..1b0b32124 --- /dev/null +++ b/src/util/logargs.sh @@ -0,0 +1,27 @@ +#! /bin/sh +# $Cambridge: exim/src/util/logargs.sh,v 1.1 2004/10/07 10:39:03 ph10 Exp $ + +# This script can be interposed between a calling program and another +# program, in order to log the arguments which are being used. This can +# be helpful in finding out what is going on if some program is calling +# Exim with arguments it doesn't understand. + +# Set this to the the path of the program that must ultimately be called. + +CALL=exim + +# Set this to the name of the file where the data is to be logged. The +# script writes on the end of it. It must be accessible to the user who +# runs the script. + +LOGFILE=/home/ph10/tmp/zz + +# The arguments are copied to the log file + +echo $@ >>$LOGFILE + +# The real program is now called + +exec $CALL $@ + +# End diff --git a/src/util/unknownuser.sh b/src/util/unknownuser.sh new file mode 100755 index 000000000..741248481 --- /dev/null +++ b/src/util/unknownuser.sh @@ -0,0 +1,33 @@ +#! /bin/sh +# $Cambridge: exim/src/util/unknownuser.sh,v 1.1 2004/10/07 10:39:03 ph10 Exp $ + +# This is a sample script for demonstrating how to handle unknown users in +# a more friendly way than just returning a "user unknown" error. It can +# be called from a pipe transport set up like this: + +# unknownuser_pipe: +# driver = pipe; +# command = "/opt/exim/util/unknownuser.sh", +# ignore_status, +# return_output, +# user = nobody + +# which is specified by a smartuser director set up like this: + +# unknownuser: +# transport = unknownuser_pipe, +# no_verify, +# driver = smartuser; + +# Any output generated by this script is then returned to the sender of +# the message. You can run any commands you like at this point, for example, +# to attempt fuzzy matches on the local part of the address. Here we just +# give a bland message, demonstrating the availability of the variables +# $LOCAL_PART and $DOMAIN. + +cat <