1 /* $Cambridge: exim/src/src/acl.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2004 */
8 /* See the file NOTICE for conditions of use and distribution. */
10 /* Code for handling Access Control Lists (ACLs) */
15 /* Default callout timeout */
17 #define CALLOUT_TIMEOUT_DEFAULT 30
19 /* ACL verb codes - keep in step with the table of verbs that follows */
21 enum { ACL_ACCEPT, ACL_DEFER, ACL_DENY, ACL_DISCARD, ACL_DROP, ACL_REQUIRE,
26 static uschar *verbs[] =
27 { US"accept", US"defer", US"deny", US"discard", US"drop", US"require",
30 /* For each verb, the condition for which "message" is used */
32 static int msgcond[] = { FAIL, OK, OK, FAIL, OK, FAIL, OK };
34 /* ACL condition and modifier codes - keep in step with the table that
37 enum { ACLC_ACL, ACLC_AUTHENTICATED, ACLC_CONDITION, ACLC_CONTROL, ACLC_DELAY,
38 ACLC_DNSLISTS, ACLC_DOMAINS, ACLC_ENCRYPTED, ACLC_ENDPASS, ACLC_HOSTS,
39 ACLC_LOCAL_PARTS, ACLC_LOG_MESSAGE, ACLC_LOGWRITE, ACLC_MESSAGE,
40 ACLC_RECIPIENTS, ACLC_SENDER_DOMAINS, ACLC_SENDERS, ACLC_SET, ACLC_VERIFY };
42 /* ACL conditions/modifiers: "delay", "control", "endpass", "message",
43 "log_message", "logwrite", and "set" are modifiers that look like conditions
44 but always return TRUE. They are used for their side effects. */
46 static uschar *conditions[] = { US"acl", US"authenticated", US"condition",
47 US"control", US"delay", US"dnslists", US"domains", US"encrypted",
48 US"endpass", US"hosts", US"local_parts", US"log_message", US"logwrite",
49 US"message", US"recipients", US"sender_domains", US"senders", US"set",
52 /* Flags to indicate for which conditions /modifiers a string expansion is done
53 at the outer level. In the other cases, expansion already occurs in the
54 checking functions. */
56 static uschar cond_expand_at_top[] = {
58 FALSE, /* authenticated */
64 FALSE, /* encrypted */
67 FALSE, /* local_parts */
68 TRUE, /* log_message */
71 FALSE, /* recipients */
72 FALSE, /* sender_domains */
78 /* Flags to identify the modifiers */
80 static uschar cond_modifiers[] = {
82 FALSE, /* authenticated */
83 FALSE, /* condition */
88 FALSE, /* encrypted */
91 FALSE, /* local_parts */
92 TRUE, /* log_message */
95 FALSE, /* recipients */
96 FALSE, /* sender_domains */
102 /* Bit map of which conditions are not allowed at certain times. For each
103 condition, there's a bitmap of dis-allowed times. */
105 static unsigned int cond_forbids[] = {
107 (1<<ACL_WHERE_NOTSMTP)|(1<<ACL_WHERE_CONNECT)| /* authenticated */
111 /* Certain types of control are always allowed, so we let it through
112 always and check in the control processing itself */
116 (1<<ACL_WHERE_NOTSMTP), /* dnslists */
118 (1<<ACL_WHERE_NOTSMTP)|(1<<ACL_WHERE_AUTH)| /* domains */
119 (1<<ACL_WHERE_CONNECT)|(1<<ACL_WHERE_HELO)|
120 (1<<ACL_WHERE_DATA)|(1<<ACL_WHERE_PREDATA)|
121 (1<<ACL_WHERE_ETRN)|(1<<ACL_WHERE_EXPN)|
122 (1<<ACL_WHERE_MAILAUTH)|(1<<ACL_WHERE_QUIT)|
123 (1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_STARTTLS)|
126 (1<<ACL_WHERE_NOTSMTP)|(1<<ACL_WHERE_CONNECT)| /* encrypted */
129 (1<<ACL_WHERE_NOTSMTP), /* hosts */
131 (1<<ACL_WHERE_NOTSMTP)|(1<<ACL_WHERE_AUTH)| /* local_parts */
132 (1<<ACL_WHERE_CONNECT)|(1<<ACL_WHERE_HELO)|
133 (1<<ACL_WHERE_DATA)|(1<<ACL_WHERE_PREDATA)|
134 (1<<ACL_WHERE_ETRN)|(1<<ACL_WHERE_EXPN)|
135 (1<<ACL_WHERE_MAILAUTH)|(1<<ACL_WHERE_QUIT)|
136 (1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_STARTTLS)|
143 (1<<ACL_WHERE_NOTSMTP)|(1<<ACL_WHERE_AUTH)| /* recipients */
144 (1<<ACL_WHERE_CONNECT)|(1<<ACL_WHERE_HELO)|
145 (1<<ACL_WHERE_DATA)|(1<<ACL_WHERE_PREDATA)|
146 (1<<ACL_WHERE_ETRN)|(1<<ACL_WHERE_EXPN)|
147 (1<<ACL_WHERE_MAILAUTH)|(1<<ACL_WHERE_QUIT)|
148 (1<<ACL_WHERE_MAIL)|(1<<ACL_WHERE_STARTTLS)|
151 (1<<ACL_WHERE_AUTH)|(1<<ACL_WHERE_CONNECT)| /* sender_domains */
153 (1<<ACL_WHERE_MAILAUTH)|(1<<ACL_WHERE_QUIT)|
154 (1<<ACL_WHERE_ETRN)|(1<<ACL_WHERE_EXPN)|
155 (1<<ACL_WHERE_STARTTLS)|(1<<ACL_WHERE_VRFY),
157 (1<<ACL_WHERE_AUTH)|(1<<ACL_WHERE_CONNECT)| /* senders */
159 (1<<ACL_WHERE_MAILAUTH)|(1<<ACL_WHERE_QUIT)|
160 (1<<ACL_WHERE_ETRN)|(1<<ACL_WHERE_EXPN)|
161 (1<<ACL_WHERE_STARTTLS)|(1<<ACL_WHERE_VRFY),
165 /* Certain types of verify are always allowed, so we let it through
166 always and check in the verify function itself */
173 /* Return values from decode_control() */
175 enum { CONTROL_ERROR, CONTROL_CASEFUL_LOCAL_PART, CONTROL_CASELOWER_LOCAL_PART,
176 CONTROL_ENFORCE_SYNC, CONTROL_NO_ENFORCE_SYNC, CONTROL_FREEZE,
177 CONTROL_QUEUE_ONLY, CONTROL_SUBMISSION, CONTROL_NO_MULTILINE };
179 /* Structure listing various control arguments, with their characteristics.
180 The maximum "where" value controls the ACLs in which the various controls are
181 permitted to occur. Specifying ACL_WHERE_RCPT limits it to just the RCPT ACL;
182 specifying ACL_WHERE_NOTSMTP limits it to "message" ACLs. */
184 typedef struct control_def {
186 int value; /* CONTROL_xxx value */
187 int where_max; /* Maximum "where" value */
188 BOOL has_option; /* Has /option(s) following */
191 static control_def controls_list[] = {
192 { US"caseful_local_part", CONTROL_CASEFUL_LOCAL_PART,
193 ACL_WHERE_RCPT, FALSE },
194 { US"caselower_local_part", CONTROL_CASELOWER_LOCAL_PART,
195 ACL_WHERE_RCPT, FALSE },
196 { US"enforce_sync", CONTROL_ENFORCE_SYNC,
198 { US"freeze", CONTROL_FREEZE,
199 ACL_WHERE_NOTSMTP, FALSE },
200 { US"no_enforce_sync", CONTROL_NO_ENFORCE_SYNC,
202 { US"no_multiline_responses", CONTROL_NO_MULTILINE,
204 { US"queue_only", CONTROL_QUEUE_ONLY,
205 ACL_WHERE_NOTSMTP, FALSE },
206 { US"submission", CONTROL_SUBMISSION,
207 ACL_WHERE_NOTSMTP, TRUE }
210 /* Enable recursion between acl_check_internal() and acl_check_condition() */
212 static int acl_check_internal(int, address_item *, uschar *, int, uschar **,
216 /*************************************************
217 * Pick out name from list *
218 *************************************************/
220 /* Use a binary chop method
227 Returns: offset in list, or -1 if not found
231 acl_checkname(uschar *name, uschar **list, int end)
237 int mid = (start + end)/2;
238 int c = Ustrcmp(name, list[mid]);
239 if (c == 0) return mid;
240 if (c < 0) end = mid; else start = mid + 1;
247 /*************************************************
248 * Read and parse one ACL *
249 *************************************************/
251 /* This function is called both from readconf in order to parse the ACLs in the
252 configuration file, and also when an ACL is encountered dynamically (e.g. as
253 the result of an expansion). It is given a function to call in order to
254 retrieve the lines of the ACL. This function handles skipping comments and
255 blank lines (where relevant).
258 func function to get next line of ACL
259 error where to put an error message
261 Returns: pointer to ACL, or NULL
262 NULL can be legal (empty ACL); in this case error will be NULL
266 acl_read(uschar *(*func)(void), uschar **error)
268 acl_block *yield = NULL;
269 acl_block **lastp = &yield;
270 acl_block *this = NULL;
271 acl_condition_block *cond;
272 acl_condition_block **condp = NULL;
277 while ((s = (*func)()) != NULL)
280 BOOL negated = FALSE;
281 uschar *saveline = s;
284 /* Conditions (but not verbs) are allowed to be negated by an initial
287 while (isspace(*s)) s++;
294 /* Read the name of a verb or a condition, or the start of a new ACL */
296 s = readconf_readname(name, sizeof(name), s);
299 if (negated || name[0] == 0)
301 *error = string_sprintf("malformed ACL name in \"%s\"", saveline);
307 /* If a verb is unrecognized, it may be another condition or modifier that
308 continues the previous verb. */
310 v = acl_checkname(name, verbs, sizeof(verbs)/sizeof(char *));
315 *error = string_sprintf("unknown ACL verb in \"%s\"", saveline);
326 *error = string_sprintf("malformed ACL line \"%s\"", saveline);
329 this = store_get(sizeof(acl_block));
331 lastp = &(this->next);
334 this->condition = NULL;
335 condp = &(this->condition);
336 if (*s == 0) continue; /* No condition on this line */
342 s = readconf_readname(name, sizeof(name), s); /* Condition name */
345 /* Handle a condition or modifier. */
347 c = acl_checkname(name, conditions, sizeof(conditions)/sizeof(char *));
350 *error = string_sprintf("unknown ACL condition/modifier in \"%s\"",
355 /* The modifiers may not be negated */
357 if (negated && cond_modifiers[c])
359 *error = string_sprintf("ACL error: negation is not allowed with "
360 "\"%s\"", conditions[c]);
364 /* ENDPASS may occur only with ACCEPT or DISCARD. */
366 if (c == ACLC_ENDPASS &&
367 this->verb != ACL_ACCEPT &&
368 this->verb != ACL_DISCARD)
370 *error = string_sprintf("ACL error: \"%s\" is not allowed with \"%s\"",
371 conditions[c], verbs[this->verb]);
375 cond = store_get(sizeof(acl_condition_block));
378 cond->u.negated = negated;
381 condp = &(cond->next);
383 /* The "set" modifier is different in that its argument is "name=value"
384 rather than just a value, and we can check the validity of the name, which
385 gives us a variable number to insert into the data block. */
389 if (Ustrncmp(s, "acl_", 4) != 0 || (s[4] != 'c' && s[4] != 'm') ||
390 !isdigit(s[5]) || (!isspace(s[6]) && s[6] != '='))
392 *error = string_sprintf("unrecognized name after \"set\" in ACL "
393 "modifier \"set %s\"", s);
397 cond->u.varnumber = s[5] - '0';
398 if (s[4] == 'm') cond->u.varnumber += ACL_C_MAX;
400 while (isspace(*s)) s++;
403 /* For "set", we are now positioned for the data. For the others, only
404 "endpass" has no data */
406 if (c != ACLC_ENDPASS)
410 *error = string_sprintf("\"=\" missing after ACL \"%s\" %s", name,
411 cond_modifiers[c]? US"modifier" : US"condition");
414 while (isspace(*s)) s++;
415 cond->arg = string_copy(s);
424 /*************************************************
426 *************************************************/
428 /* This function is called when a WARN verb's conditions are true. It adds to
429 the message's headers, and/or writes information to the log. In each case, this
430 only happens once (per message for headers, per connection for log).
433 where ACL_WHERE_xxxx indicating which ACL this is
434 user_message message for adding to headers
435 log_message message for logging, if different
441 acl_warn(int where, uschar *user_message, uschar *log_message)
445 if (log_message != NULL && log_message != user_message)
450 text = string_sprintf("%s Warning: %s", host_and_ident(TRUE),
451 string_printing(log_message));
453 /* If a sender verification has failed, and the log message is "sender verify
454 failed", add the failure message. */
456 if (sender_verified_failed != NULL &&
457 sender_verified_failed->message != NULL &&
458 strcmpic(log_message, US"sender verify failed") == 0)
459 text = string_sprintf("%s: %s", text, sender_verified_failed->message);
461 /* Search previously logged warnings. They are kept in malloc store so they
462 can be freed at the start of a new message. */
464 for (logged = acl_warn_logged; logged != NULL; logged = logged->next)
465 if (Ustrcmp(logged->text, text) == 0) break;
469 int length = Ustrlen(text) + 1;
470 log_write(0, LOG_MAIN, "%s", text);
471 logged = store_malloc(sizeof(string_item) + length);
472 logged->text = (uschar *)logged + sizeof(string_item);
473 memcpy(logged->text, text, length);
474 logged->next = acl_warn_logged;
475 acl_warn_logged = logged;
479 /* If there's no user message, we are done. */
481 if (user_message == NULL) return;
483 /* If this isn't a message ACL, we can't do anything with a user message.
486 if (where > ACL_WHERE_NOTSMTP)
488 log_write(0, LOG_MAIN|LOG_PANIC, "ACL \"warn\" with \"message\" setting "
489 "found in a non-message (%s) ACL: cannot specify header lines here: "
490 "message ignored", acl_wherenames[where]);
494 /* Treat the user message as a sequence of one or more header lines. */
496 hlen = Ustrlen(user_message);
499 uschar *text, *p, *q;
501 /* Add a final newline if not present */
503 text = ((user_message)[hlen-1] == '\n')? user_message :
504 string_sprintf("%s\n", user_message);
506 /* Loop for multiple header lines, taking care about continuations */
508 for (p = q = text; *p != 0; )
511 int newtype = htype_add_bot;
512 header_line **hptr = &acl_warn_headers;
514 /* Find next header line within the string */
518 q = Ustrchr(q, '\n');
519 if (*(++q) != ' ' && *q != '\t') break;
522 /* If the line starts with a colon, interpret the instruction for where to
523 add it. This temporarily sets up a new type. */
527 if (strncmpic(p, US":after_received:", 16) == 0)
529 newtype = htype_add_rec;
532 else if (strncmpic(p, US":at_start:", 10) == 0)
534 newtype = htype_add_top;
537 else if (strncmpic(p, US":at_end:", 8) == 0)
539 newtype = htype_add_bot;
542 while (*p == ' ' || *p == '\t') p++;
545 /* See if this line starts with a header name, and if not, add X-ACL-Warn:
546 to the front of it. */
548 for (s = p; s < q - 1; s++)
550 if (*s == ':' || !isgraph(*s)) break;
553 s = string_sprintf("%s%.*s", (*s == ':')? "" : "X-ACL-Warn: ", q - p, p);
556 /* See if this line has already been added */
558 while (*hptr != NULL)
560 if (Ustrncmp((*hptr)->text, s, hlen) == 0) break;
561 hptr = &((*hptr)->next);
564 /* Add if not previously present */
568 header_line *h = store_get(sizeof(header_line));
577 /* Advance for next header line within the string */
586 /*************************************************
587 * Verify and check reverse DNS *
588 *************************************************/
590 /* Called from acl_verify() below. We look up the host name(s) of the client IP
591 address if this has not yet been done. The host_name_lookup() function checks
592 that one of these names resolves to an address list that contains the client IP
593 address, so we don't actually have to do the check here.
596 user_msgptr pointer for user message
597 log_msgptr pointer for log message
599 Returns: OK verification condition succeeded
600 FAIL verification failed
601 DEFER there was a problem verifying
605 acl_verify_reverse(uschar **user_msgptr, uschar **log_msgptr)
609 user_msgptr = user_msgptr; /* stop compiler warning */
611 /* Previous success */
613 if (sender_host_name != NULL) return OK;
615 /* Previous failure */
617 if (host_lookup_failed)
619 *log_msgptr = string_sprintf("host lookup failed%s", host_lookup_msg);
623 /* Need to do a lookup */
626 debug_printf("looking up host name to force name/address consistency check\n");
628 if ((rc = host_name_lookup()) != OK)
630 *log_msgptr = (rc == DEFER)?
631 US"host lookup deferred for reverse lookup check"
633 string_sprintf("host lookup failed for reverse lookup check%s",
635 return rc; /* DEFER or FAIL */
638 host_build_sender_fullhost();
644 /*************************************************
645 * Handle verification (address & other) *
646 *************************************************/
648 /* This function implements the "verify" condition. It is called when
649 encountered in any ACL, because some tests are almost always permitted. Some
650 just don't make sense, and always fail (for example, an attempt to test a host
651 lookup for a non-TCP/IP message). Others are restricted to certain ACLs.
654 where where called from
655 addr the recipient address that the ACL is handling, or NULL
656 arg the argument of "verify"
657 user_msgptr pointer for user message
658 log_msgptr pointer for log message
659 basic_errno where to put verify errno
661 Returns: OK verification condition succeeded
662 FAIL verification failed
663 DEFER there was a problem verifying
668 acl_verify(int where, address_item *addr, uschar *arg,
669 uschar **user_msgptr, uschar **log_msgptr, int *basic_errno)
673 int callout_overall = -1;
674 int verify_options = 0;
676 BOOL verify_header_sender = FALSE;
677 BOOL defer_ok = FALSE;
678 BOOL callout_defer_ok = FALSE;
679 BOOL no_details = FALSE;
680 address_item *sender_vaddr = NULL;
681 uschar *verify_sender_address = NULL;
682 uschar *pm_mailfrom = NULL;
683 uschar *se_mailfrom = NULL;
685 uschar *ss = string_nextinlist(&list, &sep, big_buffer, big_buffer_size);
687 if (ss == NULL) goto BAD_VERIFY;
689 /* Handle name/address consistency verification in a separate function. */
691 if (strcmpic(ss, US"reverse_host_lookup") == 0)
693 if (sender_host_address == NULL) return OK;
694 return acl_verify_reverse(user_msgptr, log_msgptr);
697 /* TLS certificate verification is done at STARTTLS time; here we just
698 test whether it was successful or not. (This is for optional verification; for
699 mandatory verification, the connection doesn't last this long.) */
701 if (strcmpic(ss, US"certificate") == 0)
703 if (tls_certificate_verified) return OK;
704 *user_msgptr = US"no verified certificate";
708 /* We can test the result of optional HELO verification */
710 if (strcmpic(ss, US"helo") == 0) return helo_verified? OK : FAIL;
712 /* Handle header verification options - permitted only after DATA or a non-SMTP
715 if (strncmpic(ss, US"header_", 7) == 0)
717 if (where != ACL_WHERE_DATA && where != ACL_WHERE_NOTSMTP)
719 *log_msgptr = string_sprintf("cannot check header contents in ACL for %s "
720 "(only possible in ACL for DATA)", acl_wherenames[where]);
724 /* Check that all relevant header lines have the correct syntax. If there is
725 a syntax error, we return details of the error to the sender if configured to
726 send out full details. (But a "message" setting on the ACL can override, as
729 if (strcmpic(ss+7, US"syntax") == 0)
731 int rc = verify_check_headers(log_msgptr);
732 if (rc != OK && smtp_return_error_details && *log_msgptr != NULL)
733 *user_msgptr = string_sprintf("Rejected after DATA: %s", *log_msgptr);
737 /* Check that there is at least one verifiable sender address in the relevant
738 header lines. This can be followed by callout and defer options, just like
739 sender and recipient. */
741 else if (strcmpic(ss+7, US"sender") == 0) verify_header_sender = TRUE;
743 /* Unknown verify argument starting with "header_" */
745 else goto BAD_VERIFY;
748 /* Otherwise, first item in verify argument must be "sender" or "recipient".
749 In the case of a sender, this can optionally be followed by an address to use
750 in place of the actual sender (rare special-case requirement). */
752 else if (strncmpic(ss, US"sender", 6) == 0)
755 if (where > ACL_WHERE_NOTSMTP)
757 *log_msgptr = string_sprintf("cannot verify sender in ACL for %s "
758 "(only possible for MAIL, RCPT, PREDATA, or DATA)",
759 acl_wherenames[where]);
763 verify_sender_address = sender_address;
766 while (isspace(*s)) s++;
767 if (*s++ != '=') goto BAD_VERIFY;
768 while (isspace(*s)) s++;
769 verify_sender_address = string_copy(s);
774 if (strcmpic(ss, US"recipient") != 0) goto BAD_VERIFY;
777 *log_msgptr = string_sprintf("cannot verify recipient in ACL for %s "
778 "(only possible for RCPT)", acl_wherenames[where]);
783 /* Remaining items are optional */
785 while ((ss = string_nextinlist(&list, &sep, big_buffer, big_buffer_size))
788 if (strcmpic(ss, US"defer_ok") == 0) defer_ok = TRUE;
789 else if (strcmpic(ss, US"no_details") == 0) no_details = TRUE;
791 /* These two old options are left for backwards compatibility */
793 else if (strcmpic(ss, US"callout_defer_ok") == 0)
795 callout_defer_ok = TRUE;
796 if (callout == -1) callout = CALLOUT_TIMEOUT_DEFAULT;
799 else if (strcmpic(ss, US"check_postmaster") == 0)
802 if (callout == -1) callout = CALLOUT_TIMEOUT_DEFAULT;
805 /* The callout option has a number of sub-options, comma separated */
807 else if (strncmpic(ss, US"callout", 7) == 0)
809 callout = CALLOUT_TIMEOUT_DEFAULT;
813 while (isspace(*ss)) ss++;
819 while (isspace(*ss)) ss++;
820 while ((opt = string_nextinlist(&ss, &optsep, buffer, sizeof(buffer)))
823 if (strcmpic(opt, US"defer_ok") == 0) callout_defer_ok = TRUE;
824 else if (strcmpic(opt, US"no_cache") == 0)
825 verify_options |= vopt_callout_no_cache;
826 else if (strcmpic(opt, US"random") == 0)
827 verify_options |= vopt_callout_random;
828 else if (strcmpic(opt, US"use_sender") == 0)
829 verify_options |= vopt_callout_recipsender;
830 else if (strcmpic(opt, US"use_postmaster") == 0)
831 verify_options |= vopt_callout_recippmaster;
832 else if (strcmpic(opt, US"postmaster") == 0) pm_mailfrom = US"";
834 else if (strncmpic(opt, US"mailfrom", 8) == 0)
836 if (!verify_header_sender)
838 *log_msgptr = string_sprintf("\"mailfrom\" is allowed as a "
839 "callout option only for verify=header_sender (detected in ACL "
840 "condition \"%s\")", arg);
844 while (isspace(*opt)) opt++;
847 *log_msgptr = string_sprintf("'=' expected after "
848 "\"mailfrom\" in ACL condition \"%s\"", arg);
851 while (isspace(*opt)) opt++;
852 se_mailfrom = string_copy(opt);
855 else if (strncmpic(opt, US"postmaster_mailfrom", 19) == 0)
858 while (isspace(*opt)) opt++;
861 *log_msgptr = string_sprintf("'=' expected after "
862 "\"postmaster_mailfrom\" in ACL condition \"%s\"", arg);
865 while (isspace(*opt)) opt++;
866 pm_mailfrom = string_copy(opt);
869 else if (strncmpic(opt, US"maxwait", 7) == 0)
872 while (isspace(*opt)) opt++;
875 *log_msgptr = string_sprintf("'=' expected after \"maxwait\" in "
876 "ACL condition \"%s\"", arg);
879 while (isspace(*opt)) opt++;
880 callout_overall = readconf_readtime(opt, 0, FALSE);
881 if (callout_overall < 0)
883 *log_msgptr = string_sprintf("bad time value in ACL condition "
884 "\"verify %s\"", arg);
888 else /* Plain time is callout connect/command timeout */
890 callout = readconf_readtime(opt, 0, FALSE);
893 *log_msgptr = string_sprintf("bad time value in ACL condition "
894 "\"verify %s\"", arg);
902 *log_msgptr = string_sprintf("'=' expected after \"callout\" in "
903 "ACL condition \"%s\"", arg);
909 /* Option not recognized */
913 *log_msgptr = string_sprintf("unknown option \"%s\" in ACL "
914 "condition \"verify %s\"", ss, arg);
919 if ((verify_options & (vopt_callout_recipsender|vopt_callout_recippmaster)) ==
920 (vopt_callout_recipsender|vopt_callout_recippmaster))
922 *log_msgptr = US"only one of use_sender and use_postmaster can be set "
923 "for a recipient callout";
927 /* Handle sender-in-header verification. Default the user message to the log
928 message if giving out verification details. */
930 if (verify_header_sender)
932 rc = verify_check_header_address(user_msgptr, log_msgptr, callout,
933 callout_overall, se_mailfrom, pm_mailfrom, verify_options);
934 if (smtp_return_error_details)
936 if (*user_msgptr == NULL && *log_msgptr != NULL)
937 *user_msgptr = string_sprintf("Rejected after DATA: %s", *log_msgptr);
938 if (rc == DEFER) acl_temp_details = TRUE;
942 /* Handle a sender address. The default is to verify *the* sender address, but
943 optionally a different address can be given, for special requirements. If the
944 address is empty, we are dealing with a bounce message that has no sender, so
945 we cannot do any checking. If the real sender address gets rewritten during
946 verification (e.g. DNS widening), set the flag to stop it being rewritten again
947 during message reception.
949 A list of verified "sender" addresses is kept to try to avoid doing to much
950 work repetitively when there are multiple recipients in a message and they all
951 require sender verification. However, when callouts are involved, it gets too
952 complicated because different recipients may require different callout options.
953 Therefore, we always do a full sender verify when any kind of callout is
954 specified. Caching elsewhere, for instance in the DNS resolver and in the
955 callout handling, should ensure that this is not terribly inefficient. */
957 else if (verify_sender_address != NULL)
959 if ((verify_options & (vopt_callout_recipsender|vopt_callout_recippmaster))
962 *log_msgptr = US"use_sender or use_postmaster cannot be used for a "
963 "sender verify callout";
967 sender_vaddr = verify_checked_sender(verify_sender_address);
968 if (sender_vaddr != NULL && /* Previously checked */
969 callout <= 0) /* No callout needed this time */
971 /* If the "routed" flag is set, it means that routing worked before, so
972 this check can give OK (the saved return code value, if set, belongs to a
973 callout that was done previously). If the "routed" flag is not set, routing
974 must have failed, so we use the saved return code. */
976 if (testflag(sender_vaddr, af_verify_routed)) rc = OK; else
978 rc = sender_vaddr->special_action;
979 *basic_errno = sender_vaddr->basic_errno;
981 HDEBUG(D_acl) debug_printf("using cached sender verify result\n");
984 /* Do a new verification, and cache the result. The cache is used to avoid
985 verifying the sender multiple times for multiple RCPTs when callouts are not
986 specified (see comments above).
988 The cache is also used on failure to give details in response to the first
989 RCPT that gets bounced for this reason. However, this can be suppressed by
990 the no_details option, which sets the flag that says "this detail has already
991 been sent". The cache normally contains just one address, but there may be
992 more in esoteric circumstances. */
997 sender_vaddr = deliver_make_addr(verify_sender_address, TRUE);
998 if (no_details) setflag(sender_vaddr, af_sverify_told);
999 if (verify_sender_address[0] != 0)
1001 /* If this is the real sender address, save the unrewritten version
1002 for use later in receive. Otherwise, set a flag so that rewriting the
1003 sender in verify_address() does not update sender_address. */
1005 if (verify_sender_address == sender_address)
1006 sender_address_unrewritten = sender_address;
1008 verify_options |= vopt_fake_sender;
1010 /* The recipient, qualify, and expn options are never set in
1013 rc = verify_address(sender_vaddr, NULL, verify_options, callout,
1014 callout_overall, se_mailfrom, pm_mailfrom, &routed);
1016 HDEBUG(D_acl) debug_printf("----------- end verify ------------\n");
1020 if (Ustrcmp(sender_vaddr->address, verify_sender_address) != 0)
1022 DEBUG(D_acl) debug_printf("sender %s verified ok as %s\n",
1023 verify_sender_address, sender_vaddr->address);
1027 DEBUG(D_acl) debug_printf("sender %s verified ok\n",
1028 verify_sender_address);
1031 else *basic_errno = sender_vaddr->basic_errno;
1033 else rc = OK; /* Null sender */
1035 /* Cache the result code */
1037 if (routed) setflag(sender_vaddr, af_verify_routed);
1038 if (callout > 0) setflag(sender_vaddr, af_verify_callout);
1039 sender_vaddr->special_action = rc;
1040 sender_vaddr->next = sender_verified_list;
1041 sender_verified_list = sender_vaddr;
1045 /* A recipient address just gets a straightforward verify; again we must handle
1046 the DEFER overrides. */
1052 /* We must use a copy of the address for verification, because it might
1056 rc = verify_address(&addr2, NULL, verify_options|vopt_is_recipient, callout,
1057 callout_overall, se_mailfrom, pm_mailfrom, NULL);
1058 HDEBUG(D_acl) debug_printf("----------- end verify ------------\n");
1059 *log_msgptr = addr2.message;
1060 *user_msgptr = addr2.user_message;
1061 *basic_errno = addr2.basic_errno;
1063 /* Make $address_data visible */
1064 deliver_address_data = addr2.p.address_data;
1067 /* We have a result from the relevant test. Handle defer overrides first. */
1069 if (rc == DEFER && (defer_ok ||
1070 (callout_defer_ok && *basic_errno == ERRNO_CALLOUTDEFER)))
1072 HDEBUG(D_acl) debug_printf("verify defer overridden by %s\n",
1073 defer_ok? "defer_ok" : "callout_defer_ok");
1077 /* If we've failed a sender, set up a recipient message, and point
1078 sender_verified_failed to the address item that actually failed. */
1080 if (rc != OK && verify_sender_address != NULL)
1084 *log_msgptr = *user_msgptr = US"Sender verify failed";
1086 else if (*basic_errno != ERRNO_CALLOUTDEFER)
1088 *log_msgptr = *user_msgptr = US"Could not complete sender verify";
1092 *log_msgptr = US"Could not complete sender verify callout";
1093 *user_msgptr = smtp_return_error_details? sender_vaddr->user_message :
1097 sender_verified_failed = sender_vaddr;
1100 /* Verifying an address messes up the values of $domain and $local_part,
1101 so reset them before returning if this is a RCPT ACL. */
1105 deliver_domain = addr->domain;
1106 deliver_localpart = addr->local_part;
1110 /* Syntax errors in the verify argument come here. */
1113 *log_msgptr = string_sprintf("expected \"sender[=address]\", \"recipient\", "
1114 "\"header_syntax\" or \"header_sender\" at start of ACL condition "
1115 "\"verify %s\"", arg);
1122 /*************************************************
1123 * Check argument for control= modifier *
1124 *************************************************/
1126 /* Called from acl_check_condition() below
1129 arg the argument string for control=
1130 pptr set to point to the terminating character
1131 where which ACL we are in
1132 log_msgptr for error messages
1134 Returns: CONTROL_xxx value
1138 decode_control(uschar *arg, uschar **pptr, int where, uschar **log_msgptr)
1143 for (d = controls_list;
1144 d < controls_list + sizeof(controls_list)/sizeof(control_def);
1147 len = Ustrlen(d->name);
1148 if (Ustrncmp(d->name, arg, len) == 0) break;
1151 if (d >= controls_list + sizeof(controls_list)/sizeof(control_def) ||
1152 (arg[len] != 0 && (!d->has_option || arg[len] != '/')))
1154 *log_msgptr = string_sprintf("syntax error in \"control=%s\"", arg);
1155 return CONTROL_ERROR;
1158 if (where > d->where_max)
1160 *log_msgptr = string_sprintf("cannot use \"control=%s\" in %s ACL",
1161 arg, acl_wherenames[where]);
1162 return CONTROL_ERROR;
1171 /*************************************************
1172 * Handle conditions/modifiers on an ACL item *
1173 *************************************************/
1175 /* Called from acl_check() below.
1179 cb ACL condition block - if NULL, result is OK
1180 where where called from
1181 addr the address being checked for RCPT, or NULL
1182 level the nesting level
1183 epp pointer to pass back TRUE if "endpass" encountered
1184 (applies only to "accept" and "discard")
1185 user_msgptr user message pointer
1186 log_msgptr log message pointer
1187 basic_errno pointer to where to put verify error
1189 Returns: OK - all conditions are met
1190 DISCARD - an "acl" condition returned DISCARD - only allowed
1191 for "accept" or "discard" verbs
1192 FAIL - at least one condition fails
1193 FAIL_DROP - an "acl" condition returned FAIL_DROP
1194 DEFER - can't tell at the moment (typically, lookup defer,
1195 but can be temporary callout problem)
1196 ERROR - ERROR from nested ACL or expansion failure or other
1201 acl_check_condition(int verb, acl_condition_block *cb, int where,
1202 address_item *addr, int level, BOOL *epp, uschar **user_msgptr,
1203 uschar **log_msgptr, int *basic_errno)
1205 uschar *user_message = NULL;
1206 uschar *log_message = NULL;
1210 for (; cb != NULL; cb = cb->next)
1214 /* The message and log_message items set up messages to be used in
1215 case of rejection. They are expanded later. */
1217 if (cb->type == ACLC_MESSAGE)
1219 user_message = cb->arg;
1223 if (cb->type == ACLC_LOG_MESSAGE)
1225 log_message = cb->arg;
1229 /* The endpass "condition" just sets a flag to show it occurred. This is
1230 checked at compile time to be on an "accept" or "discard" item. */
1232 if (cb->type == ACLC_ENDPASS)
1238 /* For other conditions and modifiers, the argument is expanded now for some
1239 of them, but not for all, because expansion happens down in some lower level
1240 checking functions in some cases. */
1242 if (cond_expand_at_top[cb->type])
1244 arg = expand_string(cb->arg);
1247 if (expand_string_forcedfail) continue;
1248 *log_msgptr = string_sprintf("failed to expand ACL string \"%s\": %s",
1249 cb->arg, expand_string_message);
1250 return search_find_defer? DEFER : ERROR;
1255 /* Show condition, and expanded condition if it's different */
1260 debug_printf("check %s%s %n",
1261 (!cond_modifiers[cb->type] && cb->u.negated)? "!":"",
1262 conditions[cb->type], &lhswidth);
1264 if (cb->type == ACLC_SET)
1266 int n = cb->u.varnumber;
1267 int t = (n < ACL_C_MAX)? 'c' : 'm';
1268 if (n >= ACL_C_MAX) n -= ACL_C_MAX;
1269 debug_printf("acl_%c%d ", t, n);
1273 debug_printf("= %s\n", cb->arg);
1276 debug_printf("%.*s= %s\n", lhswidth,
1280 /* Check that this condition makes sense at this time */
1282 if ((cond_forbids[cb->type] & (1 << where)) != 0)
1284 *log_msgptr = string_sprintf("cannot %s %s condition in %s ACL",
1285 cond_modifiers[cb->type]? "use" : "test",
1286 conditions[cb->type], acl_wherenames[where]);
1290 /* Run the appropriate test for each condition, or take the appropriate
1291 action for the remaining modifiers. */
1295 /* A nested ACL that returns "discard" makes sense only for an "accept" or
1299 rc = acl_check_internal(where, addr, arg, level+1, user_msgptr, log_msgptr);
1300 if (rc == DISCARD && verb != ACL_ACCEPT && verb != ACL_DISCARD)
1302 *log_msgptr = string_sprintf("nested ACL returned \"discard\" for "
1303 "\"%s\" command (only allowed with \"accept\" or \"discard\")",
1309 case ACLC_AUTHENTICATED:
1310 rc = (sender_host_authenticated == NULL)? FAIL :
1311 match_isinlist(sender_host_authenticated, &arg, 0, NULL, NULL, MCL_STRING,
1315 case ACLC_CONDITION:
1316 if (Ustrspn(arg, "0123456789") == Ustrlen(arg)) /* Digits, or empty */
1317 rc = (Uatoi(arg) == 0)? FAIL : OK;
1319 rc = (strcmpic(arg, US"no") == 0 ||
1320 strcmpic(arg, US"false") == 0)? FAIL :
1321 (strcmpic(arg, US"yes") == 0 ||
1322 strcmpic(arg, US"true") == 0)? OK : DEFER;
1324 *log_msgptr = string_sprintf("invalid \"condition\" value \"%s\"", arg);
1328 switch (decode_control(arg, &p, where, log_msgptr))
1333 case CONTROL_CASEFUL_LOCAL_PART:
1334 deliver_localpart = addr->cc_local_part;
1337 case CONTROL_CASELOWER_LOCAL_PART:
1338 deliver_localpart = addr->lc_local_part;
1341 case CONTROL_ENFORCE_SYNC:
1342 smtp_enforce_sync = TRUE;
1345 case CONTROL_NO_ENFORCE_SYNC:
1346 smtp_enforce_sync = FALSE;
1349 case CONTROL_NO_MULTILINE:
1350 no_multiline_responses = TRUE;
1353 case CONTROL_FREEZE:
1354 deliver_freeze = TRUE;
1355 deliver_frozen_at = time(NULL);
1358 case CONTROL_QUEUE_ONLY:
1359 queue_only_policy = TRUE;
1362 case CONTROL_SUBMISSION:
1363 submission_mode = TRUE;
1364 if (Ustrncmp(p, "/domain=", 8) == 0)
1366 submission_domain = string_copy(p+8);
1370 *log_msgptr = string_sprintf("syntax error in argument for "
1371 "\"control\" modifier \"%s\"", arg);
1380 int delay = readconf_readtime(arg, 0, FALSE);
1383 *log_msgptr = string_sprintf("syntax error in argument for \"delay\" "
1384 "modifier: \"%s\" is not a time value", arg);
1389 HDEBUG(D_acl) debug_printf("delay modifier requests %d-second delay\n",
1394 debug_printf("delay skipped in -bh checking mode\n");
1402 rc = verify_check_dnsbl(&arg);
1406 rc = match_isinlist(addr->domain, &arg, 0, &domainlist_anchor,
1407 addr->domain_cache, MCL_DOMAIN, TRUE, &deliver_domain_data);
1410 /* The value in tls_cipher is the full cipher name, for example,
1411 TLSv1:DES-CBC3-SHA:168, whereas the values to test for are just the
1412 cipher names such as DES-CBC3-SHA. But program defensively. We don't know
1413 what may in practice come out of the SSL library - which at the time of
1414 writing is poorly documented. */
1416 case ACLC_ENCRYPTED:
1417 if (tls_cipher == NULL) rc = FAIL; else
1419 uschar *endcipher = NULL;
1420 uschar *cipher = Ustrchr(tls_cipher, ':');
1421 if (cipher == NULL) cipher = tls_cipher; else
1423 endcipher = Ustrchr(++cipher, ':');
1424 if (endcipher != NULL) *endcipher = 0;
1426 rc = match_isinlist(cipher, &arg, 0, NULL, NULL, MCL_STRING, TRUE, NULL);
1427 if (endcipher != NULL) *endcipher = ':';
1431 /* Use verify_check_this_host() instead of verify_check_host() so that
1432 we can pass over &host_data to catch any looked up data. Once it has been
1433 set, it retains its value so that it's still there if another ACL verb
1434 comes through here and uses the cache. However, we must put it into
1435 permanent store in case it is also expected to be used in a subsequent
1436 message in the same SMTP connection. */
1439 rc = verify_check_this_host(&arg, sender_host_cache, NULL,
1440 (sender_host_address == NULL)? US"" : sender_host_address, &host_data);
1441 if (host_data != NULL) host_data = string_copy_malloc(host_data);
1444 case ACLC_LOCAL_PARTS:
1445 rc = match_isinlist(addr->cc_local_part, &arg, 0,
1446 &localpartlist_anchor, addr->localpart_cache, MCL_LOCALPART, TRUE,
1447 &deliver_localpart_data);
1459 if (Ustrncmp(s, "main", 4) == 0)
1460 { logbits |= LOG_MAIN; s += 4; }
1461 else if (Ustrncmp(s, "panic", 5) == 0)
1462 { logbits |= LOG_PANIC; s += 5; }
1463 else if (Ustrncmp(s, "reject", 6) == 0)
1464 { logbits |= LOG_REJECT; s += 6; }
1467 logbits = LOG_MAIN|LOG_PANIC;
1468 s = string_sprintf(":unknown log name in \"%s\" in "
1469 "\"logwrite\" in %s ACL", arg, acl_wherenames[where]);
1475 while (isspace(*s)) s++;
1476 if (logbits == 0) logbits = LOG_MAIN;
1477 log_write(0, logbits, "%s", string_printing(s));
1481 case ACLC_RECIPIENTS:
1482 rc = match_address_list(addr->address, TRUE, TRUE, &arg, NULL, -1, 0,
1486 case ACLC_SENDER_DOMAINS:
1489 sdomain = Ustrrchr(sender_address, '@');
1490 sdomain = (sdomain == NULL)? US"" : sdomain + 1;
1491 rc = match_isinlist(sdomain, &arg, 0, &domainlist_anchor,
1492 sender_domain_cache, MCL_DOMAIN, TRUE, NULL);
1497 rc = match_address_list(sender_address, TRUE, TRUE, &arg,
1498 sender_address_cache, -1, 0, &sender_data);
1501 /* Connection variables must persist forever */
1505 int old_pool = store_pool;
1506 if (cb->u.varnumber < ACL_C_MAX) store_pool = POOL_PERM;
1507 acl_var[cb->u.varnumber] = string_copy(arg);
1508 store_pool = old_pool;
1512 /* If the verb is WARN, discard any user message from verification, because
1513 such messages are SMTP responses, not header additions. The latter come
1514 only from explicit "message" modifiers. */
1517 rc = acl_verify(where, addr, arg, user_msgptr, log_msgptr, basic_errno);
1518 if (verb == ACL_WARN) *user_msgptr = NULL;
1522 log_write(0, LOG_MAIN|LOG_PANIC_DIE, "internal ACL error: unknown "
1523 "condition %d", cb->type);
1527 /* If a condition was negated, invert OK/FAIL. */
1529 if (!cond_modifiers[cb->type] && cb->u.negated)
1531 if (rc == OK) rc = FAIL;
1532 else if (rc == FAIL || rc == FAIL_DROP) rc = OK;
1535 if (rc != OK) break; /* Conditions loop */
1539 /* If the result is the one for which "message" and/or "log_message" are used,
1540 handle the values of these options. Most verbs have but a single return for
1541 which the messages are relevant, but for "discard", it's useful to have the log
1542 message both when it succeeds and when it fails. Also, for an "accept" that
1543 appears in a QUIT ACL, we want to handle the user message. Since only "accept"
1544 and "warn" are permitted in that ACL, we don't need to test the verb.
1546 These modifiers act in different ways:
1548 "message" is a user message that will be included in an SMTP response. Unless
1549 it is empty, it overrides any previously set user message.
1551 "log_message" is a non-user message, and it adds to any existing non-user
1552 message that is already set.
1554 If there isn't a log message set, we make it the same as the user message. */
1556 if (((rc == FAIL_DROP)? FAIL : rc) == msgcond[verb] ||
1557 (verb == ACL_DISCARD && rc == OK) ||
1558 (where == ACL_WHERE_QUIT))
1562 /* If the verb is "warn", messages generated by conditions (verification or
1563 nested ACLs) are discarded. Only messages specified at this level are used.
1564 However, the value of an existing message is available in $acl_verify_message
1565 during expansions. */
1567 uschar *old_user_msgptr = *user_msgptr;
1568 uschar *old_log_msgptr = (*log_msgptr != NULL)? *log_msgptr : old_user_msgptr;
1570 if (verb == ACL_WARN) *log_msgptr = *user_msgptr = NULL;
1572 if (user_message != NULL)
1574 acl_verify_message = old_user_msgptr;
1575 expmessage = expand_string(user_message);
1576 if (expmessage == NULL)
1578 if (!expand_string_forcedfail)
1579 log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand ACL message \"%s\": %s",
1580 user_message, expand_string_message);
1582 else if (expmessage[0] != 0) *user_msgptr = expmessage;
1585 if (log_message != NULL)
1587 acl_verify_message = old_log_msgptr;
1588 expmessage = expand_string(log_message);
1589 if (expmessage == NULL)
1591 if (!expand_string_forcedfail)
1592 log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand ACL message \"%s\": %s",
1593 log_message, expand_string_message);
1595 else if (expmessage[0] != 0)
1597 *log_msgptr = (*log_msgptr == NULL)? expmessage :
1598 string_sprintf("%s: %s", expmessage, *log_msgptr);
1602 /* If no log message, default it to the user message */
1604 if (*log_msgptr == NULL) *log_msgptr = *user_msgptr;
1607 acl_verify_message = NULL;
1615 /*************************************************
1616 * Get line from a literal ACL *
1617 *************************************************/
1619 /* This function is passed to acl_read() in order to extract individual lines
1620 of a literal ACL, which we access via static pointers. We can destroy the
1621 contents because this is called only once (the compiled ACL is remembered).
1623 This code is intended to treat the data in the same way as lines in the main
1624 Exim configuration file. That is:
1626 . Leading spaces are ignored.
1628 . A \ at the end of a line is a continuation - trailing spaces after the \
1629 are permitted (this is because I don't believe in making invisible things
1630 significant). Leading spaces on the continued part of a line are ignored.
1632 . Physical lines starting (significantly) with # are totally ignored, and
1633 may appear within a sequence of backslash-continued lines.
1635 . Blank lines are ignored, but will end a sequence of continuations.
1638 Returns: a pointer to the next line
1642 static uschar *acl_text; /* Current pointer in the text */
1643 static uschar *acl_text_end; /* Points one past the terminating '0' */
1651 /* This loop handles leading blank lines and comments. */
1655 while (isspace(*acl_text)) acl_text++; /* Leading spaces/empty lines */
1656 if (*acl_text == 0) return NULL; /* No more data */
1657 yield = acl_text; /* Potential data line */
1659 while (*acl_text != 0 && *acl_text != '\n') acl_text++;
1661 /* If we hit the end before a newline, we have the whole logical line. If
1662 it's a comment, there's no more data to be given. Otherwise, yield it. */
1664 if (*acl_text == 0) return (*yield == '#')? NULL : yield;
1666 /* After reaching a newline, end this loop if the physical line does not
1667 start with '#'. If it does, it's a comment, and the loop continues. */
1669 if (*yield != '#') break;
1672 /* This loop handles continuations. We know we have some real data, ending in
1673 newline. See if there is a continuation marker at the end (ignoring trailing
1674 white space). We know that *yield is not white space, so no need to test for
1675 cont > yield in the backwards scanning loop. */
1680 for (cont = acl_text - 1; isspace(*cont); cont--);
1682 /* If no continuation follows, we are done. Mark the end of the line and
1691 /* We have encountered a continuation. Skip over whitespace at the start of
1692 the next line, and indeed the whole of the next line or lines if they are
1697 while (*(++acl_text) == ' ' || *acl_text == '\t');
1698 if (*acl_text != '#') break;
1699 while (*(++acl_text) != 0 && *acl_text != '\n');
1702 /* We have the start of a continuation line. Move all the rest of the data
1703 to join onto the previous line, and then find its end. If the end is not a
1704 newline, we are done. Otherwise loop to look for another continuation. */
1706 memmove(cont, acl_text, acl_text_end - acl_text);
1707 acl_text_end -= acl_text - cont;
1709 while (*acl_text != 0 && *acl_text != '\n') acl_text++;
1710 if (*acl_text == 0) return yield;
1713 /* Control does not reach here */
1720 /*************************************************
1721 * Check access using an ACL *
1722 *************************************************/
1724 /* This function is called from address_check. It may recurse via
1725 acl_check_condition() - hence the use of a level to stop looping. The ACL is
1726 passed as a string which is expanded. A forced failure implies no access check
1727 is required. If the result is a single word, it is taken as the name of an ACL
1728 which is sought in the global ACL tree. Otherwise, it is taken as literal ACL
1729 text, complete with newlines, and parsed as such. In both cases, the ACL check
1730 is then run. This function uses an auxiliary function for acl_read() to call
1731 for reading individual lines of a literal ACL. This is acl_getline(), which
1732 appears immediately above.
1735 where where called from
1736 addr address item when called from RCPT; otherwise NULL
1737 s the input string; NULL is the same as an empty ACL => DENY
1738 level the nesting level
1739 user_msgptr where to put a user error (for SMTP response)
1740 log_msgptr where to put a logging message (not for SMTP response)
1742 Returns: OK access is granted
1743 DISCARD access is apparently granted...
1744 FAIL access is denied
1745 FAIL_DROP access is denied; drop the connection
1746 DEFER can't tell at the moment
1751 acl_check_internal(int where, address_item *addr, uschar *s, int level,
1752 uschar **user_msgptr, uschar **log_msgptr)
1755 acl_block *acl = NULL;
1756 uschar *acl_name = US"inline ACL";
1759 /* Catch configuration loops */
1763 *log_msgptr = US"ACL nested too deep: possible loop";
1769 HDEBUG(D_acl) debug_printf("ACL is NULL: implicit DENY\n");
1773 /* At top level, we expand the incoming string. At lower levels, it has already
1774 been expanded as part of condition processing. */
1778 ss = expand_string(s);
1781 if (expand_string_forcedfail) return OK;
1782 *log_msgptr = string_sprintf("failed to expand ACL string \"%s\": %s", s,
1783 expand_string_message);
1789 while (isspace(*ss))ss++;
1791 /* If we can't find a named ACL, the default is to parse it as an inline one.
1792 (Unless it begins with a slash; non-existent files give rise to an error.) */
1796 /* Handle the case of a string that does not contain any spaces. Look for a
1797 named ACL among those read from the configuration, or a previously read file.
1798 It is possible that the pointer to the ACL is NULL if the configuration
1799 contains a name with no data. If not found, and the text begins with '/',
1800 read an ACL from a file, and save it so it can be re-used. */
1802 if (Ustrchr(ss, ' ') == NULL)
1804 tree_node *t = tree_search(acl_anchor, ss);
1807 acl = (acl_block *)(t->data.ptr);
1810 HDEBUG(D_acl) debug_printf("ACL \"%s\" is empty: implicit DENY\n", ss);
1813 acl_name = string_sprintf("ACL \"%s\"", ss);
1814 HDEBUG(D_acl) debug_printf("using ACL \"%s\"\n", ss);
1817 else if (*ss == '/')
1819 struct stat statbuf;
1820 fd = Uopen(ss, O_RDONLY, 0);
1823 *log_msgptr = string_sprintf("failed to open ACL file \"%s\": %s", ss,
1828 if (fstat(fd, &statbuf) != 0)
1830 *log_msgptr = string_sprintf("failed to fstat ACL file \"%s\": %s", ss,
1835 acl_text = store_get(statbuf.st_size + 1);
1836 acl_text_end = acl_text + statbuf.st_size + 1;
1838 if (read(fd, acl_text, statbuf.st_size) != statbuf.st_size)
1840 *log_msgptr = string_sprintf("failed to read ACL file \"%s\": %s",
1841 ss, strerror(errno));
1844 acl_text[statbuf.st_size] = 0;
1847 acl_name = string_sprintf("ACL \"%s\"", ss);
1848 HDEBUG(D_acl) debug_printf("read ACL from file %s\n", ss);
1852 /* Parse an ACL that is still in text form. If it came from a file, remember it
1853 in the ACL tree, having read it into the POOL_PERM store pool so that it
1854 persists between multiple messages. */
1858 int old_pool = store_pool;
1859 if (fd >= 0) store_pool = POOL_PERM;
1860 acl = acl_read(acl_getline, log_msgptr);
1861 store_pool = old_pool;
1862 if (acl == NULL && *log_msgptr != NULL) return ERROR;
1865 tree_node *t = store_get_perm(sizeof(tree_node) + Ustrlen(ss));
1866 Ustrcpy(t->name, ss);
1868 (void)tree_insertnode(&acl_anchor, t);
1872 /* Now we have an ACL to use. It's possible it may be NULL. */
1877 int basic_errno = 0;
1878 BOOL endpass_seen = FALSE;
1880 *log_msgptr = *user_msgptr = NULL;
1881 acl_temp_details = FALSE;
1883 if (where == ACL_WHERE_QUIT &&
1884 acl->verb != ACL_ACCEPT &&
1885 acl->verb != ACL_WARN)
1887 *log_msgptr = string_sprintf("\"%s\" is not allowed in a QUIT ACL",
1892 HDEBUG(D_acl) debug_printf("processing \"%s\"\n", verbs[acl->verb]);
1894 /* Clear out any search error message from a previous check before testing
1897 search_error_message = NULL;
1898 cond = acl_check_condition(acl->verb, acl->condition, where, addr, level,
1899 &endpass_seen, user_msgptr, log_msgptr, &basic_errno);
1901 /* Handle special returns: DEFER causes a return except on a WARN verb;
1902 ERROR always causes a return. */
1907 HDEBUG(D_acl) debug_printf("%s: condition test deferred\n", verbs[acl->verb]);
1908 if (basic_errno != ERRNO_CALLOUTDEFER)
1910 if (search_error_message != NULL && *search_error_message != 0)
1911 *log_msgptr = search_error_message;
1912 if (smtp_return_error_details) acl_temp_details = TRUE;
1916 acl_temp_details = TRUE;
1918 if (acl->verb != ACL_WARN) return DEFER;
1921 default: /* Paranoia */
1923 HDEBUG(D_acl) debug_printf("%s: condition test error\n", verbs[acl->verb]);
1927 HDEBUG(D_acl) debug_printf("%s: condition test succeeded\n",
1932 HDEBUG(D_acl) debug_printf("%s: condition test failed\n", verbs[acl->verb]);
1935 /* DISCARD and DROP can happen only from a nested ACL condition, and
1936 DISCARD can happen only for an "accept" or "discard" verb. */
1939 HDEBUG(D_acl) debug_printf("%s: condition test yielded \"discard\"\n",
1944 HDEBUG(D_acl) debug_printf("%s: condition test yielded \"drop\"\n",
1949 /* At this point, cond for most verbs is either OK or FAIL or (as a result of
1950 a nested ACL condition) FAIL_DROP. However, for WARN, cond may be DEFER, and
1951 for ACCEPT and DISCARD, it may be DISCARD after a nested ACL call. */
1956 if (cond == OK || cond == DISCARD) return cond;
1959 HDEBUG(D_acl) debug_printf("accept: endpass encountered - denying access\n");
1967 acl_temp_details = TRUE;
1973 if (cond == OK) return FAIL;
1977 if (cond == OK || cond == DISCARD) return DISCARD;
1980 HDEBUG(D_acl) debug_printf("discard: endpass encountered - denying access\n");
1986 if (cond == OK) return FAIL_DROP;
1990 if (cond != OK) return cond;
1995 acl_warn(where, *user_msgptr, *log_msgptr);
1996 else if (cond == DEFER)
1997 acl_warn(where, NULL, string_sprintf("ACL \"warn\" statement skipped: "
1998 "condition test deferred: %s",
1999 (*log_msgptr == NULL)? US"" : *log_msgptr));
2000 *log_msgptr = *user_msgptr = NULL; /* In case implicit DENY follows */
2004 log_write(0, LOG_MAIN|LOG_PANIC_DIE, "internal ACL error: unknown verb %d",
2009 /* Pass to the next ACL item */
2014 /* We have reached the end of the ACL. This is an implicit DENY. */
2016 HDEBUG(D_acl) debug_printf("end of %s: implicit DENY\n", acl_name);
2021 /*************************************************
2022 * Check access using an ACL *
2023 *************************************************/
2025 /* This is the external interface for ACL checks. It sets up an address and the
2026 expansions for $domain and $local_part when called after RCPT, then calls
2027 acl_check_internal() to do the actual work.
2030 where ACL_WHERE_xxxx indicating where called from
2031 data_string RCPT address, or SMTP command argument, or NULL
2032 s the input string; NULL is the same as an empty ACL => DENY
2033 user_msgptr where to put a user error (for SMTP response)
2034 log_msgptr where to put a logging message (not for SMTP response)
2036 Returns: OK access is granted by an ACCEPT verb
2037 DISCARD access is granted by a DISCARD verb
2038 FAIL access is denied
2039 FAIL_DROP access is denied; drop the connection
2040 DEFER can't tell at the moment
2045 acl_check(int where, uschar *data_string, uschar *s, uschar **user_msgptr,
2046 uschar **log_msgptr)
2052 *user_msgptr = *log_msgptr = NULL;
2053 sender_verified_failed = NULL;
2055 if (where == ACL_WHERE_RCPT)
2057 adb = address_defaults;
2059 addr->address = data_string;
2060 if (deliver_split_address(addr) == DEFER)
2062 *log_msgptr = US"defer in percent_hack_domains check";
2065 deliver_domain = addr->domain;
2066 deliver_localpart = addr->local_part;
2071 smtp_command_argument = data_string;
2074 rc = acl_check_internal(where, addr, s, 0, user_msgptr, log_msgptr);
2076 smtp_command_argument = deliver_domain =
2077 deliver_localpart = deliver_address_data = NULL;
2079 /* A DISCARD response is permitted only for message ACLs, excluding the PREDATA
2080 ACL, which is really in the middle of an SMTP command. */
2084 if (where > ACL_WHERE_NOTSMTP || where == ACL_WHERE_PREDATA)
2086 log_write(0, LOG_MAIN|LOG_PANIC, "\"discard\" verb not allowed in %s "
2087 "ACL", acl_wherenames[where]);
2093 /* A DROP response is not permitted from MAILAUTH */
2095 if (rc == FAIL_DROP && where == ACL_WHERE_MAILAUTH)
2097 log_write(0, LOG_MAIN|LOG_PANIC, "\"drop\" verb not allowed in %s "
2098 "ACL", acl_wherenames[where]);
2102 /* Before giving an error response, take a look at the length of any user
2103 message, and split it up into multiple lines if possible. */
2105 if (rc != OK && *user_msgptr != NULL && Ustrlen(*user_msgptr) > 75)
2107 uschar *s = *user_msgptr = string_copy(*user_msgptr);
2113 while (i < 75 && *ss != 0 && *ss != '\n') ss++, i++;
2114 if (*ss == 0) break;
2121 while (--t > s + 35)
2125 if (t[-1] == ':') { tt = t; break; }
2126 if (tt == NULL) tt = t;
2130 if (tt == NULL) /* Can't split behind - try ahead */
2135 if (*t == ' ' || *t == '\n')
2141 if (tt == NULL) break; /* Can't find anywhere to split */