Fix the handling of reverse time in the ratelimit code.
[exim.git] / src / src / acl.c
CommitLineData
e5d5a95f 1/* $Cambridge: exim/src/src/acl.c,v 1.48 2005/09/13 18:06:30 fanf2 Exp $ */
059ec3d9
PH
2
3/*************************************************
4* Exim - an Internet mail transport agent *
5*************************************************/
6
c988f1f4 7/* Copyright (c) University of Cambridge 1995 - 2005 */
059ec3d9
PH
8/* See the file NOTICE for conditions of use and distribution. */
9
10/* Code for handling Access Control Lists (ACLs) */
11
12#include "exim.h"
13
14
15/* Default callout timeout */
16
17#define CALLOUT_TIMEOUT_DEFAULT 30
18
19/* ACL verb codes - keep in step with the table of verbs that follows */
20
21enum { ACL_ACCEPT, ACL_DEFER, ACL_DENY, ACL_DISCARD, ACL_DROP, ACL_REQUIRE,
22 ACL_WARN };
23
24/* ACL verbs */
25
26static uschar *verbs[] =
27 { US"accept", US"defer", US"deny", US"discard", US"drop", US"require",
28 US"warn" };
29
30/* For each verb, the condition for which "message" is used */
31
32static int msgcond[] = { FAIL, OK, OK, FAIL, OK, FAIL, OK };
33
34/* ACL condition and modifier codes - keep in step with the table that
35follows. */
36
8523533c
TK
37enum { ACLC_ACL, ACLC_AUTHENTICATED,
38#ifdef EXPERIMENTAL_BRIGHTMAIL
39 ACLC_BMI_OPTIN,
40#endif
41ACLC_CONDITION, ACLC_CONTROL,
42#ifdef WITH_CONTENT_SCAN
43 ACLC_DECODE,
44#endif
45 ACLC_DELAY,
46#ifdef WITH_OLD_DEMIME
47 ACLC_DEMIME,
8e669ac1 48#endif
fb2274d4
TK
49#ifdef EXPERIMENTAL_DOMAINKEYS
50 ACLC_DK_DOMAIN_SOURCE,
51 ACLC_DK_POLICY,
52 ACLC_DK_SENDER_DOMAINS,
53 ACLC_DK_SENDER_LOCAL_PARTS,
54 ACLC_DK_SENDERS,
55 ACLC_DK_STATUS,
56#endif
8523533c
TK
57 ACLC_DNSLISTS, ACLC_DOMAINS, ACLC_ENCRYPTED, ACLC_ENDPASS,
58 ACLC_HOSTS, ACLC_LOCAL_PARTS, ACLC_LOG_MESSAGE, ACLC_LOGWRITE,
59#ifdef WITH_CONTENT_SCAN
60 ACLC_MALWARE,
61#endif
62 ACLC_MESSAGE,
63#ifdef WITH_CONTENT_SCAN
64 ACLC_MIME_REGEX,
65#endif
870f6ba8 66 ACLC_RATELIMIT,
8523533c
TK
67 ACLC_RECIPIENTS,
68#ifdef WITH_CONTENT_SCAN
69 ACLC_REGEX,
70#endif
71 ACLC_SENDER_DOMAINS, ACLC_SENDERS, ACLC_SET,
72#ifdef WITH_CONTENT_SCAN
8e669ac1 73 ACLC_SPAM,
8523533c
TK
74#endif
75#ifdef EXPERIMENTAL_SPF
76 ACLC_SPF,
77#endif
78 ACLC_VERIFY };
059ec3d9
PH
79
80/* ACL conditions/modifiers: "delay", "control", "endpass", "message",
81"log_message", "logwrite", and "set" are modifiers that look like conditions
82but always return TRUE. They are used for their side effects. */
83
9a26b6b2
PH
84static uschar *conditions[] = {
85 US"acl",
86 US"authenticated",
8523533c
TK
87#ifdef EXPERIMENTAL_BRIGHTMAIL
88 US"bmi_optin",
89#endif
90 US"condition",
8e669ac1 91 US"control",
8523533c
TK
92#ifdef WITH_CONTENT_SCAN
93 US"decode",
94#endif
95 US"delay",
96#ifdef WITH_OLD_DEMIME
97 US"demime",
98#endif
fb2274d4 99#ifdef EXPERIMENTAL_DOMAINKEYS
7f45268c
PH
100 US"dk_domain_source",
101 US"dk_policy",
102 US"dk_sender_domains",
103 US"dk_sender_local_parts",
104 US"dk_senders",
105 US"dk_status",
fb2274d4 106#endif
8523533c 107 US"dnslists", US"domains", US"encrypted",
059ec3d9 108 US"endpass", US"hosts", US"local_parts", US"log_message", US"logwrite",
8523533c
TK
109#ifdef WITH_CONTENT_SCAN
110 US"malware",
111#endif
112 US"message",
113#ifdef WITH_CONTENT_SCAN
114 US"mime_regex",
115#endif
870f6ba8 116 US"ratelimit",
8523533c
TK
117 US"recipients",
118#ifdef WITH_CONTENT_SCAN
119 US"regex",
120#endif
121 US"sender_domains", US"senders", US"set",
122#ifdef WITH_CONTENT_SCAN
123 US"spam",
124#endif
125#ifdef EXPERIMENTAL_SPF
126 US"spf",
127#endif
059ec3d9 128 US"verify" };
8e669ac1 129
c5fcb476 130
9a26b6b2
PH
131/* Return values from decode_control(); keep in step with the table of names
132that follows! */
133
134enum {
135#ifdef EXPERIMENTAL_BRIGHTMAIL
136 CONTROL_BMI_RUN,
137#endif
138#ifdef EXPERIMENTAL_DOMAINKEYS
139 CONTROL_DK_VERIFY,
140#endif
141 CONTROL_ERROR, CONTROL_CASEFUL_LOCAL_PART, CONTROL_CASELOWER_LOCAL_PART,
142 CONTROL_ENFORCE_SYNC, CONTROL_NO_ENFORCE_SYNC, CONTROL_FREEZE,
8800895a 143 CONTROL_QUEUE_ONLY, CONTROL_SUBMISSION, CONTROL_SUPPRESS_LOCAL_FIXUPS,
9a26b6b2
PH
144#ifdef WITH_CONTENT_SCAN
145 CONTROL_NO_MBOX_UNSPOOL,
146#endif
147 CONTROL_FAKEDEFER, CONTROL_FAKEREJECT, CONTROL_NO_MULTILINE };
148
8800895a
PH
149/* ACL control names; keep in step with the table above! This list is used for
150turning ids into names. The actual list of recognized names is in the variable
151control_def controls_list[] below. The fact that there are two lists is a mess
152and should be tidied up. */
9a26b6b2
PH
153
154static uschar *controls[] = {
155 #ifdef EXPERIMENTAL_BRIGHTMAIL
156 US"bmi_run",
157 #endif
158 #ifdef EXPERIMENTAL_DOMAINKEYS
159 US"dk_verify",
160 #endif
161 US"error", US"caseful_local_part",
c5fcb476 162 US"caselower_local_part", US"enforce_sync", US"no_enforce_sync", US"freeze",
8800895a 163 US"queue_only", US"submission", US"suppress_local_fixups",
9a26b6b2
PH
164 #ifdef WITH_CONTENT_SCAN
165 US"no_mbox_unspool",
166 #endif
8800895a 167
9a26b6b2 168 US"no_multiline"};
059ec3d9
PH
169
170/* Flags to indicate for which conditions /modifiers a string expansion is done
171at the outer level. In the other cases, expansion already occurs in the
172checking functions. */
173
174static uschar cond_expand_at_top[] = {
175 TRUE, /* acl */
176 FALSE, /* authenticated */
8523533c
TK
177#ifdef EXPERIMENTAL_BRIGHTMAIL
178 TRUE, /* bmi_optin */
8e669ac1 179#endif
059ec3d9
PH
180 TRUE, /* condition */
181 TRUE, /* control */
8523533c
TK
182#ifdef WITH_CONTENT_SCAN
183 TRUE, /* decode */
184#endif
059ec3d9 185 TRUE, /* delay */
8523533c
TK
186#ifdef WITH_OLD_DEMIME
187 TRUE, /* demime */
188#endif
fb2274d4 189#ifdef EXPERIMENTAL_DOMAINKEYS
7f45268c
PH
190 TRUE, /* dk_domain_source */
191 TRUE, /* dk_policy */
192 TRUE, /* dk_sender_domains */
e715ad22 193 TRUE, /* dk_sender_local_parts */
7f45268c
PH
194 TRUE, /* dk_senders */
195 TRUE, /* dk_status */
fb2274d4 196#endif
059ec3d9
PH
197 TRUE, /* dnslists */
198 FALSE, /* domains */
199 FALSE, /* encrypted */
200 TRUE, /* endpass */
201 FALSE, /* hosts */
202 FALSE, /* local_parts */
203 TRUE, /* log_message */
204 TRUE, /* logwrite */
8523533c
TK
205#ifdef WITH_CONTENT_SCAN
206 TRUE, /* malware */
207#endif
059ec3d9 208 TRUE, /* message */
8523533c
TK
209#ifdef WITH_CONTENT_SCAN
210 TRUE, /* mime_regex */
211#endif
870f6ba8 212 TRUE, /* ratelimit */
059ec3d9 213 FALSE, /* recipients */
8523533c
TK
214#ifdef WITH_CONTENT_SCAN
215 TRUE, /* regex */
216#endif
059ec3d9
PH
217 FALSE, /* sender_domains */
218 FALSE, /* senders */
219 TRUE, /* set */
8523533c
TK
220#ifdef WITH_CONTENT_SCAN
221 TRUE, /* spam */
222#endif
223#ifdef EXPERIMENTAL_SPF
224 TRUE, /* spf */
225#endif
059ec3d9
PH
226 TRUE /* verify */
227};
228
229/* Flags to identify the modifiers */
230
231static uschar cond_modifiers[] = {
232 FALSE, /* acl */
233 FALSE, /* authenticated */
8523533c
TK
234#ifdef EXPERIMENTAL_BRIGHTMAIL
235 TRUE, /* bmi_optin */
8e669ac1 236#endif
059ec3d9
PH
237 FALSE, /* condition */
238 TRUE, /* control */
8523533c
TK
239#ifdef WITH_CONTENT_SCAN
240 FALSE, /* decode */
241#endif
059ec3d9 242 TRUE, /* delay */
8523533c
TK
243#ifdef WITH_OLD_DEMIME
244 FALSE, /* demime */
245#endif
fb2274d4 246#ifdef EXPERIMENTAL_DOMAINKEYS
7f45268c
PH
247 FALSE, /* dk_domain_source */
248 FALSE, /* dk_policy */
249 FALSE, /* dk_sender_domains */
e715ad22 250 FALSE, /* dk_sender_local_parts */
7f45268c
PH
251 FALSE, /* dk_senders */
252 FALSE, /* dk_status */
fb2274d4 253#endif
059ec3d9
PH
254 FALSE, /* dnslists */
255 FALSE, /* domains */
256 FALSE, /* encrypted */
257 TRUE, /* endpass */
258 FALSE, /* hosts */
259 FALSE, /* local_parts */
260 TRUE, /* log_message */
8523533c
TK
261 TRUE, /* logwrite */
262#ifdef WITH_CONTENT_SCAN
263 FALSE, /* malware */
264#endif
059ec3d9 265 TRUE, /* message */
8523533c
TK
266#ifdef WITH_CONTENT_SCAN
267 FALSE, /* mime_regex */
268#endif
870f6ba8 269 FALSE, /* ratelimit */
059ec3d9 270 FALSE, /* recipients */
8523533c
TK
271#ifdef WITH_CONTENT_SCAN
272 FALSE, /* regex */
273#endif
059ec3d9
PH
274 FALSE, /* sender_domains */
275 FALSE, /* senders */
276 TRUE, /* set */
8523533c
TK
277#ifdef WITH_CONTENT_SCAN
278 FALSE, /* spam */
279#endif
280#ifdef EXPERIMENTAL_SPF
281 FALSE, /* spf */
282#endif
059ec3d9
PH
283 FALSE /* verify */
284};
285
c5fcb476 286/* Bit map vector of which conditions are not allowed at certain times. For
2f079f46
PH
287each condition, there's a bitmap of dis-allowed times. For some, it is easier
288to specify the negation of a small number of allowed times. */
059ec3d9
PH
289
290static unsigned int cond_forbids[] = {
291 0, /* acl */
8e669ac1 292
059ec3d9
PH
293 (1<<ACL_WHERE_NOTSMTP)|(1<<ACL_WHERE_CONNECT)| /* authenticated */
294 (1<<ACL_WHERE_HELO),
8e669ac1 295
8523533c 296#ifdef EXPERIMENTAL_BRIGHTMAIL
3864bb8e 297 (1<<ACL_WHERE_AUTH)| /* bmi_optin */
8523533c
TK
298 (1<<ACL_WHERE_CONNECT)|(1<<ACL_WHERE_HELO)|
299 (1<<ACL_WHERE_DATA)|(1<<ACL_WHERE_MIME)|
8e669ac1 300 (1<<ACL_WHERE_ETRN)|(1<<ACL_WHERE_EXPN)|
8523533c
TK
301 (1<<ACL_WHERE_MAILAUTH)|
302 (1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_STARTTLS)|
303 (1<<ACL_WHERE_VRFY)|(1<<ACL_WHERE_PREDATA),
304#endif
8e669ac1 305
059ec3d9 306 0, /* condition */
8e669ac1 307
059ec3d9 308 /* Certain types of control are always allowed, so we let it through
2f079f46 309 always and check in the control processing itself. */
8e669ac1 310
059ec3d9 311 0, /* control */
8e669ac1 312
8523533c 313#ifdef WITH_CONTENT_SCAN
2f079f46
PH
314 (unsigned int)
315 ~(1<<ACL_WHERE_MIME), /* decode */
8523533c
TK
316#endif
317
059ec3d9 318 0, /* delay */
8e669ac1 319
f7b63901 320#ifdef WITH_OLD_DEMIME
2f079f46
PH
321 (unsigned int)
322 ~((1<<ACL_WHERE_DATA)|(1<<ACL_WHERE_NOTSMTP)), /* demime */
8523533c 323#endif
8e669ac1 324
fb2274d4 325#ifdef EXPERIMENTAL_DOMAINKEYS
2f079f46 326 (1<<ACL_WHERE_AUTH)| /* dk_domain_source */
fb2274d4
TK
327 (1<<ACL_WHERE_CONNECT)|(1<<ACL_WHERE_HELO)|
328 (1<<ACL_WHERE_RCPT)|(1<<ACL_WHERE_PREDATA)|
329 (1<<ACL_WHERE_ETRN)|(1<<ACL_WHERE_EXPN)|
330 (1<<ACL_WHERE_MAILAUTH)|(1<<ACL_WHERE_QUIT)|
331 (1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_STARTTLS)|
332 (1<<ACL_WHERE_VRFY),
84330b7b 333
2f079f46 334 (1<<ACL_WHERE_AUTH)| /* dk_policy */
fb2274d4
TK
335 (1<<ACL_WHERE_CONNECT)|(1<<ACL_WHERE_HELO)|
336 (1<<ACL_WHERE_RCPT)|(1<<ACL_WHERE_PREDATA)|
337 (1<<ACL_WHERE_ETRN)|(1<<ACL_WHERE_EXPN)|
338 (1<<ACL_WHERE_MAILAUTH)|(1<<ACL_WHERE_QUIT)|
339 (1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_STARTTLS)|
340 (1<<ACL_WHERE_VRFY),
84330b7b 341
2f079f46 342 (1<<ACL_WHERE_AUTH)| /* dk_sender_domains */
fb2274d4
TK
343 (1<<ACL_WHERE_CONNECT)|(1<<ACL_WHERE_HELO)|
344 (1<<ACL_WHERE_RCPT)|(1<<ACL_WHERE_PREDATA)|
345 (1<<ACL_WHERE_ETRN)|(1<<ACL_WHERE_EXPN)|
346 (1<<ACL_WHERE_MAILAUTH)|(1<<ACL_WHERE_QUIT)|
347 (1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_STARTTLS)|
348 (1<<ACL_WHERE_VRFY),
84330b7b 349
2f079f46 350 (1<<ACL_WHERE_AUTH)| /* dk_sender_local_parts */
fb2274d4
TK
351 (1<<ACL_WHERE_CONNECT)|(1<<ACL_WHERE_HELO)|
352 (1<<ACL_WHERE_RCPT)|(1<<ACL_WHERE_PREDATA)|
353 (1<<ACL_WHERE_ETRN)|(1<<ACL_WHERE_EXPN)|
354 (1<<ACL_WHERE_MAILAUTH)|(1<<ACL_WHERE_QUIT)|
355 (1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_STARTTLS)|
356 (1<<ACL_WHERE_VRFY),
84330b7b 357
2f079f46 358 (1<<ACL_WHERE_AUTH)| /* dk_senders */
fb2274d4
TK
359 (1<<ACL_WHERE_CONNECT)|(1<<ACL_WHERE_HELO)|
360 (1<<ACL_WHERE_RCPT)|(1<<ACL_WHERE_PREDATA)|
361 (1<<ACL_WHERE_ETRN)|(1<<ACL_WHERE_EXPN)|
362 (1<<ACL_WHERE_MAILAUTH)|(1<<ACL_WHERE_QUIT)|
363 (1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_STARTTLS)|
364 (1<<ACL_WHERE_VRFY),
84330b7b 365
2f079f46 366 (1<<ACL_WHERE_AUTH)| /* dk_status */
fb2274d4
TK
367 (1<<ACL_WHERE_CONNECT)|(1<<ACL_WHERE_HELO)|
368 (1<<ACL_WHERE_RCPT)|(1<<ACL_WHERE_PREDATA)|
369 (1<<ACL_WHERE_ETRN)|(1<<ACL_WHERE_EXPN)|
370 (1<<ACL_WHERE_MAILAUTH)|(1<<ACL_WHERE_QUIT)|
371 (1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_STARTTLS)|
372 (1<<ACL_WHERE_VRFY),
373#endif
374
059ec3d9
PH
375 (1<<ACL_WHERE_NOTSMTP), /* dnslists */
376
2f079f46
PH
377 (unsigned int)
378 ~(1<<ACL_WHERE_RCPT), /* domains */
059ec3d9
PH
379
380 (1<<ACL_WHERE_NOTSMTP)|(1<<ACL_WHERE_CONNECT)| /* encrypted */
381 (1<<ACL_WHERE_HELO),
8e669ac1 382
059ec3d9 383 0, /* endpass */
8e669ac1 384
059ec3d9
PH
385 (1<<ACL_WHERE_NOTSMTP), /* hosts */
386
2f079f46
PH
387 (unsigned int)
388 ~(1<<ACL_WHERE_RCPT), /* local_parts */
059ec3d9
PH
389
390 0, /* log_message */
8e669ac1 391
059ec3d9 392 0, /* logwrite */
8e669ac1 393
8523533c 394#ifdef WITH_CONTENT_SCAN
2f079f46
PH
395 (unsigned int)
396 ~((1<<ACL_WHERE_DATA)|(1<<ACL_WHERE_NOTSMTP)), /* malware */
8523533c
TK
397#endif
398
059ec3d9
PH
399 0, /* message */
400
8523533c 401#ifdef WITH_CONTENT_SCAN
2f079f46
PH
402 (unsigned int)
403 ~(1<<ACL_WHERE_MIME), /* mime_regex */
8523533c
TK
404#endif
405
870f6ba8
TF
406 0, /* ratelimit */
407
2f079f46
PH
408 (unsigned int)
409 ~(1<<ACL_WHERE_RCPT), /* recipients */
059ec3d9 410
8523533c 411#ifdef WITH_CONTENT_SCAN
2f079f46
PH
412 (unsigned int)
413 ~((1<<ACL_WHERE_DATA)|(1<<ACL_WHERE_NOTSMTP)| /* regex */
414 (1<<ACL_WHERE_MIME)),
8523533c
TK
415#endif
416
059ec3d9
PH
417 (1<<ACL_WHERE_AUTH)|(1<<ACL_WHERE_CONNECT)| /* sender_domains */
418 (1<<ACL_WHERE_HELO)|
419 (1<<ACL_WHERE_MAILAUTH)|(1<<ACL_WHERE_QUIT)|
420 (1<<ACL_WHERE_ETRN)|(1<<ACL_WHERE_EXPN)|
421 (1<<ACL_WHERE_STARTTLS)|(1<<ACL_WHERE_VRFY),
422
423 (1<<ACL_WHERE_AUTH)|(1<<ACL_WHERE_CONNECT)| /* senders */
424 (1<<ACL_WHERE_HELO)|
425 (1<<ACL_WHERE_MAILAUTH)|(1<<ACL_WHERE_QUIT)|
426 (1<<ACL_WHERE_ETRN)|(1<<ACL_WHERE_EXPN)|
427 (1<<ACL_WHERE_STARTTLS)|(1<<ACL_WHERE_VRFY),
428
429 0, /* set */
430
8523533c 431#ifdef WITH_CONTENT_SCAN
2f079f46
PH
432 (unsigned int)
433 ~((1<<ACL_WHERE_DATA)|(1<<ACL_WHERE_NOTSMTP)), /* spam */
8523533c
TK
434#endif
435
436#ifdef EXPERIMENTAL_SPF
437 (1<<ACL_WHERE_AUTH)|(1<<ACL_WHERE_CONNECT)| /* spf */
438 (1<<ACL_WHERE_HELO)|
439 (1<<ACL_WHERE_MAILAUTH)|
440 (1<<ACL_WHERE_ETRN)|(1<<ACL_WHERE_EXPN)|
441 (1<<ACL_WHERE_STARTTLS)|(1<<ACL_WHERE_VRFY),
442#endif
443
059ec3d9
PH
444 /* Certain types of verify are always allowed, so we let it through
445 always and check in the verify function itself */
446
447 0 /* verify */
059ec3d9
PH
448};
449
450
c5fcb476
PH
451/* Bit map vector of which controls are not allowed at certain times. For
452each control, there's a bitmap of dis-allowed times. For some, it is easier to
453specify the negation of a small number of allowed times. */
454
455static unsigned int control_forbids[] = {
8523533c
TK
456#ifdef EXPERIMENTAL_BRIGHTMAIL
457 0, /* bmi_run */
458#endif
fb2274d4 459#ifdef EXPERIMENTAL_DOMAINKEYS
e715ad22 460 (1<<ACL_WHERE_DATA)|(1<<ACL_WHERE_NOTSMTP), /* dk_verify */
fb2274d4 461#endif
3e11c26b 462
c5fcb476 463 0, /* error */
8e669ac1
PH
464
465 (unsigned int)
c5fcb476 466 ~(1<<ACL_WHERE_RCPT), /* caseful_local_part */
8e669ac1
PH
467
468 (unsigned int)
c5fcb476 469 ~(1<<ACL_WHERE_RCPT), /* caselower_local_part */
8e669ac1 470
c5fcb476 471 (1<<ACL_WHERE_NOTSMTP), /* enforce_sync */
8e669ac1 472
c5fcb476 473 (1<<ACL_WHERE_NOTSMTP), /* no_enforce_sync */
8e669ac1
PH
474
475 (unsigned int)
c5fcb476
PH
476 ~((1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_RCPT)| /* freeze */
477 (1<<ACL_WHERE_PREDATA)|(1<<ACL_WHERE_DATA)|
e715ad22 478 (1<<ACL_WHERE_NOTSMTP)|(1<<ACL_WHERE_MIME)),
8e669ac1
PH
479
480 (unsigned int)
c5fcb476
PH
481 ~((1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_RCPT)| /* queue_only */
482 (1<<ACL_WHERE_PREDATA)|(1<<ACL_WHERE_DATA)|
e715ad22 483 (1<<ACL_WHERE_NOTSMTP)|(1<<ACL_WHERE_MIME)),
8e669ac1
PH
484
485 (unsigned int)
c5fcb476 486 ~((1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_RCPT)| /* submission */
8e669ac1 487 (1<<ACL_WHERE_PREDATA)),
8523533c 488
8800895a
PH
489 (unsigned int)
490 ~((1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_RCPT)| /* suppress_local_fixups */
491 (1<<ACL_WHERE_NOTSMTP)|(1<<ACL_WHERE_PREDATA)),
492
8523533c 493#ifdef WITH_CONTENT_SCAN
8e669ac1 494 (unsigned int)
14d57970 495 ~((1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_RCPT)| /* no_mbox_unspool */
e715ad22
TK
496 (1<<ACL_WHERE_PREDATA)|(1<<ACL_WHERE_DATA)|
497 (1<<ACL_WHERE_MIME)),
8523533c 498#endif
a6c4ab60 499
8e669ac1 500 (unsigned int)
29aba418
TF
501 ~((1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_RCPT)| /* fakedefer */
502 (1<<ACL_WHERE_PREDATA)|(1<<ACL_WHERE_DATA)|
503 (1<<ACL_WHERE_MIME)),
504
505 (unsigned int)
a6c4ab60 506 ~((1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_RCPT)| /* fakereject */
e715ad22
TK
507 (1<<ACL_WHERE_PREDATA)|(1<<ACL_WHERE_DATA)|
508 (1<<ACL_WHERE_MIME)),
8523533c 509
c5fcb476
PH
510 (1<<ACL_WHERE_NOTSMTP) /* no_multiline */
511};
512
513/* Structure listing various control arguments, with their characteristics. */
059ec3d9
PH
514
515typedef struct control_def {
516 uschar *name;
517 int value; /* CONTROL_xxx value */
059ec3d9
PH
518 BOOL has_option; /* Has /option(s) following */
519} control_def;
520
521static control_def controls_list[] = {
8523533c 522#ifdef EXPERIMENTAL_BRIGHTMAIL
8800895a 523 { US"bmi_run", CONTROL_BMI_RUN, FALSE },
8523533c 524#endif
fb2274d4 525#ifdef EXPERIMENTAL_DOMAINKEYS
8800895a 526 { US"dk_verify", CONTROL_DK_VERIFY, FALSE },
fb2274d4 527#endif
8800895a
PH
528 { US"caseful_local_part", CONTROL_CASEFUL_LOCAL_PART, FALSE },
529 { US"caselower_local_part", CONTROL_CASELOWER_LOCAL_PART, FALSE },
530 { US"enforce_sync", CONTROL_ENFORCE_SYNC, FALSE },
531 { US"freeze", CONTROL_FREEZE, FALSE },
532 { US"no_enforce_sync", CONTROL_NO_ENFORCE_SYNC, FALSE },
533 { US"no_multiline_responses", CONTROL_NO_MULTILINE, FALSE },
534 { US"queue_only", CONTROL_QUEUE_ONLY, FALSE },
8523533c 535#ifdef WITH_CONTENT_SCAN
8800895a 536 { US"no_mbox_unspool", CONTROL_NO_MBOX_UNSPOOL, FALSE },
8523533c 537#endif
8800895a
PH
538 { US"fakedefer", CONTROL_FAKEDEFER, TRUE },
539 { US"fakereject", CONTROL_FAKEREJECT, TRUE },
540 { US"submission", CONTROL_SUBMISSION, TRUE },
541 { US"suppress_local_fixups", CONTROL_SUPPRESS_LOCAL_FIXUPS, FALSE }
059ec3d9
PH
542 };
543
e5a9dba6
PH
544/* Support data structures for Client SMTP Authorization. acl_verify_csa()
545caches its result in a tree to avoid repeated DNS queries. The result is an
546integer code which is used as an index into the following tables of
547explanatory strings and verification return codes. */
548
549static tree_node *csa_cache = NULL;
550
551enum { CSA_UNKNOWN, CSA_OK, CSA_DEFER_SRV, CSA_DEFER_ADDR,
552 CSA_FAIL_EXPLICIT, CSA_FAIL_DOMAIN, CSA_FAIL_NOADDR, CSA_FAIL_MISMATCH };
553
554/* The acl_verify_csa() return code is translated into an acl_verify() return
555code using the following table. It is OK unless the client is definitely not
556authorized. This is because CSA is supposed to be optional for sending sites,
557so recipients should not be too strict about checking it - especially because
558DNS problems are quite likely to occur. It's possible to use $csa_status in
559further ACL conditions to distinguish ok, unknown, and defer if required, but
560the aim is to make the usual configuration simple. */
561
562static int csa_return_code[] = {
563 OK, OK, OK, OK,
564 FAIL, FAIL, FAIL, FAIL
565};
566
567static uschar *csa_status_string[] = {
568 US"unknown", US"ok", US"defer", US"defer",
569 US"fail", US"fail", US"fail", US"fail"
570};
571
572static uschar *csa_reason_string[] = {
573 US"unknown",
574 US"ok",
575 US"deferred (SRV lookup failed)",
576 US"deferred (target address lookup failed)",
577 US"failed (explicit authorization required)",
578 US"failed (host name not authorized)",
579 US"failed (no authorized addresses)",
580 US"failed (client address mismatch)"
581};
582
059ec3d9
PH
583/* Enable recursion between acl_check_internal() and acl_check_condition() */
584
585static int acl_check_internal(int, address_item *, uschar *, int, uschar **,
586 uschar **);
587
588
589/*************************************************
590* Pick out name from list *
591*************************************************/
592
593/* Use a binary chop method
594
595Arguments:
596 name name to find
597 list list of names
598 end size of list
599
600Returns: offset in list, or -1 if not found
601*/
602
603static int
604acl_checkname(uschar *name, uschar **list, int end)
605{
606int start = 0;
607
608while (start < end)
609 {
610 int mid = (start + end)/2;
611 int c = Ustrcmp(name, list[mid]);
612 if (c == 0) return mid;
613 if (c < 0) end = mid; else start = mid + 1;
614 }
615
616return -1;
617}
618
619
620/*************************************************
621* Read and parse one ACL *
622*************************************************/
623
624/* This function is called both from readconf in order to parse the ACLs in the
625configuration file, and also when an ACL is encountered dynamically (e.g. as
626the result of an expansion). It is given a function to call in order to
627retrieve the lines of the ACL. This function handles skipping comments and
628blank lines (where relevant).
629
630Arguments:
631 func function to get next line of ACL
632 error where to put an error message
633
634Returns: pointer to ACL, or NULL
635 NULL can be legal (empty ACL); in this case error will be NULL
636*/
637
638acl_block *
639acl_read(uschar *(*func)(void), uschar **error)
640{
641acl_block *yield = NULL;
642acl_block **lastp = &yield;
643acl_block *this = NULL;
644acl_condition_block *cond;
645acl_condition_block **condp = NULL;
646uschar *s;
647
648*error = NULL;
649
650while ((s = (*func)()) != NULL)
651 {
652 int v, c;
653 BOOL negated = FALSE;
654 uschar *saveline = s;
655 uschar name[64];
656
657 /* Conditions (but not verbs) are allowed to be negated by an initial
658 exclamation mark. */
659
660 while (isspace(*s)) s++;
661 if (*s == '!')
662 {
663 negated = TRUE;
664 s++;
665 }
666
cf00dad6
PH
667 /* Read the name of a verb or a condition, or the start of a new ACL, which
668 can be started by a name, or by a macro definition. */
059ec3d9
PH
669
670 s = readconf_readname(name, sizeof(name), s);
cf00dad6 671 if (*s == ':' || isupper(name[0] && *s == '=')) return yield;
059ec3d9
PH
672
673 /* If a verb is unrecognized, it may be another condition or modifier that
674 continues the previous verb. */
675
676 v = acl_checkname(name, verbs, sizeof(verbs)/sizeof(char *));
677 if (v < 0)
678 {
679 if (this == NULL)
680 {
681 *error = string_sprintf("unknown ACL verb in \"%s\"", saveline);
682 return NULL;
683 }
684 }
685
686 /* New verb */
687
688 else
689 {
690 if (negated)
691 {
692 *error = string_sprintf("malformed ACL line \"%s\"", saveline);
693 return NULL;
694 }
695 this = store_get(sizeof(acl_block));
696 *lastp = this;
697 lastp = &(this->next);
698 this->next = NULL;
699 this->verb = v;
700 this->condition = NULL;
701 condp = &(this->condition);
702 if (*s == 0) continue; /* No condition on this line */
703 if (*s == '!')
704 {
705 negated = TRUE;
706 s++;
707 }
708 s = readconf_readname(name, sizeof(name), s); /* Condition name */
709 }
710
711 /* Handle a condition or modifier. */
712
713 c = acl_checkname(name, conditions, sizeof(conditions)/sizeof(char *));
714 if (c < 0)
715 {
716 *error = string_sprintf("unknown ACL condition/modifier in \"%s\"",
717 saveline);
718 return NULL;
719 }
720
721 /* The modifiers may not be negated */
722
723 if (negated && cond_modifiers[c])
724 {
725 *error = string_sprintf("ACL error: negation is not allowed with "
726 "\"%s\"", conditions[c]);
727 return NULL;
728 }
729
730 /* ENDPASS may occur only with ACCEPT or DISCARD. */
731
732 if (c == ACLC_ENDPASS &&
733 this->verb != ACL_ACCEPT &&
734 this->verb != ACL_DISCARD)
735 {
736 *error = string_sprintf("ACL error: \"%s\" is not allowed with \"%s\"",
737 conditions[c], verbs[this->verb]);
738 return NULL;
739 }
740
741 cond = store_get(sizeof(acl_condition_block));
742 cond->next = NULL;
743 cond->type = c;
744 cond->u.negated = negated;
745
746 *condp = cond;
747 condp = &(cond->next);
748
749 /* The "set" modifier is different in that its argument is "name=value"
750 rather than just a value, and we can check the validity of the name, which
751 gives us a variable number to insert into the data block. */
752
753 if (c == ACLC_SET)
754 {
755 if (Ustrncmp(s, "acl_", 4) != 0 || (s[4] != 'c' && s[4] != 'm') ||
756 !isdigit(s[5]) || (!isspace(s[6]) && s[6] != '='))
757 {
758 *error = string_sprintf("unrecognized name after \"set\" in ACL "
759 "modifier \"set %s\"", s);
760 return NULL;
761 }
762
763 cond->u.varnumber = s[5] - '0';
764 if (s[4] == 'm') cond->u.varnumber += ACL_C_MAX;
765 s += 6;
766 while (isspace(*s)) s++;
767 }
768
769 /* For "set", we are now positioned for the data. For the others, only
770 "endpass" has no data */
771
772 if (c != ACLC_ENDPASS)
773 {
774 if (*s++ != '=')
775 {
776 *error = string_sprintf("\"=\" missing after ACL \"%s\" %s", name,
777 cond_modifiers[c]? US"modifier" : US"condition");
778 return NULL;
779 }
780 while (isspace(*s)) s++;
781 cond->arg = string_copy(s);
782 }
783 }
784
785return yield;
786}
787
788
789
790/*************************************************
791* Handle warnings *
792*************************************************/
793
794/* This function is called when a WARN verb's conditions are true. It adds to
795the message's headers, and/or writes information to the log. In each case, this
796only happens once (per message for headers, per connection for log).
797
798Arguments:
799 where ACL_WHERE_xxxx indicating which ACL this is
800 user_message message for adding to headers
801 log_message message for logging, if different
802
803Returns: nothing
804*/
805
806static void
807acl_warn(int where, uschar *user_message, uschar *log_message)
808{
809int hlen;
810
811if (log_message != NULL && log_message != user_message)
812 {
813 uschar *text;
814 string_item *logged;
815
816 text = string_sprintf("%s Warning: %s", host_and_ident(TRUE),
817 string_printing(log_message));
818
819 /* If a sender verification has failed, and the log message is "sender verify
820 failed", add the failure message. */
821
822 if (sender_verified_failed != NULL &&
823 sender_verified_failed->message != NULL &&
824 strcmpic(log_message, US"sender verify failed") == 0)
825 text = string_sprintf("%s: %s", text, sender_verified_failed->message);
826
9c7a242c
PH
827 /* Search previously logged warnings. They are kept in malloc
828 store so they can be freed at the start of a new message. */
059ec3d9
PH
829
830 for (logged = acl_warn_logged; logged != NULL; logged = logged->next)
831 if (Ustrcmp(logged->text, text) == 0) break;
832
833 if (logged == NULL)
834 {
835 int length = Ustrlen(text) + 1;
836 log_write(0, LOG_MAIN, "%s", text);
837 logged = store_malloc(sizeof(string_item) + length);
838 logged->text = (uschar *)logged + sizeof(string_item);
839 memcpy(logged->text, text, length);
840 logged->next = acl_warn_logged;
841 acl_warn_logged = logged;
842 }
843 }
844
845/* If there's no user message, we are done. */
846
847if (user_message == NULL) return;
848
849/* If this isn't a message ACL, we can't do anything with a user message.
850Log an error. */
851
852if (where > ACL_WHERE_NOTSMTP)
853 {
854 log_write(0, LOG_MAIN|LOG_PANIC, "ACL \"warn\" with \"message\" setting "
855 "found in a non-message (%s) ACL: cannot specify header lines here: "
856 "message ignored", acl_wherenames[where]);
857 return;
858 }
859
860/* Treat the user message as a sequence of one or more header lines. */
861
862hlen = Ustrlen(user_message);
863if (hlen > 0)
864 {
865 uschar *text, *p, *q;
866
867 /* Add a final newline if not present */
868
869 text = ((user_message)[hlen-1] == '\n')? user_message :
870 string_sprintf("%s\n", user_message);
871
872 /* Loop for multiple header lines, taking care about continuations */
873
874 for (p = q = text; *p != 0; )
875 {
876 uschar *s;
877 int newtype = htype_add_bot;
878 header_line **hptr = &acl_warn_headers;
879
880 /* Find next header line within the string */
881
882 for (;;)
883 {
884 q = Ustrchr(q, '\n');
885 if (*(++q) != ' ' && *q != '\t') break;
886 }
887
888 /* If the line starts with a colon, interpret the instruction for where to
889 add it. This temporarily sets up a new type. */
890
891 if (*p == ':')
892 {
893 if (strncmpic(p, US":after_received:", 16) == 0)
894 {
895 newtype = htype_add_rec;
896 p += 16;
897 }
8523533c
TK
898 else if (strncmpic(p, US":at_start_rfc:", 14) == 0)
899 {
900 newtype = htype_add_rfc;
901 p += 14;
8e669ac1 902 }
059ec3d9
PH
903 else if (strncmpic(p, US":at_start:", 10) == 0)
904 {
905 newtype = htype_add_top;
906 p += 10;
907 }
908 else if (strncmpic(p, US":at_end:", 8) == 0)
909 {
910 newtype = htype_add_bot;
911 p += 8;
912 }
913 while (*p == ' ' || *p == '\t') p++;
914 }
915
916 /* See if this line starts with a header name, and if not, add X-ACL-Warn:
917 to the front of it. */
918
919 for (s = p; s < q - 1; s++)
920 {
921 if (*s == ':' || !isgraph(*s)) break;
922 }
923
924 s = string_sprintf("%s%.*s", (*s == ':')? "" : "X-ACL-Warn: ", q - p, p);
925 hlen = Ustrlen(s);
926
927 /* See if this line has already been added */
928
929 while (*hptr != NULL)
930 {
931 if (Ustrncmp((*hptr)->text, s, hlen) == 0) break;
932 hptr = &((*hptr)->next);
933 }
934
935 /* Add if not previously present */
936
937 if (*hptr == NULL)
938 {
939 header_line *h = store_get(sizeof(header_line));
940 h->text = s;
941 h->next = NULL;
942 h->type = newtype;
943 h->slen = hlen;
944 *hptr = h;
945 hptr = &(h->next);
946 }
947
948 /* Advance for next header line within the string */
949
950 p = q;
951 }
952 }
953}
954
955
956
957/*************************************************
958* Verify and check reverse DNS *
959*************************************************/
960
961/* Called from acl_verify() below. We look up the host name(s) of the client IP
962address if this has not yet been done. The host_name_lookup() function checks
963that one of these names resolves to an address list that contains the client IP
964address, so we don't actually have to do the check here.
965
966Arguments:
967 user_msgptr pointer for user message
968 log_msgptr pointer for log message
969
970Returns: OK verification condition succeeded
971 FAIL verification failed
972 DEFER there was a problem verifying
973*/
974
975static int
976acl_verify_reverse(uschar **user_msgptr, uschar **log_msgptr)
977{
978int rc;
979
980user_msgptr = user_msgptr; /* stop compiler warning */
981
982/* Previous success */
983
984if (sender_host_name != NULL) return OK;
985
986/* Previous failure */
987
988if (host_lookup_failed)
989 {
990 *log_msgptr = string_sprintf("host lookup failed%s", host_lookup_msg);
991 return FAIL;
992 }
993
994/* Need to do a lookup */
995
996HDEBUG(D_acl)
997 debug_printf("looking up host name to force name/address consistency check\n");
998
999if ((rc = host_name_lookup()) != OK)
1000 {
1001 *log_msgptr = (rc == DEFER)?
1002 US"host lookup deferred for reverse lookup check"
1003 :
1004 string_sprintf("host lookup failed for reverse lookup check%s",
1005 host_lookup_msg);
1006 return rc; /* DEFER or FAIL */
1007 }
1008
1009host_build_sender_fullhost();
1010return OK;
1011}
1012
1013
1014
1015/*************************************************
e5a9dba6
PH
1016* Check client IP address matches CSA target *
1017*************************************************/
1018
1019/* Called from acl_verify_csa() below. This routine scans a section of a DNS
1020response for address records belonging to the CSA target hostname. The section
1021is specified by the reset argument, either RESET_ADDITIONAL or RESET_ANSWERS.
1022If one of the addresses matches the client's IP address, then the client is
1023authorized by CSA. If there are target IP addresses but none of them match
1024then the client is using an unauthorized IP address. If there are no target IP
1025addresses then the client cannot be using an authorized IP address. (This is
1026an odd configuration - why didn't the SRV record have a weight of 1 instead?)
1027
1028Arguments:
1029 dnsa the DNS answer block
1030 dnss a DNS scan block for us to use
1031 reset option specifing what portion to scan, as described above
1032 target the target hostname to use for matching RR names
1033
1034Returns: CSA_OK successfully authorized
1035 CSA_FAIL_MISMATCH addresses found but none matched
1036 CSA_FAIL_NOADDR no target addresses found
1037*/
1038
1039static int
1040acl_verify_csa_address(dns_answer *dnsa, dns_scan *dnss, int reset,
1041 uschar *target)
1042{
1043dns_record *rr;
1044dns_address *da;
1045
1046BOOL target_found = FALSE;
1047
1048for (rr = dns_next_rr(dnsa, dnss, reset);
1049 rr != NULL;
1050 rr = dns_next_rr(dnsa, dnss, RESET_NEXT))
1051 {
1052 /* Check this is an address RR for the target hostname. */
1053
1054 if (rr->type != T_A
1055 #if HAVE_IPV6
1056 && rr->type != T_AAAA
1057 #ifdef SUPPORT_A6
1058 && rr->type != T_A6
1059 #endif
1060 #endif
1061 ) continue;
1062
1063 if (strcmpic(target, rr->name) != 0) continue;
1064
1065 target_found = TRUE;
1066
1067 /* Turn the target address RR into a list of textual IP addresses and scan
1068 the list. There may be more than one if it is an A6 RR. */
1069
1070 for (da = dns_address_from_rr(dnsa, rr); da != NULL; da = da->next)
1071 {
1072 /* If the client IP address matches the target IP address, it's good! */
1073
1074 DEBUG(D_acl) debug_printf("CSA target address is %s\n", da->address);
1075
1076 if (strcmpic(sender_host_address, da->address) == 0) return CSA_OK;
1077 }
1078 }
1079
1080/* If we found some target addresses but none of them matched, the client is
1081using an unauthorized IP address, otherwise the target has no authorized IP
1082addresses. */
1083
1084if (target_found) return CSA_FAIL_MISMATCH;
1085else return CSA_FAIL_NOADDR;
1086}
1087
1088
1089
1090/*************************************************
1091* Verify Client SMTP Authorization *
1092*************************************************/
1093
1094/* Called from acl_verify() below. This routine calls dns_lookup_special()
1095to find the CSA SRV record corresponding to the domain argument, or
1096$sender_helo_name if no argument is provided. It then checks that the
1097client is authorized, and that its IP address corresponds to the SRV
1098target's address by calling acl_verify_csa_address() above. The address
1099should have been returned in the DNS response's ADDITIONAL section, but if
1100not we perform another DNS lookup to get it.
1101
1102Arguments:
1103 domain pointer to optional parameter following verify = csa
1104
1105Returns: CSA_UNKNOWN no valid CSA record found
1106 CSA_OK successfully authorized
1107 CSA_FAIL_* client is definitely not authorized
1108 CSA_DEFER_* there was a DNS problem
1109*/
1110
1111static int
1112acl_verify_csa(uschar *domain)
1113{
1114tree_node *t;
1115uschar *found, *p;
1116int priority, weight, port;
1117dns_answer dnsa;
1118dns_scan dnss;
1119dns_record *rr;
1120int rc, type;
1121uschar target[256];
1122
1123/* Work out the domain we are using for the CSA lookup. The default is the
1124client's HELO domain. If the client has not said HELO, use its IP address
1125instead. If it's a local client (exim -bs), CSA isn't applicable. */
1126
1127while (isspace(*domain) && *domain != '\0') ++domain;
1128if (*domain == '\0') domain = sender_helo_name;
1129if (domain == NULL) domain = sender_host_address;
1130if (sender_host_address == NULL) return CSA_UNKNOWN;
1131
1132/* If we have an address literal, strip off the framing ready for turning it
1133into a domain. The framing consists of matched square brackets possibly
1134containing a keyword and a colon before the actual IP address. */
1135
1136if (domain[0] == '[')
1137 {
1138 uschar *start = Ustrchr(domain, ':');
1139 if (start == NULL) start = domain;
1140 domain = string_copyn(start + 1, Ustrlen(start) - 2);
1141 }
1142
1143/* Turn domains that look like bare IP addresses into domains in the reverse
1144DNS. This code also deals with address literals and $sender_host_address. It's
1145not quite kosher to treat bare domains such as EHLO 192.0.2.57 the same as
1146address literals, but it's probably the most friendly thing to do. This is an
1147extension to CSA, so we allow it to be turned off for proper conformance. */
1148
1149if (string_is_ip_address(domain, NULL))
1150 {
1151 if (!dns_csa_use_reverse) return CSA_UNKNOWN;
1152 dns_build_reverse(domain, target);
1153 domain = target;
1154 }
1155
1156/* Find out if we've already done the CSA check for this domain. If we have,
1157return the same result again. Otherwise build a new cached result structure
1158for this domain. The name is filled in now, and the value is filled in when
1159we return from this function. */
1160
1161t = tree_search(csa_cache, domain);
1162if (t != NULL) return t->data.val;
1163
1164t = store_get_perm(sizeof(tree_node) + Ustrlen(domain));
1165Ustrcpy(t->name, domain);
1166(void)tree_insertnode(&csa_cache, t);
1167
1168/* Now we are ready to do the actual DNS lookup(s). */
1169
28e6ef29 1170found = domain;
e5a9dba6
PH
1171switch (dns_special_lookup(&dnsa, domain, T_CSA, &found))
1172 {
1173 /* If something bad happened (most commonly DNS_AGAIN), defer. */
1174
1175 default:
1176 return t->data.val = CSA_DEFER_SRV;
1177
1178 /* If we found nothing, the client's authorization is unknown. */
1179
1180 case DNS_NOMATCH:
1181 case DNS_NODATA:
1182 return t->data.val = CSA_UNKNOWN;
1183
1184 /* We got something! Go on to look at the reply in more detail. */
1185
1186 case DNS_SUCCEED:
1187 break;
1188 }
1189
1190/* Scan the reply for well-formed CSA SRV records. */
1191
1192for (rr = dns_next_rr(&dnsa, &dnss, RESET_ANSWERS);
1193 rr != NULL;
1194 rr = dns_next_rr(&dnsa, &dnss, RESET_NEXT))
1195 {
1196 if (rr->type != T_SRV) continue;
1197
1198 /* Extract the numerical SRV fields (p is incremented) */
1199
1200 p = rr->data;
1201 GETSHORT(priority, p);
1202 GETSHORT(weight, p);
1203 GETSHORT(port, p);
1204
1205 DEBUG(D_acl)
1206 debug_printf("CSA priority=%d weight=%d port=%d\n", priority, weight, port);
1207
1208 /* Check the CSA version number */
1209
1210 if (priority != 1) continue;
1211
1212 /* If the domain does not have a CSA SRV record of its own (i.e. the domain
1213 found by dns_special_lookup() is a parent of the one we asked for), we check
1214 the subdomain assertions in the port field. At the moment there's only one
1215 assertion: legitimate SMTP clients are all explicitly authorized with CSA
1216 SRV records of their own. */
1217
1218 if (found != domain)
1219 {
1220 if (port & 1)
1221 return t->data.val = CSA_FAIL_EXPLICIT;
1222 else
1223 return t->data.val = CSA_UNKNOWN;
1224 }
1225
1226 /* This CSA SRV record refers directly to our domain, so we check the value
1227 in the weight field to work out the domain's authorization. 0 and 1 are
1228 unauthorized; 3 means the client is authorized but we can't check the IP
1229 address in order to authenticate it, so we treat it as unknown; values
1230 greater than 3 are undefined. */
1231
1232 if (weight < 2) return t->data.val = CSA_FAIL_DOMAIN;
1233
1234 if (weight > 2) continue;
1235
1236 /* Weight == 2, which means the domain is authorized. We must check that the
1237 client's IP address is listed as one of the SRV target addresses. Save the
1238 target hostname then break to scan the additional data for its addresses. */
1239
1240 (void)dn_expand(dnsa.answer, dnsa.answer + dnsa.answerlen, p,
1241 (DN_EXPAND_ARG4_TYPE)target, sizeof(target));
1242
1243 DEBUG(D_acl) debug_printf("CSA target is %s\n", target);
1244
1245 break;
1246 }
1247
1248/* If we didn't break the loop then no appropriate records were found. */
1249
1250if (rr == NULL) return t->data.val = CSA_UNKNOWN;
1251
1252/* Do not check addresses if the target is ".", in accordance with RFC 2782.
1253A target of "." indicates there are no valid addresses, so the client cannot
1254be authorized. (This is an odd configuration because weight=2 target=. is
1255equivalent to weight=1, but we check for it in order to keep load off the
1256root name servers.) Note that dn_expand() turns "." into "". */
1257
1258if (Ustrcmp(target, "") == 0) return t->data.val = CSA_FAIL_NOADDR;
1259
1260/* Scan the additional section of the CSA SRV reply for addresses belonging
1261to the target. If the name server didn't return any additional data (e.g.
1262because it does not fully support SRV records), we need to do another lookup
1263to obtain the target addresses; otherwise we have a definitive result. */
1264
1265rc = acl_verify_csa_address(&dnsa, &dnss, RESET_ADDITIONAL, target);
1266if (rc != CSA_FAIL_NOADDR) return t->data.val = rc;
1267
1268/* The DNS lookup type corresponds to the IP version used by the client. */
1269
1270#if HAVE_IPV6
1271if (Ustrchr(sender_host_address, ':') != NULL)
1272 type = T_AAAA;
1273else
1274#endif /* HAVE_IPV6 */
1275 type = T_A;
1276
1277
1278#if HAVE_IPV6 && defined(SUPPORT_A6)
1279DNS_LOOKUP_AGAIN:
1280#endif
1281
1282switch (dns_lookup(&dnsa, target, type, NULL))
1283 {
1284 /* If something bad happened (most commonly DNS_AGAIN), defer. */
1285
1286 default:
1287 return t->data.val = CSA_DEFER_ADDR;
1288
1289 /* If the query succeeded, scan the addresses and return the result. */
1290
1291 case DNS_SUCCEED:
1292 rc = acl_verify_csa_address(&dnsa, &dnss, RESET_ANSWERS, target);
1293 if (rc != CSA_FAIL_NOADDR) return t->data.val = rc;
1294 /* else fall through */
1295
1296 /* If the target has no IP addresses, the client cannot have an authorized
1297 IP address. However, if the target site uses A6 records (not AAAA records)
1298 we have to do yet another lookup in order to check them. */
1299
1300 case DNS_NOMATCH:
1301 case DNS_NODATA:
1302
1303 #if HAVE_IPV6 && defined(SUPPORT_A6)
1304 if (type == T_AAAA) { type = T_A6; goto DNS_LOOKUP_AGAIN; }
1305 #endif
1306
1307 return t->data.val = CSA_FAIL_NOADDR;
1308 }
1309}
1310
1311
1312
1313/*************************************************
059ec3d9
PH
1314* Handle verification (address & other) *
1315*************************************************/
1316
1317/* This function implements the "verify" condition. It is called when
1318encountered in any ACL, because some tests are almost always permitted. Some
1319just don't make sense, and always fail (for example, an attempt to test a host
1320lookup for a non-TCP/IP message). Others are restricted to certain ACLs.
1321
1322Arguments:
1323 where where called from
1324 addr the recipient address that the ACL is handling, or NULL
1325 arg the argument of "verify"
1326 user_msgptr pointer for user message
1327 log_msgptr pointer for log message
1328 basic_errno where to put verify errno
1329
1330Returns: OK verification condition succeeded
1331 FAIL verification failed
1332 DEFER there was a problem verifying
1333 ERROR syntax error
1334*/
1335
1336static int
1337acl_verify(int where, address_item *addr, uschar *arg,
1338 uschar **user_msgptr, uschar **log_msgptr, int *basic_errno)
1339{
1340int sep = '/';
1341int callout = -1;
1342int callout_overall = -1;
4deaf07d 1343int callout_connect = -1;
059ec3d9
PH
1344int verify_options = 0;
1345int rc;
1346BOOL verify_header_sender = FALSE;
1347BOOL defer_ok = FALSE;
1348BOOL callout_defer_ok = FALSE;
1349BOOL no_details = FALSE;
eafd343b 1350BOOL success_on_redirect = FALSE;
059ec3d9
PH
1351address_item *sender_vaddr = NULL;
1352uschar *verify_sender_address = NULL;
1353uschar *pm_mailfrom = NULL;
1354uschar *se_mailfrom = NULL;
596875b3
PH
1355
1356/* Some of the verify items have slash-separated options; some do not. Diagnose
1357an error if options are given for items that don't expect them. This code has
1358now got very message. Refactoring to use a table would be a good idea one day.
1359*/
1360
1361uschar *slash = Ustrchr(arg, '/');
059ec3d9
PH
1362uschar *list = arg;
1363uschar *ss = string_nextinlist(&list, &sep, big_buffer, big_buffer_size);
1364
1365if (ss == NULL) goto BAD_VERIFY;
1366
1367/* Handle name/address consistency verification in a separate function. */
1368
1369if (strcmpic(ss, US"reverse_host_lookup") == 0)
1370 {
596875b3 1371 if (slash != NULL) goto NO_OPTIONS;
059ec3d9
PH
1372 if (sender_host_address == NULL) return OK;
1373 return acl_verify_reverse(user_msgptr, log_msgptr);
1374 }
1375
1376/* TLS certificate verification is done at STARTTLS time; here we just
1377test whether it was successful or not. (This is for optional verification; for
1378mandatory verification, the connection doesn't last this long.) */
1379
1380if (strcmpic(ss, US"certificate") == 0)
1381 {
596875b3 1382 if (slash != NULL) goto NO_OPTIONS;
059ec3d9
PH
1383 if (tls_certificate_verified) return OK;
1384 *user_msgptr = US"no verified certificate";
1385 return FAIL;
1386 }
1387
d7b47fd0
PH
1388/* We can test the result of optional HELO verification that might have
1389occurred earlier. If not, we can attempt the verification now. */
059ec3d9 1390
596875b3
PH
1391if (strcmpic(ss, US"helo") == 0)
1392 {
1393 if (slash != NULL) goto NO_OPTIONS;
d7b47fd0
PH
1394 if (helo_verified) return OK;
1395 if (helo_verify_failed) return FAIL;
1396 if (smtp_verify_helo()) return helo_verified? OK : FAIL;
1397 return DEFER;
596875b3 1398 }
059ec3d9 1399
e5a9dba6
PH
1400/* Do Client SMTP Authorization checks in a separate function, and turn the
1401result code into user-friendly strings. */
1402
1403if (strcmpic(ss, US"csa") == 0)
1404 {
1405 rc = acl_verify_csa(list);
1406 *log_msgptr = *user_msgptr = string_sprintf("client SMTP authorization %s",
1407 csa_reason_string[rc]);
1408 csa_status = csa_status_string[rc];
1409 DEBUG(D_acl) debug_printf("CSA result %s\n", csa_status);
1410 return csa_return_code[rc];
1411 }
1412
596875b3
PH
1413/* Check that all relevant header lines have the correct syntax. If there is
1414a syntax error, we return details of the error to the sender if configured to
1415send out full details. (But a "message" setting on the ACL can override, as
1416always). */
059ec3d9 1417
596875b3 1418if (strcmpic(ss, US"header_syntax") == 0)
059ec3d9 1419 {
596875b3 1420 if (slash != NULL) goto NO_OPTIONS;
1c41c9cc 1421 if (where != ACL_WHERE_DATA && where != ACL_WHERE_NOTSMTP) goto WRONG_ACL;
596875b3
PH
1422 rc = verify_check_headers(log_msgptr);
1423 if (rc != OK && smtp_return_error_details && *log_msgptr != NULL)
1424 *user_msgptr = string_sprintf("Rejected after DATA: %s", *log_msgptr);
1425 return rc;
1426 }
059ec3d9 1427
1c41c9cc
PH
1428/* Check that no recipient of this message is "blind", that is, every envelope
1429recipient must be mentioned in either To: or Cc:. */
1430
1431if (strcmpic(ss, US"not_blind") == 0)
1432 {
1433 if (slash != NULL) goto NO_OPTIONS;
1434 if (where != ACL_WHERE_DATA && where != ACL_WHERE_NOTSMTP) goto WRONG_ACL;
1435 rc = verify_check_notblind();
1436 if (rc != OK)
1437 {
1438 *log_msgptr = string_sprintf("bcc recipient detected");
1439 if (smtp_return_error_details)
1440 *user_msgptr = string_sprintf("Rejected after DATA: %s", *log_msgptr);
1441 }
1442 return rc;
1443 }
059ec3d9 1444
596875b3
PH
1445/* The remaining verification tests check recipient and sender addresses,
1446either from the envelope or from the header. There are a number of
1447slash-separated options that are common to all of them. */
059ec3d9 1448
059ec3d9 1449
596875b3
PH
1450/* Check that there is at least one verifiable sender address in the relevant
1451header lines. This can be followed by callout and defer options, just like
1452sender and recipient. */
059ec3d9 1453
596875b3
PH
1454if (strcmpic(ss, US"header_sender") == 0)
1455 {
1c41c9cc 1456 if (where != ACL_WHERE_DATA && where != ACL_WHERE_NOTSMTP) goto WRONG_ACL;
596875b3 1457 verify_header_sender = TRUE;
059ec3d9
PH
1458 }
1459
1460/* Otherwise, first item in verify argument must be "sender" or "recipient".
1461In the case of a sender, this can optionally be followed by an address to use
1462in place of the actual sender (rare special-case requirement). */
1463
1464else if (strncmpic(ss, US"sender", 6) == 0)
1465 {
1466 uschar *s = ss + 6;
1467 if (where > ACL_WHERE_NOTSMTP)
1468 {
1469 *log_msgptr = string_sprintf("cannot verify sender in ACL for %s "
1470 "(only possible for MAIL, RCPT, PREDATA, or DATA)",
1471 acl_wherenames[where]);
1472 return ERROR;
1473 }
1474 if (*s == 0)
1475 verify_sender_address = sender_address;
1476 else
1477 {
1478 while (isspace(*s)) s++;
1479 if (*s++ != '=') goto BAD_VERIFY;
1480 while (isspace(*s)) s++;
1481 verify_sender_address = string_copy(s);
1482 }
1483 }
1484else
1485 {
1486 if (strcmpic(ss, US"recipient") != 0) goto BAD_VERIFY;
1487 if (addr == NULL)
1488 {
1489 *log_msgptr = string_sprintf("cannot verify recipient in ACL for %s "
1490 "(only possible for RCPT)", acl_wherenames[where]);
1491 return ERROR;
1492 }
1493 }
1494
596875b3
PH
1495/* Remaining items are optional; they apply to sender and recipient
1496verification, including "header sender" verification. */
059ec3d9
PH
1497
1498while ((ss = string_nextinlist(&list, &sep, big_buffer, big_buffer_size))
1499 != NULL)
1500 {
1501 if (strcmpic(ss, US"defer_ok") == 0) defer_ok = TRUE;
1502 else if (strcmpic(ss, US"no_details") == 0) no_details = TRUE;
eafd343b 1503 else if (strcmpic(ss, US"success_on_redirect") == 0) success_on_redirect = TRUE;
059ec3d9
PH
1504
1505 /* These two old options are left for backwards compatibility */
1506
1507 else if (strcmpic(ss, US"callout_defer_ok") == 0)
1508 {
1509 callout_defer_ok = TRUE;
1510 if (callout == -1) callout = CALLOUT_TIMEOUT_DEFAULT;
1511 }
1512
1513 else if (strcmpic(ss, US"check_postmaster") == 0)
1514 {
1515 pm_mailfrom = US"";
1516 if (callout == -1) callout = CALLOUT_TIMEOUT_DEFAULT;
1517 }
1518
1519 /* The callout option has a number of sub-options, comma separated */
1520
1521 else if (strncmpic(ss, US"callout", 7) == 0)
1522 {
1523 callout = CALLOUT_TIMEOUT_DEFAULT;
1524 ss += 7;
1525 if (*ss != 0)
1526 {
1527 while (isspace(*ss)) ss++;
1528 if (*ss++ == '=')
1529 {
1530 int optsep = ',';
1531 uschar *opt;
1532 uschar buffer[256];
1533 while (isspace(*ss)) ss++;
8e669ac1
PH
1534
1535 /* This callout option handling code has become a mess as new options
1536 have been added in an ad hoc manner. It should be tidied up into some
4deaf07d 1537 kind of table-driven thing. */
8e669ac1 1538
059ec3d9
PH
1539 while ((opt = string_nextinlist(&ss, &optsep, buffer, sizeof(buffer)))
1540 != NULL)
1541 {
1542 if (strcmpic(opt, US"defer_ok") == 0) callout_defer_ok = TRUE;
1543 else if (strcmpic(opt, US"no_cache") == 0)
1544 verify_options |= vopt_callout_no_cache;
1545 else if (strcmpic(opt, US"random") == 0)
1546 verify_options |= vopt_callout_random;
1547 else if (strcmpic(opt, US"use_sender") == 0)
1548 verify_options |= vopt_callout_recipsender;
1549 else if (strcmpic(opt, US"use_postmaster") == 0)
1550 verify_options |= vopt_callout_recippmaster;
1551 else if (strcmpic(opt, US"postmaster") == 0) pm_mailfrom = US"";
2a4be8f9
PH
1552 else if (strcmpic(opt, US"fullpostmaster") == 0)
1553 {
1554 pm_mailfrom = US"";
1555 verify_options |= vopt_callout_fullpm;
1556 }
059ec3d9
PH
1557
1558 else if (strncmpic(opt, US"mailfrom", 8) == 0)
1559 {
1560 if (!verify_header_sender)
1561 {
1562 *log_msgptr = string_sprintf("\"mailfrom\" is allowed as a "
1563 "callout option only for verify=header_sender (detected in ACL "
1564 "condition \"%s\")", arg);
1565 return ERROR;
1566 }
1567 opt += 8;
1568 while (isspace(*opt)) opt++;
1569 if (*opt++ != '=')
1570 {
1571 *log_msgptr = string_sprintf("'=' expected after "
1572 "\"mailfrom\" in ACL condition \"%s\"", arg);
1573 return ERROR;
1574 }
1575 while (isspace(*opt)) opt++;
1576 se_mailfrom = string_copy(opt);
1577 }
1578
1579 else if (strncmpic(opt, US"postmaster_mailfrom", 19) == 0)
1580 {
1581 opt += 19;
1582 while (isspace(*opt)) opt++;
1583 if (*opt++ != '=')
1584 {
1585 *log_msgptr = string_sprintf("'=' expected after "
1586 "\"postmaster_mailfrom\" in ACL condition \"%s\"", arg);
1587 return ERROR;
1588 }
1589 while (isspace(*opt)) opt++;
1590 pm_mailfrom = string_copy(opt);
1591 }
1592
1593 else if (strncmpic(opt, US"maxwait", 7) == 0)
1594 {
1595 opt += 7;
1596 while (isspace(*opt)) opt++;
1597 if (*opt++ != '=')
1598 {
1599 *log_msgptr = string_sprintf("'=' expected after \"maxwait\" in "
1600 "ACL condition \"%s\"", arg);
1601 return ERROR;
1602 }
1603 while (isspace(*opt)) opt++;
1604 callout_overall = readconf_readtime(opt, 0, FALSE);
1605 if (callout_overall < 0)
1606 {
1607 *log_msgptr = string_sprintf("bad time value in ACL condition "
1608 "\"verify %s\"", arg);
1609 return ERROR;
1610 }
1611 }
4deaf07d
PH
1612 else if (strncmpic(opt, US"connect", 7) == 0)
1613 {
1614 opt += 7;
1615 while (isspace(*opt)) opt++;
1616 if (*opt++ != '=')
1617 {
1618 *log_msgptr = string_sprintf("'=' expected after "
1619 "\"callout_overaall\" in ACL condition \"%s\"", arg);
1620 return ERROR;
1621 }
1622 while (isspace(*opt)) opt++;
1623 callout_connect = readconf_readtime(opt, 0, FALSE);
1624 if (callout_connect < 0)
1625 {
1626 *log_msgptr = string_sprintf("bad time value in ACL condition "
1627 "\"verify %s\"", arg);
1628 return ERROR;
1629 }
1630 }
059ec3d9
PH
1631 else /* Plain time is callout connect/command timeout */
1632 {
1633 callout = readconf_readtime(opt, 0, FALSE);
1634 if (callout < 0)
1635 {
1636 *log_msgptr = string_sprintf("bad time value in ACL condition "
1637 "\"verify %s\"", arg);
1638 return ERROR;
1639 }
1640 }
1641 }
1642 }
1643 else
1644 {
1645 *log_msgptr = string_sprintf("'=' expected after \"callout\" in "
1646 "ACL condition \"%s\"", arg);
1647 return ERROR;
1648 }
1649 }
1650 }
1651
1652 /* Option not recognized */
1653
1654 else
1655 {
1656 *log_msgptr = string_sprintf("unknown option \"%s\" in ACL "
1657 "condition \"verify %s\"", ss, arg);
1658 return ERROR;
1659 }
1660 }
1661
1662if ((verify_options & (vopt_callout_recipsender|vopt_callout_recippmaster)) ==
1663 (vopt_callout_recipsender|vopt_callout_recippmaster))
1664 {
1665 *log_msgptr = US"only one of use_sender and use_postmaster can be set "
1666 "for a recipient callout";
1667 return ERROR;
1668 }
1669
1670/* Handle sender-in-header verification. Default the user message to the log
1671message if giving out verification details. */
1672
1673if (verify_header_sender)
1674 {
8e669ac1 1675 int verrno;
059ec3d9 1676 rc = verify_check_header_address(user_msgptr, log_msgptr, callout,
fe5b5d0b
PH
1677 callout_overall, callout_connect, se_mailfrom, pm_mailfrom, verify_options,
1678 &verrno);
1679 if (rc != OK)
8e669ac1 1680 {
fe5b5d0b
PH
1681 *basic_errno = verrno;
1682 if (smtp_return_error_details)
1683 {
1684 if (*user_msgptr == NULL && *log_msgptr != NULL)
1685 *user_msgptr = string_sprintf("Rejected after DATA: %s", *log_msgptr);
1686 if (rc == DEFER) acl_temp_details = TRUE;
1687 }
8e669ac1 1688 }
059ec3d9
PH
1689 }
1690
1691/* Handle a sender address. The default is to verify *the* sender address, but
1692optionally a different address can be given, for special requirements. If the
1693address is empty, we are dealing with a bounce message that has no sender, so
1694we cannot do any checking. If the real sender address gets rewritten during
1695verification (e.g. DNS widening), set the flag to stop it being rewritten again
1696during message reception.
1697
1698A list of verified "sender" addresses is kept to try to avoid doing to much
1699work repetitively when there are multiple recipients in a message and they all
1700require sender verification. However, when callouts are involved, it gets too
1701complicated because different recipients may require different callout options.
1702Therefore, we always do a full sender verify when any kind of callout is
1703specified. Caching elsewhere, for instance in the DNS resolver and in the
1704callout handling, should ensure that this is not terribly inefficient. */
1705
1706else if (verify_sender_address != NULL)
1707 {
1708 if ((verify_options & (vopt_callout_recipsender|vopt_callout_recippmaster))
1709 != 0)
1710 {
1711 *log_msgptr = US"use_sender or use_postmaster cannot be used for a "
1712 "sender verify callout";
1713 return ERROR;
1714 }
1715
1716 sender_vaddr = verify_checked_sender(verify_sender_address);
1717 if (sender_vaddr != NULL && /* Previously checked */
1718 callout <= 0) /* No callout needed this time */
1719 {
1720 /* If the "routed" flag is set, it means that routing worked before, so
1721 this check can give OK (the saved return code value, if set, belongs to a
1722 callout that was done previously). If the "routed" flag is not set, routing
1723 must have failed, so we use the saved return code. */
1724
1725 if (testflag(sender_vaddr, af_verify_routed)) rc = OK; else
1726 {
1727 rc = sender_vaddr->special_action;
1728 *basic_errno = sender_vaddr->basic_errno;
1729 }
1730 HDEBUG(D_acl) debug_printf("using cached sender verify result\n");
1731 }
1732
1733 /* Do a new verification, and cache the result. The cache is used to avoid
1734 verifying the sender multiple times for multiple RCPTs when callouts are not
1735 specified (see comments above).
1736
1737 The cache is also used on failure to give details in response to the first
1738 RCPT that gets bounced for this reason. However, this can be suppressed by
1739 the no_details option, which sets the flag that says "this detail has already
1740 been sent". The cache normally contains just one address, but there may be
1741 more in esoteric circumstances. */
1742
1743 else
1744 {
1745 BOOL routed = TRUE;
2a3eea10 1746 uschar *save_address_data = deliver_address_data;
8e669ac1 1747
059ec3d9
PH
1748 sender_vaddr = deliver_make_addr(verify_sender_address, TRUE);
1749 if (no_details) setflag(sender_vaddr, af_sverify_told);
1750 if (verify_sender_address[0] != 0)
1751 {
1752 /* If this is the real sender address, save the unrewritten version
1753 for use later in receive. Otherwise, set a flag so that rewriting the
1754 sender in verify_address() does not update sender_address. */
1755
1756 if (verify_sender_address == sender_address)
1757 sender_address_unrewritten = sender_address;
1758 else
1759 verify_options |= vopt_fake_sender;
1760
eafd343b
TK
1761 if (success_on_redirect)
1762 verify_options |= vopt_success_on_redirect;
1763
059ec3d9
PH
1764 /* The recipient, qualify, and expn options are never set in
1765 verify_options. */
1766
1767 rc = verify_address(sender_vaddr, NULL, verify_options, callout,
4deaf07d 1768 callout_overall, callout_connect, se_mailfrom, pm_mailfrom, &routed);
059ec3d9
PH
1769
1770 HDEBUG(D_acl) debug_printf("----------- end verify ------------\n");
1771
1772 if (rc == OK)
1773 {
1774 if (Ustrcmp(sender_vaddr->address, verify_sender_address) != 0)
1775 {
1776 DEBUG(D_acl) debug_printf("sender %s verified ok as %s\n",
1777 verify_sender_address, sender_vaddr->address);
1778 }
1779 else
1780 {
1781 DEBUG(D_acl) debug_printf("sender %s verified ok\n",
1782 verify_sender_address);
1783 }
1784 }
1785 else *basic_errno = sender_vaddr->basic_errno;
1786 }
1787 else rc = OK; /* Null sender */
1788
1789 /* Cache the result code */
1790
1791 if (routed) setflag(sender_vaddr, af_verify_routed);
1792 if (callout > 0) setflag(sender_vaddr, af_verify_callout);
1793 sender_vaddr->special_action = rc;
1794 sender_vaddr->next = sender_verified_list;
1795 sender_verified_list = sender_vaddr;
8e669ac1
PH
1796
1797 /* Restore the recipient address data, which might have been clobbered by
2a3eea10 1798 the sender verification. */
8e669ac1 1799
2a3eea10 1800 deliver_address_data = save_address_data;
059ec3d9 1801 }
8e669ac1 1802
2a3eea10
PH
1803 /* Put the sender address_data value into $sender_address_data */
1804
8e669ac1 1805 sender_address_data = sender_vaddr->p.address_data;
059ec3d9
PH
1806 }
1807
1808/* A recipient address just gets a straightforward verify; again we must handle
1809the DEFER overrides. */
1810
1811else
1812 {
1813 address_item addr2;
1814
eafd343b
TK
1815 if (success_on_redirect)
1816 verify_options |= vopt_success_on_redirect;
1817
059ec3d9
PH
1818 /* We must use a copy of the address for verification, because it might
1819 get rewritten. */
1820
1821 addr2 = *addr;
1822 rc = verify_address(&addr2, NULL, verify_options|vopt_is_recipient, callout,
4deaf07d 1823 callout_overall, callout_connect, se_mailfrom, pm_mailfrom, NULL);
059ec3d9 1824 HDEBUG(D_acl) debug_printf("----------- end verify ------------\n");
8e669ac1 1825
059ec3d9 1826 *log_msgptr = addr2.message;
8e669ac1 1827 *user_msgptr = (addr2.user_message != NULL)?
6729cf78 1828 addr2.user_message : addr2.message;
059ec3d9
PH
1829 *basic_errno = addr2.basic_errno;
1830
1831 /* Make $address_data visible */
1832 deliver_address_data = addr2.p.address_data;
1833 }
1834
1835/* We have a result from the relevant test. Handle defer overrides first. */
1836
1837if (rc == DEFER && (defer_ok ||
1838 (callout_defer_ok && *basic_errno == ERRNO_CALLOUTDEFER)))
1839 {
1840 HDEBUG(D_acl) debug_printf("verify defer overridden by %s\n",
1841 defer_ok? "defer_ok" : "callout_defer_ok");
1842 rc = OK;
1843 }
1844
1845/* If we've failed a sender, set up a recipient message, and point
1846sender_verified_failed to the address item that actually failed. */
1847
1848if (rc != OK && verify_sender_address != NULL)
1849 {
1850 if (rc != DEFER)
1851 {
1852 *log_msgptr = *user_msgptr = US"Sender verify failed";
1853 }
1854 else if (*basic_errno != ERRNO_CALLOUTDEFER)
1855 {
1856 *log_msgptr = *user_msgptr = US"Could not complete sender verify";
1857 }
1858 else
1859 {
1860 *log_msgptr = US"Could not complete sender verify callout";
1861 *user_msgptr = smtp_return_error_details? sender_vaddr->user_message :
1862 *log_msgptr;
1863 }
1864
1865 sender_verified_failed = sender_vaddr;
1866 }
1867
1868/* Verifying an address messes up the values of $domain and $local_part,
1869so reset them before returning if this is a RCPT ACL. */
1870
1871if (addr != NULL)
1872 {
1873 deliver_domain = addr->domain;
1874 deliver_localpart = addr->local_part;
1875 }
1876return rc;
1877
1878/* Syntax errors in the verify argument come here. */
1879
1880BAD_VERIFY:
1881*log_msgptr = string_sprintf("expected \"sender[=address]\", \"recipient\", "
596875b3
PH
1882 "\"helo\", \"header_syntax\", \"header_sender\" or "
1883 "\"reverse_host_lookup\" at start of ACL condition "
059ec3d9
PH
1884 "\"verify %s\"", arg);
1885return ERROR;
596875b3
PH
1886
1887/* Options supplied when not allowed come here */
1888
1889NO_OPTIONS:
1890*log_msgptr = string_sprintf("unexpected '/' found in \"%s\" "
1891 "(this verify item has no options)", arg);
1892return ERROR;
1c41c9cc
PH
1893
1894/* Calls in the wrong ACL come here */
1895
1896WRONG_ACL:
1897*log_msgptr = string_sprintf("cannot check header contents in ACL for %s "
1898 "(only possible in ACL for DATA)", acl_wherenames[where]);
1899return ERROR;
059ec3d9
PH
1900}
1901
1902
1903
1904
1905/*************************************************
1906* Check argument for control= modifier *
1907*************************************************/
1908
1909/* Called from acl_check_condition() below
1910
1911Arguments:
1912 arg the argument string for control=
1913 pptr set to point to the terminating character
1914 where which ACL we are in
1915 log_msgptr for error messages
1916
1917Returns: CONTROL_xxx value
1918*/
1919
1920static int
1921decode_control(uschar *arg, uschar **pptr, int where, uschar **log_msgptr)
1922{
1923int len;
1924control_def *d;
1925
1926for (d = controls_list;
1927 d < controls_list + sizeof(controls_list)/sizeof(control_def);
1928 d++)
1929 {
1930 len = Ustrlen(d->name);
1931 if (Ustrncmp(d->name, arg, len) == 0) break;
1932 }
1933
1934if (d >= controls_list + sizeof(controls_list)/sizeof(control_def) ||
1935 (arg[len] != 0 && (!d->has_option || arg[len] != '/')))
1936 {
1937 *log_msgptr = string_sprintf("syntax error in \"control=%s\"", arg);
1938 return CONTROL_ERROR;
1939 }
1940
059ec3d9
PH
1941*pptr = arg + len;
1942return d->value;
1943}
1944
1945
1946
1947/*************************************************
870f6ba8
TF
1948* Handle rate limiting *
1949*************************************************/
1950
1951/* Called by acl_check_condition() below to calculate the result
1952of the ACL ratelimit condition.
1953
1954Note that the return value might be slightly unexpected: if the
1955sender's rate is above the limit then the result is OK. This is
1956similar to the dnslists condition, and is so that you can write
1957ACL clauses like: defer ratelimit = 15 / 1h
1958
1959Arguments:
1960 arg the option string for ratelimit=
1961 log_msgptr for error messages
1962
1963Returns: OK - Sender's rate is above limit
1964 FAIL - Sender's rate is below limit
1965 DEFER - Problem opening ratelimit database
1966 ERROR - Syntax error in options.
1967*/
1968
1969static int
1970acl_ratelimit(uschar *arg, uschar **log_msgptr)
1971{
1972double limit, period;
f3dcf4a2 1973uschar *ss, *key;
870f6ba8
TF
1974int sep = '/';
1975BOOL have_key = FALSE, leaky = FALSE, strict = FALSE;
1976BOOL per_byte = FALSE, per_cmd = FALSE, per_conn = FALSE, per_mail = FALSE;
1977int old_pool, rc;
1978tree_node **anchor, *t;
1979open_db dbblock, *dbm;
1980dbdata_ratelimit *dbd;
1981struct timeval tv;
1982
1983/* Parse the first two options and record their values in expansion
1984variables. These variables allow the configuration to have informative
1985error messages based on rate limits obtained from a table lookup. */
1986
1987/* First is the maximum number of messages per period and maximum burst
1988size, which must be greater than or equal to zero. Zero is useful for
1989rate measurement as opposed to rate limiting. */
1990
1991sender_rate_limit = string_nextinlist(&arg, &sep, NULL, 0);
1992if (sender_rate_limit == NULL)
1993 limit = -1.0;
1994else
1995 {
1996 limit = Ustrtod(sender_rate_limit, &ss);
1997 if (tolower(*ss) == 'k') { limit *= 1024.0; ss++; }
1998 else if (tolower(*ss) == 'm') { limit *= 1024.0*1024.0; ss++; }
1999 else if (tolower(*ss) == 'g') { limit *= 1024.0*1024.0*1024.0; ss++; }
2000 }
2001if (limit < 0.0 || *ss != 0)
2002 {
2003 *log_msgptr = string_sprintf("syntax error in argument for "
2004 "\"ratelimit\" condition: \"%s\" is not a positive number",
2005 sender_rate_limit);
2006 return ERROR;
2007 }
2008
f3dcf4a2
TF
2009/* We use the rest of the argument list following the limit as the
2010lookup key, because it doesn't make sense to use the same stored data
2011if the period or options are different. */
2012
2013key = arg;
2014
870f6ba8
TF
2015/* Second is the rate measurement period and exponential smoothing time
2016constant. This must be strictly greater than zero, because zero leads to
2017run-time division errors. */
2018
2019sender_rate_period = string_nextinlist(&arg, &sep, NULL, 0);
2020if (sender_rate_period == NULL) period = -1.0;
2021else period = readconf_readtime(sender_rate_period, 0, FALSE);
2022if (period <= 0.0)
2023 {
2024 *log_msgptr = string_sprintf("syntax error in argument for "
2025 "\"ratelimit\" condition: \"%s\" is not a time value",
2026 sender_rate_period);
2027 return ERROR;
2028 }
2029
2030/* Parse the other options. Should we check if the per_* options are being
2031used in ACLs where they don't make sense, e.g. per_mail in the connect ACL? */
2032
2033while ((ss = string_nextinlist(&arg, &sep, big_buffer, big_buffer_size))
2034 != NULL)
2035 {
2036 if (strcmpic(ss, US"leaky") == 0) leaky = TRUE;
2037 else if (strcmpic(ss, US"strict") == 0) strict = TRUE;
2038 else if (strcmpic(ss, US"per_byte") == 0) per_byte = TRUE;
2039 else if (strcmpic(ss, US"per_cmd") == 0) per_cmd = TRUE;
2040 else if (strcmpic(ss, US"per_conn") == 0) per_conn = TRUE;
2041 else if (strcmpic(ss, US"per_mail") == 0) per_mail = TRUE;
2042 else if (strcmpic(ss, US"per_rcpt") == 0) per_cmd = TRUE; /* alias */
2043 else have_key = TRUE;
2044 }
2045if (leaky + strict > 1 || per_byte + per_cmd + per_conn + per_mail > 1)
2046 {
2047 *log_msgptr = US"conflicting options for \"ratelimit\" condition";
2048 return ERROR;
2049 }
2050
2051/* Default option values */
2052if (!strict) leaky = TRUE;
2053if (!per_byte && !per_cmd && !per_conn) per_mail = TRUE;
2054
f3dcf4a2 2055/* If there is no explicit key, use the sender_host_address. If there is no
870f6ba8
TF
2056sender_host_address (e.g. -bs or acl_not_smtp) then we simply omit it. */
2057
2058if (!have_key && sender_host_address != NULL)
2059 key = string_sprintf("%s / %s", key, sender_host_address);
2060
2061HDEBUG(D_acl) debug_printf("ratelimit condition limit=%.0f period=%.0f key=%s\n",
2062 limit, period, key);
2063
fe0dab11 2064/* See if we have already computed the rate by looking in the relevant tree. For
870f6ba8
TF
2065per-connection rate limiting, store tree nodes and dbdata in the permanent pool
2066so that they survive across resets. */
2067
2068anchor = NULL;
2069old_pool = store_pool;
2070
2071if (per_conn)
2072 {
2073 anchor = &ratelimiters_conn;
2074 store_pool = POOL_PERM;
2075 }
fe0dab11 2076else if (per_mail || per_byte)
870f6ba8 2077 anchor = &ratelimiters_mail;
fe0dab11
TF
2078else if (per_cmd)
2079 anchor = &ratelimiters_cmd;
870f6ba8
TF
2080
2081if (anchor != NULL && (t = tree_search(*anchor, key)) != NULL)
2082 {
2083 dbd = t->data.ptr;
2084 /* The following few lines duplicate some of the code below. */
3348576f
TF
2085 if (dbd->rate < limit) rc = FAIL;
2086 else rc = OK;
870f6ba8
TF
2087 store_pool = old_pool;
2088 sender_rate = string_sprintf("%.1f", dbd->rate);
2089 HDEBUG(D_acl)
2090 debug_printf("ratelimit found pre-computed rate %s\n", sender_rate);
2091 return rc;
2092 }
2093
2094/* We aren't using a pre-computed rate, so get a previously recorded
2095rate from the database, update it, and write it back. If there's no
2096previous rate for this key, create one. */
2097
2098dbm = dbfn_open(US"ratelimit", O_RDWR, &dbblock, TRUE);
2099if (dbm == NULL)
2100 {
2101 store_pool = old_pool;
2102 sender_rate = NULL;
2103 HDEBUG(D_acl) debug_printf("ratelimit database not available\n");
2104 *log_msgptr = US"ratelimit database not available";
2105 return DEFER;
2106 }
2107dbd = dbfn_read(dbm, key);
2108
2109gettimeofday(&tv, NULL);
2110
2111if (dbd == NULL)
2112 {
2113 HDEBUG(D_acl) debug_printf("ratelimit initializing new key's data\n");
2114 dbd = store_get(sizeof(dbdata_ratelimit));
2115 dbd->time_stamp = tv.tv_sec;
2116 dbd->time_usec = tv.tv_usec;
2117 dbd->rate = 0.0;
2118 }
2119else
2120 {
2121 /* The smoothed rate is computed using an exponentially weighted moving
2122 average adjusted for variable sampling intervals. The standard EWMA for
2123 a fixed sampling interval is: f'(t) = (1 - a) * f(t) + a * f'(t - 1)
2124 where f() is the measured value and f'() is the smoothed value.
2125
2126 Old data decays out of the smoothed value exponentially, such that data n
2127 samples old is multiplied by a^n. The exponential decay time constant p
2128 is defined such that data p samples old is multiplied by 1/e, which means
2129 that a = exp(-1/p). We can maintain the same time constant for a variable
2130 sampling interval i by using a = exp(-i/p).
2131
2132 The rate we are measuring is messages per period, suitable for directly
2133 comparing with the limit. The average rate between now and the previous
2134 message is period / interval, which we feed into the EWMA as the sample.
2135
2136 It turns out that the number of messages required for the smoothed rate
2137 to reach the limit when they are sent in a burst is equal to the limit.
2138 This can be seen by analysing the value of the smoothed rate after N
2139 messages sent at even intervals. Let k = (1 - a) * p/i
2140
2141 rate_1 = (1 - a) * p/i + a * rate_0
2142 = k + a * rate_0
2143 rate_2 = k + a * rate_1
2144 = k + a * k + a^2 * rate_0
2145 rate_3 = k + a * k + a^2 * k + a^3 * rate_0
2146 rate_N = rate_0 * a^N + k * SUM(x=0..N-1)(a^x)
2147 = rate_0 * a^N + k * (1 - a^N) / (1 - a)
2148 = rate_0 * a^N + p/i * (1 - a^N)
2149
2150 When N is large, a^N -> 0 so rate_N -> p/i as desired.
2151
2152 rate_N = p/i + (rate_0 - p/i) * a^N
2153 a^N = (rate_N - p/i) / (rate_0 - p/i)
2154 N * -i/p = log((rate_N - p/i) / (rate_0 - p/i))
2155 N = p/i * log((rate_0 - p/i) / (rate_N - p/i))
2156
2157 Numerical analysis of the above equation, setting the computed rate to
2158 increase from rate_0 = 0 to rate_N = limit, shows that for large sending
2159 rates, p/i, the number of messages N = limit. So limit serves as both the
2160 maximum rate measured in messages per period, and the maximum number of
2161 messages that can be sent in a fast burst. */
2162
2163 double this_time = (double)tv.tv_sec
2164 + (double)tv.tv_usec / 1000000.0;
2165 double prev_time = (double)dbd->time_stamp
2166 + (double)dbd->time_usec / 1000000.0;
870f6ba8
TF
2167
2168 /* We must avoid division by zero, and deal gracefully with the clock going
2169 backwards. If we blunder ahead when time is in reverse then the computed
e5d5a95f 2170 rate will be bogus. To be safe we clamp interval to a very small number. */
870f6ba8 2171
e5d5a95f
TF
2172 double interval = this_time - prev_time <= 0.0 ? 1e-9
2173 : this_time - prev_time;
2174
2175 double i_over_p = interval / period;
2176 double a = exp(-i_over_p);
870f6ba8
TF
2177
2178 dbd->time_stamp = tv.tv_sec;
2179 dbd->time_usec = tv.tv_usec;
2180
2181 /* If we are measuring the rate in bytes per period, multiply the
2182 measured rate by the message size. If we don't know the message size
2183 then it's safe to just use a value of zero and let the recorded rate
2184 decay as if nothing happened. */
2185
2186 if (per_byte)
2187 dbd->rate = (message_size < 0 ? 0.0 : (double)message_size)
2188 * (1 - a) / i_over_p + a * dbd->rate;
2189 else
2190 dbd->rate = (1 - a) / i_over_p + a * dbd->rate;
2191 }
2192
3348576f
TF
2193/* Clients sending at the limit are considered to be over the limit. This
2194matters for edge cases such the first message sent by a client (which gets
2195the initial rate of 0.0) when the rate limit is zero (i.e. the client should
2196be completely blocked). */
2197
2198if (dbd->rate < limit) rc = FAIL;
2199 else rc = OK;
870f6ba8
TF
2200
2201/* Update the state if the rate is low or if we are being strict. If we
2202are in leaky mode and the sender's rate is too high, we do not update
2203the recorded rate in order to avoid an over-aggressive sender's retry
2204rate preventing them from getting any email through. */
2205
2206if (rc == FAIL || !leaky)
2207 dbfn_write(dbm, key, dbd, sizeof(dbdata_ratelimit));
2208dbfn_close(dbm);
2209
2210/* Store the result in the tree for future reference, if necessary. */
2211
2212if (anchor != NULL)
2213 {
2214 t = store_get(sizeof(tree_node) + Ustrlen(key));
2215 t->data.ptr = dbd;
2216 Ustrcpy(t->name, key);
2217 (void)tree_insertnode(anchor, t);
2218 }
2219
2220/* We create the formatted version of the sender's rate very late in
2221order to ensure that it is done using the correct storage pool. */
2222
2223store_pool = old_pool;
2224sender_rate = string_sprintf("%.1f", dbd->rate);
2225
2226HDEBUG(D_acl)
2227 debug_printf("ratelimit computed rate %s\n", sender_rate);
2228
2229return rc;
2230}
2231
2232
2233
2234/*************************************************
059ec3d9
PH
2235* Handle conditions/modifiers on an ACL item *
2236*************************************************/
2237
2238/* Called from acl_check() below.
2239
2240Arguments:
2241 verb ACL verb
2242 cb ACL condition block - if NULL, result is OK
2243 where where called from
2244 addr the address being checked for RCPT, or NULL
2245 level the nesting level
2246 epp pointer to pass back TRUE if "endpass" encountered
2247 (applies only to "accept" and "discard")
2248 user_msgptr user message pointer
2249 log_msgptr log message pointer
2250 basic_errno pointer to where to put verify error
2251
2252Returns: OK - all conditions are met
2253 DISCARD - an "acl" condition returned DISCARD - only allowed
2254 for "accept" or "discard" verbs
2255 FAIL - at least one condition fails
2256 FAIL_DROP - an "acl" condition returned FAIL_DROP
2257 DEFER - can't tell at the moment (typically, lookup defer,
2258 but can be temporary callout problem)
2259 ERROR - ERROR from nested ACL or expansion failure or other
2260 error
2261*/
2262
2263static int
2264acl_check_condition(int verb, acl_condition_block *cb, int where,
2265 address_item *addr, int level, BOOL *epp, uschar **user_msgptr,
2266 uschar **log_msgptr, int *basic_errno)
2267{
2268uschar *user_message = NULL;
2269uschar *log_message = NULL;
2270uschar *p;
2271int rc = OK;
8523533c
TK
2272#ifdef WITH_CONTENT_SCAN
2273int sep = '/';
2274#endif
059ec3d9
PH
2275
2276for (; cb != NULL; cb = cb->next)
2277 {
2278 uschar *arg;
8e669ac1 2279 int control_type;
059ec3d9
PH
2280
2281 /* The message and log_message items set up messages to be used in
2282 case of rejection. They are expanded later. */
2283
2284 if (cb->type == ACLC_MESSAGE)
2285 {
2286 user_message = cb->arg;
2287 continue;
2288 }
2289
2290 if (cb->type == ACLC_LOG_MESSAGE)
2291 {
2292 log_message = cb->arg;
2293 continue;
2294 }
2295
2296 /* The endpass "condition" just sets a flag to show it occurred. This is
2297 checked at compile time to be on an "accept" or "discard" item. */
2298
2299 if (cb->type == ACLC_ENDPASS)
2300 {
2301 *epp = TRUE;
2302 continue;
2303 }
2304
2305 /* For other conditions and modifiers, the argument is expanded now for some
2306 of them, but not for all, because expansion happens down in some lower level
2307 checking functions in some cases. */
2308
2309 if (cond_expand_at_top[cb->type])
2310 {
2311 arg = expand_string(cb->arg);
2312 if (arg == NULL)
2313 {
2314 if (expand_string_forcedfail) continue;
2315 *log_msgptr = string_sprintf("failed to expand ACL string \"%s\": %s",
2316 cb->arg, expand_string_message);
2317 return search_find_defer? DEFER : ERROR;
2318 }
2319 }
2320 else arg = cb->arg;
2321
2322 /* Show condition, and expanded condition if it's different */
2323
2324 HDEBUG(D_acl)
2325 {
2326 int lhswidth = 0;
2327 debug_printf("check %s%s %n",
2328 (!cond_modifiers[cb->type] && cb->u.negated)? "!":"",
2329 conditions[cb->type], &lhswidth);
2330
2331 if (cb->type == ACLC_SET)
2332 {
2333 int n = cb->u.varnumber;
2334 int t = (n < ACL_C_MAX)? 'c' : 'm';
2335 if (n >= ACL_C_MAX) n -= ACL_C_MAX;
2336 debug_printf("acl_%c%d ", t, n);
2337 lhswidth += 7;
2338 }
2339
2340 debug_printf("= %s\n", cb->arg);
2341
2342 if (arg != cb->arg)
2343 debug_printf("%.*s= %s\n", lhswidth,
2344 US" ", CS arg);
2345 }
2346
2347 /* Check that this condition makes sense at this time */
2348
2349 if ((cond_forbids[cb->type] & (1 << where)) != 0)
2350 {
2351 *log_msgptr = string_sprintf("cannot %s %s condition in %s ACL",
2352 cond_modifiers[cb->type]? "use" : "test",
2353 conditions[cb->type], acl_wherenames[where]);
2354 return ERROR;
2355 }
2356
2357 /* Run the appropriate test for each condition, or take the appropriate
2358 action for the remaining modifiers. */
2359
2360 switch(cb->type)
2361 {
2362 /* A nested ACL that returns "discard" makes sense only for an "accept" or
2363 "discard" verb. */
2364
2365 case ACLC_ACL:
2366 rc = acl_check_internal(where, addr, arg, level+1, user_msgptr, log_msgptr);
2367 if (rc == DISCARD && verb != ACL_ACCEPT && verb != ACL_DISCARD)
2368 {
2369 *log_msgptr = string_sprintf("nested ACL returned \"discard\" for "
2370 "\"%s\" command (only allowed with \"accept\" or \"discard\")",
2371 verbs[verb]);
2372 return ERROR;
2373 }
2374 break;
2375
2376 case ACLC_AUTHENTICATED:
2377 rc = (sender_host_authenticated == NULL)? FAIL :
2378 match_isinlist(sender_host_authenticated, &arg, 0, NULL, NULL, MCL_STRING,
2379 TRUE, NULL);
2380 break;
2381
8523533c
TK
2382#ifdef EXPERIMENTAL_BRIGHTMAIL
2383 case ACLC_BMI_OPTIN:
2384 {
2385 int old_pool = store_pool;
2386 store_pool = POOL_PERM;
2387 bmi_current_optin = string_copy(arg);
2388 store_pool = old_pool;
2389 }
2390 break;
2391#endif
2392
059ec3d9
PH
2393 case ACLC_CONDITION:
2394 if (Ustrspn(arg, "0123456789") == Ustrlen(arg)) /* Digits, or empty */
2395 rc = (Uatoi(arg) == 0)? FAIL : OK;
2396 else
2397 rc = (strcmpic(arg, US"no") == 0 ||
2398 strcmpic(arg, US"false") == 0)? FAIL :
2399 (strcmpic(arg, US"yes") == 0 ||
2400 strcmpic(arg, US"true") == 0)? OK : DEFER;
2401 if (rc == DEFER)
2402 *log_msgptr = string_sprintf("invalid \"condition\" value \"%s\"", arg);
2403 break;
2404
2405 case ACLC_CONTROL:
c5fcb476
PH
2406 control_type = decode_control(arg, &p, where, log_msgptr);
2407
8523533c 2408 /* Check if this control makes sense at this time */
c5fcb476
PH
2409
2410 if ((control_forbids[control_type] & (1 << where)) != 0)
2411 {
2412 *log_msgptr = string_sprintf("cannot use \"control=%s\" in %s ACL",
2413 controls[control_type], acl_wherenames[where]);
2414 return ERROR;
8e669ac1 2415 }
c5fcb476
PH
2416
2417 switch(control_type)
059ec3d9 2418 {
8523533c
TK
2419#ifdef EXPERIMENTAL_BRIGHTMAIL
2420 case CONTROL_BMI_RUN:
2421 bmi_run = 1;
2422 break;
2423#endif
fb2274d4
TK
2424#ifdef EXPERIMENTAL_DOMAINKEYS
2425 case CONTROL_DK_VERIFY:
2426 dk_do_verify = 1;
2427 break;
2428#endif
059ec3d9
PH
2429 case CONTROL_ERROR:
2430 return ERROR;
2431
2432 case CONTROL_CASEFUL_LOCAL_PART:
2433 deliver_localpart = addr->cc_local_part;
2434 break;
2435
2436 case CONTROL_CASELOWER_LOCAL_PART:
2437 deliver_localpart = addr->lc_local_part;
2438 break;
2439
2440 case CONTROL_ENFORCE_SYNC:
2441 smtp_enforce_sync = TRUE;
2442 break;
2443
2444 case CONTROL_NO_ENFORCE_SYNC:
2445 smtp_enforce_sync = FALSE;
2446 break;
2447
8523533c
TK
2448#ifdef WITH_CONTENT_SCAN
2449 case CONTROL_NO_MBOX_UNSPOOL:
2450 no_mbox_unspool = TRUE;
2451 break;
2452#endif
2453
059ec3d9
PH
2454 case CONTROL_NO_MULTILINE:
2455 no_multiline_responses = TRUE;
2456 break;
2457
29aba418 2458 case CONTROL_FAKEDEFER:
8523533c 2459 case CONTROL_FAKEREJECT:
29aba418 2460 fake_response = (control_type == CONTROL_FAKEDEFER) ? DEFER : FAIL;
8523533c 2461 if (*p == '/')
8e669ac1 2462 {
8523533c 2463 uschar *pp = p + 1;
8e669ac1 2464 while (*pp != 0) pp++;
29aba418 2465 fake_response_text = expand_string(string_copyn(p+1, pp-p-1));
8523533c
TK
2466 p = pp;
2467 }
2468 else
2469 {
2470 /* Explicitly reset to default string */
29aba418 2471 fake_response_text = US"Your message has been rejected but is being kept for evaluation.\nIf it was a legitimate message, it may still be delivered to the target recipient(s).";
8523533c
TK
2472 }
2473 break;
8523533c 2474
059ec3d9
PH
2475 case CONTROL_FREEZE:
2476 deliver_freeze = TRUE;
2477 deliver_frozen_at = time(NULL);
2478 break;
2479
2480 case CONTROL_QUEUE_ONLY:
2481 queue_only_policy = TRUE;
2482 break;
2483
2484 case CONTROL_SUBMISSION:
87ba3f5f 2485 originator_name = US"";
059ec3d9 2486 submission_mode = TRUE;
69358f02 2487 while (*p == '/')
8e669ac1 2488 {
69358f02
PH
2489 if (Ustrncmp(p, "/sender_retain", 14) == 0)
2490 {
2491 p += 14;
2492 active_local_sender_retain = TRUE;
8e669ac1
PH
2493 active_local_from_check = FALSE;
2494 }
69358f02
PH
2495 else if (Ustrncmp(p, "/domain=", 8) == 0)
2496 {
2497 uschar *pp = p + 8;
8e669ac1 2498 while (*pp != 0 && *pp != '/') pp++;
87ba3f5f
PH
2499 submission_domain = string_copyn(p+8, pp-p-8);
2500 p = pp;
2501 }
2502 else if (Ustrncmp(p, "/name=", 6) == 0)
2503 {
2504 uschar *pp = p + 6;
2505 while (*pp != 0 && *pp != '/') pp++;
2fe1a124 2506 submission_name = string_copy(parse_fix_phrase(p+6, pp-p-6,
87ba3f5f 2507 big_buffer, big_buffer_size));
8e669ac1 2508 p = pp;
69358f02 2509 }
8e669ac1
PH
2510 else break;
2511 }
69358f02 2512 if (*p != 0)
059ec3d9 2513 {
69358f02 2514 *log_msgptr = string_sprintf("syntax error in \"control=%s\"", arg);
059ec3d9
PH
2515 return ERROR;
2516 }
2517 break;
8800895a
PH
2518
2519 case CONTROL_SUPPRESS_LOCAL_FIXUPS:
2520 suppress_local_fixups = TRUE;
2521 break;
059ec3d9
PH
2522 }
2523 break;
2524
8523533c
TK
2525#ifdef WITH_CONTENT_SCAN
2526 case ACLC_DECODE:
2527 rc = mime_decode(&arg);
2528 break;
2529#endif
2530
059ec3d9
PH
2531 case ACLC_DELAY:
2532 {
2533 int delay = readconf_readtime(arg, 0, FALSE);
2534 if (delay < 0)
2535 {
2536 *log_msgptr = string_sprintf("syntax error in argument for \"delay\" "
2537 "modifier: \"%s\" is not a time value", arg);
2538 return ERROR;
2539 }
2540 else
2541 {
2542 HDEBUG(D_acl) debug_printf("delay modifier requests %d-second delay\n",
2543 delay);
2544 if (host_checking)
2545 {
2546 HDEBUG(D_acl)
2547 debug_printf("delay skipped in -bh checking mode\n");
2548 }
010c2d14
PH
2549
2550 /* It appears to be impossible to detect that a TCP/IP connection has
2551 gone away without reading from it. This means that we cannot shorten
2552 the delay below if the client goes away, because we cannot discover
2553 that the client has closed its end of the connection. (The connection
2554 is actually in a half-closed state, waiting for the server to close its
2555 end.) It would be nice to be able to detect this state, so that the
2556 Exim process is not held up unnecessarily. However, it seems that we
2557 can't. The poll() function does not do the right thing, and in any case
2558 it is not always available.
2559
2560 NOTE: If ever this state of affairs changes, remember that we may be
2561 dealing with stdin/stdout here, in addition to TCP/IP connections.
2562 Whatever is done must work in both cases. To detected the stdin/stdout
2563 case, check for smtp_in or smtp_out being NULL. */
2564
8e669ac1 2565 else
86b8287f
PH
2566 {
2567 while (delay > 0) delay = sleep(delay);
8e669ac1 2568 }
059ec3d9
PH
2569 }
2570 }
2571 break;
2572
8523533c
TK
2573#ifdef WITH_OLD_DEMIME
2574 case ACLC_DEMIME:
2575 rc = demime(&arg);
2576 break;
2577#endif
2578
fb2274d4
TK
2579#ifdef EXPERIMENTAL_DOMAINKEYS
2580 case ACLC_DK_DOMAIN_SOURCE:
2581 if (dk_verify_block == NULL) { rc = FAIL; break; };
2582 /* check header source of domain against given string */
2583 switch (dk_verify_block->address_source) {
2584 case DK_EXIM_ADDRESS_FROM_FROM:
2585 rc = match_isinlist(US"from", &arg, 0, NULL,
2586 NULL, MCL_STRING, TRUE, NULL);
2587 break;
2588 case DK_EXIM_ADDRESS_FROM_SENDER:
2589 rc = match_isinlist(US"sender", &arg, 0, NULL,
2590 NULL, MCL_STRING, TRUE, NULL);
2591 break;
2592 case DK_EXIM_ADDRESS_NONE:
2593 rc = match_isinlist(US"none", &arg, 0, NULL,
2594 NULL, MCL_STRING, TRUE, NULL);
2595 break;
2596 }
2597 break;
2598 case ACLC_DK_POLICY:
2599 if (dk_verify_block == NULL) { rc = FAIL; break; };
2600 /* check policy against given string, default FAIL */
2601 rc = FAIL;
2602 if (dk_verify_block->signsall)
2603 rc = match_isinlist(US"signsall", &arg, 0, NULL,
2604 NULL, MCL_STRING, TRUE, NULL);
2605 if (dk_verify_block->testing)
2606 rc = match_isinlist(US"testing", &arg, 0, NULL,
2607 NULL, MCL_STRING, TRUE, NULL);
2608 break;
2609 case ACLC_DK_SENDER_DOMAINS:
2610 if (dk_verify_block == NULL) { rc = FAIL; break; };
2611 if (dk_verify_block->domain != NULL)
2612 rc = match_isinlist(dk_verify_block->domain, &arg, 0, &domainlist_anchor,
2613 NULL, MCL_DOMAIN, TRUE, NULL);
2614 else rc = FAIL;
2615 break;
2616 case ACLC_DK_SENDER_LOCAL_PARTS:
2617 if (dk_verify_block == NULL) { rc = FAIL; break; };
2618 if (dk_verify_block->local_part != NULL)
2619 rc = match_isinlist(dk_verify_block->local_part, &arg, 0, &localpartlist_anchor,
2620 NULL, MCL_LOCALPART, TRUE, NULL);
2621 else rc = FAIL;
2622 break;
2623 case ACLC_DK_SENDERS:
2624 if (dk_verify_block == NULL) { rc = FAIL; break; };
2625 if (dk_verify_block->address != NULL)
2626 rc = match_address_list(dk_verify_block->address, TRUE, TRUE, &arg, NULL, -1, 0, NULL);
2627 else rc = FAIL;
2628 break;
2629 case ACLC_DK_STATUS:
2630 if (dk_verify_block == NULL) { rc = FAIL; break; };
2631 if (dk_verify_block->result > 0) {
2632 switch(dk_verify_block->result) {
2633 case DK_EXIM_RESULT_BAD_FORMAT:
2634 rc = match_isinlist(US"bad format", &arg, 0, NULL,
2635 NULL, MCL_STRING, TRUE, NULL);
2636 break;
2637 case DK_EXIM_RESULT_NO_KEY:
2638 rc = match_isinlist(US"no key", &arg, 0, NULL,
2639 NULL, MCL_STRING, TRUE, NULL);
2640 break;
2641 case DK_EXIM_RESULT_NO_SIGNATURE:
2642 rc = match_isinlist(US"no signature", &arg, 0, NULL,
2643 NULL, MCL_STRING, TRUE, NULL);
2644 break;
2645 case DK_EXIM_RESULT_REVOKED:
2646 rc = match_isinlist(US"revoked", &arg, 0, NULL,
2647 NULL, MCL_STRING, TRUE, NULL);
2648 break;
2649 case DK_EXIM_RESULT_NON_PARTICIPANT:
2650 rc = match_isinlist(US"non-participant", &arg, 0, NULL,
2651 NULL, MCL_STRING, TRUE, NULL);
2652 break;
2653 case DK_EXIM_RESULT_GOOD:
2654 rc = match_isinlist(US"good", &arg, 0, NULL,
2655 NULL, MCL_STRING, TRUE, NULL);
2656 break;
2657 case DK_EXIM_RESULT_BAD:
2658 rc = match_isinlist(US"bad", &arg, 0, NULL,
2659 NULL, MCL_STRING, TRUE, NULL);
2660 break;
2661 }
2662 }
2663 break;
2664#endif
2665
059ec3d9
PH
2666 case ACLC_DNSLISTS:
2667 rc = verify_check_dnsbl(&arg);
2668 break;
2669
2670 case ACLC_DOMAINS:
2671 rc = match_isinlist(addr->domain, &arg, 0, &domainlist_anchor,
2672 addr->domain_cache, MCL_DOMAIN, TRUE, &deliver_domain_data);
2673 break;
2674
2675 /* The value in tls_cipher is the full cipher name, for example,
2676 TLSv1:DES-CBC3-SHA:168, whereas the values to test for are just the
2677 cipher names such as DES-CBC3-SHA. But program defensively. We don't know
2678 what may in practice come out of the SSL library - which at the time of
2679 writing is poorly documented. */
2680
2681 case ACLC_ENCRYPTED:
2682 if (tls_cipher == NULL) rc = FAIL; else
2683 {
2684 uschar *endcipher = NULL;
2685 uschar *cipher = Ustrchr(tls_cipher, ':');
2686 if (cipher == NULL) cipher = tls_cipher; else
2687 {
2688 endcipher = Ustrchr(++cipher, ':');
2689 if (endcipher != NULL) *endcipher = 0;
2690 }
2691 rc = match_isinlist(cipher, &arg, 0, NULL, NULL, MCL_STRING, TRUE, NULL);
2692 if (endcipher != NULL) *endcipher = ':';
2693 }
2694 break;
2695
2696 /* Use verify_check_this_host() instead of verify_check_host() so that
2697 we can pass over &host_data to catch any looked up data. Once it has been
2698 set, it retains its value so that it's still there if another ACL verb
2699 comes through here and uses the cache. However, we must put it into
2700 permanent store in case it is also expected to be used in a subsequent
2701 message in the same SMTP connection. */
2702
2703 case ACLC_HOSTS:
2704 rc = verify_check_this_host(&arg, sender_host_cache, NULL,
2705 (sender_host_address == NULL)? US"" : sender_host_address, &host_data);
2706 if (host_data != NULL) host_data = string_copy_malloc(host_data);
2707 break;
2708
2709 case ACLC_LOCAL_PARTS:
2710 rc = match_isinlist(addr->cc_local_part, &arg, 0,
2711 &localpartlist_anchor, addr->localpart_cache, MCL_LOCALPART, TRUE,
2712 &deliver_localpart_data);
2713 break;
2714
2715 case ACLC_LOGWRITE:
2716 {
2717 int logbits = 0;
2718 uschar *s = arg;
2719 if (*s == ':')
2720 {
2721 s++;
2722 while (*s != ':')
2723 {
2724 if (Ustrncmp(s, "main", 4) == 0)
2725 { logbits |= LOG_MAIN; s += 4; }
2726 else if (Ustrncmp(s, "panic", 5) == 0)
2727 { logbits |= LOG_PANIC; s += 5; }
2728 else if (Ustrncmp(s, "reject", 6) == 0)
2729 { logbits |= LOG_REJECT; s += 6; }
2730 else
2731 {
2732 logbits = LOG_MAIN|LOG_PANIC;
2733 s = string_sprintf(":unknown log name in \"%s\" in "
2734 "\"logwrite\" in %s ACL", arg, acl_wherenames[where]);
2735 }
2736 if (*s == ',') s++;
2737 }
2738 s++;
2739 }
2740 while (isspace(*s)) s++;
2741 if (logbits == 0) logbits = LOG_MAIN;
2742 log_write(0, logbits, "%s", string_printing(s));
2743 }
2744 break;
8e669ac1 2745
8523533c
TK
2746#ifdef WITH_CONTENT_SCAN
2747 case ACLC_MALWARE:
2748 {
2749 /* Seperate the regular expression and any optional parameters. */
2750 uschar *ss = string_nextinlist(&arg, &sep, big_buffer, big_buffer_size);
2751 /* Run the malware backend. */
2752 rc = malware(&ss);
2753 /* Modify return code based upon the existance of options. */
2754 while ((ss = string_nextinlist(&arg, &sep, big_buffer, big_buffer_size))
2755 != NULL) {
2756 if (strcmpic(ss, US"defer_ok") == 0 && rc == DEFER)
2757 {
2758 /* FAIL so that the message is passed to the next ACL */
2759 rc = FAIL;
2760 }
2761 }
2762 }
2763 break;
2764
2765 case ACLC_MIME_REGEX:
2766 rc = mime_regex(&arg);
2767 break;
2768#endif
059ec3d9 2769
870f6ba8
TF
2770 case ACLC_RATELIMIT:
2771 rc = acl_ratelimit(arg, log_msgptr);
2772 break;
2773
059ec3d9
PH
2774 case ACLC_RECIPIENTS:
2775 rc = match_address_list(addr->address, TRUE, TRUE, &arg, NULL, -1, 0,
2776 &recipient_data);
2777 break;
2778
8523533c
TK
2779#ifdef WITH_CONTENT_SCAN
2780 case ACLC_REGEX:
2781 rc = regex(&arg);
2782 break;
2783#endif
2784
059ec3d9
PH
2785 case ACLC_SENDER_DOMAINS:
2786 {
2787 uschar *sdomain;
2788 sdomain = Ustrrchr(sender_address, '@');
2789 sdomain = (sdomain == NULL)? US"" : sdomain + 1;
2790 rc = match_isinlist(sdomain, &arg, 0, &domainlist_anchor,
2791 sender_domain_cache, MCL_DOMAIN, TRUE, NULL);
2792 }
2793 break;
2794
2795 case ACLC_SENDERS:
2796 rc = match_address_list(sender_address, TRUE, TRUE, &arg,
2797 sender_address_cache, -1, 0, &sender_data);
2798 break;
2799
2800 /* Connection variables must persist forever */
2801
2802 case ACLC_SET:
2803 {
2804 int old_pool = store_pool;
2805 if (cb->u.varnumber < ACL_C_MAX) store_pool = POOL_PERM;
2806 acl_var[cb->u.varnumber] = string_copy(arg);
2807 store_pool = old_pool;
2808 }
2809 break;
2810
8523533c
TK
2811#ifdef WITH_CONTENT_SCAN
2812 case ACLC_SPAM:
2813 {
2814 /* Seperate the regular expression and any optional parameters. */
2815 uschar *ss = string_nextinlist(&arg, &sep, big_buffer, big_buffer_size);
2816 /* Run the spam backend. */
2817 rc = spam(&ss);
2818 /* Modify return code based upon the existance of options. */
2819 while ((ss = string_nextinlist(&arg, &sep, big_buffer, big_buffer_size))
2820 != NULL) {
2821 if (strcmpic(ss, US"defer_ok") == 0 && rc == DEFER)
2822 {
2823 /* FAIL so that the message is passed to the next ACL */
2824 rc = FAIL;
2825 }
2826 }
2827 }
2828 break;
2829#endif
2830
2831#ifdef EXPERIMENTAL_SPF
2832 case ACLC_SPF:
2833 rc = spf_process(&arg, sender_address);
2834 break;
2835#endif
2836
059ec3d9
PH
2837 /* If the verb is WARN, discard any user message from verification, because
2838 such messages are SMTP responses, not header additions. The latter come
475fe28a
PH
2839 only from explicit "message" modifiers. However, put the user message into
2840 $acl_verify_message so it can be used in subsequent conditions or modifiers
2841 (until something changes it). */
059ec3d9
PH
2842
2843 case ACLC_VERIFY:
2844 rc = acl_verify(where, addr, arg, user_msgptr, log_msgptr, basic_errno);
475fe28a 2845 acl_verify_message = *user_msgptr;
059ec3d9
PH
2846 if (verb == ACL_WARN) *user_msgptr = NULL;
2847 break;
2848
2849 default:
2850 log_write(0, LOG_MAIN|LOG_PANIC_DIE, "internal ACL error: unknown "
2851 "condition %d", cb->type);
2852 break;
2853 }
2854
2855 /* If a condition was negated, invert OK/FAIL. */
2856
2857 if (!cond_modifiers[cb->type] && cb->u.negated)
2858 {
2859 if (rc == OK) rc = FAIL;
2860 else if (rc == FAIL || rc == FAIL_DROP) rc = OK;
2861 }
2862
2863 if (rc != OK) break; /* Conditions loop */
2864 }
2865
2866
2867/* If the result is the one for which "message" and/or "log_message" are used,
2868handle the values of these options. Most verbs have but a single return for
2869which the messages are relevant, but for "discard", it's useful to have the log
2870message both when it succeeds and when it fails. Also, for an "accept" that
2871appears in a QUIT ACL, we want to handle the user message. Since only "accept"
2872and "warn" are permitted in that ACL, we don't need to test the verb.
2873
2874These modifiers act in different ways:
2875
2876"message" is a user message that will be included in an SMTP response. Unless
2877it is empty, it overrides any previously set user message.
2878
2879"log_message" is a non-user message, and it adds to any existing non-user
2880message that is already set.
2881
2882If there isn't a log message set, we make it the same as the user message. */
2883
2884if (((rc == FAIL_DROP)? FAIL : rc) == msgcond[verb] ||
2885 (verb == ACL_DISCARD && rc == OK) ||
2886 (where == ACL_WHERE_QUIT))
2887 {
2888 uschar *expmessage;
2889
2890 /* If the verb is "warn", messages generated by conditions (verification or
2891 nested ACLs) are discarded. Only messages specified at this level are used.
2892 However, the value of an existing message is available in $acl_verify_message
2893 during expansions. */
2894
2895 uschar *old_user_msgptr = *user_msgptr;
2896 uschar *old_log_msgptr = (*log_msgptr != NULL)? *log_msgptr : old_user_msgptr;
2897
2898 if (verb == ACL_WARN) *log_msgptr = *user_msgptr = NULL;
2899
2900 if (user_message != NULL)
2901 {
2902 acl_verify_message = old_user_msgptr;
2903 expmessage = expand_string(user_message);
2904 if (expmessage == NULL)
2905 {
2906 if (!expand_string_forcedfail)
2907 log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand ACL message \"%s\": %s",
2908 user_message, expand_string_message);
2909 }
2910 else if (expmessage[0] != 0) *user_msgptr = expmessage;
2911 }
2912
2913 if (log_message != NULL)
2914 {
2915 acl_verify_message = old_log_msgptr;
2916 expmessage = expand_string(log_message);
2917 if (expmessage == NULL)
2918 {
2919 if (!expand_string_forcedfail)
2920 log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand ACL message \"%s\": %s",
2921 log_message, expand_string_message);
2922 }
2923 else if (expmessage[0] != 0)
2924 {
2925 *log_msgptr = (*log_msgptr == NULL)? expmessage :
2926 string_sprintf("%s: %s", expmessage, *log_msgptr);
2927 }
2928 }
2929
2930 /* If no log message, default it to the user message */
2931
2932 if (*log_msgptr == NULL) *log_msgptr = *user_msgptr;
2933 }
2934
2935acl_verify_message = NULL;
2936return rc;
2937}
2938
2939
2940
2941
2942
2943/*************************************************
2944* Get line from a literal ACL *
2945*************************************************/
2946
2947/* This function is passed to acl_read() in order to extract individual lines
2948of a literal ACL, which we access via static pointers. We can destroy the
2949contents because this is called only once (the compiled ACL is remembered).
2950
2951This code is intended to treat the data in the same way as lines in the main
2952Exim configuration file. That is:
2953
2954 . Leading spaces are ignored.
2955
2956 . A \ at the end of a line is a continuation - trailing spaces after the \
2957 are permitted (this is because I don't believe in making invisible things
2958 significant). Leading spaces on the continued part of a line are ignored.
2959
2960 . Physical lines starting (significantly) with # are totally ignored, and
2961 may appear within a sequence of backslash-continued lines.
2962
2963 . Blank lines are ignored, but will end a sequence of continuations.
2964
2965Arguments: none
2966Returns: a pointer to the next line
2967*/
2968
2969
2970static uschar *acl_text; /* Current pointer in the text */
2971static uschar *acl_text_end; /* Points one past the terminating '0' */
2972
2973
2974static uschar *
2975acl_getline(void)
2976{
2977uschar *yield;
2978
2979/* This loop handles leading blank lines and comments. */
2980
2981for(;;)
2982 {
2983 while (isspace(*acl_text)) acl_text++; /* Leading spaces/empty lines */
2984 if (*acl_text == 0) return NULL; /* No more data */
2985 yield = acl_text; /* Potential data line */
2986
2987 while (*acl_text != 0 && *acl_text != '\n') acl_text++;
2988
2989 /* If we hit the end before a newline, we have the whole logical line. If
2990 it's a comment, there's no more data to be given. Otherwise, yield it. */
2991
2992 if (*acl_text == 0) return (*yield == '#')? NULL : yield;
2993
2994 /* After reaching a newline, end this loop if the physical line does not
2995 start with '#'. If it does, it's a comment, and the loop continues. */
2996
2997 if (*yield != '#') break;
2998 }
2999
3000/* This loop handles continuations. We know we have some real data, ending in
3001newline. See if there is a continuation marker at the end (ignoring trailing
3002white space). We know that *yield is not white space, so no need to test for
3003cont > yield in the backwards scanning loop. */
3004
3005for(;;)
3006 {
3007 uschar *cont;
3008 for (cont = acl_text - 1; isspace(*cont); cont--);
3009
3010 /* If no continuation follows, we are done. Mark the end of the line and
3011 return it. */
3012
3013 if (*cont != '\\')
3014 {
3015 *acl_text++ = 0;
3016 return yield;
3017 }
3018
3019 /* We have encountered a continuation. Skip over whitespace at the start of
3020 the next line, and indeed the whole of the next line or lines if they are
3021 comment lines. */
3022
3023 for (;;)
3024 {
3025 while (*(++acl_text) == ' ' || *acl_text == '\t');
3026 if (*acl_text != '#') break;
3027 while (*(++acl_text) != 0 && *acl_text != '\n');
3028 }
3029
3030 /* We have the start of a continuation line. Move all the rest of the data
3031 to join onto the previous line, and then find its end. If the end is not a
3032 newline, we are done. Otherwise loop to look for another continuation. */
3033
3034 memmove(cont, acl_text, acl_text_end - acl_text);
3035 acl_text_end -= acl_text - cont;
3036 acl_text = cont;
3037 while (*acl_text != 0 && *acl_text != '\n') acl_text++;
3038 if (*acl_text == 0) return yield;
3039 }
3040
3041/* Control does not reach here */
3042}
3043
3044
3045
3046
3047
3048/*************************************************
3049* Check access using an ACL *
3050*************************************************/
3051
3052/* This function is called from address_check. It may recurse via
3053acl_check_condition() - hence the use of a level to stop looping. The ACL is
3054passed as a string which is expanded. A forced failure implies no access check
3055is required. If the result is a single word, it is taken as the name of an ACL
3056which is sought in the global ACL tree. Otherwise, it is taken as literal ACL
3057text, complete with newlines, and parsed as such. In both cases, the ACL check
3058is then run. This function uses an auxiliary function for acl_read() to call
3059for reading individual lines of a literal ACL. This is acl_getline(), which
3060appears immediately above.
3061
3062Arguments:
3063 where where called from
3064 addr address item when called from RCPT; otherwise NULL
3065 s the input string; NULL is the same as an empty ACL => DENY
3066 level the nesting level
3067 user_msgptr where to put a user error (for SMTP response)
3068 log_msgptr where to put a logging message (not for SMTP response)
3069
3070Returns: OK access is granted
3071 DISCARD access is apparently granted...
3072 FAIL access is denied
3073 FAIL_DROP access is denied; drop the connection
3074 DEFER can't tell at the moment
3075 ERROR disaster
3076*/
3077
3078static int
3079acl_check_internal(int where, address_item *addr, uschar *s, int level,
3080 uschar **user_msgptr, uschar **log_msgptr)
3081{
3082int fd = -1;
3083acl_block *acl = NULL;
3084uschar *acl_name = US"inline ACL";
3085uschar *ss;
3086
3087/* Catch configuration loops */
3088
3089if (level > 20)
3090 {
3091 *log_msgptr = US"ACL nested too deep: possible loop";
3092 return ERROR;
3093 }
3094
3095if (s == NULL)
3096 {
3097 HDEBUG(D_acl) debug_printf("ACL is NULL: implicit DENY\n");
3098 return FAIL;
3099 }
3100
3101/* At top level, we expand the incoming string. At lower levels, it has already
3102been expanded as part of condition processing. */
3103
3104if (level == 0)
3105 {
3106 ss = expand_string(s);
3107 if (ss == NULL)
3108 {
3109 if (expand_string_forcedfail) return OK;
3110 *log_msgptr = string_sprintf("failed to expand ACL string \"%s\": %s", s,
3111 expand_string_message);
3112 return ERROR;
3113 }
3114 }
3115else ss = s;
3116
3117while (isspace(*ss))ss++;
3118
3119/* If we can't find a named ACL, the default is to parse it as an inline one.
3120(Unless it begins with a slash; non-existent files give rise to an error.) */
3121
3122acl_text = ss;
3123
3124/* Handle the case of a string that does not contain any spaces. Look for a
3125named ACL among those read from the configuration, or a previously read file.
3126It is possible that the pointer to the ACL is NULL if the configuration
3127contains a name with no data. If not found, and the text begins with '/',
3128read an ACL from a file, and save it so it can be re-used. */
3129
3130if (Ustrchr(ss, ' ') == NULL)
3131 {
3132 tree_node *t = tree_search(acl_anchor, ss);
3133 if (t != NULL)
3134 {
3135 acl = (acl_block *)(t->data.ptr);
3136 if (acl == NULL)
3137 {
3138 HDEBUG(D_acl) debug_printf("ACL \"%s\" is empty: implicit DENY\n", ss);
3139 return FAIL;
3140 }
3141 acl_name = string_sprintf("ACL \"%s\"", ss);
3142 HDEBUG(D_acl) debug_printf("using ACL \"%s\"\n", ss);
3143 }
3144
3145 else if (*ss == '/')
3146 {
3147 struct stat statbuf;
3148 fd = Uopen(ss, O_RDONLY, 0);
3149 if (fd < 0)
3150 {
3151 *log_msgptr = string_sprintf("failed to open ACL file \"%s\": %s", ss,
3152 strerror(errno));
3153 return ERROR;
3154 }
3155
3156 if (fstat(fd, &statbuf) != 0)
3157 {
3158 *log_msgptr = string_sprintf("failed to fstat ACL file \"%s\": %s", ss,
3159 strerror(errno));
3160 return ERROR;
3161 }
3162
3163 acl_text = store_get(statbuf.st_size + 1);
3164 acl_text_end = acl_text + statbuf.st_size + 1;
3165
3166 if (read(fd, acl_text, statbuf.st_size) != statbuf.st_size)
3167 {
3168 *log_msgptr = string_sprintf("failed to read ACL file \"%s\": %s",
3169 ss, strerror(errno));
3170 return ERROR;
3171 }
3172 acl_text[statbuf.st_size] = 0;
f1e894f3 3173 (void)close(fd);
059ec3d9
PH
3174
3175 acl_name = string_sprintf("ACL \"%s\"", ss);
3176 HDEBUG(D_acl) debug_printf("read ACL from file %s\n", ss);
3177 }
3178 }
3179
3180/* Parse an ACL that is still in text form. If it came from a file, remember it
3181in the ACL tree, having read it into the POOL_PERM store pool so that it
3182persists between multiple messages. */
3183
3184if (acl == NULL)
3185 {
3186 int old_pool = store_pool;
3187 if (fd >= 0) store_pool = POOL_PERM;
3188 acl = acl_read(acl_getline, log_msgptr);
3189 store_pool = old_pool;
3190 if (acl == NULL && *log_msgptr != NULL) return ERROR;
3191 if (fd >= 0)
3192 {
3193 tree_node *t = store_get_perm(sizeof(tree_node) + Ustrlen(ss));
3194 Ustrcpy(t->name, ss);
3195 t->data.ptr = acl;
3196 (void)tree_insertnode(&acl_anchor, t);
3197 }
3198 }
3199
3200/* Now we have an ACL to use. It's possible it may be NULL. */
3201
3202while (acl != NULL)
3203 {
3204 int cond;
3205 int basic_errno = 0;
3206 BOOL endpass_seen = FALSE;
3207
3208 *log_msgptr = *user_msgptr = NULL;
3209 acl_temp_details = FALSE;
3210
3211 if (where == ACL_WHERE_QUIT &&
3212 acl->verb != ACL_ACCEPT &&
3213 acl->verb != ACL_WARN)
3214 {
3215 *log_msgptr = string_sprintf("\"%s\" is not allowed in a QUIT ACL",
3216 verbs[acl->verb]);
3217 return ERROR;
3218 }
3219
3220 HDEBUG(D_acl) debug_printf("processing \"%s\"\n", verbs[acl->verb]);
3221
3222 /* Clear out any search error message from a previous check before testing
3223 this condition. */
3224
3225 search_error_message = NULL;
3226 cond = acl_check_condition(acl->verb, acl->condition, where, addr, level,
3227 &endpass_seen, user_msgptr, log_msgptr, &basic_errno);
3228
3229 /* Handle special returns: DEFER causes a return except on a WARN verb;
3230 ERROR always causes a return. */
3231
3232 switch (cond)
3233 {
3234 case DEFER:
3235 HDEBUG(D_acl) debug_printf("%s: condition test deferred\n", verbs[acl->verb]);
3236 if (basic_errno != ERRNO_CALLOUTDEFER)
3237 {
3238 if (search_error_message != NULL && *search_error_message != 0)
3239 *log_msgptr = search_error_message;
3240 if (smtp_return_error_details) acl_temp_details = TRUE;
3241 }
3242 else
3243 {
3244 acl_temp_details = TRUE;
3245 }
3246 if (acl->verb != ACL_WARN) return DEFER;
3247 break;
3248
3249 default: /* Paranoia */
3250 case ERROR:
3251 HDEBUG(D_acl) debug_printf("%s: condition test error\n", verbs[acl->verb]);
3252 return ERROR;
3253
3254 case OK:
3255 HDEBUG(D_acl) debug_printf("%s: condition test succeeded\n",
3256 verbs[acl->verb]);
3257 break;
3258
3259 case FAIL:
3260 HDEBUG(D_acl) debug_printf("%s: condition test failed\n", verbs[acl->verb]);
3261 break;
3262
3263 /* DISCARD and DROP can happen only from a nested ACL condition, and
3264 DISCARD can happen only for an "accept" or "discard" verb. */
3265
3266 case DISCARD:
3267 HDEBUG(D_acl) debug_printf("%s: condition test yielded \"discard\"\n",
3268 verbs[acl->verb]);
3269 break;
3270
3271 case FAIL_DROP:
3272 HDEBUG(D_acl) debug_printf("%s: condition test yielded \"drop\"\n",
3273 verbs[acl->verb]);
3274 break;
3275 }
3276
3277 /* At this point, cond for most verbs is either OK or FAIL or (as a result of
3278 a nested ACL condition) FAIL_DROP. However, for WARN, cond may be DEFER, and
3279 for ACCEPT and DISCARD, it may be DISCARD after a nested ACL call. */
3280
3281 switch(acl->verb)
3282 {
3283 case ACL_ACCEPT:
3284 if (cond == OK || cond == DISCARD) return cond;
3285 if (endpass_seen)
3286 {
3287 HDEBUG(D_acl) debug_printf("accept: endpass encountered - denying access\n");
3288 return cond;
3289 }
3290 break;
3291
3292 case ACL_DEFER:
3293 if (cond == OK)
3294 {
3295 acl_temp_details = TRUE;
3296 return DEFER;
3297 }
3298 break;
3299
3300 case ACL_DENY:
3301 if (cond == OK) return FAIL;
3302 break;
3303
3304 case ACL_DISCARD:
3305 if (cond == OK || cond == DISCARD) return DISCARD;
3306 if (endpass_seen)
3307 {
3308 HDEBUG(D_acl) debug_printf("discard: endpass encountered - denying access\n");
3309 return cond;
3310 }
3311 break;
3312
3313 case ACL_DROP:
3314 if (cond == OK) return FAIL_DROP;
3315 break;
3316
3317 case ACL_REQUIRE:
3318 if (cond != OK) return cond;
3319 break;
3320
3321 case ACL_WARN:
3322 if (cond == OK)
3323 acl_warn(where, *user_msgptr, *log_msgptr);
3324 else if (cond == DEFER)
9c7a242c
PH
3325 log_write(0, LOG_MAIN, "%s Warning: ACL \"warn\" statement skipped: "
3326 "condition test deferred%s%s", host_and_ident(TRUE),
3327 (*log_msgptr == NULL)? US"" : US": ",
3328 (*log_msgptr == NULL)? US"" : *log_msgptr);
059ec3d9
PH
3329 *log_msgptr = *user_msgptr = NULL; /* In case implicit DENY follows */
3330 break;
3331
3332 default:
3333 log_write(0, LOG_MAIN|LOG_PANIC_DIE, "internal ACL error: unknown verb %d",
3334 acl->verb);
3335 break;
3336 }
3337
3338 /* Pass to the next ACL item */
3339
3340 acl = acl->next;
3341 }
3342
3343/* We have reached the end of the ACL. This is an implicit DENY. */
3344
3345HDEBUG(D_acl) debug_printf("end of %s: implicit DENY\n", acl_name);
3346return FAIL;
3347}
3348
3349
3350/*************************************************
3351* Check access using an ACL *
3352*************************************************/
3353
3354/* This is the external interface for ACL checks. It sets up an address and the
3355expansions for $domain and $local_part when called after RCPT, then calls
3356acl_check_internal() to do the actual work.
3357
3358Arguments:
3359 where ACL_WHERE_xxxx indicating where called from
64ffc24f 3360 recipient RCPT address for RCPT check, else NULL
059ec3d9
PH
3361 s the input string; NULL is the same as an empty ACL => DENY
3362 user_msgptr where to put a user error (for SMTP response)
3363 log_msgptr where to put a logging message (not for SMTP response)
3364
3365Returns: OK access is granted by an ACCEPT verb
3366 DISCARD access is granted by a DISCARD verb
3367 FAIL access is denied
3368 FAIL_DROP access is denied; drop the connection
3369 DEFER can't tell at the moment
3370 ERROR disaster
3371*/
3372
3373int
64ffc24f 3374acl_check(int where, uschar *recipient, uschar *s, uschar **user_msgptr,
059ec3d9
PH
3375 uschar **log_msgptr)
3376{
3377int rc;
3378address_item adb;
64ffc24f 3379address_item *addr = NULL;
059ec3d9
PH
3380
3381*user_msgptr = *log_msgptr = NULL;
3382sender_verified_failed = NULL;
fe0dab11 3383ratelimiters_cmd = NULL;
059ec3d9
PH
3384
3385if (where == ACL_WHERE_RCPT)
3386 {
3387 adb = address_defaults;
3388 addr = &adb;
64ffc24f 3389 addr->address = recipient;
059ec3d9
PH
3390 if (deliver_split_address(addr) == DEFER)
3391 {
3392 *log_msgptr = US"defer in percent_hack_domains check";
3393 return DEFER;
3394 }
3395 deliver_domain = addr->domain;
3396 deliver_localpart = addr->local_part;
3397 }
059ec3d9
PH
3398
3399rc = acl_check_internal(where, addr, s, 0, user_msgptr, log_msgptr);
3400
64ffc24f
PH
3401deliver_domain = deliver_localpart = deliver_address_data =
3402 sender_address_data = NULL;
059ec3d9
PH
3403
3404/* A DISCARD response is permitted only for message ACLs, excluding the PREDATA
3405ACL, which is really in the middle of an SMTP command. */
3406
3407if (rc == DISCARD)
3408 {
3409 if (where > ACL_WHERE_NOTSMTP || where == ACL_WHERE_PREDATA)
3410 {
3411 log_write(0, LOG_MAIN|LOG_PANIC, "\"discard\" verb not allowed in %s "
3412 "ACL", acl_wherenames[where]);
3413 return ERROR;
3414 }
3415 return DISCARD;
3416 }
3417
3418/* A DROP response is not permitted from MAILAUTH */
3419
3420if (rc == FAIL_DROP && where == ACL_WHERE_MAILAUTH)
3421 {
3422 log_write(0, LOG_MAIN|LOG_PANIC, "\"drop\" verb not allowed in %s "
3423 "ACL", acl_wherenames[where]);
3424 return ERROR;
3425 }
3426
3427/* Before giving an error response, take a look at the length of any user
3428message, and split it up into multiple lines if possible. */
3429
3430if (rc != OK && *user_msgptr != NULL && Ustrlen(*user_msgptr) > 75)
3431 {
3432 uschar *s = *user_msgptr = string_copy(*user_msgptr);
3433 uschar *ss = s;
3434
3435 for (;;)
3436 {
3437 int i = 0;
3438 while (i < 75 && *ss != 0 && *ss != '\n') ss++, i++;
3439 if (*ss == 0) break;
3440 if (*ss == '\n')
3441 s = ++ss;
3442 else
3443 {
3444 uschar *t = ss + 1;
3445 uschar *tt = NULL;
3446 while (--t > s + 35)
3447 {
3448 if (*t == ' ')
3449 {
3450 if (t[-1] == ':') { tt = t; break; }
3451 if (tt == NULL) tt = t;
3452 }
3453 }
3454
3455 if (tt == NULL) /* Can't split behind - try ahead */
3456 {
3457 t = ss + 1;
3458 while (*t != 0)
3459 {
3460 if (*t == ' ' || *t == '\n')
3461 { tt = t; break; }
3462 t++;
3463 }
3464 }
3465
3466 if (tt == NULL) break; /* Can't find anywhere to split */
3467 *tt = '\n';
3468 s = ss = tt+1;
3469 }
3470 }
3471 }
3472
3473return rc;
3474}
3475
3476/* End of acl.c */