#include <string.h>
#include <regex.h>
+#include <errno.h>
#include "mlmmj.h"
-#include "log_error.h"
-#include "log_oper.h"
+#include "xmalloc.h"
static char *action_strs[] = {
"allowed",
};
actions_t
-do_access(strlist *rules, strlist *headers, const char *from, int listfd)
+do_access(strlist *rules, strlist *headers, const char *from, char **msg)
{
unsigned int match;
char *rule_ptr;
regex_t regexp;
char *hdr;
size_t i = 0;
+ if (msg != NULL)
+ *msg = NULL;
if (rules == NULL)
return (ACT_ALLOW);
act = ACT_DISCARD;
} else {
errno = 0;
- log_error(LOG_ARGS, "Unable to parse rule #%d \"%s\":"
- " Missing action keyword. Denying post from \"%s\"",
- i, it->item, from);
- log_oper(listfd, OPLOGFNAME, "Unable to parse rule #%d \"%s\":"
+ if (msg != NULL)
+ xasprintf(msg, "Unable to parse rule #%d \"%s\":"
" Missing action keyword. Denying post from \"%s\"",
i, it->item, from);
return ACT_DENY;
rule_ptr++;
} else if (*rule_ptr == '\0') {
/* the rule is a keyword and no regexp */
- log_oper(listfd, OPLOGFNAME, "mlmmj-process: access -"
+ if (msg != NULL)
+ xasprintf(msg, "access -"
" A mail from \"%s\" was %s by rule #%d \"%s\"",
from, action_strs[act], i, it->item);
return act;
} else {
/* we must have space or end of string */
errno = 0;
- log_error(LOG_ARGS, "Unable to parse rule #%d \"%s\":"
- " Invalid character after action keyword."
- " Denying post from \"%s\"", i, it->item, from);
- log_oper(listfd, OPLOGFNAME, "Unable to parse rule #%d \"%s\":"
+ if (msg != NULL)
+ xasprintf(msg, "Unable to parse rule #%d \"%s\":"
" Invalid character after action keyword."
" Denying post from \"%s\"", i, it->item, from);
return ACT_DENY;
regerror(err, ®exp, errbuf, sizeof(errbuf));
regfree(®exp);
errno = 0;
- log_error(LOG_ARGS, "regcomp() failed for rule #%d \"%s\""
+ if (msg != NULL)
+ xasprintf(msg, "regcomp() failed for rule #%d \"%s\""
" (message: '%s') (expression: '%s')"
" Denying post from \"%s\"",
i, it->item, errbuf, rule_ptr, from);
- log_oper(listfd, OPLOGFNAME, "regcomp() failed for rule"
- " #%d \"%s\" (message: '%s') (expression: '%s')"
- " Denying post from \"%s\"",
- i, it->item, errbuf, rule_ptr, from);
return ACT_DENY;
}
if (match != not) {
if (match) {
- log_oper(listfd, OPLOGFNAME, "mlmmj-process: access -"
+ if (msg != NULL)
+ xasprintf(msg, "access -"
" A mail from \"%s\" with header \"%s\" was %s by"
" rule #%d \"%s\"", from, hdr, action_strs[act],
i, it->item);
} else {
- log_oper(listfd, OPLOGFNAME, "mlmmj-process: access -"
+ if (msg != NULL)
+ xasprintf(msg, "access -"
" A mail from \"%s\" was %s by rule #%d \"%s\""
" because no header matched.", from,
action_strs[act], i, it->item);
i++;
}
- log_oper(listfd, OPLOGFNAME, "mlmmj-process: access -"
+ xasprintf(msg, "access -"
" A mail from \"%s\" didn't match any rules, and"
" was denied by default.", from);
return ACT_DENY;
ATF_TC_BODY(do_access, tc)
{
- int listfd = -1;
strlist rules = tll_init();
strlist headers = tll_init();
tll_push_back(rules, "allow");
+ char *msg = NULL;
- ATF_REQUIRE_EQ(do_access(NULL, NULL, "test@plop", listfd), ACT_ALLOW);
- ATF_REQUIRE_EQ(do_access(&rules, NULL, "test@plop", listfd), ACT_ALLOW);
+ ATF_REQUIRE_EQ(do_access(NULL, NULL, "test@plop", &msg), ACT_ALLOW);
+ ATF_REQUIRE(msg == NULL);
+
+ ATF_REQUIRE_EQ(do_access(&rules, NULL, "test@plop", &msg), ACT_ALLOW);
+ ATF_REQUIRE_STREQ(msg, "access - A mail from \"test@plop\" was allowed by rule #0 \"allow\"");
tll_pop_back(rules);
tll_push_back(rules, "deny");
- ATF_REQUIRE_EQ(do_access(&rules, NULL, "test@plop", listfd), ACT_DENY);
+ ATF_REQUIRE_EQ(do_access(&rules, NULL, "test@plop", &msg), ACT_DENY);
+ ATF_REQUIRE_STREQ(msg, "access - A mail from \"test@plop\" was denied by rule #0 \"deny\"");
tll_pop_back(rules);
tll_push_back(rules, "crap");
- ATF_REQUIRE_EQ(do_access(&rules, NULL, "test@plop", listfd), ACT_DENY);
+ ATF_REQUIRE_EQ(do_access(&rules, NULL, "test@plop", &msg), ACT_DENY);
+ ATF_REQUIRE_STREQ(msg, "Unable to parse rule #0 \"crap\": Missing action keyword. Denying post from \"test@plop\"");
tll_pop_back(rules);
tll_push_back(headers, "from: toto@bla");
tll_push_back(rules, "moderate ^from: toto@");
- ATF_REQUIRE_EQ(do_access(&rules, &headers, "test@plop", listfd), ACT_MODERATE);
+ ATF_REQUIRE_EQ(do_access(&rules, &headers, "test@plop", &msg), ACT_MODERATE);
+ ATF_REQUIRE_STREQ(msg, "access - A mail from \"test@plop\" with header \"from: toto@bla\" was moderated by rule #0 \"moderate ^from: toto@\"");
}
ATF_TP_ADD_TCS(tp)