UTF8: MSA downconversions
[exim.git] / src / src / routers / redirect.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) University of Cambridge 1995 - 2009 */
6 /* See the file NOTICE for conditions of use and distribution. */
7
8
9 #include "../exim.h"
10 #include "rf_functions.h"
11 #include "redirect.h"
12
13
14
15 /* Options specific to the redirect router. */
16
17 optionlist redirect_router_options[] = {
18   { "allow_defer",        opt_bit | (RDON_DEFER << 16),
19       (void *)offsetof(redirect_router_options_block, bit_options) },
20   { "allow_fail",         opt_bit | (RDON_FAIL << 16),
21       (void *)offsetof(redirect_router_options_block, bit_options) },
22   { "allow_filter",       opt_bit | (RDON_FILTER << 16),
23       (void *)offsetof(redirect_router_options_block, bit_options) },
24   { "allow_freeze",       opt_bit | (RDON_FREEZE << 16),
25       (void *)offsetof(redirect_router_options_block, bit_options) },
26   { "check_ancestor",     opt_bool,
27       (void *)offsetof(redirect_router_options_block, check_ancestor) },
28   { "check_group",        opt_bool,
29       (void *)offsetof(redirect_router_options_block, check_group) },
30   { "check_owner",        opt_bool,
31       (void *)offsetof(redirect_router_options_block, check_owner) },
32   { "data",               opt_stringptr,
33       (void *)offsetof(redirect_router_options_block, data) },
34   { "directory_transport",opt_stringptr,
35       (void *)offsetof(redirect_router_options_block, directory_transport_name) },
36   { "file",               opt_stringptr,
37       (void *)offsetof(redirect_router_options_block, file) },
38   { "file_transport",     opt_stringptr,
39       (void *)offsetof(redirect_router_options_block, file_transport_name) },
40   { "filter_prepend_home",opt_bit | (RDON_PREPEND_HOME << 16),
41       (void *)offsetof(redirect_router_options_block, bit_options) },
42   { "forbid_blackhole",   opt_bit | (RDON_BLACKHOLE << 16),
43       (void *)offsetof(redirect_router_options_block, bit_options) },
44   { "forbid_exim_filter", opt_bit | (RDON_EXIM_FILTER << 16),
45       (void *)offsetof(redirect_router_options_block, bit_options) },
46   { "forbid_file",        opt_bool,
47       (void *)offsetof(redirect_router_options_block, forbid_file) },
48   { "forbid_filter_dlfunc", opt_bit | (RDON_DLFUNC << 16),
49       (void *)offsetof(redirect_router_options_block, bit_options) },
50   { "forbid_filter_existstest",  opt_bit | (RDON_EXISTS << 16),
51       (void *)offsetof(redirect_router_options_block, bit_options) },
52   { "forbid_filter_logwrite",opt_bit | (RDON_LOG << 16),
53       (void *)offsetof(redirect_router_options_block, bit_options) },
54   { "forbid_filter_lookup", opt_bit | (RDON_LOOKUP << 16),
55       (void *)offsetof(redirect_router_options_block, bit_options) },
56   { "forbid_filter_perl", opt_bit | (RDON_PERL << 16),
57       (void *)offsetof(redirect_router_options_block, bit_options) },
58   { "forbid_filter_readfile", opt_bit | (RDON_READFILE << 16),
59       (void *)offsetof(redirect_router_options_block, bit_options) },
60   { "forbid_filter_readsocket", opt_bit | (RDON_READSOCK << 16),
61       (void *)offsetof(redirect_router_options_block, bit_options) },
62   { "forbid_filter_reply",opt_bool,
63       (void *)offsetof(redirect_router_options_block, forbid_filter_reply) },
64   { "forbid_filter_run",  opt_bit | (RDON_RUN << 16),
65       (void *)offsetof(redirect_router_options_block, bit_options) },
66   { "forbid_include",     opt_bit | (RDON_INCLUDE << 16),
67       (void *)offsetof(redirect_router_options_block, bit_options) },
68   { "forbid_pipe",        opt_bool,
69       (void *)offsetof(redirect_router_options_block, forbid_pipe) },
70   { "forbid_sieve_filter",opt_bit | (RDON_SIEVE_FILTER << 16),
71       (void *)offsetof(redirect_router_options_block, bit_options) },
72   { "forbid_smtp_code",     opt_bool,
73       (void *)offsetof(redirect_router_options_block, forbid_smtp_code) },
74   { "hide_child_in_errmsg", opt_bool,
75       (void *)offsetof(redirect_router_options_block,  hide_child_in_errmsg) },
76   { "ignore_eacces",      opt_bit | (RDON_EACCES << 16),
77       (void *)offsetof(redirect_router_options_block, bit_options) },
78   { "ignore_enotdir",     opt_bit | (RDON_ENOTDIR << 16),
79       (void *)offsetof(redirect_router_options_block, bit_options) },
80   { "include_directory",  opt_stringptr,
81       (void *)offsetof(redirect_router_options_block,  include_directory) },
82   { "modemask",           opt_octint,
83       (void *)offsetof(redirect_router_options_block, modemask) },
84   { "one_time",           opt_bool,
85       (void *)offsetof(redirect_router_options_block, one_time) },
86   { "owners",             opt_uidlist,
87       (void *)offsetof(redirect_router_options_block, owners) },
88   { "owngroups",          opt_gidlist,
89       (void *)offsetof(redirect_router_options_block, owngroups) },
90   { "pipe_transport",     opt_stringptr,
91       (void *)offsetof(redirect_router_options_block, pipe_transport_name) },
92   { "qualify_domain",     opt_stringptr,
93       (void *)offsetof(redirect_router_options_block, qualify_domain) },
94   { "qualify_preserve_domain", opt_bool,
95       (void *)offsetof(redirect_router_options_block, qualify_preserve_domain) },
96   { "repeat_use",         opt_bool | opt_public,
97       (void *)offsetof(router_instance, repeat_use) },
98   { "reply_transport",    opt_stringptr,
99       (void *)offsetof(redirect_router_options_block, reply_transport_name) },
100   { "rewrite",            opt_bit | (RDON_REWRITE << 16),
101       (void *)offsetof(redirect_router_options_block, bit_options) },
102   { "sieve_enotify_mailto_owner", opt_stringptr,
103       (void *)offsetof(redirect_router_options_block, sieve_enotify_mailto_owner) },
104   { "sieve_subaddress", opt_stringptr,
105       (void *)offsetof(redirect_router_options_block, sieve_subaddress) },
106   { "sieve_useraddress", opt_stringptr,
107       (void *)offsetof(redirect_router_options_block, sieve_useraddress) },
108   { "sieve_vacation_directory", opt_stringptr,
109       (void *)offsetof(redirect_router_options_block, sieve_vacation_directory) },
110   { "skip_syntax_errors", opt_bool,
111       (void *)offsetof(redirect_router_options_block, skip_syntax_errors) },
112 #ifdef EXPERIMENTAL_SRS
113   { "srs",                opt_stringptr,
114       (void *)offsetof(redirect_router_options_block, srs) },
115   { "srs_alias",          opt_stringptr,
116       (void *)offsetof(redirect_router_options_block, srs_alias) },
117   { "srs_condition",      opt_stringptr,
118       (void *)offsetof(redirect_router_options_block, srs_condition) },
119   { "srs_dbinsert",       opt_stringptr,
120       (void *)offsetof(redirect_router_options_block, srs_dbinsert) },
121   { "srs_dbselect",       opt_stringptr,
122       (void *)offsetof(redirect_router_options_block, srs_dbselect) },
123 #endif
124   { "syntax_errors_text", opt_stringptr,
125       (void *)offsetof(redirect_router_options_block, syntax_errors_text) },
126   { "syntax_errors_to",   opt_stringptr,
127       (void *)offsetof(redirect_router_options_block, syntax_errors_to) }
128 };
129
130 /* Size of the options list. An extern variable has to be used so that its
131 address can appear in the tables drtables.c. */
132
133 int redirect_router_options_count =
134   sizeof(redirect_router_options)/sizeof(optionlist);
135
136 /* Default private options block for the redirect router. */
137
138 redirect_router_options_block redirect_router_option_defaults = {
139   NULL,        /* directory_transport */
140   NULL,        /* file_transport */
141   NULL,        /* pipe_transport */
142   NULL,        /* reply_transport */
143   NULL,        /* data */
144   NULL,        /* directory_transport_name */
145   NULL,        /* file */
146   NULL,        /* file_dir */
147   NULL,        /* file_transport_name */
148   NULL,        /* include_directory */
149   NULL,        /* pipe_transport_name */
150   NULL,        /* reply_transport_name */
151   NULL,        /* sieve_subaddress */
152   NULL,        /* sieve_useraddress */
153   NULL,        /* sieve_vacation_directory */
154   NULL,        /* sieve_enotify_mailto_owner */
155   NULL,        /* syntax_errors_text */
156   NULL,        /* syntax_errors_to */
157   NULL,        /* qualify_domain */
158   NULL,        /* owners */
159   NULL,        /* owngroups */
160 #ifdef EXPERIMENTAL_SRS
161   NULL,        /* srs */
162   NULL,        /* srs_alias */
163   NULL,        /* srs_condition */
164   NULL,        /* srs_dbinsert */
165   NULL,        /* srs_dbselect */
166 #endif
167   022,         /* modemask */
168   RDO_REWRITE | RDO_PREPEND_HOME, /* bit_options */
169   FALSE,       /* check_ancestor */
170   TRUE_UNSET,  /* check_owner */
171   TRUE_UNSET,  /* check_group */
172   FALSE,       /* forbid_file */
173   FALSE,       /* forbid_filter_reply */
174   FALSE,       /* forbid_pipe */
175   FALSE,       /* forbid_smtp_code */
176   FALSE,       /* hide_child_in_errmsg */
177   FALSE,       /* one_time */
178   FALSE,       /* qualify_preserve_domain */
179   FALSE        /* skip_syntax_errors */
180 };
181
182
183
184 /*************************************************
185 *          Initialization entry point            *
186 *************************************************/
187
188 /* Called for each instance, after its options have been read, to enable
189 consistency checks to be done, or anything else that needs to be set up. */
190
191 void redirect_router_init(router_instance *rblock)
192 {
193 redirect_router_options_block *ob =
194   (redirect_router_options_block *)(rblock->options_block);
195
196 /* Either file or data must be set, but not both */
197
198 if ((ob->file == NULL) == (ob->data == NULL))
199   log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n  "
200     "%sone of \"file\" or \"data\" must be specified",
201     rblock->name, (ob->file == NULL)? "" : "only ");
202
203 /* Onetime aliases can only be real addresses. Headers can't be manipulated.
204 The combination of one_time and unseen is not allowed. We can't check the
205 expansion of "unseen" here, but we assume that if it is set to anything other
206 than false, there is likely to be a problem. */
207
208 if (ob->one_time)
209   {
210   ob->forbid_pipe = ob->forbid_file = ob->forbid_filter_reply = TRUE;
211   if (rblock->extra_headers != NULL || rblock->remove_headers != NULL)
212     log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n  "
213       "\"headers_add\" and \"headers_remove\" are not permitted with "
214       "\"one_time\"", rblock->name);
215   if (rblock->unseen || rblock->expand_unseen != NULL)
216     log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n  "
217       "\"unseen\" may not be used with \"one_time\"", rblock->name);
218   }
219
220 /* The defaults for check_owner and check_group depend on other settings. The
221 defaults are: Check the owner if check_local_user or owners is set; check the
222 group if check_local_user is set without a restriction on the group write bit,
223 or if owngroups is set. */
224
225 if (ob->check_owner == TRUE_UNSET)
226   ob->check_owner = rblock->check_local_user ||
227                     (ob->owners != NULL && ob->owners[0] != 0);
228
229 if (ob->check_group == TRUE_UNSET)
230   ob->check_group = (rblock->check_local_user && (ob->modemask & 020) == 0) ||
231                     (ob->owngroups != NULL && ob->owngroups[0] != 0);
232
233 /* If explicit qualify domain set, the preserve option is locked out */
234
235 if (ob->qualify_domain != NULL && ob->qualify_preserve_domain)
236   log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n  "
237     "only one of \"qualify_domain\" or \"qualify_preserve_domain\" must be set",
238     rblock->name);
239
240 /* If allow_filter is set, either user or check_local_user must be set. */
241
242 if (!rblock->check_local_user &&
243     !rblock->uid_set &&
244     rblock->expand_uid == NULL &&
245     (ob->bit_options & RDO_FILTER) != 0)
246   log_write(0, LOG_PANIC_DIE|LOG_CONFIG_FOR, "%s router:\n  "
247     "\"user\" or \"check_local_user\" must be set with \"allow_filter\"",
248     rblock->name);
249 }
250
251
252
253 /*************************************************
254 *       Get errors address and header mods       *
255 *************************************************/
256
257 /* This function is called when new addresses are generated, in order to
258 sort out errors address and header modifications. We put the errors address
259 into the parent address (even though it is never used from there because that
260 address is never transported) so that it can be retrieved if any of the
261 children gets routed by an "unseen" router. The clone of the child that is
262 passed on must have the original errors_address value.
263
264 Arguments:
265   rblock               the router control block
266   addr                 the address being routed
267   verify               v_none/v_recipient/v_sender/v_expn
268   addr_prop            point to the propagated block, which is where the
269                          new values are to be placed
270
271 Returns:    the result of rf_get_errors_address() or rf_get_munge_headers(),
272             which is either OK or DEFER
273 */
274
275 static int
276 sort_errors_and_headers(router_instance *rblock, address_item *addr,
277   int verify, address_item_propagated *addr_prop)
278 {
279 int frc = rf_get_errors_address(addr, rblock, verify,
280   &addr_prop->errors_address);
281 if (frc != OK) return frc;
282 addr->prop.errors_address = addr_prop->errors_address;
283 return rf_get_munge_headers(addr, rblock, &addr_prop->extra_headers,
284   &addr_prop->remove_headers);
285 }
286
287
288
289 /*************************************************
290 *    Process a set of generated new addresses    *
291 *************************************************/
292
293 /* This function sets up a set of newly generated child addresses and puts them
294 on the new address chain. Copy in the uid, gid and permission flags for use by
295 pipes and files, set the parent, and "or" its af_ignore_error flag. Also record
296 the setting for any starting router.
297
298 If the generated address is the same as one of its ancestors, and the
299 check_ancestor flag is set, do not use this generated address, but replace it
300 with a copy of the input address. This is to cope with cases where A is aliased
301 to B and B has a .forward file pointing to A, though it is usually set on the
302 forwardfile rather than the aliasfile. We can't just pass on the old
303 address by returning FAIL, because it must act as a general parent for
304 generated addresses, and only get marked "done" when all its children are
305 delivered.
306
307 Arguments:
308   rblock                  router block
309   addr_new                new address chain
310   addr                    original address
311   generated               list of generated addresses
312   addr_prop               the propagated block, containing the errors_address,
313                             header modification stuff, and address_data
314   ugidptr                 points to uid/gid data for files, pipes, autoreplies
315   pw                      password entry, set if ob->check_local_user is TRUE
316
317 Returns:         nothing
318 */
319
320 static void
321 add_generated(router_instance *rblock, address_item **addr_new,
322   address_item *addr, address_item *generated,
323   address_item_propagated *addr_prop, ugid_block *ugidptr, struct passwd *pw)
324 {
325 redirect_router_options_block *ob =
326   (redirect_router_options_block *)(rblock->options_block);
327
328 while (generated != NULL)
329   {
330   address_item *parent;
331   address_item *next = generated;
332   uschar *errors_address = next->prop.errors_address;
333
334   generated = next->next;
335   next->parent = addr;
336   orflag(next, addr, af_ignore_error);
337   next->start_router = rblock->redirect_router;
338   if (addr->child_count == SHRT_MAX)
339     log_write(0, LOG_MAIN|LOG_PANIC_DIE, "%s router generated more than %d "
340       "child addresses for <%s>", rblock->name, SHRT_MAX, addr->address);
341   addr->child_count++;
342
343   next->next = *addr_new;
344   *addr_new = next;
345
346   /* Don't do the "one_time" thing for the first pass of a 2-stage queue run. */
347
348   if (ob->one_time && !queue_2stage)
349     {
350     for (parent = addr; parent->parent != NULL; parent = parent->parent);
351     next->onetime_parent = parent->address;
352     }
353
354   if (ob->hide_child_in_errmsg) setflag(next, af_hide_child);
355
356   /* If check_ancestor is set, we want to know if any ancestor of this address
357   is the address we are about to generate. The check must be done caselessly
358   unless the ancestor was routed by a case-sensitive router. */
359
360   if (ob->check_ancestor)
361     {
362     for (parent = addr; parent != NULL; parent = parent->parent)
363       {
364       if (((parent->router != NULL && parent->router->caseful_local_part)?
365            Ustrcmp(next->address, parent->address)
366            :
367            strcmpic(next->address, parent->address)
368           ) == 0)
369         {
370         DEBUG(D_route) debug_printf("generated parent replaced by child\n");
371         next->address = string_copy(addr->address);
372         break;
373         }
374       }
375     }
376
377   /* A user filter may, under some circumstances, set up an errors address.
378   If so, we must take care to re-instate it when we copy in the propagated
379   data so that it overrides any errors_to setting on the router. */
380
381   next->prop = *addr_prop;
382   if (errors_address != NULL) next->prop.errors_address = errors_address;
383
384   /* For pipes, files, and autoreplies, record this router as handling them,
385   because they don't go through the routing process again. Then set up uid,
386   gid, home and current directories for transporting. */
387
388   if (testflag(next, af_pfr))
389     {
390     next->router = rblock;
391     rf_set_ugid(next, ugidptr);   /* Will contain pw values if not overridden */
392
393     /* When getting the home directory out of the password information, wrap it
394     in \N...\N to avoid expansion later. In Cygwin, home directories can
395     contain $ characters. */
396
397     if (rblock->home_directory != NULL)
398       next->home_dir = rblock->home_directory;
399     else if (rblock->check_local_user)
400       next->home_dir = string_sprintf("\\N%s\\N", pw->pw_dir);
401     else if (rblock->router_home_directory != NULL &&
402              testflag(addr, af_home_expanded))
403       {
404       next->home_dir = deliver_home;
405       setflag(next, af_home_expanded);
406       }
407
408     next->current_dir = rblock->current_directory;
409
410     /* Permission options */
411
412     if (!ob->forbid_pipe) setflag(next, af_allow_pipe);
413     if (!ob->forbid_file) setflag(next, af_allow_file);
414     if (!ob->forbid_filter_reply) setflag(next, af_allow_reply);
415
416     /* If the transport setting fails, the error gets picked up at the outer
417     level from the setting of basic_errno in the address. */
418
419     if (next->address[0] == '|')
420       {
421       address_pipe = next->address;
422       if (rf_get_transport(ob->pipe_transport_name, &(ob->pipe_transport),
423           next, rblock->name, US"pipe_transport"))
424         next->transport = ob->pipe_transport;
425       address_pipe = NULL;
426       }
427     else if (next->address[0] == '>')
428       {
429       if (rf_get_transport(ob->reply_transport_name, &(ob->reply_transport),
430           next, rblock->name, US"reply_transport"))
431         next->transport = ob->reply_transport;
432       }
433     else  /* must be file or directory */
434       {
435       int len = Ustrlen(next->address);
436       address_file = next->address;
437       if (next->address[len-1] == '/')
438         {
439         if (rf_get_transport(ob->directory_transport_name,
440             &(ob->directory_transport), next, rblock->name,
441             US"directory_transport"))
442           next->transport = ob->directory_transport;
443         }
444       else
445         {
446         if (rf_get_transport(ob->file_transport_name, &(ob->file_transport),
447             next, rblock->name, US"file_transport"))
448           next->transport = ob->file_transport;
449         }
450       address_file = NULL;
451       }
452     }
453
454 #ifdef EXPERIMENTAL_INTERNATIONAL
455     next->prop.utf8_msg = string_is_utf8(next->address)
456       || (sender_address && string_is_utf8(sender_address));
457 #endif
458
459   DEBUG(D_route)
460     {
461     debug_printf("%s router generated %s\n  %serrors_to=%s transport=%s\n",
462       rblock->name,
463       next->address,
464       testflag(next, af_pfr)? "pipe, file, or autoreply\n  " : "",
465       next->prop.errors_address,
466       (next->transport == NULL)? US"NULL" : next->transport->name);
467
468     if (testflag(next, af_uid_set))
469       debug_printf("  uid=%ld ", (long int)(next->uid));
470     else
471       debug_printf("  uid=unset ");
472
473     if (testflag(next, af_gid_set))
474       debug_printf("gid=%ld ", (long int)(next->gid));
475     else
476       debug_printf("gid=unset ");
477
478 #ifdef EXPERIMENTAL_INTERNATIONAL
479     if (next->prop.utf8_msg) debug_printf("utf8 ");
480 #endif
481
482     debug_printf("home=%s\n", next->home_dir);
483     }
484   }
485 }
486
487
488 /*************************************************
489 *              Main entry point                  *
490 *************************************************/
491
492 /* See local README for interface description. This router returns:
493
494 DECLINE
495   . empty address list, or filter did nothing significant
496
497 DEFER
498   . verifying the errors address caused a deferment or a big disaster such
499       as an expansion failure (rf_get_errors_address)
500   . expanding a headers_{add,remove} string caused a deferment or another
501       expansion error (rf_get_munge_headers)
502   . :defer: or "freeze" in a filter
503   . error in address list or filter
504   . skipped syntax errors, but failed to send the message
505
506 DISCARD
507   . address was :blackhole:d or "seen finish"ed
508
509 FAIL
510   . :fail:
511
512 OK
513   . new addresses added to addr_new
514 */
515
516 int redirect_router_entry(
517   router_instance *rblock,        /* data for this instantiation */
518   address_item *addr,             /* address we are working on */
519   struct passwd *pw,              /* passwd entry after check_local_user */
520   int verify,                     /* v_none/v_recipient/v_sender/v_expn */
521   address_item **addr_local,      /* add it to this if it's local */
522   address_item **addr_remote,     /* add it to this if it's remote */
523   address_item **addr_new,        /* put new addresses on here */
524   address_item **addr_succeed)    /* put old address here on success */
525 {
526 redirect_router_options_block *ob =
527   (redirect_router_options_block *)(rblock->options_block);
528 address_item *generated = NULL;
529 const uschar *save_qualify_domain_recipient = qualify_domain_recipient;
530 uschar *discarded = US"discarded";
531 address_item_propagated addr_prop;
532 error_block *eblock = NULL;
533 ugid_block ugid;
534 redirect_block redirect;
535 int filtertype = FILTER_UNSET;
536 int yield = OK;
537 int options = ob->bit_options;
538 int frc = 0;
539 int xrc = 0;
540
541 addr_local = addr_local;     /* Keep picky compilers happy */
542 addr_remote = addr_remote;
543
544 /* Initialize the data to be propagated to the children */
545
546 addr_prop.address_data = deliver_address_data;
547 addr_prop.domain_data = deliver_domain_data;
548 addr_prop.localpart_data = deliver_localpart_data;
549 addr_prop.errors_address = NULL;
550 addr_prop.extra_headers = NULL;
551 addr_prop.remove_headers = NULL;
552
553 #ifdef EXPERIMENTAL_SRS
554 addr_prop.srs_sender = NULL;
555 #endif
556
557 /* When verifying and testing addresses, the "logwrite" command in filters
558 must be bypassed. */
559
560 if (verify == v_none && !address_test_mode) options |= RDO_REALLOG;
561
562 /* Sort out the fixed or dynamic uid/gid. This uid is used (a) for reading the
563 file (and interpreting a filter) and (b) for running the transports for
564 generated file and pipe addresses. It is not (necessarily) the same as the uids
565 that may own the file. Exim panics if an expanded string is not a number and
566 can't be found in the password file. Other errors set the freezing bit. */
567
568 if (!rf_get_ugid(rblock, addr, &ugid)) return DEFER;
569
570 if (!ugid.uid_set && pw != NULL)
571   {
572   ugid.uid = pw->pw_uid;
573   ugid.uid_set = TRUE;
574   }
575
576 if (!ugid.gid_set && pw != NULL)
577   {
578   ugid.gid = pw->pw_gid;
579   ugid.gid_set = TRUE;
580   }
581
582 #ifdef EXPERIMENTAL_SRS
583   /* Perform SRS on recipient/return-path as required  */
584
585   if(ob->srs != NULL)
586   {
587     BOOL usesrs = TRUE;
588
589     if(ob->srs_condition != NULL)
590       usesrs = expand_check_condition(ob->srs_condition, "srs_condition expansion failed", NULL);
591
592     if(usesrs)
593     {
594       int srs_action = 0, n_srs;
595       uschar *res;
596       uschar *usedomain;
597
598       /* What are we doing? */
599       if(Ustrcmp(ob->srs, "forward") == 0)
600         srs_action = 1;
601       else if(Ustrcmp(ob->srs, "reverseandforward") == 0)
602       {
603         srs_action = 3;
604
605         if((ob->srs_dbinsert == NULL) ^ (ob->srs_dbselect == NULL))
606           return DEFER;
607       }
608       else if(Ustrcmp(ob->srs, "reverse") == 0)
609         srs_action = 2;
610
611       /* Reverse SRS */
612       if(srs_action & 2)
613       {
614         srs_orig_recipient = addr->address;
615
616         eximsrs_init();
617         if(ob->srs_dbselect)
618           eximsrs_db_set(TRUE, ob->srs_dbselect);
619 /* Comment this out for now...
620 //        else
621 //          eximsrs_db_set(TRUE, NULL);
622 */
623
624         if((n_srs = eximsrs_reverse(&res, addr->address)) == OK)
625         {
626           srs_recipient = res;
627           DEBUG(D_any)
628             debug_printf("SRS (reverse): Recipient '%s' rewritten to '%s'\n", srs_orig_recipient, srs_recipient);
629         }
630
631         eximsrs_done();
632
633         if(n_srs != OK)
634           return n_srs;
635       }
636
637       /* Forward SRS */
638       /* No point in actually performing SRS if we are just verifying a recipient */
639       if((srs_action & 1) && verify == v_none &&
640          (sender_address ? sender_address[0] != 0 : FALSE))
641       {
642
643         srs_orig_sender = sender_address;
644         eximsrs_init();
645         if(ob->srs_dbinsert)
646           eximsrs_db_set(FALSE, ob->srs_dbinsert);
647 /* Comment this out for now...
648 //        else
649 //          eximsrs_db_set(FALSE, NULL);
650 */
651
652         if(ob->srs_alias != NULL ? (usedomain = expand_string(ob->srs_alias)) == NULL : 1)
653           usedomain = deliver_domain;
654
655         if((n_srs = eximsrs_forward(&res, sender_address, usedomain)) == OK)
656         {
657           addr_prop.srs_sender = res;
658           DEBUG(D_any)
659             debug_printf("SRS (forward): Sender '%s' rewritten to '%s'\n", srs_orig_sender, res);
660         }
661
662         eximsrs_done();
663
664         if(n_srs != OK)
665           return n_srs;
666       }
667     }
668   }
669 #endif
670
671 /* Call the function that interprets redirection data, either inline or from a
672 file. This is a separate function so that the system filter can use it. It will
673 run the function in a subprocess if necessary. If qualify_preserve_domain is
674 set, temporarily reset qualify_domain_recipient to the current domain so that
675 any unqualified addresses get qualified with the same domain as the incoming
676 address. Otherwise, if a local qualify_domain is provided, set that up. */
677
678 if (ob->qualify_preserve_domain)
679   qualify_domain_recipient = addr->domain;
680 else if (ob->qualify_domain != NULL)
681   {
682   uschar *new_qdr = rf_expand_data(addr, ob->qualify_domain, &xrc);
683   if (new_qdr == NULL) return xrc;
684   qualify_domain_recipient = new_qdr;
685   }
686
687 redirect.owners = ob->owners;
688 redirect.owngroups = ob->owngroups;
689 redirect.modemask = ob->modemask;
690 redirect.check_owner = ob->check_owner;
691 redirect.check_group = ob->check_group;
692 redirect.pw = pw;
693
694 if (ob->file != NULL)
695   {
696   redirect.string = ob->file;
697   redirect.isfile = TRUE;
698   }
699 else
700   {
701   redirect.string = ob->data;
702   redirect.isfile = FALSE;
703   }
704
705 frc = rda_interpret(&redirect, options, ob->include_directory,
706   ob->sieve_vacation_directory, ob->sieve_enotify_mailto_owner,
707   ob->sieve_useraddress, ob->sieve_subaddress, &ugid, &generated,
708   &(addr->message), ob->skip_syntax_errors? &eblock : NULL, &filtertype,
709   string_sprintf("%s router (recipient is %s)", rblock->name, addr->address));
710
711 qualify_domain_recipient = save_qualify_domain_recipient;
712
713 /* Handle exceptional returns from filtering or processing an address list.
714 For FAIL and FREEZE we honour any previously set up deliveries by a filter. */
715
716 switch (frc)
717   {
718   case FF_NONEXIST:
719   addr->message = addr->user_message = NULL;
720   return DECLINE;
721
722   case FF_BLACKHOLE:
723   DEBUG(D_route) debug_printf("address :blackhole:d\n");
724   generated = NULL;
725   discarded = US":blackhole:";
726   frc = FF_DELIVERED;
727   break;
728
729   /* FF_DEFER and FF_FAIL can arise only as a result of explicit commands
730   (:defer: or :fail: in an alias file or "fail" in a filter). If a configured
731   message was supplied, allow it to be included in an SMTP response after
732   verifying. Remove any SMTP code if it is not allowed. */
733
734   case FF_DEFER:
735   yield = DEFER;
736   goto SORT_MESSAGE;
737
738   case FF_FAIL:
739   if ((xrc = sort_errors_and_headers(rblock, addr, verify, &addr_prop)) != OK)
740     return xrc;
741   add_generated(rblock, addr_new, addr, generated, &addr_prop, &ugid, pw);
742   yield = FAIL;
743
744   SORT_MESSAGE:
745   if (addr->message == NULL)
746     addr->message = (yield == FAIL)? US"forced rejection" : US"forced defer";
747   else
748     {
749     int ovector[3];
750     if (ob->forbid_smtp_code &&
751         pcre_exec(regex_smtp_code, NULL, CS addr->message,
752           Ustrlen(addr->message), 0, PCRE_EOPT,
753           ovector, sizeof(ovector)/sizeof(int)) >= 0)
754       {
755       DEBUG(D_route) debug_printf("SMTP code at start of error message "
756         "is ignored because forbid_smtp_code is set\n");
757       addr->message += ovector[1];
758       }
759     addr->user_message = addr->message;
760     setflag(addr, af_pass_message);
761     }
762   return yield;
763
764   /* As in the case of a system filter, a freeze does not happen after a manual
765   thaw. In case deliveries were set up by the filter, we set the child count
766   high so that their completion does not mark the original address done. */
767
768   case FF_FREEZE:
769   if (!deliver_manual_thaw)
770     {
771     if ((xrc = sort_errors_and_headers(rblock, addr, verify, &addr_prop))
772       != OK) return xrc;
773     add_generated(rblock, addr_new, addr, generated, &addr_prop, &ugid, pw);
774     if (addr->message == NULL) addr->message = US"frozen by filter";
775     addr->special_action = SPECIAL_FREEZE;
776     addr->child_count = 9999;
777     return DEFER;
778     }
779   frc = FF_NOTDELIVERED;
780   break;
781
782   /* Handle syntax errors and :include: failures and lookup defers */
783
784   case FF_ERROR:
785   case FF_INCLUDEFAIL:
786
787   /* If filtertype is still FILTER_UNSET, it means that the redirection data
788   was never inspected, so the error was an expansion failure or failure to open
789   the file, or whatever. In these cases, the existing error message is probably
790   sufficient. */
791
792   if (filtertype == FILTER_UNSET) return DEFER;
793
794   /* If it was a filter and skip_syntax_errors is set, we want to set up
795   the error message so that it can be logged and mailed to somebody. */
796
797   if (filtertype != FILTER_FORWARD && ob->skip_syntax_errors)
798     {
799     eblock = store_get(sizeof(error_block));
800     eblock->next = NULL;
801     eblock->text1 = addr->message;
802     eblock->text2 = NULL;
803     addr->message = addr->user_message = NULL;
804     }
805
806   /* Otherwise set up the error for the address and defer. */
807
808   else
809     {
810     addr->basic_errno = ERRNO_BADREDIRECT;
811     addr->message = string_sprintf("error in %s %s: %s",
812       (filtertype != FILTER_FORWARD)? "filter" : "redirect",
813       (ob->data == NULL)? "file" : "data",
814       addr->message);
815     return DEFER;
816     }
817   }
818
819
820 /* Yield is either FF_DELIVERED (significant action) or FF_NOTDELIVERED (no
821 significant action). Before dealing with these, however, we must handle the
822 effect of skip_syntax_errors.
823
824 If skip_syntax_errors was set and there were syntax errors in an address list,
825 error messages will be present in eblock. Log them and send a message if so
826 configured. We cannot do this earlier, because the error message must not be
827 sent as the local user. If there were no valid addresses, generated will be
828 NULL. In this case, the router declines.
829
830 For a filter file, the error message has been fudged into an eblock. After
831 dealing with it, the router declines. */
832
833 if (eblock != NULL)
834   {
835   if (!moan_skipped_syntax_errors(
836         rblock->name,                            /* For message content */
837         eblock,                                  /* Ditto */
838         (verify != v_none || address_test_mode)?
839           NULL : ob->syntax_errors_to,           /* Who to mail */
840         generated != NULL,                       /* True if not all failed */
841         ob->syntax_errors_text))                 /* Custom message */
842     return DEFER;
843
844   if (filtertype != FILTER_FORWARD || generated == NULL)
845     {
846     addr->message = US"syntax error in redirection data";
847     return DECLINE;
848     }
849   }
850
851 /* Sort out the errors address and any header modifications, and handle the
852 generated addresses, if any. If there are no generated addresses, we must avoid
853 calling sort_errors_and_headers() in case this router declines - that function
854 may modify the errors_address field in the current address, and we don't want
855 to do that for a decline. */
856
857 if (generated != NULL)
858   {
859   if ((xrc = sort_errors_and_headers(rblock, addr, verify, &addr_prop)) != OK)
860     return xrc;
861   add_generated(rblock, addr_new, addr, generated, &addr_prop, &ugid, pw);
862   }
863
864 /* FF_DELIVERED with no generated addresses is what we get when an address list
865 contains :blackhole: or a filter contains "seen finish" without having
866 generated anything. Log what happened to this address, and return DISCARD. */
867
868 if (frc == FF_DELIVERED)
869   {
870   if (generated == NULL && verify == v_none && !address_test_mode)
871     {
872     log_write(0, LOG_MAIN, "=> %s <%s> R=%s", discarded, addr->address,
873       rblock->name);
874     yield = DISCARD;
875     }
876   }
877
878 /* For an address list, FF_NOTDELIVERED always means that no addresses were
879 generated. For a filter, addresses may or may not have been generated. If none
880 were, it's the same as an empty address list, and the router declines. However,
881 if addresses were generated, we can't just decline because successful delivery
882 of the base address gets it marked "done", so deferred generated addresses
883 never get tried again. We have to generate a new version of the base address,
884 as if there were a "deliver" command in the filter file, with the original
885 address as parent. */
886
887 else
888   {
889   address_item *next;
890
891   if (generated == NULL) return DECLINE;
892
893   next = deliver_make_addr(addr->address, FALSE);
894   next->parent = addr;
895   addr->child_count++;
896   next->next = *addr_new;
897   *addr_new = next;
898
899   /* Copy relevant flags (af_propagate is a name for the set), and set the
900   data that propagates. */
901
902   copyflag(next, addr, af_propagate);
903   next->prop = addr_prop;
904
905   DEBUG(D_route) debug_printf("%s router autogenerated %s\n%s%s%s",
906     rblock->name,
907     next->address,
908     (addr_prop.errors_address != NULL)? "  errors to " : "",
909     (addr_prop.errors_address != NULL)? addr_prop.errors_address : US"",
910     (addr_prop.errors_address != NULL)? "\n" : "");
911   }
912
913 /* Control gets here only when the address has been completely handled. Put the
914 original address onto the succeed queue so that any retry items that get
915 attached to it get processed. */
916
917 addr->next = *addr_succeed;
918 *addr_succeed = addr;
919
920 return yield;
921 }
922
923 /* End of routers/redirect.c */