From f75b60b97e7f3732a6fbd3bea8f8e9db3f049086 Mon Sep 17 00:00:00 2001 From: Taylor R Campbell Date: Sat, 23 Aug 2025 03:09:36 +0000 Subject: [PATCH] local(8), virtual(8): Add support for mboxrd format. Based on a suggestion by Wietse Venema back in 2012: https://marc.info/?l=postfix-users&m=135161915820933&w=2 > A new parameter to select the mailbox type is OK, however the > default setting will remain 100% backwards compatible. The mboxrd format stores messages with `From ' lines escaped reversibly, by also escaping escaped `From ' lines and so on. Thus, unlike the traditional mbox file format, called mboxo, writing mail to the mboxrd format doesn't lose information. However, for compatibility, mboxo remains the default format. This change comes in several parts, with interdependencies between the parts so it's not really useful to split into separate commits other than maybe the postconf(5) documentation update: 1. New MAIL_COPY_QUOTE_REV flag, like MAIL_COPY_QUOTE but instead of prepending `>' to lines matching /^From /, it prepends `>' to lines matching /^>*From /, so it's reversible. 2. Rename MAIL_COPY_MBOX to MAIL_COPY_MBOXO -- so the compiler will confirm I caught every case where it might matter -- and add MAIL_COPY_MBOXRD with MAIL_COPY_QUOTE_REV instead of MAIL_COPY_QUOTE. 3. Implement MAIL_COPY_QUOTE_REV in mail_copy(3). 4. New postconf(5) variables mailbox_file_format, used by local(8), and virtual_mailbox_file_format, used by virtual(8), spread across: - man/man5/postconf.5 (documentation) - src/global/mail_params.h (declaration) - src/global/mail_params.c (definition) These variables can take on any of the following values: - mboxo - mboxrd In principle, support could be added here for other variants like mboxcl or mboxcl2 but this is not on my radar. The default is mboxo, which preserves the traditional behaviour of Postfix. 5. New mbox_format_copy_flags(3) in mbox_format.c/h maps a string like "mboxo" to the appropriate mail_copy(3) flags. 6. Teach local(8) and virtual(8) to set MAIL_COPY_* flags according to the respective config variables with mbox_format_copy_flags(3). No other changes to local(8) and virtual(8) -- almost all the new logic is in mail_copy(3) and mbox_format_copy_flags(3), which are easier to write automatic tests for. 7. New automatic tests for mail_copy(3) using a few sets of options (not exhaustive but all the flags are exercised both on and off, at least). The configuration is per-service, not per-mailbox -- you can't have a .forward file that asks for mboxrd if /etc/postfix/main.cf is set up to use mboxo. This change could be adapted to support that, but aside from the additional implementation complexity, it would require carving out syntax in forward(5) for the matter. This did not strike me as worthwhile. The alternative I considered would be to write a custom local delivery agent. However, this requires copying all the logic that's already in local(8) (and virtual(8)) to handle edge cases like error after partial writes, which are presumably well-tested in postfix already. --- postfix/man/man5/postconf.5 | 26 +++++ postfix/src/global/Makefile.in | 53 ++++++++- postfix/src/global/mail_copy.c | 129 +++++++++++++++++++++- postfix/src/global/mail_copy.h | 7 +- postfix/src/global/mail_copy_test.in | Bin 0 -> 346 bytes postfix/src/global/mail_copy_test.ref1 | 15 +++ postfix/src/global/mail_copy_test.refnone | 12 ++ postfix/src/global/mail_copy_test.refo | 17 +++ postfix/src/global/mail_copy_test.refrd | 17 +++ postfix/src/global/mail_params.h | 11 ++ postfix/src/global/mbox_format.c | 80 ++++++++++++++ postfix/src/global/mbox_format.h | 27 +++++ postfix/src/local/file.c | 2 +- postfix/src/local/local.c | 16 ++- postfix/src/local/local.h | 5 + postfix/src/local/mailbox.c | 2 +- postfix/src/virtual/mailbox.c | 2 +- postfix/src/virtual/virtual.c | 6 + postfix/src/virtual/virtual.h | 5 + 19 files changed, 419 insertions(+), 13 deletions(-) create mode 100644 postfix/src/global/mail_copy_test.in create mode 100644 postfix/src/global/mail_copy_test.ref1 create mode 100644 postfix/src/global/mail_copy_test.refnone create mode 100644 postfix/src/global/mail_copy_test.refo create mode 100644 postfix/src/global/mail_copy_test.refrd create mode 100644 postfix/src/global/mbox_format.c create mode 100644 postfix/src/global/mbox_format.h diff --git a/postfix/man/man5/postconf.5 b/postfix/man/man5/postconf.5 index 22757a2e1..1c05ef8a9 100644 --- a/postfix/man/man5/postconf.5 +++ b/postfix/man/man5/postconf.5 @@ -4132,6 +4132,19 @@ Note: The \fBdotlock\fR method requires that the recipient UID or GID has write access to the parent directory of the mailbox file. .PP Note: the default setting of this parameter is system dependent. +.SH mailbox_file_format (default: mboxo) +The mailbox file format variant to use for UNIX\-style \fBlocal\fR(8) +mailbox delivery. +.PP +Specify one of the following: +.IP "\fBmboxo\fR" +Prepend a > character to each line beginning with "From ". +This is the original mailbox format, but the transformation is +irreversible: you can't tell whether a line originally started with +"From " or ">From ". +.IP "\fBmboxrd\fR" +Prepend a > character to each line beginning with "From ", ">From ", +">>From ", and so on. .SH mailbox_size_limit (default: 51200000) The maximal size of any \fBlocal\fR(8) individual mailbox or maildir file, or zero (no limit). In fact, this limits the size of any @@ -15913,6 +15926,19 @@ or GID has write access to the parent directory of the recipient's mailbox file. .PP Note 2: the default setting of this parameter is system dependent. +.SH virtual_mailbox_file_format (default: mboxo) +The mailbox file format variant to use for UNIX\-style \fBvirtual\fR(8) +mailbox delivery. +.PP +Specify one of the following: +.IP "\fBmboxo\fR" +Prepend a > character to each line beginning with "From ". +This is the original mailbox format, but the transformation is +irreversible: you can't tell whether a line originally started with +"From " or ">From ". +.IP "\fBmboxrd\fR" +Prepend a > character to each line beginning with "From ", ">From ", +">>From ", and so on. .SH virtual_mailbox_maps (default: empty) Optional lookup tables with all valid addresses in the domains that match $virtual_mailbox_domains. diff --git a/postfix/src/global/Makefile.in b/postfix/src/global/Makefile.in index dfc28b631..81d0d7f38 100644 --- a/postfix/src/global/Makefile.in +++ b/postfix/src/global/Makefile.in @@ -15,7 +15,8 @@ SRCS = abounce.c anvil_clnt.c been_here.c bounce.c bounce_log.c \ mail_date.c mail_dict.c mail_error.c mail_flush.c mail_open_ok.c \ mail_params.c mail_pathname.c mail_queue.c mail_run.c \ mail_scan_dir.c mail_stream.c mail_task.c mail_trigger.c maps.c \ - mark_corrupt.c match_parent_style.c mbox_conf.c mbox_open.c \ + mark_corrupt.c match_parent_style.c mbox_conf.c \ + mbox_format.c mbox_open.c \ mime_state.c msg_stats_print.c msg_stats_scan.c mynetworks.c \ mypwd.c namadr_list.c off_cvt.c opened.c own_inet_addr.c \ pipe_command.c post_mail.c quote_821_local.c quote_822_local.c \ @@ -54,7 +55,8 @@ OBJS = abounce.o anvil_clnt.o been_here.o bounce.o bounce_log.o \ mail_date.o mail_dict.o mail_error.o mail_flush.o mail_open_ok.o \ mail_params.o mail_pathname.o mail_queue.o mail_run.o \ mail_scan_dir.o mail_stream.o mail_task.o mail_trigger.o maps.o \ - mark_corrupt.o match_parent_style.o mbox_conf.o mbox_open.o \ + mark_corrupt.o match_parent_style.o mbox_conf.o \ + mbox_format.o mbox_open.o \ mime_state.o msg_stats_print.o msg_stats_scan.o mynetworks.o \ mypwd.o namadr_list.o off_cvt.o opened.o own_inet_addr.o \ pipe_command.o post_mail.o quote_821_local.o quote_822_local.o \ @@ -95,7 +97,8 @@ HDRS = abounce.h anvil_clnt.h been_here.h bounce.h bounce_log.h \ mail_copy.h mail_date.h mail_dict.h mail_error.h mail_flush.h \ mail_open_ok.h mail_params.h mail_proto.h mail_queue.h mail_run.h \ mail_scan_dir.h mail_stream.h mail_task.h mail_version.h maps.h \ - mark_corrupt.h match_parent_style.h mbox_conf.h mbox_open.h \ + mark_corrupt.h match_parent_style.h mbox_conf.h \ + mbox_format.h mbox_open.h \ mime_state.h msg_stats.h mynetworks.h mypwd.h namadr_list.h \ off_cvt.h opened.h own_inet_addr.h pipe_command.h post_mail.h \ qmgr_user.h qmqp_proto.h quote_821_local.h quote_822_local.h \ @@ -120,7 +123,7 @@ CFLAGS = $(DEBUG) $(OPT) $(DEFS) INCL = LIB = lib$(LIB_PREFIX)global$(LIB_SUFFIX) TESTPROG= domain_list dot_lockfile mail_addr_crunch mail_addr_find \ - mail_addr_map mail_date maps mynetworks mypwd namadr_list \ + mail_addr_map mail_copy mail_date maps mynetworks mypwd namadr_list \ off_cvt quote_822_local rec2stream recdump resolve_clnt \ resolve_local rewrite_clnt stream2rec string_list tok822_parse \ quote_821_local mail_conf_time mime_state strip_addr \ @@ -260,6 +263,11 @@ mail_addr_find: $(LIB) $(LIBS) $(CC) $(CFLAGS) -DTEST -o $@ $@.c $(LIB) $(LIBS) $(SYSLIBS) mv junk $@.o +mail_copy: $(LIB) $(LIBS) + mv $@.o junk + $(CC) $(CFLAGS) -DTEST -o $@ $@.c $(LIB) $(LIBS) $(SYSLIBS) + mv junk $@.o + maps: $(LIB) $(LIBS) mv $@.o junk $(CC) $(CFLAGS) -DTEST -o $@ $@.c $(LIB) $(LIBS) $(SYSLIBS) @@ -422,6 +430,7 @@ tests: tok822_test mime_tests strip_addr_test tok822_limit_test \ safe_ultostr_test mail_parm_split_test fold_addr_test \ smtp_reply_footer_test off_cvt_test mail_addr_crunch_test \ mail_addr_find_test mail_addr_map_test quote_822_local_test \ + mail_copy_test1 mail_copy_testnone mail_copy_testo mail_copy_testrd \ normalize_mailhost_addr_test test_haproxy_srvr map_search_test \ delivered_hdr_test login_sender_match_test compat_level_test \ config_known_tcp_ports_test hfrom_format_test rfc2047_code_test \ @@ -733,6 +742,42 @@ mail_addr_map_test: update mail_addr_map mail_addr_map.ref diff mail_addr_map.ref mail_addr_map.tmp rm -f mail_addr_map.tmp +mail_copy_test1: update mail_copy mail_copy_test.in mail_copy_test.ref1 + -$(SHLIB_ENV) $(VALGRIND) ./mail_copy \ + -DOR. \ + -d washington+c.example.com@example.org \ + -o washington@c.example.com \ + -r norman@workstation.a.example.com \ + mail_copy_test.tmp + diff -u mail_copy_test.ref1 mail_copy_test.tmp + rm -f mail_copy_test.tmp + +mail_copy_testnone: update mail_copy mail_copy_test.in mail_copy_test.refnone + -$(SHLIB_ENV) $(VALGRIND) ./mail_copy \ + mail_copy_test.tmp + diff -u mail_copy_test.refnone mail_copy_test.tmp + rm -f mail_copy_test.tmp + +mail_copy_testo: update mail_copy mail_copy_test.in mail_copy_test.refo + -$(SHLIB_ENV) $(VALGRIND) ./mail_copy \ + -f mboxo \ + -d washington+c.example.com@example.org \ + -o washington@c.example.com \ + -r norman@workstation.a.example.com \ + mail_copy_test.tmp + diff -u mail_copy_test.refo mail_copy_test.tmp + rm -f mail_copy_test.tmp + +mail_copy_testrd: update mail_copy mail_copy_test.in mail_copy_test.refrd + -$(SHLIB_ENV) $(VALGRIND) ./mail_copy \ + -f mboxrd \ + -d washington+c.example.com@example.org \ + -o washington@c.example.com \ + -r norman@workstation.a.example.com \ + mail_copy_test.tmp + diff -u mail_copy_test.refrd mail_copy_test.tmp + rm -f mail_copy_test.tmp + quote_822_local_test: update quote_822_local quote_822_local.in quote_822_local.ref -$(SHLIB_ENV) $(VALGRIND) ./quote_822_local < quote_822_local.in >quote_822_local.tmp 2>&1 diff quote_822_local.ref quote_822_local.tmp diff --git a/postfix/src/global/mail_copy.c b/postfix/src/global/mail_copy.c index de35e9429..a74510cea 100644 --- a/postfix/src/global/mail_copy.c +++ b/postfix/src/global/mail_copy.c @@ -35,7 +35,13 @@ /* The binary OR of zero or more of the following: /* .RS /* .IP MAIL_COPY_QUOTE -/* Prepend a `>' character to lines beginning with `From '. +/* Prepend a `>' character to lines beginning with `From ', as in the +/* mboxo format. Irreversible, unlike MAIL_COPY_QUOTE_REV. Mutually +/* exclusive with MAIL_COPY_QUOTE_REV. +/* .IP MAIL_COPY_QUOTE_REV +/* Prepend a `>' character to lines matching /^>*From /, as in the +/* mboxrd format. Reversible, unlike MAIL_COPY_QUOTE. Mutually +/* exclusive with MAIL_COPY_QUOTE. /* .IP MAIL_COPY_DOT /* Prepend a `.' character to lines beginning with `.'. /* .IP MAIL_COPY_TOFILE @@ -58,9 +64,11 @@ /* Prepend a Return-Path: header with the value of the /* \fIsender\fR attribute. /* .RE -/* The manifest constant MAIL_COPY_MBOX is a convenient shorthand for -/* all MAIL_COPY_XXX options that are appropriate for mailbox delivery. -/* Use MAIL_COPY_NONE to copy a message without any options enabled. +/* The manifest constants MAIL_COPY_MBOXO and MAIL_COPY_MBOXRD are +/* convenient shorthands for all MAIL_COPY_XXX options that are +/* appropriate for mailbox delivery with the mboxo and mboxrd file +/* formats, respectively. Use MAIL_COPY_NONE to copy a message +/* without any options enabled. /* .IP eol /* Record delimiter, for example, LF or CF LF. /* .IP why @@ -129,6 +137,15 @@ #include "dsn_buf.h" #include "sys_exits.h" +/* mail_quotefromline - true if recursively quoted From line, false if not */ + +static int mail_quotefromline(const char *s) +{ + while (*s == '>') + s++; + return *s == 'F' && strncmp(s, "From ", 5) == 0; +} + /* mail_copy - copy message with extreme prejudice */ int mail_copy(const char *sender, @@ -177,6 +194,13 @@ int mail_copy(const char *sender, #endif buf = vstring_alloc(100); + /* + * Check for misuse. + */ + if ((flags & (MAIL_COPY_QUOTE | MAIL_COPY_QUOTE_REV)) == + (MAIL_COPY_QUOTE | MAIL_COPY_QUOTE_REV)) + msg_panic("MAIL_COPY_QUOTE and MAIL_COPY_QUOTE_REV are exclusive"); + /* * Prepend a bunch of headers to the message. */ @@ -234,6 +258,8 @@ int mail_copy(const char *sender, if (prev_type == REC_TYPE_NORM) { if ((flags & MAIL_COPY_QUOTE) && *bp == 'F' && !strncmp(bp, "From ", 5)) VSTREAM_PUTC('>', dst); + if ((flags & MAIL_COPY_QUOTE_REV) && mail_quotefromline(bp)) + VSTREAM_PUTC('>', dst); if ((flags & MAIL_COPY_DOT) && *bp == '.') VSTREAM_PUTC('.', dst); } @@ -313,3 +339,98 @@ int mail_copy(const char *sender, | (read_error ? MAIL_COPY_STAT_READ : 0) | (write_error ? MAIL_COPY_STAT_WRITE : 0)); } + +#ifdef TEST + +#include + +time_t +time(time_t *t) +{ + return *t = 1755913917; /* XXX make this an argument */ +} + +int main(int argc, char **argv) +{ + int copy_flags = MAIL_COPY_NONE; + const char *sender = NULL; + const char *orig_to = NULL; + const char *delivered = NULL; + const char *eol = "\n"; + int limit = -1; + int ch; + + while ((ch = getopt(argc, argv, "Bd:De:f:Fl:o:Or:R.>")) != -1) { + switch (ch) { + case 'B': + copy_flags |= MAIL_COPY_BLANK; + break; + case 'd': /* Delivered to */ + delivered = optarg; + break; + case 'e': /* End of line */ + eol = optarg; + break; + case 'D': + copy_flags |= MAIL_COPY_DELIVERED; + break; + case 'f': /* Format */ + if (strcmp(optarg, "mboxo") == 0) + copy_flags = MAIL_COPY_MBOXO; + else if (strcmp(optarg, "mboxrd") == 0) + copy_flags = MAIL_COPY_MBOXRD; + else + msg_fatal("unknown mail copy format"); + break; + case 'F': + copy_flags |= MAIL_COPY_FROM; + break; + case 'l': /* Limit */ + if ((limit = atoi(optarg)) < 0) + msg_fatal("invalid file size limit"); + break; + case 'o': /* Original recipient */ + orig_to = optarg; + break; + case 'O': + copy_flags |= MAIL_COPY_ORIG_RCPT; + break; + case 'Q': + copy_flags |= MAIL_COPY_QUOTE_REV; + break; + case 'r': /* Return-path */ + sender = optarg; + case 'R': + copy_flags |= MAIL_COPY_RETURN_PATH; + break; + case '.': + copy_flags |= MAIL_COPY_DOT; + break; + case '>': + if (copy_flags & MAIL_COPY_QUOTE) { + copy_flags &= ~MAIL_COPY_QUOTE; + copy_flags |= MAIL_COPY_QUOTE_REV; + } else { + copy_flags |= MAIL_COPY_QUOTE; + } + break; + } + } + + /* + * Enable X-Original-To if the caller wants. + */ + var_enable_orcpt = 1; + + /* + * If a file size limit was requested, apply it. + */ + if (limit >= 0) { + set_file_limit(limit); + } + + return mail_copy(sender, orig_to, delivered, VSTREAM_IN, VSTREAM_OUT, + copy_flags, eol, NULL); +} + +#endif diff --git a/postfix/src/global/mail_copy.h b/postfix/src/global/mail_copy.h index 4f2d77365..e3abd7582 100644 --- a/postfix/src/global/mail_copy.h +++ b/postfix/src/global/mail_copy.h @@ -37,7 +37,12 @@ extern int mail_copy(const char *, const char *, const char *, #define MAIL_COPY_DOT (1<<5) /* escape dots - needed for bsmtp */ #define MAIL_COPY_BLANK (1<<6) /* append blank line */ #define MAIL_COPY_ORIG_RCPT (1<<7) /* prepend X-Original-To: */ -#define MAIL_COPY_MBOX (MAIL_COPY_FROM | MAIL_COPY_QUOTE | \ +#define MAIL_COPY_QUOTE_REV (1<<8) /* prepend > to >*From_ (reversible) */ +#define MAIL_COPY_MBOXO (MAIL_COPY_FROM | MAIL_COPY_QUOTE | \ + MAIL_COPY_TOFILE | MAIL_COPY_DELIVERED | \ + MAIL_COPY_RETURN_PATH | MAIL_COPY_BLANK | \ + MAIL_COPY_ORIG_RCPT) +#define MAIL_COPY_MBOXRD (MAIL_COPY_FROM | MAIL_COPY_QUOTE_REV | \ MAIL_COPY_TOFILE | MAIL_COPY_DELIVERED | \ MAIL_COPY_RETURN_PATH | MAIL_COPY_BLANK | \ MAIL_COPY_ORIG_RCPT) diff --git a/postfix/src/global/mail_copy_test.in b/postfix/src/global/mail_copy_test.in new file mode 100644 index 0000000000000000000000000000000000000000..9557e876936fb205d918fe54e6109f8e851dc86b GIT binary patch literal 346 zcmY*VJx{|h6fD01SkWJn9@NKmn0hBC0eCaRg18D=f+LR)GL{48euPU>d?43ZR<*c= zzWb6P(DiKb8C#JC^Ekrg=2UsH?l(VNrwZ?Etza*~HhQ9O7mJ|}`yC~8edr`IayCb# zor$oV5{GAfew2;ezwA2OuSPS+nmXfKxQ!g(I8j51kVKy;y%G +X-Original-To: washington@c.example.com +Delivered-To: washington+c.example.com@example.org +Date: Sat, 23 Aug 2025 01:51:52 +0000 +From: Norman Mailer +To: Samuel Osgood +Subject: Hello world + +>>From what purpose comes this letter? +>From this purpose! + +From escaping mbox metadata to +.. +on a line of its own, this message exercises various edge cases in the +....mail data formats. diff --git a/postfix/src/global/mail_copy_test.refnone b/postfix/src/global/mail_copy_test.refnone new file mode 100644 index 000000000..7d7ee2d1d --- /dev/null +++ b/postfix/src/global/mail_copy_test.refnone @@ -0,0 +1,12 @@ +Date: Sat, 23 Aug 2025 01:51:52 +0000 +From: Norman Mailer +To: Samuel Osgood +Subject: Hello world + +>>From what purpose comes this letter? +>From this purpose! + +From escaping mbox metadata to +. +on a line of its own, this message exercises various edge cases in the +...mail data formats. diff --git a/postfix/src/global/mail_copy_test.refo b/postfix/src/global/mail_copy_test.refo new file mode 100644 index 000000000..b439b95b3 --- /dev/null +++ b/postfix/src/global/mail_copy_test.refo @@ -0,0 +1,17 @@ +From norman@workstation.a.example.com Fri Aug 22 21:51:57 2025 +Return-Path: +X-Original-To: washington@c.example.com +Delivered-To: washington+c.example.com@example.org +Date: Sat, 23 Aug 2025 01:51:52 +0000 +From: Norman Mailer +To: Samuel Osgood +Subject: Hello world + +>>From what purpose comes this letter? +>From this purpose! + +>From escaping mbox metadata to +. +on a line of its own, this message exercises various edge cases in the +...mail data formats. + diff --git a/postfix/src/global/mail_copy_test.refrd b/postfix/src/global/mail_copy_test.refrd new file mode 100644 index 000000000..f981b10fd --- /dev/null +++ b/postfix/src/global/mail_copy_test.refrd @@ -0,0 +1,17 @@ +From norman@workstation.a.example.com Fri Aug 22 21:51:57 2025 +Return-Path: +X-Original-To: washington@c.example.com +Delivered-To: washington+c.example.com@example.org +Date: Sat, 23 Aug 2025 01:51:52 +0000 +From: Norman Mailer +To: Samuel Osgood +Subject: Hello world + +>>>From what purpose comes this letter? +>>From this purpose! + +>From escaping mbox metadata to +. +on a line of its own, this message exercises various edge cases in the +...mail data formats. + diff --git a/postfix/src/global/mail_params.h b/postfix/src/global/mail_params.h index 551020a86..c3fb826ff 100644 --- a/postfix/src/global/mail_params.h +++ b/postfix/src/global/mail_params.h @@ -662,6 +662,13 @@ extern char *var_exec_exp_filter; #define VAR_MAILBOX_LOCK "mailbox_delivery_lock" extern char *var_mailbox_lock; + /* + * Mailbox file format. + */ +#define VAR_MBOX_FILE_FORMAT "mailbox_file_format" +#define DEF_MBOX_FILE_FORMAT "mboxo" +extern char *var_mbox_file_format; + /* * Mailbox size limit. This used to be enforced as a side effect of the way * the message size limit is implemented, but that is not clean. @@ -2715,6 +2722,10 @@ extern long var_virt_mailbox_limit; #define DEF_VIRT_MAILBOX_LOCK "fcntl, dotlock" extern char *var_virt_mailbox_lock; +#define VAR_VIRT_MAILBOX_FORMAT "virtual_mailbox_file_format" +#define DEF_VIRT_MAILBOX_FORMAT "mboxo" +extern char *var_virt_mailbox_format; + /* * Distinct logging tag for multiple Postfix instances. */ diff --git a/postfix/src/global/mbox_format.c b/postfix/src/global/mbox_format.c new file mode 100644 index 000000000..d2ccc9458 --- /dev/null +++ b/postfix/src/global/mbox_format.c @@ -0,0 +1,80 @@ +/*++ +/* NAME +/* mbox_format 3 +/* SUMMARY +/* mailbox file format configuration +/* SYNOPSIS +/* #include +/* +/* int mbox_format_copy_flags(context, string) +/* const char *context; +/* const char *string; +/* DESCRIPTION +/* The functions in this module translate between external +/* mailbox format names and internal representations. +/* +/* mbox_format_copy_flags() translates a string with format names +/* into a bit mask of MAIL_COPY_\* flags. The following gives the +/* format names and corresponding flag masks: +/* .IP "mboxo (MAIL_COPY_MBOXO)" +/* Replaces "From " by ">From " at the beginning of each line. This +/* is the original format, but it loses information: you cannot +/* tell whether the original message had "From " or ">From ". +/* .IP "mboxrd (MAIL_COPY_MBOXRD)" +/* Prepends ">" to any line matching />*From /. That is, replaces +/* "From " by ">From ", ">From " by ">>From ", and so on. +/* DIAGNOSTICS +/* Fatal errors: undefined format name. +/* LICENSE +/* .ad +/* .fi +/* The Secure Mailer license must be distributed with this software. +/* AUTHOR(S) +/* Taylor R Campbell +/*--*/ + +/* System library. */ + +#include + +/* Utility library. */ + +#include +#include + +/* Global library. */ + +#include +#include +#include + + /* + * The table with available mailbox formats. + */ +typedef enum { + MBOX_FORMAT_MBOXO, + MBOX_FORMAT_MBOXRD, + MBOX_FORMAT_INVALID = -1, +} MBOX_FORMAT; +static const NAME_CODE mbox_format[] = { + "mboxo", MBOX_FORMAT_MBOXO, + "mboxrd", MBOX_FORMAT_MBOXRD, + 0, MBOX_FORMAT_INVALID, +}; + +/* mbox_format_copy_flags - translate mailbox format names to copy flags */ + +int mbox_format_copy_flags(const char *context, const char *string) +{ + MBOX_FORMAT format = + name_code(mbox_format, NAME_CODE_FLAG_NONE, string); + + switch (format) { + case MBOX_FORMAT_MBOXO: + return MAIL_COPY_MBOXO; + case MBOX_FORMAT_MBOXRD: + return MAIL_COPY_MBOXRD; + default: + msg_fatal("invalid %s: \"%s\"", context, string); + } +} diff --git a/postfix/src/global/mbox_format.h b/postfix/src/global/mbox_format.h new file mode 100644 index 000000000..d5b3516fb --- /dev/null +++ b/postfix/src/global/mbox_format.h @@ -0,0 +1,27 @@ +#ifndef _MBOX_FORMAT_H_INCLUDED_ +#define _MBOX_FORMAT_H_INCLUDED_ + +/*++ +/* NAME +/* mbox_format 3h +/* SUMMARY +/* mailbox file format configuration +/* SYNOPSIS +/* #include +/* DESCRIPTION +/* .nf + + /* + * External interface. + */ +extern int mbox_format_copy_flags(const char *, const char *); + +/* LICENSE +/* .ad +/* .fi +/* The Secure Mailer license must be distributed with this software. +/* AUTHOR(S) +/* Taylor R Campbell +/*--*/ + +#endif diff --git a/postfix/src/local/file.c b/postfix/src/local/file.c index 0cc4c1828..cbbe40c0a 100644 --- a/postfix/src/local/file.c +++ b/postfix/src/local/file.c @@ -153,7 +153,7 @@ int deliver_file(LOCAL_STATE state, USER_ATTR usr_attr, char *path) * As the specified user, open or create the file, lock it, and append * the message. */ - copy_flags = MAIL_COPY_MBOX; + copy_flags = local_mbox_copy_flags; if ((local_deliver_hdr_mask & DELIVER_HDR_FILE) == 0) copy_flags &= ~MAIL_COPY_DELIVERED; diff --git a/postfix/src/local/local.c b/postfix/src/local/local.c index f77c1a543..245e73eaf 100644 --- a/postfix/src/local/local.c +++ b/postfix/src/local/local.c @@ -661,6 +661,7 @@ #include #include #include +#include /* Single server skeleton. */ @@ -701,6 +702,7 @@ char *var_deliver_hdr; int var_stat_home_dir; int var_mailtool_compat; char *var_mailbox_lock; +char *var_mbox_file_format; long var_mailbox_limit; bool var_frozen_delivered; bool var_reset_owner_attr; @@ -711,6 +713,7 @@ int local_file_deliver_mask; int local_ext_prop_mask; int local_deliver_hdr_mask; int local_mbox_lock_mask; +int local_mbox_copy_flags; MAPS *alias_maps; char *var_local_dsn_filter; @@ -858,6 +861,14 @@ static void local_mask_init(void) VAR_MAILBOX_LOCK); } +/* local_mbox_format_init - initialize mbox format */ + +static void local_mbox_format_init(void) +{ + local_mbox_copy_flags = + mbox_format_copy_flags(VAR_MBOX_FILE_FORMAT, var_mbox_file_format); +} + /* pre_accept - see if tables have changed */ static void pre_accept(char *unused_name, char **unused_argv) @@ -876,10 +887,12 @@ static void post_init(char *unused_name, char **unused_argv) { /* - * Drop privileges most of the time, and set up delivery restrictions. + * Drop privileges most of the time, set up delivery restrictions, + * and determine the mbox file format. */ set_eugid(var_owner_uid, var_owner_gid); local_mask_init(); + local_mbox_format_init(); } /* pre_init - pre-jail initialization */ @@ -952,6 +965,7 @@ int main(int argc, char **argv) VAR_PROP_EXTENSION, DEF_PROP_EXTENSION, &var_prop_extension, 0, 0, VAR_DELIVER_HDR, DEF_DELIVER_HDR, &var_deliver_hdr, 0, 0, VAR_MAILBOX_LOCK, DEF_MAILBOX_LOCK, &var_mailbox_lock, 1, 0, + VAR_MBOX_FILE_FORMAT, DEF_MBOX_FILE_FORMAT, &var_mbox_file_format, 0, 0, VAR_MAILBOX_CMD_MAPS, DEF_MAILBOX_CMD_MAPS, &var_mailbox_cmd_maps, 0, 0, VAR_LOCAL_DSN_FILTER, DEF_LOCAL_DSN_FILTER, &var_local_dsn_filter, 0, 0, 0, diff --git a/postfix/src/local/local.h b/postfix/src/local/local.h index 13d0dc79e..0632daf36 100644 --- a/postfix/src/local/local.h +++ b/postfix/src/local/local.h @@ -205,6 +205,11 @@ extern int local_mbox_lock_mask; extern int local_deliver_hdr_mask; + /* + * Format of mailbox files determined by mailbox_file_format + */ +extern int local_mbox_copy_flags; + /* * forward.c */ diff --git a/postfix/src/local/mailbox.c b/postfix/src/local/mailbox.c index ed552916c..16412b323 100644 --- a/postfix/src/local/mailbox.c +++ b/postfix/src/local/mailbox.c @@ -185,7 +185,7 @@ static int deliver_mailbox_file(LOCAL_STATE state, USER_ATTR usr_attr) * * Write the file as the recipient, so that file quota work. */ - copy_flags = MAIL_COPY_MBOX; + copy_flags = local_mbox_copy_flags; if ((local_deliver_hdr_mask & DELIVER_HDR_FILE) == 0) copy_flags &= ~MAIL_COPY_DELIVERED; diff --git a/postfix/src/virtual/mailbox.c b/postfix/src/virtual/mailbox.c index 19afca877..b237cb9aa 100644 --- a/postfix/src/virtual/mailbox.c +++ b/postfix/src/virtual/mailbox.c @@ -117,7 +117,7 @@ static int deliver_mailbox_file(LOCAL_STATE state, USER_ATTR usr_attr) * * Write the file as the recipient, so that file quota work. */ - copy_flags = MAIL_COPY_MBOX; + copy_flags = virtual_mbox_copy_flags; set_eugid(usr_attr.uid, usr_attr.gid); mp = mbox_open(usr_attr.mailbox, O_APPEND | O_WRONLY | O_CREAT, diff --git a/postfix/src/virtual/virtual.c b/postfix/src/virtual/virtual.c index ed8025352..04a110b96 100644 --- a/postfix/src/virtual/virtual.c +++ b/postfix/src/virtual/virtual.c @@ -341,6 +341,7 @@ #include #include #include +#include /* Single server skeleton. */ @@ -359,6 +360,7 @@ char *var_virt_gid_maps; int var_virt_minimum_uid; char *var_virt_mailbox_base; char *var_virt_mailbox_lock; +char *var_virt_mailbox_format; long var_virt_mailbox_limit; char *var_mail_spool_dir; /* XXX dependency fix */ bool var_strict_mbox_owner; @@ -375,6 +377,7 @@ MAPS *virtual_gid_maps; * Bit masks. */ int virtual_mbox_lock_mask; +int virtual_mbox_copy_flags; /* local_deliver - deliver message with extreme prejudice */ @@ -493,6 +496,9 @@ static void post_init(char *unused_name, char **unused_argv) | DICT_FLAG_UTF8_REQUEST); virtual_mbox_lock_mask = mbox_lock_mask(var_virt_mailbox_lock); + virtual_mbox_copy_flags = + mbox_format_copy_flags(VAR_VIRT_MAILBOX_FORMAT, + var_virt_mailbox_format); } /* pre_init - pre-jail initialization */ diff --git a/postfix/src/virtual/virtual.h b/postfix/src/virtual/virtual.h index 75dd6cd2e..b1f4db4af 100644 --- a/postfix/src/virtual/virtual.h +++ b/postfix/src/virtual/virtual.h @@ -133,6 +133,11 @@ extern int deliver_unknown(LOCAL_STATE); */ extern int virtual_mbox_lock_mask; + /* + * Mailbox file format. + */ +extern int virtual_mbox_copy_flags; + /* * Silly little macros. */