Skip to content

Commit 889e867

Browse files
committed
make ascii control characters in object names illegal
It is a bad idea to create objects containing newlines or tabs but it's not that easy to add those characters to the illegal_object_name_chars list. So make them illegal by default. Also add a usefull log message help debugging the issue. Signed-off-by: Sven Nierlein <sven@consol.de>
1 parent f9cc604 commit 889e867

File tree

7 files changed

+61
-3
lines changed

7 files changed

+61
-3
lines changed

src/naemon/objects_common.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "nm_alloc.h"
44
#include "xodtemplate.h"
55
#include <string.h>
6+
#include <ctype.h>
67

78
char *illegal_object_chars = NULL;
89

@@ -94,7 +95,7 @@ int contains_illegal_object_chars(const char *name)
9495
register int x = 0;
9596
register int y = 0;
9697

97-
if (name == NULL || illegal_object_chars == NULL)
98+
if (name == NULL)
9899
return FALSE;
99100

100101
x = (int)strlen(name) - 1;
@@ -103,8 +104,15 @@ int contains_illegal_object_chars(const char *name)
103104
/* illegal user-specified characters */
104105
if (illegal_object_chars != NULL)
105106
for (y = 0; illegal_object_chars[y]; y++)
106-
if (name[x] == illegal_object_chars[y])
107+
if (name[x] == illegal_object_chars[y]) {
108+
nm_log(NSLOG_CONFIG_ERROR, "Error: illegal ascii character dec(%d) at pos %d in '%s'\n", (unsigned int)name[x], x, name);
107109
return TRUE;
110+
}
111+
/* ascii control codes are illegal */
112+
if (iscntrl(name[x])) {
113+
nm_log(NSLOG_CONFIG_ERROR, "Error: illegal ascii character dec(%d) at pos %d in '%s'\n", (unsigned int)name[x], x, name);
114+
return TRUE;
115+
}
108116
}
109117

110118
return FALSE;

tests.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ distclean-local:
4040
CLEANFILES += t-tap/smallconfig/naemon.log
4141
EXTRA_DIST += t-tap/smallconfig/minimal.cfg t-tap/smallconfig/naemon.cfg \
4242
t-tap/smallconfig/resource.cfg t-tap/smallconfig/retention.dat
43-
EXTRA_DIST += tests/configs/recursive tests/configs/services tests/configs/inc
43+
EXTRA_DIST += tests/configs/recursive tests/configs/services tests/configs/inc tests/configs/umlauts tests/configs/tabs
4444
EXTRA_DIST += $(dist_check_SCRIPTS)
4545
EXTRA_DIST += t/etc/* t/var/*
4646
TESTS_ENVIRONMENT = \

tests/configs/tabs/host.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
define host {
2+
host_name H stname
3+
max_check_attempts 1
4+
}
5+

tests/configs/tabs/naemon.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
log_file=foo
2+
cfg_file=host.cfg

tests/configs/umlauts/host.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
define host {
2+
host_name Höstname
3+
max_check_attempts 1
4+
}
5+

tests/configs/umlauts/naemon.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
log_file=foo
2+
cfg_file=host.cfg

tests/test-config.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,40 @@ START_TEST(main_include)
102102
}
103103
END_TEST
104104

105+
START_TEST(umlauts)
106+
{
107+
int res;
108+
objcfg_files = NULL;
109+
objcfg_dirs = NULL;
110+
config_file_dir = nspath_absolute_dirname(TESTDIR "umlauts/naemon.cfg", NULL);
111+
config_rel_path = nm_strdup(config_file_dir);
112+
res = read_main_config_file(TESTDIR "umlauts/naemon.cfg");
113+
ck_assert_int_eq(OK, res);
114+
ck_assert(NULL != objcfg_files);
115+
res = read_all_object_data(TESTDIR "umlauts/naemon.cfg");
116+
ck_assert_int_eq(OK, res);
117+
nm_free(config_file_dir);
118+
nm_free(config_rel_path);
119+
}
120+
END_TEST
121+
122+
START_TEST(tabs)
123+
{
124+
int res;
125+
objcfg_files = NULL;
126+
objcfg_dirs = NULL;
127+
config_file_dir = nspath_absolute_dirname(TESTDIR "tabs/naemon.cfg", NULL);
128+
config_rel_path = nm_strdup(config_file_dir);
129+
res = read_main_config_file(TESTDIR "tabs/naemon.cfg");
130+
ck_assert_int_eq(OK, res);
131+
ck_assert(NULL != objcfg_files);
132+
res = read_all_object_data(TESTDIR "tabs/naemon.cfg");
133+
ck_assert_int_eq(ERROR, res);
134+
nm_free(config_file_dir);
135+
nm_free(config_rel_path);
136+
}
137+
END_TEST
138+
105139
Suite *
106140
config_suite(void)
107141
{
@@ -110,6 +144,8 @@ config_suite(void)
110144
tcase_add_test(parse, recursive);
111145
tcase_add_test(parse, services);
112146
tcase_add_test(parse, main_include);
147+
tcase_add_test(parse, umlauts);
148+
tcase_add_test(parse, tabs);
113149
suite_add_tcase(s, parse);
114150
return s;
115151
}

0 commit comments

Comments
 (0)