From: Thibault Godouet Date: Sat, 24 Aug 2024 21:25:45 +0000 (+0100) Subject: Move format_displayname() and make_mailbox_addr() into their own file. X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=892b7213f3556a177f3e923830c45c7f242529e9;p=thirdparty%2Ffcron.git Move format_displayname() and make_mailbox_addr() into their own file. --- diff --git a/Makefile.in b/Makefile.in index cfcfd95..4c046dc 100644 --- a/Makefile.in +++ b/Makefile.in @@ -76,11 +76,11 @@ CFLAGS += $(OPTIM) $(OPTION) $(DEFS) $(CPPFLAGS) $(LDFLAGS) ifeq ($(FCRONDYN), 1) LIBOBJS := $(LIBOBJS) endif -OBJSD := fcron.o cl.o subs.o mem.o save.o temp_file.o log.o database.o job.o conf.o u_list.o exe_list.o lavg_list.o env_list.o fcronconf.o filesubs.o select.o fcrondyn_svr.o suspend.o $(LIBOBJS) -OBJSTAB := fcrontab.o cl.o subs.o mem.o save.o temp_file.o log.o fileconf.o allow.o read_string.o u_list.o env_list.o fcronconf.o filesubs.o -OBJSDYN := fcrondyn.o subs.o mem.o log.o allow.o read_string.o fcronconf.o filesubs.o -OBJCONV := convert-fcrontab.o cl.o subs.o mem.o save.o log.o u_list.o env_list.o fcronconf.o filesubs.o -OBJSIG := fcronsighup.o subs.o mem.o log.o allow.o fcronconf.o filesubs.o +OBJSD := fcron.o cl.o subs.o mem.o save.o temp_file.o log.o database.o job.o conf.o u_list.o exe_list.o lavg_list.o env_list.o fcronconf.o mail.o filesubs.o select.o fcrondyn_svr.o suspend.o $(LIBOBJS) +OBJSTAB := fcrontab.o cl.o subs.o mem.o save.o temp_file.o log.o fileconf.o allow.o read_string.o u_list.o env_list.o fcronconf.o mail.o filesubs.o +OBJSDYN := fcrondyn.o subs.o mem.o log.o allow.o read_string.o fcronconf.o mail.o filesubs.o +OBJCONV := convert-fcrontab.o cl.o subs.o mem.o save.o log.o u_list.o env_list.o fcronconf.o mail.o filesubs.o +OBJSIG := fcronsighup.o subs.o mem.o log.o allow.o fcronconf.o mail.o filesubs.o HEADERSALL := config.h $(SRCDIR)/global.h $(SRCDIR)/cl.h $(SRCDIR)/log.h $(SRCDIR)/subs.h $(SRCDIR)/mem.h $(SRCDIR)/save.h $(SRCDIR)/option.h $(SRCDIR)/dyncom.h $(SRCDIR)/mail.h # this is a regular expression : diff --git a/fcronconf.c b/fcronconf.c index b5280e2..d743ea6 100644 --- a/fcronconf.c +++ b/fcronconf.c @@ -47,63 +47,6 @@ char *sendmail = NULL; char *editor = NULL; char *displayname = NULL; -char -*format_displayname(char *displayname_conf) - /* Format the input string `conf_value` according to RFC5322 sec. 3.2.3. - * . - * Returns: either the formatted displayname (possibly unchanged or empty) - * as a new dynamically allocated string (must be properly freed by the - * caller) or NULL on errors like buffer overflow. */ -{ - bool need_quotes = false; - char c = '\0'; - char *ipos = NULL; /* Input position */ - char *output = NULL, *quoted_output = NULL; - - const uint buf_len = MAIL_FROM_VALUE_LEN_MAX; - uint cwritten = 0; /* how many chars we have written */ - - if (strlen(displayname_conf) == 0) return strdup2(""); - - output = (char *)alloc_safe(buf_len * sizeof(char), "output buffer"); - - /* walk the conf_value and rebuild it in buf1 */ - for (ipos = displayname_conf; *ipos; ipos++) { - c = *ipos; - if (strchr(SPECIAL_MBOX_CHARS, c)) { - /* insert escape */ - if (c == DQUOTE) { - output[cwritten] = BSLASH; - cwritten++; - } - need_quotes = true; - } - if (cwritten >= buf_len) { - error("Formatted 'displayname' exceeds %u chars", buf_len); - Free_safe(output); - return NULL; - } - output[cwritten] = c; - cwritten++; - } - - if (need_quotes) { - quoted_output = (char *)alloc_safe(buf_len * sizeof(char), "quoted output buffer"); - int needed_len = snprintf(quoted_output, buf_len, "\"%s\"", output); - if (needed_len >= buf_len){ - error("Formatted 'displayname' too long: length:%u > max:%u chars", needed_len, buf_len); - Free_safe(output); - Free_safe(quoted_output); - return NULL; - } - Free_safe(output); - - return quoted_output; - } - - return output; -} - void init_conf(void) /* initialises config with compiled in constants */ diff --git a/job.c b/job.c index 5191255..f956fb4 100644 --- a/job.c +++ b/job.c @@ -278,43 +278,6 @@ sig_dfl(void) signal(SIGPIPE, SIG_DFL); } -char * -make_mailbox_addr(char *displayname_conf, char *mail_from, char *hostname) - /* Produce a "mailbox" header as per RFC5322 sec. 3.2.3 - * . - * Returns: either the formatted mailbox header as a new dynamically - * allocated string (must be properly freed by the caller) or NULL on - * errors like buffer overflow. */ -{ - char *buf = NULL; - uint written = 0; - bool need_anglebrackets = false; - - const uint buf_len = MAIL_FROM_VALUE_LEN_MAX+1; - - buf = (char *)alloc_safe(buf_len * sizeof(char), "mailbox addr buffer"); - - if (displayname_conf[0] != '\0') { - /* displayname_conf isn't an empty string */ - need_anglebrackets = true; - } - - /* no @ here, it's handled upstream */ - if (need_anglebrackets) - written = snprintf(buf, buf_len, "%s %c%s%s%c", - displayname_conf, '<', mail_from, hostname, '>'); - else - written = snprintf(buf, buf_len, "%s%s", mail_from, hostname); - - if (written >= buf_len) { - error("Mailbox addr exceeds %u chars", buf_len); - Free_safe(buf); - return NULL; - } - - return buf; -} - FILE * create_mail(cl_t * line, char *subject, char *content_type, char *encoding, char **env) diff --git a/mail.c b/mail.c new file mode 100644 index 0000000..e42a5ca --- /dev/null +++ b/mail.c @@ -0,0 +1,120 @@ +/* + * FCRON - periodic command scheduler + * + * Copyright 2000-2021 Thibault Godouet + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * The GNU General Public License can also be found in the file + * `LICENSE' that comes with the fcron source distribution. + */ + + +#include "fcron.h" +#include "mail.h" + +char +*format_displayname(char *displayname_conf) + /* Format the input string `conf_value` according to RFC5322 sec. 3.2.3. + * . + * Returns: either the formatted displayname (possibly unchanged or empty) + * as a new dynamically allocated string (must be properly freed by the + * caller) or NULL on errors like buffer overflow. */ +{ + bool need_quotes = false; + char c = '\0'; + char *ipos = NULL; /* Input position */ + char *output = NULL, *quoted_output = NULL; + + const uint buf_len = MAIL_FROM_VALUE_LEN_MAX; + uint cwritten = 0; /* how many chars we have written */ + + if (strlen(displayname_conf) == 0) return strdup2(""); + + output = (char *)alloc_safe(buf_len * sizeof(char), "output buffer"); + + /* walk the conf_value and rebuild it in buf1 */ + for (ipos = displayname_conf; *ipos; ipos++) { + c = *ipos; + if (strchr(SPECIAL_MBOX_CHARS, c)) { + /* insert escape */ + if (c == DQUOTE) { + output[cwritten] = BSLASH; + cwritten++; + } + need_quotes = true; + } + if (cwritten >= buf_len) { + error("Formatted 'displayname' exceeds %u chars", buf_len); + Free_safe(output); + return NULL; + } + output[cwritten] = c; + cwritten++; + } + + if (need_quotes) { + quoted_output = (char *)alloc_safe(buf_len * sizeof(char), "quoted output buffer"); + int needed_len = snprintf(quoted_output, buf_len, "\"%s\"", output); + if (needed_len >= buf_len){ + error("Formatted 'displayname' too long: length:%u > max:%u chars", needed_len, buf_len); + Free_safe(output); + Free_safe(quoted_output); + return NULL; + } + Free_safe(output); + + return quoted_output; + } + + return output; +} + +char * +make_mailbox_addr(char *displayname_conf, char *mail_from, char *hostname) + /* Produce a "mailbox" header as per RFC5322 sec. 3.2.3 + * . + * Returns: either the formatted mailbox header as a new dynamically + * allocated string (must be properly freed by the caller) or NULL on + * errors like buffer overflow. */ +{ + char *buf = NULL; + uint written = 0; + bool need_anglebrackets = false; + + const uint buf_len = MAIL_FROM_VALUE_LEN_MAX+1; + + buf = (char *)alloc_safe(buf_len * sizeof(char), "mailbox addr buffer"); + + if (displayname_conf[0] != '\0') { + /* displayname_conf isn't an empty string */ + need_anglebrackets = true; + } + + /* no @ here, it's handled upstream */ + if (need_anglebrackets) + written = snprintf(buf, buf_len, "%s %c%s%s%c", + displayname_conf, '<', mail_from, hostname, '>'); + else + written = snprintf(buf, buf_len, "%s%s", mail_from, hostname); + + if (written >= buf_len) { + error("Mailbox addr exceeds %u chars", buf_len); + Free_safe(buf); + return NULL; + } + + return buf; +} diff --git a/mail.h b/mail.h index 86a82c5..ff16064 100644 --- a/mail.h +++ b/mail.h @@ -29,4 +29,7 @@ #define FROM_HEADER_KEY "From: " #define MAIL_FROM_VALUE_LEN_MAX (MAIL_LINE_LEN_MAX - sizeof(FROM_HEADER_KEY)) +extern char *format_displayname(char *displayname_conf); +extern char *make_mailbox_addr(char *displayname_conf, char *mail_from, char *hostname); + #endif /* __MAIL_H__ */ diff --git a/test/Makefile.in b/test/Makefile.in index 74a18cf..38c5476 100644 --- a/test/Makefile.in +++ b/test/Makefile.in @@ -4,7 +4,7 @@ LIBS := @LIBS@ SRCST := mailbox_addr.c OBJST := $(patsubst %.c,%.o,$(SRCST)) EXEST := $(patsubst %.c,%,$(SRCST)) -OBJSD := $(addprefix $(SRCDIR)/,log.o job.o mem.o filesubs.o subs.o fcronconf.o temp_file.o env_list.o u_list.o) +OBJSD := $(addprefix $(SRCDIR)/,log.o job.o mem.o filesubs.o subs.o fcronconf.o mail.o temp_file.o env_list.o u_list.o) CFLAGS += -I$(SRCDIR) CFLAGS += @CFLAGS@