1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
// SPDX-FileCopyrightText: 2025 Markus Uhlin <maxxe@rpblc.net>
// SPDX-License-Identifier: ISC
#include <err.h>
#include <errno.h>
#include <string>
#include <vector>
#include "addgroup.h"
#include "copyfile.h"
#include "interpreter.h"
#include "settings.h"
#if __linux__
#include <bsd/string.h>
#endif
struct setting {
std::string name;
enum setting_type type;
std::string value;
setting()
{
this->name.assign("");
this->type = STYPE_STRING;
this->value.assign("");
}
setting(const char *p_name, enum setting_type p_type,
const char *p_value)
{
this->name.assign(p_name);
this->type = p_type;
this->value.assign(p_value);
}
};
static std::vector<setting> settings;
#if defined(__cplusplus) && __cplusplus >= 201103L
#define SET_PB(obj) settings.emplace_back(obj)
#else
#define SET_PB(obj) settings.push_back(obj)
#endif
void
settings_init(void)
{
struct setting s1("server_hostname", STYPE_STRING, "");
struct setting s2("server_name", STYPE_STRING, "Xfics");
struct setting s3("server_location", STYPE_STRING, "");
struct setting s4("HADMINHANDLE", STYPE_STRING, "");
struct setting s5("HADMINEMAIL", STYPE_STRING, "");
struct setting s6("REGMAIL", STYPE_STRING, "");
struct setting s7("privdrop_user", STYPE_STRING, "nobody");
struct setting s8("sysgroup", STYPE_STRING, "chess");
SET_PB(s1);
SET_PB(s2);
SET_PB(s3);
SET_PB(s4);
SET_PB(s5);
SET_PB(s6);
SET_PB(s7);
SET_PB(s8);
}
void
settings_deinit(void)
{
fprintf(stderr, "FICS: Deinitialized the conf settings\n");
}
static bool
is_recognized_setting(const char *name)
{
if (name == nullptr || strings_match(name, ""))
return false;
for (auto it = settings.begin(); it != settings.end(); ++it) {
if (strings_match((*it).name.c_str(), name))
return true;
}
return false;
}
static int
install_setting(const char *name, const char *value)
{
if (name == nullptr || value == nullptr)
return EINVAL;
for (auto it = settings.begin(); it != settings.end(); ++it) {
if (strings_match((*it).name.c_str(), name)) {
(*it).value.assign(value);
return 0;
}
}
return ENOENT;
}
void
check_some_settings_strictly(void)
{
err_reason_t reason;
if (!is_valid_hostname(settings_get("server_hostname"), &reason))
errx(1, "error: server_hostname: %s", reason.data);
else if (!is_valid_username(settings_get("privdrop_user"), &reason))
errx(1, "error: privdrop_user: %s", reason.data);
else if (!is_valid_group_name(settings_get("sysgroup")))
errx(1, "error: sysgroup: invalid group name");
}
bool
is_valid_hostname(const char *p_str, err_reason_t *p_reason)
{
const char legal_index[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789-.";
const size_t HOST_MIN = 3;
const size_t HOST_MAX = 253;
size_t len = 0;
if (p_str == nullptr || strcmp(p_str, "") == 0) {
strlcpy(p_reason->data, "no input", sizeof p_reason->data);
return false;
} else if ((len = strlen(p_str)) < HOST_MIN) {
snprintf(p_reason->data, sizeof p_reason->data, "hostname too "
"short (%zu): min=%zu", len, HOST_MIN);
return false;
} else if (len > HOST_MAX) {
snprintf(p_reason->data, sizeof p_reason->data, "hostname too "
"long (%zu): max=%zu", len, HOST_MAX);
return false;
} else if (strstr(p_str, "..")) {
strlcpy(p_reason->data, "dot followed by dot",
sizeof p_reason->data);
return false;
}
for (const char *cp = p_str; *cp != '\0'; cp++) {
if (strchr(legal_index, *cp) == nullptr) {
snprintf(p_reason->data, sizeof p_reason->data,
"invalid chars found: first was '%c'", *cp);
return false;
}
}
return true;
}
bool
is_valid_username(const char *p_str, err_reason_t *p_reason)
{
const char legal_index[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789_-";
const size_t USER_MIN = 2;
const size_t USER_MAX = 30;
size_t len = 0;
if (p_str == nullptr || strcmp(p_str, "") == 0) {
strlcpy(p_reason->data, "no input", sizeof p_reason->data);
return false;
} else if ((len = strlen(p_str)) < USER_MIN) {
snprintf(p_reason->data, sizeof p_reason->data, "username too "
"short (%zu): min=%zu", len, USER_MIN);
return false;
} else if (len > USER_MAX) {
snprintf(p_reason->data, sizeof p_reason->data, "username too "
"long (%zu): max=%zu", len, USER_MAX);
return false;
}
for (const char *cp = p_str; *cp != '\0'; cp++) {
if (strchr(legal_index, *cp) == nullptr) {
snprintf(p_reason->data, sizeof p_reason->data,
"invalid chars found: first was '%c'", *cp);
return false;
}
}
return true;
}
const char *
settings_get(const char *set_name)
{
if (set_name == nullptr || strings_match(set_name, ""))
return ("");
for (auto it = settings.begin(); it != settings.end(); ++it) {
if (strings_match((*it).name.c_str(), set_name))
return (*it).value.c_str();
}
return ("");
}
void
settings_read_conf(const char *path)
{
FILE *fp = nullptr;
if (!is_regular_file(path)) {
errx(1, "%s: either the config file is nonexistent"
" -- or it isn't a regular file", __func__);
} else if ((fp = fopen(path, "r")) == nullptr) {
err(1, "%s: fopen", __func__);
}
Interpreter_processAllLines(fp, path, is_recognized_setting,
install_setting);
if (feof(fp))
fclose(fp);
else if (ferror(fp))
errx(1, "%s: %s", __func__, g_fgets_nullret_err1);
else
errx(1, "%s: %s", __func__, g_fgets_nullret_err2);
}
|