+
+/* Look up a DNS dmarc record for the given domain. Return it or NULL */
+
+static uschar *
+dmarc_dns_lookup(uschar * dom)
+{
+dns_answer * dnsa = store_get_dns_answer();
+dns_scan dnss;
+int rc = dns_lookup(dnsa, string_sprintf("_dmarc.%s", dom), T_TXT, NULL);
+
+if (rc == DNS_SUCCEED)
+ for (dns_record * rr = dns_next_rr(dnsa, &dnss, RESET_ANSWERS); rr;
+ rr = dns_next_rr(dnsa, &dnss, RESET_NEXT))
+ if (rr->type == T_TXT && rr->size > 3)
+ {
+ uschar *record = string_copyn_taint(US rr->data, rr->size, GET_TAINTED);
+ store_free_dns_answer(dnsa);
+ return record;
+ }
+store_free_dns_answer(dnsa);
+return NULL;
+}
+
+
+static int
+dmarc_write_history_file(const gstring * dkim_history_buffer)
+{
+int history_file_fd = 0;
+ssize_t written_len;
+int tmp_ans;
+u_char ** rua; /* aggregate report addressees */
+gstring * g;
+
+if (!dmarc_history_file)
+ {
+ DEBUG(D_receive) debug_printf_indent("DMARC history file not set\n");
+ return DMARC_HIST_DISABLED;
+ }
+if (!host_checking)
+ {
+ uschar * s = string_copy(dmarc_history_file); /* need a writeable copy */
+ if ((history_file_fd = log_open_as_exim(s)) < 0)
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "failure to create DMARC history file: %s: %s",
+ s, strerror(errno));
+ return DMARC_HIST_FILE_ERR;
+ }
+ }
+
+/* Generate the contents of the history file entry */
+
+g = string_fmt_append(NULL,
+ "job %s\nreporter %s\nreceived %ld\nipaddr %s\nfrom %s\nmfrom %s\n",
+ message_id, primary_hostname, time(NULL), sender_host_address,
+ header_from_sender, expand_string(US"$sender_address_domain"));
+
+if (spf_response)
+ g = string_fmt_append(g, "spf %d\n", dmarc_spf_ares_result);
+
+if (dkim_history_buffer)
+ g = string_fmt_append(g, "%Y", dkim_history_buffer);
+
+g = string_fmt_append(g, "pdomain %s\npolicy %d\n",
+ dmarc_used_domain, dmarc_policy);
+
+if ((rua = opendmarc_policy_fetch_rua(dmarc_pctx, NULL, 0, 1)))
+ for (tmp_ans = 0; rua[tmp_ans]; tmp_ans++)
+ g = string_fmt_append(g, "rua %s\n", rua[tmp_ans]);
+else
+ g = string_catn(g, US"rua -\n", 6);
+
+opendmarc_policy_fetch_pct(dmarc_pctx, &tmp_ans);
+g = string_fmt_append(g, "pct %d\n", tmp_ans);
+
+opendmarc_policy_fetch_adkim(dmarc_pctx, &tmp_ans);
+g = string_fmt_append(g, "adkim %d\n", tmp_ans);
+
+opendmarc_policy_fetch_aspf(dmarc_pctx, &tmp_ans);
+g = string_fmt_append(g, "aspf %d\n", tmp_ans);
+
+opendmarc_policy_fetch_p(dmarc_pctx, &tmp_ans);
+g = string_fmt_append(g, "p %d\n", tmp_ans);
+
+opendmarc_policy_fetch_sp(dmarc_pctx, &tmp_ans);
+g = string_fmt_append(g, "sp %d\n", tmp_ans);
+
+g = string_fmt_append(g, "align_dkim %d\nalign_spf %d\naction %d\n",
+ da, sa, action);
+
+#if DMARC_API >= 100400
+# ifdef EXPERIMENTAL_ARC
+g = arc_dmarc_hist_append(g);
+# else
+g = string_fmt_append(g, "arc %d\narc_policy $d json:[]\n",
+ ARES_RESULT_UNKNOWN, DMARC_ARC_POLICY_RESULT_UNUSED);
+# endif
+#endif
+
+/* Write the contents to the history file */
+DEBUG(D_receive)
+ {
+ debug_printf_indent("DMARC logging history data for opendmarc reporting%s\n",
+ host_checking ? " (not really)" : "");
+ debug_printf_indent("DMARC history data for debugging:\n");
+ expand_level++;
+ debug_printf_indent("%Y", g);
+ expand_level--;
+ }
+
+if (!host_checking)
+ {
+ written_len = write_to_fd_buf(history_file_fd,
+ g->s,
+ gstring_length(g));
+ if (written_len == 0)
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC, "failure to write to DMARC history file: %s",
+ dmarc_history_file);
+ return DMARC_HIST_WRITE_ERR;
+ }
+ (void)close(history_file_fd);
+ }
+return DMARC_HIST_OK;
+}
+
+