Previous   Next   Contents       (Exim 4.20 Specification)

38. Adding a local scan function to Exim

In these days of email worms, viruses, and ever-increasing spam, some sites want to apply a lot of checking to messages before accepting them. You can do a certain amount through string expansions and the condition condition in the ACL that runs after the SMTP DATA command or the ACL for non-SMTP messages (see chapter 37), but this has its limitations. To allow for more general checking that can be customized to a site's own requirements, there is the possibility of linking Exim with a private message scanning function, written in C. If you want to run code that is written in something other than C, you can of course use a little C stub to call it.

The local scan function is run for every incoming message. It can therefore be used to control non-SMTP messages from local processes as well as messages arriving via SMTP.

Exim applies a timeout to calls of the local scan function, and there is an option called local_scan_timeout for setting it. The default is 5 minutes. Zero means ``no timeout''. Exim also sets up signal handlers for SIGSEGV, SIGILL, SIGFPE, and SIGBUS before calling the local scan function, so that the most common types of crash are caught. If the timeout is exceeded or one of those signals is caught, the incoming message is rejected with a temporary error if it is an SMTP message. For a non-SMTP message, the message is dropped and Exim ends with a non-zero code. The incident is logged on the main and reject logs.

38.1. Building Exim to use a local scan function

To make use of the local scan function feature, you must tell Exim where your function is before building Exim, by setting LOCAL_SCAN_SOURCE in your Local/Makefile. A recommended place to put it is in the Local directory, so you might set

  LOCAL_SCAN_SOURCE=Local/local_scan.c

for example. The function must be called local_scan(). It is called by Exim after it has received a message, when the success return code is about to be sent. This is after all the ACLs have been run. The return code from your function controls whether the message is actually accepted or not. There is a commented template function (that just accepts the message) in the file src/local_scan.c.

If you want to make use of Exim's run time configuration file to set options for your local_scan() function, you must also set

  LOCAL_SCAN_HAS_OPTIONS=yes

in Local/Makefile (see section 38.3 below).

38.2. API for local_scan()

You must include this line near the start of your code:

  #include "local_scan.h"

This header file defines a number of variables and other values, and the prototype for the function itself. Exim is coded to use unsigned char values almost exclusively, and one of the things this header defines is a shorthand for unsigned char called uschar. It also contains the following macro definitions, to simplify casting character strings and pointers to character 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 function prototype for local_scan() is:

  extern int local_scan(int fd, uschar **return_text);

The arguments are as follows:

The function must return an int value which is one of the following macros:

If the message is not being received by interactive SMTP, rejections are reported by writing to stderr or by sending an email, as configured by the -oe command line options.

38.3. Configuration options for local_scan()

It is possible to have option settings in the main configuration file that set values in static variables in the local_scan() module. If you want to do this, you must have the line

  LOCAL_SCAN_HAS_OPTIONS=yes

in your Local/Makefile when you build Exim. (This line is in OS/Makefile-Default, commented out). Then, in the local_scan() source file, you must define static variables to hold the option values, and a table to define them.

The table must be a vector called local_scan_options, of type optionlist. Each entry is a triplet, consisting of a name, an option type, and a pointer to the variable that holds the value. The entries must appear in alphabetical order. Following local_scan_options you must also define a variable called local_scan_options_count that contains the number of entries in the table. Here is a short example, showing two kinds of option:

  static int my_integer_option = 42;
  static uschar *my_string_option = US"a default string";
  
  optionlist local_scan_options[] = {
    { "my_integer", opt_int,       &my_integer_option },
    { "my_string",  opt_stringptr, &my_string_option }
  };
  int local_scan_options_count =
    sizeof(local_scan_options)/sizeof(optionlist);

The values of the variables can now be changed from Exim's runtime configuration file by including a local scan section as in this example:

  begin local_scan
  my_integer = 99
  my_string = some string of text...

The available types of option data are as follows:


opt_bool

This specifies a boolean (true/false) option. The address should point to a variable of type BOOL, which will be set to TRUE or FALSE, which are macros that are defined as ``1'' and ``0'', respectively. If you want to detect whether such a variable has been set at all, you can initialize it to TRUE_UNSET. (BOOL variables are integers underneath, so can hold more than two values.)


opt_fixed

This specifies a fixed point number, such as is used for load averages. The address should point to a variable of type int. The value is stored multiplied by 1000, so, for example, 1.4142 is truncated and stored as 1414.


opt_int

This specifies an integer; the address should point to a variable of type int. The value may be specified in any of the integer formats accepted by Exim.


opt_mkint

This is the same as opt_int, except that when such a value is output in a -bP listing, if it is an exact number of kilobytes or megabytes, it is printed with the suffix K or M.


opt_octint

This also specifies an integer, but the value is always interpeted as an octal integer, whether or not it starts with the digit zero, and it is always output in octal.


opt_stringptr

This specifies a string value; the address must be a pointer to a variable that points to a string (for example, of type uschar *).


opt_time

This specifies a time interval value. The address must point to a variable of type int. The value that is placed there is a number of seconds.



If the -bP command line option is followed by local_scan, Exim prints out the values of all the local_scan() options.

38.4. Available Exim variables

The header local_scan.h gives you access to a number of C variables. These are the only ones that are guaranteed to be maintained from release to release. Note, however, that you can obtain the value of any Exim variable by calling expand_string(). The exported variables are as follows:


unsigned int debug_selector

This variable is set to zero when no debugging is taking place. Otherwise, it is a bitmap of debugging selectors. Two bits are identified for use in local_scan(); they are defined as macros:

Thus, to write to the debugging output only when +local_scan has been selected, you should use code like this:

  if ((debug_selector & D_local_scan) != 0) 
    debug_printf("xxx", ...);

uschar *expand_string_message

After a failing call to expand_string() (returned value NULL), the variable expand_string_message contains the error message, zero-terminated.


header_line *header_list

A pointer to a chain of header lines. The header_line structure is discussed below.


header_line *header_last

A pointer to the last of the header lines.


BOOL host_checking

This variable is TRUE during a host checking session that is initiated by the -bh command line option.


uschar *interface_address

The IP address of the interface that received the message, as a string. This is NULL for locally submitted messages.


int interface_port

The port on which this message was received.


uschar *message_id

This variable contains the message id for the incoming message as a zero-terminated string.


uschar *received_protocol

The name of the protocol by which the message was received.


int recipients_count

The number of accepted recipients.


recipient_item *recipients_list

The list of accepted recipients, held in a vector of length recipients_count. The recipient_item structure is discussed below. You can add additional recipients by calling receive_add_recipient() (see below). You can delete recipients by removing them from the vector and adusting the value in recipients_count. In particular, by setting recipients_count to zero you remove all recipients. If you then return the value LOCAL_SCAN_ACCEPT, the message is accepted, but immediately blackholed. To replace the recipients, set recipients_count to zero and then call receive_add_recipient() as often as needed.


uschar *sender_address

The envelope sender address. For bounce messages this is the empty string.


uschar *sender_host_address

The IP address of the sending host, as a string. This is NULL for locally-submitted messages.


uschar *sender_host_authenticated

The name of the authentication mechanism that was used, or NULL if the message was not received over an authenticated SMTP connection.


uschar *sender_host_name

The name of the sending host, if known.


int sender_host_port

The port on the sending host.


int store_pool

The contents of this variable control which pool of memory is used for new requests. See section 38.8 for details.



38.5. Structure of header lines

The header_line structure contains the members listed below. You can add additional header lines by calling the header_add() function (see below). You can cause header lines to be ignored (deleted) by setting their type to *.


struct header_line *next

A pointer to the next header line, or NULL for the last line.


int type

A code identifying certain headers that Exim recognizes. The codes are printing characters, and are documented in chapter 48 of this manual. Notice in particular that any header line whose type is * is not transmitted with the message. This flagging is used for header lines that have been rewritten, or are to be removed (for example, Envelope-sender: header lines.) Effectively, * means ``deleted''.


int slen

The number of characters in the header line, including the terminating and any internal newlines.


uschar *text

A pointer to the text of the header. It always ends with a newline, followed by a zero byte. Internal newlines are preserved.



38.6. Structure of recipient items

The recipient_item structure contains these members:


uschar *address

This is a pointer to the recipient address as it was received.


int pno

This is used in later Exim processing when top level addresses are created by the one_time option. It is not relevant at the time local_scan() is run and must always contain -1 at this stage.


uschar *errors_to

If this value is not NULL, bounce messages caused by failing to deliver to the recipient are sent to the address it contains. In other words, it overrides the envelope sender for this one recipient. (Compare the errors_to generic router option.) If a local_scan() function sets an errors_to field to an unqualified address, Exim qualifies it using the domain from qualify_recipient. When local_scan() is called, the errors_to field is NULL for all recipients.



38.7. Available Exim functions

The header local_scan.h gives you access to a number of Exim functions. These are the only ones that are guaranteed to be maintained from release to release:


pid_t child_open(uschar **argv, uschar **envp, int newumask, int *infdptr, int *outfdptr, BOOL make_leader)

This function creates a child process that runs the command specified by argv. The environment for the process is specified by envp, which can be NULL if no environment variables are to be passed. A new umask is supplied for the process in newumask.

Pipes to the standard input and output of the new process are set up and returned to the caller via the infdptr and outfdptr arguments. The standard error is cloned to the standard output. If there are any file descriptors ``in the way'' in the new process, they are closed. If the final argument is TRUE, the new process is made into a process group leader.

The function returns the pid of the new process, or -1 if things go wrong.


int child_close(pid_t pid, int timeout)

This function waits for a child process to terminate, or for a timeout (in seconds) to expire. A timeout value of zero means wait as long as it takes. The return value is as follows:


pid_t child_open_exim(int *fd)

This function provide you with a means of submitting a new message to Exim. (Of course, you can also call /usr/sbin/sendmail yourself if you want, but this packages it all up for you.) The function creates a pipe, forks a subprocess that is running

  exim -t -oem -oi -f <>

and returns to you (via the int * argument) a file descriptor for the pipe that is connected to the standard input. The yield of the function is the PID of the subprocess. You can then write a message to the file descriptor, with recipients in To:, Cc:, and/or Bcc: header lines.

When you have finished, call child_close() to wait for the process to finish and to collect its ending status. A timeout value of zero is usually fine in this circumstance. Unless you have made a mistake with the recipient addresses, you should get a return code of zero.


void debug_printf(char *, ...)

This is Exim's debugging function, with arguments as for (printf(). The output is written to the standard error stream. If no debugging is selected, calls to debug_printf() have no effect. Normally, you should make calls conditional on the local_scan debug selector by coding like this:

  if ((debug_selector & D_local_scan) != 0) 
    debug_printf("xxx", ...);


uschar *expand_string(uschar *string)

This is an interface to Exim's string expansion code. The return value is the expanded string, or NULL if there was an expansion failure. The C variable expand_string_message contains an error message after an expansion failure. If expansion does not change the string, the return value is the pointer to the input string. Otherwise, the return value points to a new block of memory that was obtained by a call to store_get(). See section 38.8 below for a discussion of memory handling.


void header_add(int type, char *format, ...)

This function allows you to add additional header lines. The first argument is the type, and should normally be a space character. The second argument is a format string and any number of substitution arguments as for sprintf(). You may include internal newlines if you want, and you must ensure that the string ends with a newline.


uschar *lss_b64encode(uschar *cleartext, int length)

This function base64-encodes a string, which is passed by address and length. The text may contain bytes of any value, including zero. The result is passed back in dynamic memory that is obtained by calling store_get(). It is zero-terminated.


int lss_b64decode(uschar *codetext, uschar **cleartext)

This function decodes a base64-encoded string. Its arguments are a zero-terminated base64-encoded string and the address of a variable that is set to point to the result, which is in dynamic memory. The length of the decoded string is the yield of the function. If the input is invalid base64 data, the yield is -1. A zero byte is added to the end of the output string to make it easy to interpret as a C string (assuming it contains no zeros of its own). The added zero byte is not included in the returned count.


int lss_match_domain(uschar *domain, uschar *list)

This function checks for a match in a domain list. Domains are always matched caselessly. The return value is one of the following:

  OK match succeeded
  FAIL match failed
  DEFER match deferred

DEFER is usually caused by some kind of lookup defer, such as the inability to contact a database.


int lss_match_local_part(uschar *localpart, uschar *list, BOOL caseless)

This function checks for a match in a local part list. The third argument controls case-sensitivity. The return values are as for lss_match_domain().


int lss_match_address(uschar *address, uschar *list, BOOL caseless)

This function checks for a match in an address list. The third argument controls the case-sensitivity of the local part match. The domain is always matched caselessly. The return values are as for lss_match_domain().


int lss_match_host(uschar *host_name, uschar *host_address, uschar *list)

This function checks for a match in a host list. The most common usage is expected to be

  lss_match_host(sender_host_name, sender_host_address, ...)

An empty address field matches an empty item in the host list. If the host name is NULL, the name corresponding to $sender_host_address is automatically looked up if a host name is required to match an item in the list. The return values are as for lss_match_domain(), but in addition, lss_match_host() returns ERROR in the case when it had to look up a host name, but the lookup failed.


void log_write(unsigned int selector, int which, char *format, ...)

This function writes to Exim's log files. The first argument should be zero (it is concerned with log_selector). The second argument can be LOG_MAIN or LOG_REJECT or LOG_PANIC or the inclusive ``or'' of any combination of them. It specifies to which log or logs the message is written. The remaining arguments are a format and relevant insertion arguments. The string should not contain any newlines, not even at the end.


void receive_add_recipient(uschar *address, int pno)

This function adds an additional recipient to the message. The first argument is the recipient address. If if is unqualified (has no domain), it is qualified with the qualify_recipient domain. The second argument must always be -1.

This function does not allow you to specify a private errors_to address (as described with the structure of recipient_item above), because it pre-dates the addition of that field to the structure. However, it is easy to add such a value afterwards. For example:

  receive_add_recipient(US"monitor@mydom.example", -1);
  recipients_list[recipients_count-1].errors_to = 
    US"postmaster@mydom.example";

void *store_get(int)

This function accesses Exim's internal store (memory) manager. It gets a new chunk of memory whose size is given by the argument. Exim bombs out if it ever runs out of memory. See the next section for a discussion of memory handling.


void *store_get_perm(int)

This function is like store_get(), but it always gets memory from the permanent pool. See the next section for a discussion of memory handling.


uschar *string_copy(uschar *string)
"uschar *string_copyn(uschar *string, int length)" 0
"uschar *string_sprintf(char *format, ...)" 0

These three functions create strings using Exim's dynamic memory facilities. The first makes a copy of an entire string. The second copies up to a maximum number of characters, indicated by the second argument. The third uses a format and insertion arguments to create a new string. In each case, the result is a pointer to a new string in the current memory pool. See the next section for more discussion.



38.8. More about Exim's memory handling

No function is provided for freeing memory, because that is never needed. The dynamic memory that Exim uses when receiving a message is automatically recycled if another message is received by the same process (this applies only to incoming SMTP connections - other input methods can supply only one message at a time). After receiving the last message, a reception process terminates.

Because it is recycled, the normal dynamic memory cannot be used for holding data that must be preserved over a number of incoming messages on the same SMTP connection. However, Exim in fact uses two pools of dynamic memory; the second one is not recycled, and can be used for this purpose.

If you want to allocate memory that remains available for subsequent messages in the same SMTP connection, you should set

  store_pool = POOL_PERM

before calling the function that does the allocation. There is no need to restore the value if you do not need to; however, if you do want to revert to the normal pool, you can either restore the previous value of store_pool or set it explicitly to POOL_MAIN.

The pool setting applies to all functions that get dynamic memory, including expand_string(), store_get(), and the string_xxx() functions. There is also a convenience function called store_get_perm() that gets a block of memory from the permanent pool while preserving the value of store_pool.


Previous  Next  Contents       (Exim 4.20 Specification)