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