Skip to content

Commit 026c7f5

Browse files
committed
daemons: avoid dying with SIGSEGV when config is unreadable
config_file::get_value was called with this==null. Catch that.
1 parent 36a1c22 commit 026c7f5

File tree

13 files changed

+49
-16
lines changed

13 files changed

+49
-16
lines changed

exch/http/main.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ static bool http_reload_config(std::shared_ptr<CONFIG_FILE> xcfg = nullptr,
136136
mlog(LV_ERR, "config_file_init %s: %s", opt_config_file, strerror(errno));
137137
return false;
138138
}
139+
if (cfg == nullptr)
140+
return false;
139141
mlog_init("gromox-http", cfg->get_value("http_log_file"),
140142
cfg->get_ll("http_log_level"), cfg->get_value("running_identity"));
141143
g_http_debug = cfg->get_ll("http_debug");
@@ -149,6 +151,8 @@ static bool http_reload_config(std::shared_ptr<CONFIG_FILE> xcfg = nullptr,
149151
mlog(LV_ERR, "config_file_init %s: %s", opt_config_file, strerror(errno));
150152
return false;
151153
}
154+
if (xcfg == nullptr)
155+
return false;
152156
g_http_remote_host_hdr = znul(xcfg->get_value("http_remote_host_hdr"));
153157
g_http_basic_auth_validity = std::chrono::duration_cast<time_duration>(std::chrono::nanoseconds(xcfg->get_ll("http_basic_auth_cred_caching")));
154158
return true;
@@ -187,7 +191,9 @@ int main(int argc, char **argv)
187191
auto gxconfig = config_file_prg(opt_config_file, "gromox.cfg", gromox_cfg_defaults);
188192
if (opt_config_file != nullptr && gxconfig == nullptr)
189193
mlog(LV_ERR, "%s: %s", opt_config_file, strerror(errno));
190-
if (g_config_file == nullptr || !http_reload_config(gxconfig, g_config_file))
194+
if (g_config_file == nullptr || gxconfig == nullptr)
195+
return EXIT_FAILURE; /* e.g. permission error */
196+
if (!http_reload_config(gxconfig, g_config_file))
191197
return EXIT_FAILURE;
192198

193199
auto str_val = g_config_file->get_value("host_id");

exch/midb/main.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ static bool midb_reload_config(std::shared_ptr<config_file> gxconfig = nullptr,
119119
mlog(LV_ERR, "config_file_init %s: %s", opt_config_file, strerror(errno));
120120
return false;
121121
}
122+
if (pconfig == nullptr)
123+
return false;
122124
mlog_init("gromox-midb", pconfig->get_value("midb_log_file"),
123125
pconfig->get_ll("midb_log_level"),
124126
pconfig->get_value("running_identity"));
@@ -297,11 +299,11 @@ int main(int argc, char **argv)
297299
midb_cfg_defaults);
298300
if (opt_config_file != nullptr && pconfig == nullptr)
299301
mlog(LV_ERR, "system: config_file_init %s: %s", opt_config_file, strerror(errno));
300-
if (pconfig == nullptr)
301-
return EXIT_FAILURE;
302302
auto gxconfig = config_file_prg(opt_config_file, "gromox.cfg", gromox_cfg_defaults);
303303
if (opt_config_file != nullptr && gxconfig == nullptr)
304304
mlog(LV_ERR, "%s: %s", opt_config_file, strerror(errno));
305+
if (pconfig == nullptr || gxconfig == nullptr)
306+
return EXIT_FAILURE; /* e.g. permission error */
305307
if (!midb_reload_config(gxconfig, pconfig))
306308
return EXIT_FAILURE;
307309

exch/zcore/main.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ static bool zcore_reload_config(std::shared_ptr<CONFIG_FILE> gxcfg = nullptr,
130130
mlog(LV_ERR, "config_file_init %s: %s", opt_config_file, strerror(errno));
131131
return false;
132132
}
133+
if (gxcfg == nullptr)
134+
return false;
133135
zcore_backfill_transporthdr = gxcfg->get_ll("backfill_transport_headers");
134136

135137
if (pconfig == nullptr)
@@ -140,6 +142,8 @@ static bool zcore_reload_config(std::shared_ptr<CONFIG_FILE> gxcfg = nullptr,
140142
opt_config_file, strerror(errno));
141143
return false;
142144
}
145+
if (pconfig == nullptr)
146+
return false;
143147
mlog_init("gromox-zcore", pconfig->get_value("zcore_log_file"),
144148
pconfig->get_ll("zcore_log_level"),
145149
pconfig->get_value("running_identity"));
@@ -263,11 +267,12 @@ int main(int argc, char **argv)
263267
if (opt_config_file != nullptr && pconfig == nullptr)
264268
mlog(LV_ERR, "system: config_file_init %s: %s",
265269
opt_config_file, strerror(errno));
266-
if (pconfig == nullptr)
267-
return EXIT_FAILURE;
268270
auto gxconfig = config_file_prg(opt_config_file, "gromox.cfg", zcore_gxcfg_dflt);
269271
if (opt_config_file != nullptr && gxconfig == nullptr)
270272
mlog(LV_ERR, "%s: %s", opt_config_file, strerror(errno));
273+
if (pconfig == nullptr || gxconfig == nullptr)
274+
/* e.g. permission error */
275+
return EXIT_FAILURE;
271276
if (!zcore_reload_config(gxconfig, pconfig))
272277
return EXIT_FAILURE;
273278

lib/ruleproc.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1356,14 +1356,17 @@ BOOL SVC_ruleproc(enum plugin_op reason, const struct dlfuncs &param)
13561356
if (!register_service("rules_execute", exmdb_local_rules_execute))
13571357
return false;
13581358
auto cfg = config_file_prg(nullptr, "gromox.cfg", rp_config_defaults);
1359+
if (cfg == nullptr)
1360+
/* e.g. permission error */
1361+
return false;
13591362
auto str = cfg->get_value("outgoing_smtp_url");
13601363
if (str != nullptr) {
13611364
try {
13621365
rp_smtp_url = vmime::utility::url(str);
13631366
} catch (const vmime::exceptions::malformed_url &e) {
13641367
mlog(LV_ERR, "Malformed URL: outgoing_smtp_url=\"%s\": %s",
13651368
str, e.what());
1366-
return EXIT_FAILURE;
1369+
return false;
13671370
}
13681371
}
13691372
return TRUE;

mda/delivery_app/main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ static bool delivery_reload_config(std::shared_ptr<CONFIG_FILE> cfg)
8383
mlog(LV_ERR, "config_file_init %s: %s", opt_config_file, strerror(errno));
8484
return false;
8585
}
86+
if (cfg == nullptr)
87+
return false;
8688
mlog_init("gromox-delivery", cfg->get_value("lda_log_file"),
8789
cfg->get_ll("lda_log_level"), cfg->get_value("running_identity"));
8890
return true;
@@ -118,7 +120,9 @@ int main(int argc, char **argv)
118120
auto gxconfig = config_file_prg(opt_config_file, "gromox.cfg", gromox_cfg_defaults);
119121
if (opt_config_file != nullptr && gxconfig == nullptr)
120122
mlog(LV_ERR, "%s: %s", opt_config_file, strerror(errno));
121-
if (g_config_file == nullptr || !delivery_reload_config(g_config_file))
123+
if (g_config_file == nullptr || gxconfig == nullptr)
124+
return EXIT_FAILURE; /* e.g. permission error */
125+
if (!delivery_reload_config(g_config_file))
122126
return EXIT_FAILURE;
123127

124128
auto str_val = g_config_file->get_value("host_id");

mda/smtp/main.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ static bool dq_reload_config(std::shared_ptr<CONFIG_FILE> gxcfg = nullptr,
116116
mlog(LV_ERR, "config_file_init %s: %s", opt_config_file, strerror(errno));
117117
return false;
118118
}
119+
if (gxcfg == nullptr)
120+
return false;
119121
g_rcpt_delimiter = znul(gxcfg->get_value("lda_recipient_delimiter"));
120122
g_haproxy_level = gxcfg->get_ll("lda_accept_haproxy");
121123
if (g_haproxy_level > 0)
@@ -290,8 +292,8 @@ int main(int argc, char **argv)
290292
auto gxconfig = config_file_prg(opt_config_file, "gromox.cfg", gromox_cfg_defaults);
291293
if (opt_config_file != nullptr && gxconfig == nullptr)
292294
mlog(LV_ERR, "%s: %s", opt_config_file, strerror(errno));
293-
if (g_config_file == nullptr)
294-
return EXIT_FAILURE;
295+
if (g_config_file == nullptr || gxconfig == nullptr)
296+
return EXIT_FAILURE; /* e.g. permission error */
295297
if (!dq_reload_config(gxconfig, g_config_file))
296298
return EXIT_FAILURE;
297299

mra/imap/main.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ static bool imap_reload_config(std::shared_ptr<config_file> gxcfg = nullptr,
137137
fprintf(stderr, "config_file_init %s: %s\n", opt_config_file, strerror(errno));
138138
return false;
139139
}
140+
if (cfg == nullptr)
141+
return false;
140142
mlog_init("gromox-imap", cfg->get_value("imap_log_file"),
141143
cfg->get_ll("imap_log_level"), cfg->get_value("running_identity"));
142144
g_imapcmd_debug = cfg->get_ll("imap_cmd_debug");
@@ -397,11 +399,11 @@ int main(int argc, char **argv)
397399
imap_cfg_defaults);
398400
if (opt_config_file != nullptr && g_config_file == nullptr)
399401
printf("[resource]: config_file_init %s: %s\n", opt_config_file, strerror(errno));
400-
if (g_config_file == nullptr)
401-
return EXIT_FAILURE;
402402
auto gxconfig = config_file_prg(opt_config_file, "gromox.cfg", gromox_cfg_defaults);
403403
if (opt_config_file != nullptr && gxconfig == nullptr)
404404
mlog(LV_ERR, "%s: %s", opt_config_file, strerror(errno));
405+
if (g_config_file == nullptr || gxconfig == nullptr)
406+
return EXIT_FAILURE; /* e.g. permission error */
405407
if (!imap_reload_config(gxconfig, g_config_file))
406408
return EXIT_FAILURE;
407409

mra/pop3/main.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ static bool pop3_reload_config(std::shared_ptr<config_file> gxcfg = nullptr,
134134
printf("config_file_init %s: %s\n", opt_config_file, strerror(errno));
135135
return false;
136136
}
137+
if (pconfig == nullptr)
138+
return false;
137139
mlog_init("gromox-pop3", pconfig->get_value("pop3_log_file"),
138140
pconfig->get_ll("pop3_log_level"),
139141
pconfig->get_value("running_identity"));
@@ -145,6 +147,8 @@ static bool pop3_reload_config(std::shared_ptr<config_file> gxcfg = nullptr,
145147
mlog(LV_ERR, "config_file_init %s: %s", opt_config_file, strerror(errno));
146148
return false;
147149
}
150+
if (gxcfg == nullptr)
151+
return false;
148152
g_haproxy_level = gxcfg->get_ll("pop3_accept_haproxy");
149153
if (g_haproxy_level > 0)
150154
mlog(LV_NOTICE, "All incoming connections must be HAPROXY type %u", g_haproxy_level);
@@ -382,11 +386,11 @@ int main(int argc, char **argv)
382386
pop3_cfg_defaults);
383387
if (opt_config_file != nullptr && g_config_file == nullptr)
384388
printf("[resource]: config_file_init %s: %s\n", opt_config_file, strerror(errno));
385-
if (g_config_file == nullptr)
386-
return EXIT_FAILURE;
387389
auto gxconfig = config_file_prg(opt_config_file, "gromox.cfg", gromox_cfg_defaults);
388390
if (opt_config_file != nullptr && gxconfig == nullptr)
389391
mlog(LV_ERR, "%s: %s", opt_config_file, strerror(errno));
392+
if (g_config_file == nullptr || gxconfig == nullptr)
393+
return EXIT_FAILURE; /* e.g. permission error */
390394
if (!pop3_reload_config(gxconfig, g_config_file))
391395
return EXIT_FAILURE;
392396

tools/authtry.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ int main(int argc, char **argv)
113113
return direct_ldap(g_ldap_uri, g_auth_user, password);
114114

115115
auto cfg = config_file_prg(nullptr, "http.cfg", no_defaults);
116+
if (cfg == nullptr)
117+
return EXIT_FAILURE; /* permission error */
116118
service_init({std::move(cfg), g_dfl_svc_plugins, 0, "authtest"});
117119
auto cl_1 = HX::make_scope_exit(service_stop);
118120
if (service_run_early() != 0) {

tools/eml2mt.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ int main(int argc, char **argv) try
314314
textmaps_init(PKGDATADIR);
315315
g_config_file = config_file_prg(nullptr, "midb.cfg", eml2mt_cfg_defaults);
316316
if (g_config_file == nullptr) {
317-
fprintf(stderr, "Something went wrong with config files\n");
317+
fprintf(stderr, "Something went wrong with config files (e.g. permission denied)\n");
318318
return EXIT_FAILURE;
319319
}
320320
service_init({g_config_file, g_dfl_svc_plugins, 1});
@@ -345,6 +345,8 @@ int main(int argc, char **argv) try
345345
}
346346

347347
auto cfg = config_file_prg(nullptr, "delivery.cfg", delivery_cfg_defaults);
348+
if (cfg == nullptr)
349+
return EXIT_FAILURE;
348350
std::vector<message_ptr> msgs;
349351

350352
for (int i = 1; i < argc; ++i) {

0 commit comments

Comments
 (0)