static const char * client_get_log_str(struct client *client, const char *msg) { static const struct var_expand_table static_tab[3] = { { 's', NULL, NULL }, { '$', NULL, NULL }, { '\0', NULL, NULL } }; static const struct var_expand_func_table func_table[] = { { "passdb", client_var_expand_func_passdb }, { NULL, NULL } }; const struct var_expand_table *var_expand_table; struct var_expand_table *tab; char *const *e; string_t *str, *str2; unsigned int pos; var_expand_table = get_var_expand_table(client); tab = t_malloc(sizeof(static_tab)); memcpy(tab, static_tab, sizeof(static_tab)); str = t_str_new(256); str2 = t_str_new(128); for (e = client->set->log_format_elements_split; *e != NULL; e++) { pos = str_len(str); var_expand_with_funcs(str, *e, var_expand_table, func_table, client); if (have_username_key(*e)) { /* username is added even if it's empty */ } else { str_truncate(str2, 0); var_expand(str2, *e, login_var_expand_empty_tab); if (strcmp(str_c(str)+pos, str_c(str2)) == 0) { /* empty %variables, don't add */ str_truncate(str, pos); continue; } } if (str_len(str) > 0) str_append(str, ", "); } if (str_len(str) > 0) str_truncate(str, str_len(str)-2); tab[0].value = t_strdup(str_c(str)); tab[1].value = msg; str_truncate(str, 0); var_expand(str, client->set->login_log_format, tab); return str_c(str); }
const struct var_expand_table * mail_storage_service_get_var_expand_table(struct mail_storage_service_ctx *ctx, struct mail_storage_service_input *input) { struct mail_storage_service_privileges priv; memset(&priv, 0, sizeof(priv)); priv.uid = (uid_t)-1; priv.gid = (gid_t)-1; return get_var_expand_table(ctx->service, NULL, input, &priv); }
static void mail_storage_service_var_expand(struct mail_storage_service_ctx *ctx, string_t *str, const char *format, struct mail_storage_service_user *user, const struct mail_storage_service_input *input, const struct mail_storage_service_privileges *priv) { static const struct var_expand_func_table func_table[] = { { "userdb", mail_storage_service_input_var_userdb }, { NULL, NULL } }; var_expand_with_funcs(str, format, get_var_expand_table(ctx->service, user, input, priv), func_table, user); }
static const char * client_get_log_str(struct client *client, const char *msg) { static struct var_expand_table static_tab[3] = { { 's', NULL, NULL }, { '$', NULL, NULL }, { '\0', NULL, NULL } }; const struct var_expand_table *var_expand_table; struct var_expand_table *tab; const char *p; char *const *e; string_t *str; var_expand_table = get_var_expand_table(client); tab = t_malloc(sizeof(static_tab)); memcpy(tab, static_tab, sizeof(static_tab)); str = t_str_new(256); for (e = client->set->log_format_elements_split; *e != NULL; e++) { for (p = *e; *p != '\0'; p++) { if (*p != '%' || p[1] == '\0') continue; p++; if (have_key(var_expand_table, p)) { if (str_len(str) > 0) str_append(str, ", "); var_expand(str, *e, var_expand_table); break; } } } tab[0].value = t_strdup(str_c(str)); tab[1].value = msg; str_truncate(str, 0); var_expand(str, client->set->login_log_format, tab); return str_c(str); }
int mail_send_rejection(struct mail_deliver_context *ctx, const char *recipient, const char *reason) { struct mail *mail = ctx->src_mail; struct istream *input; struct smtp_client *smtp_client; struct ostream *output; const char *return_addr, *hdr; const char *value, *msgid, *orig_msgid, *boundary; string_t *str; int ret; if (mail_get_first_header(mail, "Message-ID", &orig_msgid) < 0) orig_msgid = NULL; if (mail_get_first_header(mail, "Auto-Submitted", &value) > 0 && strcasecmp(value, "no") != 0) { i_info("msgid=%s: Auto-submitted message discarded: %s", orig_msgid == NULL ? "" : str_sanitize(orig_msgid, 80), str_sanitize(reason, 512)); return 0; } return_addr = mail_deliver_get_return_address(ctx); if (return_addr == NULL) { i_info("msgid=%s: Return-Path missing, rejection reason: %s", orig_msgid == NULL ? "" : str_sanitize(orig_msgid, 80), str_sanitize(reason, 512)); return 0; } if (mailbox_get_settings(mail->box)->mail_debug) { i_debug("Sending a rejection to %s: %s", recipient, str_sanitize(reason, 512)); } smtp_client = smtp_client_open(ctx->set, return_addr, NULL, &output); msgid = mail_deliver_get_new_message_id(ctx); boundary = t_strdup_printf("%s/%s", my_pid, ctx->set->hostname); str = t_str_new(512); str_printfa(str, "Message-ID: %s\r\n", msgid); str_printfa(str, "Date: %s\r\n", message_date_create(ioloop_time)); str_printfa(str, "From: Mail Delivery Subsystem <%s>\r\n", ctx->set->postmaster_address); str_printfa(str, "To: <%s>\r\n", return_addr); str_append(str, "MIME-Version: 1.0\r\n"); str_printfa(str, "Content-Type: " "multipart/report; report-type=%s;\r\n" "\tboundary=\"%s\"\r\n", ctx->dsn ? "delivery-status" : "disposition-notification", boundary); str_append(str, "Subject: "); var_expand(str, ctx->set->rejection_subject, get_var_expand_table(mail, reason, recipient)); str_append(str, "\r\n"); str_append(str, "Auto-Submitted: auto-replied (rejected)\r\n"); str_append(str, "Precedence: bulk\r\n"); str_append(str, "\r\nThis is a MIME-encapsulated message\r\n\r\n"); /* human readable status report */ str_printfa(str, "--%s\r\n", boundary); str_append(str, "Content-Type: text/plain; charset=utf-8\r\n"); str_append(str, "Content-Disposition: inline\r\n"); str_append(str, "Content-Transfer-Encoding: 8bit\r\n\r\n"); var_expand(str, ctx->set->rejection_reason, get_var_expand_table(mail, reason, recipient)); str_append(str, "\r\n"); if (ctx->dsn) { /* DSN status report: For LDA rejects. currently only used when user is out of quota */ str_printfa(str, "--%s\r\n" "Content-Type: message/delivery-status\r\n\r\n", boundary); str_printfa(str, "Reporting-MTA: dns; %s\r\n", ctx->set->hostname); if (mail_get_first_header(mail, "Original-Recipient", &hdr) > 0) str_printfa(str, "Original-Recipient: rfc822; %s\r\n", hdr); str_printfa(str, "Final-Recipient: rfc822; %s\r\n", recipient); str_append(str, "Action: failed\r\n"); str_printfa(str, "Status: %s\r\n", ctx->mailbox_full ? "5.2.2" : "5.2.0"); } else { /* MDN status report: For Sieve "reject" */ str_printfa(str, "--%s\r\n" "Content-Type: message/disposition-notification\r\n\r\n", boundary); str_printfa(str, "Reporting-UA: %s; Dovecot Mail Delivery Agent\r\n", ctx->set->hostname); if (mail_get_first_header(mail, "Original-Recipient", &hdr) > 0) str_printfa(str, "Original-Recipient: rfc822; %s\r\n", hdr); str_printfa(str, "Final-Recipient: rfc822; %s\r\n", recipient); if (orig_msgid != NULL) str_printfa(str, "Original-Message-ID: %s\r\n", orig_msgid); str_append(str, "Disposition: " "automatic-action/MDN-sent-automatically; deleted\r\n"); } str_append(str, "\r\n"); /* original message's headers */ str_printfa(str, "--%s\r\nContent-Type: message/rfc822\r\n\r\n", boundary); o_stream_nsend(output, str_data(str), str_len(str)); if (mail_get_hdr_stream(mail, NULL, &input) == 0) { /* Note: If you add more headers, they need to be sorted. We'll drop Content-Type because we're not including the message body, and having a multipart Content-Type may confuse some MIME parsers when they don't see the message boundaries. */ static const char *const exclude_headers[] = { "Content-Type" }; input = i_stream_create_header_filter(input, HEADER_FILTER_EXCLUDE | HEADER_FILTER_NO_CR | HEADER_FILTER_HIDE_BODY, exclude_headers, N_ELEMENTS(exclude_headers), *null_header_filter_callback, (void *)NULL); ret = o_stream_send_istream(output, input); i_stream_unref(&input); i_assert(ret != 0); } str_truncate(str, 0); str_printfa(str, "\r\n\r\n--%s--\r\n", boundary); o_stream_nsend(output, str_data(str), str_len(str)); return smtp_client_close(smtp_client); }