From: Baptiste Daroussin Date: Mon, 24 Oct 2022 15:30:23 +0000 (+0200) Subject: utils: add function to convert a control file to time_t X-Git-Tag: RELEASE_1_4_0a1~55 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=161d001e100b030f9a97ca192bd14f294b7e711b;p=thirdparty%2Fmlmmj.git utils: add function to convert a control file to time_t --- diff --git a/include/ctrlvalue.h b/include/ctrlvalue.h index 3551ffef..e9417bb3 100644 --- a/include/ctrlvalue.h +++ b/include/ctrlvalue.h @@ -26,6 +26,7 @@ #include #include +#include char *ctrlvalue(const char *listdir, const char *ctrlstr); char *ctrlcontent(const char *listdir, const char *ctrlstr); @@ -49,5 +50,6 @@ ctrlushort(const char *listdir, const char *ctrlstr, unsigned short fallback) { return (ctrluim(listdir, ctrlstr, 0, USHRT_MAX, fallback)); } +time_t ctrltimet(const char *listdir, const char *ctrlstr, time_t fallback); #endif /* CTRLVALUE_H */ diff --git a/include/utils.h b/include/utils.h index 7e4b4bd8..6fd6afdc 100644 --- a/include/utils.h +++ b/include/utils.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021 Baptiste Daroussin + * Copyright (C) 2022 Baptiste Daroussin * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to @@ -22,9 +22,12 @@ #pragma once +#include + char *lowercase(const char *); intmax_t strtoim(const char *np, intmax_t minval, intmax_t maxval, const char **errpp); uintmax_t strtouim(const char *, uintmax_t, uintmax_t, const char **); void exec_or_die(const char *arg, ...); int exec_and_wait(const char *arg, ...); +time_t strtotimet(const char *np, const char **errpp); diff --git a/src/ctrlvalue.c b/src/ctrlvalue.c index a2b8f548..15b698f1 100644 --- a/src/ctrlvalue.c +++ b/src/ctrlvalue.c @@ -124,3 +124,23 @@ ctrluim(const char *listdir, const char *ctrlstr, uintmax_t min, uintmax_t max, free(val); return (ret); } + +time_t +ctrltimet(const char *listdir, const char *ctrlstr, time_t fallback) +{ + const char *errstr; + char *val = ctrlvalue(listdir, ctrlstr); + time_t ret; + + if (val == NULL) + return (fallback); + + ret = strtotimet(val, &errstr); + if (errstr != NULL) { + log_error(LOG_ARGS, "Invalid value for '%s': %s", ctrlstr, + errstr); + return (fallback); + } + free(val); + return (ret); +} diff --git a/src/mlmmj-maintd.c b/src/mlmmj-maintd.c index f64236f5..aebde8dc 100644 --- a/src/mlmmj-maintd.c +++ b/src/mlmmj-maintd.c @@ -111,19 +111,11 @@ int delolder(const char *dirname, time_t than) int clean_moderation(const char *listdir) { - time_t modreqlife = 0; - char *modreqlifestr; + time_t modreqlife; char *moddirname; int ret; - modreqlifestr = ctrlvalue(listdir, "modreqlife"); - if(modreqlifestr) { - modreqlife = atol(modreqlifestr); - free(modreqlifestr); - } - if(modreqlife == 0) - modreqlife = MODREQLIFE; - + modreqlife = ctrltimet(listdir, "modreqlife", MODREQLIFE); moddirname = concatstr(2, listdir, "/moderation"); ret = delolder(moddirname, modreqlife); @@ -192,10 +184,9 @@ int resend_queue(const char *listdir, const char *mlmmjsend) struct dirent *dp; char *mailname, *fromname, *toname, *reptoname, *from, *to, *repto; char *ch, *dirname = concatstr(2, listdir, "/queue/"); - char *bouncelifestr; struct stat st; int fromfd, tofd, fd, err = 0; - time_t t, bouncelife = 0; + time_t t, bouncelife; if(chdir(dirname) < 0) { log_error(LOG_ARGS, "Could not chdir(%s)", dirname); @@ -293,14 +284,7 @@ int resend_queue(const char *listdir, const char *mlmmjsend) } /* before we try again, check and see if it's old */ - bouncelifestr = ctrlvalue(listdir, "bouncelife"); - if(bouncelifestr) { - bouncelife = atol(bouncelifestr); - free(bouncelifestr); - } - if(bouncelife == 0) - bouncelife = BOUNCELIFE; - + bouncelife = ctrltimet(listdir, "bouncelife", BOUNCELIFE); t = time(NULL); if(t - st.st_mtime > bouncelife) { unlink(mailname); @@ -561,11 +545,10 @@ int unsub_bouncers(const char *listdir, const char *mlmmjunsub) DIR *bouncedir; char *dirname = concatstr(2, listdir, "/bounce/"); char *probefile, *address, *a, *firstbounce, *bouncedata; - char *bouncelifestr; struct dirent *dp; struct stat st; int fd; - time_t bouncetime, t, bouncelife = 0; + time_t bouncetime, t, bouncelife; if(chdir(dirname) < 0) { log_error(LOG_ARGS, "Could not chdir(%s)", dirname); @@ -581,14 +564,7 @@ int unsub_bouncers(const char *listdir, const char *mlmmjunsub) free(dirname); - bouncelifestr = ctrlvalue(listdir, "bouncelife"); - if(bouncelifestr) { - bouncelife = atol(bouncelifestr); - free(bouncelifestr); - } - - if(bouncelife == 0) - bouncelife = BOUNCELIFE; + bouncelife = ctrltimet(listdir, "bouncelife", BOUNCELIFE); while((dp = readdir(bouncedir)) != NULL) { if((strcmp(dp->d_name, "..") == 0) || diff --git a/src/utils.c b/src/utils.c index 0a6eef6b..cab6f7bb 100644 --- a/src/utils.c +++ b/src/utils.c @@ -21,6 +21,7 @@ */ #include +#include #include #include @@ -66,6 +67,14 @@ strtoim(const char *np, intmax_t minval, intmax_t maxval, const char **errpp) return (ret); } +time_t +strtotimet(const char *np, const char **errpp) +{ + if (sizeof(time_t) == 4) + return ((time_t)strtoim(np, INT_MIN, INT_MAX, errpp)); + return ((time_t)strtoim(np, LLONG_MIN, LLONG_MAX, errpp)); +} + uintmax_t strtouim(const char *np, uintmax_t minval, uintmax_t maxval, const char **errpp) { diff --git a/tests/mlmmj.c b/tests/mlmmj.c index 0bbb67bd..dfe5b66d 100644 --- a/tests/mlmmj.c +++ b/tests/mlmmj.c @@ -69,6 +69,7 @@ ATF_TC_WITHOUT_HEAD(write_replyto); ATF_TC_WITHOUT_HEAD(write_rcpt_to); ATF_TC_WITHOUT_HEAD(write_mail_from); ATF_TC_WITHOUT_HEAD(write_mailbody_from_map); +ATF_TC_WITHOUT_HEAD(strtotimet); #ifndef NELEM #define NELEM(array) (sizeof(array) / sizeof((array)[0])) @@ -673,6 +674,18 @@ ATF_TC_BODY(write_mailbody_from_map, tc) close(fd1); } +ATF_TC_BODY(strtotimet, tc) +{ + const char *errp; + char *str; + ATF_REQUIRE_EQ(strtotimet("10", &errp), 10); + ATF_CHECK(errp == NULL); + asprintf(&str, "1%"PRIdMAX, INTMAX_MAX); + ATF_REQUIRE_EQ(strtotimet(str, &errp), 0); + free(str); + ATF_REQUIRE(errp != NULL); + ATF_REQUIRE_STREQ(errp, "too large"); +} ATF_TP_ADD_TCS(tp) { @@ -698,6 +711,7 @@ ATF_TP_ADD_TCS(tp) ATF_TP_ADD_TC(tp, write_rcpt_to); ATF_TP_ADD_TC(tp, write_mail_from); ATF_TP_ADD_TC(tp, write_mailbody_from_map); + ATF_TP_ADD_TC(tp, strtotimet); return (atf_no_error()); }