Skip to content

Commit

Permalink
Ignore comments in domains-file
Browse files Browse the repository at this point in the history
Fixes #181
  • Loading branch information
roehling committed Jul 23, 2024
1 parent 240af96 commit 3def248
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 15 deletions.
46 changes: 31 additions & 15 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ static void show_help()
);
}

cfg_t* config_from_commandline(int argc, char* const* argv)
cfg_t* config_defaults()
{
static cfg_opt_t opts[] = {
CFG_STR("srs-domain", NULL, CFGF_NODEFAULT),
Expand Down Expand Up @@ -172,6 +172,12 @@ cfg_t* config_from_commandline(int argc, char* const* argv)
cfg_set_validate_func(cfg, "srs-domain", validate_domain_names);
cfg_set_validate_func(cfg, "domains", validate_domain_names);
cfg_set_validate_func(cfg, "keep-alive", validate_uint);
return cfg;
}

cfg_t* config_from_commandline(int argc, char* const* argv)
{
cfg_t* cfg = config_defaults();
int opt;
char* config_file = NULL;
char* pid_file = NULL;
Expand Down Expand Up @@ -340,24 +346,34 @@ bool srs_domains_from_config(cfg_t* cfg, char** srs_domain,
if (f)
{
char buffer[1024];
char* end;
while ((domain = fgets(buffer, sizeof(buffer), f)) != NULL)
{
domain = strtok(domain, "\r\n");
if (domain && domain[0])
if (domain == NULL)
continue;
while (isspace(domain[0]))
++domain;
end = strchr(domain, '#');
if (end != NULL)
*end = 0;
end = domain + strlen(domain);
while (end != domain && isspace(*(end - 1)))
*--end = 0;
if (domain[0] == 0)
continue;
if (is_valid_domain_name(domain))
{
domain_set_add(*local_domains, domain);
if (*srs_domain == NULL)
*srs_domain =
strdup(domain[0] == '.' ? domain + 1 : domain);
}
else
{
if (is_valid_domain_name(domain))
{
domain_set_add(*local_domains, domain);
if (*srs_domain == NULL)
*srs_domain =
strdup(domain[0] == '.' ? domain + 1 : domain);
}
else
{
log_error("invalid domain name '%s' in domains file",
domain);
goto fail;
}
log_error("invalid domain name '%s' in domains file",
domain);
goto fail;
}
}
fclose(f);
Expand Down
1 change: 1 addition & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <confuse.h>

cfg_t* config_defaults();
cfg_t* config_from_commandline(int argc, char* const* argv);
srs_t* srs_from_config(cfg_t* cfg);
bool srs_domains_from_config(cfg_t* cfg, char** srs_domain,
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ target_link_libraries(
$<$<BOOL:${WITH_REDIS}>:${HIREDIS_TARGET}>
)
add_postsrsd_test(test_srs2 ${SRCDIR}/srs2.c ${SRCDIR}/sha1.c)
add_postsrsd_test(
test_config ${SRCDIR}/config.c ${SRCDIR}/sha1.c ${SRCDIR}/srs2.c
${SRCDIR}/util.c
)
target_link_libraries(test_config_executable PRIVATE Confuse::Confuse)
76 changes: 76 additions & 0 deletions tests/unit/test_config.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* PostSRSd - Sender Rewriting Scheme daemon for Postfix
* Copyright 2012-2024 Timo Röhling <timo@gaussglocke.de>
* SPDX-License-Identifier: GPL-3.0-only
*
* 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, version 3.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "common.h"
#include "config.h"

#include <check.h>
#include <stdlib.h>
#include <unistd.h>

static char pwd[500];
static char tmpdir[sizeof(pwd) + 7];

void setup_fs()
{
ck_assert_ptr_nonnull(getcwd(pwd, sizeof(pwd)));
strcpy(tmpdir, pwd);
strcat(tmpdir, "/XXXXXX");
ck_assert_ptr_eq(mkdtemp(tmpdir), tmpdir);
ck_assert_int_eq(chdir(tmpdir), 0);
}

void teardown_fs()
{
ck_assert_int_eq(chdir(pwd), 0);
ck_assert_int_eq(rmdir(tmpdir), 0);
}

START_TEST(config_domains_file)
{
FILE* f = fopen("domains.txt", "w");
fprintf(f,
"# This is a comment at the beginning of the file\n"
"example.com\n"
" # This is a comment with preceding white space\n"
"\t tabspace.org\n"
"\n"
"commented.de # This is a comment after a domain name\n"
"trailing.net ");
fclose(f);
cfg_t* cfg = config_defaults();
cfg_setstr(cfg, "domains-file", "domains.txt");

domain_set_t* D = NULL;
char* srs_domain = NULL;
ck_assert_int_eq(srs_domains_from_config(cfg, &srs_domain, &D), true);
ck_assert_int_eq(domain_set_contains(D, "commented.de"), true);
ck_assert_int_eq(domain_set_contains(D, "example.com"), true);
ck_assert_int_eq(domain_set_contains(D, "tabspace.org"), true);
ck_assert_int_eq(domain_set_contains(D, "trailing.net"), true);
ck_assert_str_eq(srs_domain, "example.com");
ck_assert_int_eq(unlink("domains.txt"), 0);
domain_set_destroy(D);
free(srs_domain);
cfg_free(cfg);
}
END_TEST

BEGIN_TEST_SUITE(config)
ADD_TEST_CASE_WITH_UNCHECKED_FIXTURE(fs, setup_fs, teardown_fs)
ADD_TEST_TO_TEST_CASE(fs, config_domains_file)
END_TEST_SUITE()
TEST_MAIN(config)

0 comments on commit 3def248

Please sign in to comment.