forked from dbmail/dbmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdm_config.c
498 lines (422 loc) · 15.3 KB
/
dm_config.c
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
Copyright (C) 1999-2004 IC & S dbmail@ic-s.nl
Copyright (c) 2004-2012 NFG Net Facilities Group BV support@nfg.nl
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; either
version 2 of the License, or (at your option) any later
version.
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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
*
* \file config.c
* \brief read configuration values from a config file
*/
#include <libgen.h>
#include "dbmail.h"
#define THIS_MODULE "config"
DBParam_T db_params;
char configFile[PATH_MAX];
/** dictionary which holds the configuration */
static GKeyFile *config_dict = NULL;
static int configured = 0;
void config_get_file(void)
{
strncpy(configFile, DEFAULT_CONFIG_FILE, sizeof(configFile)-1);
}
//
// local functions
//
int config_create(const char *config_filename)
{
int fd;
int serr;
char *copy = strdup(config_filename);
char *config_dir = dirname(copy);
g_mkdir_with_parents(config_dir, 0700);
free(copy);
if ((fd = open(config_filename, O_RDWR|O_CREAT|O_EXCL, 00600)) == -1) {
serr = errno;
TRACE(TRACE_EMERG, "unable to create [%s]: %s",
config_filename, strerror(serr));
return -1;
}
const char *config = DM_DEFAULT_CONFIGURATION;
ssize_t config_length = (ssize_t)strlen(config);
if (write(fd, config, config_length) < config_length) {
serr = errno;
TRACE(TRACE_EMERG, "error writing [%s] %s",
config_filename, strerror(serr));
return -1;
}
return 0;
}
/**
* read the configuration file and stores the configuration
* directives in an internal structure.
*
* to to create a default configation if possible, using the
* distro dbmail.conf
*/
int config_read(const char *config_filename)
{
if (configured)
config_free();
assert(config_filename != NULL);
struct stat buf;
if (stat(config_filename, &buf) == -1)
config_create(config_filename);
config_dict = g_key_file_new();
if (! g_key_file_load_from_file(config_dict, config_filename, G_KEY_FILE_NONE, NULL)) {
g_key_file_free(config_dict);
TRACE(TRACE_EMERG, "error reading config [%s]", config_filename);
_exit(1);
return -1;
}
// silence the glib logger
g_log_set_default_handler((GLogFunc)null_logger, NULL);
configured = 1;
return 0;
}
/**
* free all memory related to config
*/
void config_free(void)
{
if (!configured) return;
g_key_file_free(config_dict);
config_dict = NULL;
configured = 0;
}
/* Return 1 if found, 0 if not. */
/* This function also strips any... # Trailing comments. */
/* value is not modified unless something is found. */
static int config_get_value_once(const Field_T field_name,
const char * const service_name,
Field_T value)
{
char *dict_value;
int retval = 0;
assert(service_name);
assert(config_dict);
dict_value = g_key_file_get_value(config_dict, service_name, field_name, NULL);
if (dict_value) {
char *end;
end = g_strstr_len(dict_value, FIELDSIZE, "#");
if (end) *end = '\0';
g_strstrip(dict_value);
g_strlcpy(value, dict_value, FIELDSIZE);
g_free(dict_value);
retval = 1;
}
return retval;
}
int config_get_value(const Field_T field_name,
const char * const service_name,
Field_T value)
{
char *key;
gssize field_len;
field_len = strlen(field_name);
// First look in the SERVICE section.
// For each attempt, try as-is, upper, lower.
key = NULL;
if (config_get_value_once(field_name, service_name, value))
goto config_get_value_done;
key = g_ascii_strup(field_name, field_len);
if (config_get_value_once(key, service_name, value))
goto config_get_value_done;
g_free(key);
key = g_ascii_strdown(field_name, field_len);
if (config_get_value_once(key, service_name, value))
goto config_get_value_done;
g_free(key);
// if not found, get the DBMAIL section.
// For each attempt, try as-is, upper, lower.
key = NULL;
if (config_get_value_once(field_name, "DBMAIL", value))
goto config_get_value_done;
key = g_ascii_strup(field_name, field_len);
if (config_get_value_once(key, "DBMAIL", value))
goto config_get_value_done;
g_free(key);
key = g_ascii_strdown(field_name, field_len);
if (config_get_value_once(key, "DBMAIL", value))
goto config_get_value_done;
g_free(key);
/* give up */
value[0] = '\0';
return -1;
config_get_value_done:
g_free(key);
return 0;
}
void SetTraceLevel(const char *service_name)
{
Trace_T trace_stderr_int, trace_syslog_int;
Field_T trace_level, trace_syslog, trace_stderr, syslog_logging_levels, file_logging_levels;
/* Warn about the deprecated "trace_level" config item,
* but we will use this value for trace_syslog if needed. */
config_get_value("trace_level", service_name, trace_level);
if (strlen(trace_level)) {
TRACE(TRACE_ERR,
"Config item TRACE_LEVEL is deprecated and ignored. "
"Please use SYSLOG_LOGGING_LEVELS and FILE_LOGGING_LEVELS instead.");
}
config_get_value("syslog_logging_levels", service_name, syslog_logging_levels);
config_get_value("file_logging_levels", service_name, file_logging_levels);
if (strlen(syslog_logging_levels)) {
trace_syslog_int = atoi(syslog_logging_levels);
} else {
config_get_value("trace_syslog", service_name, trace_syslog);
if (strlen(trace_syslog)) {
TRACE(TRACE_WARNING,
"Config item TRACE_SYSLOG is deprecated. "
"Please use SYSLOG_LOGGING_LEVELS and FILE_LOGGING_LEVELS instead.");
int old_syslog_int = atoi(trace_syslog);
switch(old_syslog_int) { // Convert old value to new system
case 0:
trace_syslog_int = TRACE_EMERG | TRACE_ALERT | TRACE_CRIT;
break;
case 1:
trace_syslog_int = TRACE_EMERG | TRACE_ALERT | TRACE_CRIT | TRACE_ERR;
break;
case 2:
trace_syslog_int = TRACE_EMERG | TRACE_ALERT | TRACE_CRIT | TRACE_ERR | TRACE_WARNING;
break;
case 3:
trace_syslog_int = TRACE_EMERG | TRACE_ALERT | TRACE_CRIT | TRACE_ERR | TRACE_WARNING | TRACE_NOTICE;
break;
case 4:
trace_syslog_int = TRACE_EMERG | TRACE_ALERT | TRACE_CRIT | TRACE_ERR | TRACE_WARNING | TRACE_NOTICE | TRACE_INFO;
break;
case 5:
default:
trace_syslog_int = TRACE_EMERG | TRACE_ALERT | TRACE_CRIT | TRACE_ERR | TRACE_WARNING | TRACE_NOTICE | TRACE_INFO | TRACE_DEBUG;
break;
}
} else {
trace_syslog_int = TRACE_EMERG | TRACE_ALERT | TRACE_CRIT | TRACE_ERR | TRACE_WARNING; // Use Default Levels
}
}
if (strlen(file_logging_levels)) {
trace_stderr_int = atoi(file_logging_levels);
} else {
config_get_value("trace_stderr", service_name, trace_stderr);
if (strlen(trace_stderr)) {
TRACE(TRACE_WARNING,
"Config item TRACE_STDERR is deprecated. "
"Please use SYSLOG_LOGGING_LEVELS and FILE_LOGGING_LEVELS instead.");
int old_stderr_int = atoi(trace_stderr);
switch(old_stderr_int) { // Convert old value to new system
case 0:
trace_stderr_int = TRACE_EMERG | TRACE_ALERT | TRACE_CRIT;
break;
case 1:
trace_stderr_int = TRACE_EMERG | TRACE_ALERT | TRACE_CRIT | TRACE_ERR;
break;
case 2:
trace_stderr_int = TRACE_EMERG | TRACE_ALERT | TRACE_CRIT | TRACE_ERR | TRACE_WARNING;
break;
case 3:
trace_stderr_int = TRACE_EMERG | TRACE_ALERT | TRACE_CRIT | TRACE_ERR | TRACE_WARNING | TRACE_NOTICE;
break;
case 4:
trace_stderr_int = TRACE_EMERG | TRACE_ALERT | TRACE_CRIT | TRACE_ERR | TRACE_WARNING | TRACE_NOTICE | TRACE_INFO;
break;
case 5:
default:
trace_stderr_int = TRACE_EMERG | TRACE_ALERT | TRACE_CRIT | TRACE_ERR | TRACE_WARNING | TRACE_NOTICE | TRACE_INFO | TRACE_DEBUG;
break;
}
} else {
trace_stderr_int = TRACE_EMERG | TRACE_ALERT | TRACE_CRIT; // Use Default Levels
}
}
configure_debug(service_name, trace_syslog_int, trace_stderr_int);
}
void GetDBParams(void)
{
Field_T port_string, sock_string, serverid_string, query_time;
Field_T max_db_connections;
if (config_get_value("dburi", "DBMAIL", db_params.dburi) < 0) {
TRACE(TRACE_WARNING, "deprecation warning! [dburi] missing");
if (config_get_value("driver", "DBMAIL", db_params.driver) < 0)
TRACE(TRACE_EMERG, "error getting config! [driver]");
if (MATCH((const char *)db_params.driver,"sqlite"))
db_params.db_driver = DM_DRIVER_SQLITE;
else if (MATCH((const char *)db_params.driver,"mysql"))
db_params.db_driver = DM_DRIVER_MYSQL;
else if (MATCH((const char *)db_params.driver,"postgresql"))
db_params.db_driver = DM_DRIVER_POSTGRESQL;
else if (MATCH((const char *)db_params.driver,"oracle"))
db_params.db_driver = DM_DRIVER_ORACLE;
else
TRACE(TRACE_EMERG,"driver not supported");
if (config_get_value("host", "DBMAIL", db_params.host) < 0)
TRACE(TRACE_EMERG, "error getting config! [host]");
if (config_get_value("db", "DBMAIL", db_params.db) < 0)
TRACE(TRACE_EMERG, "error getting config! [db]");
if (config_get_value("user", "DBMAIL", db_params.user) < 0)
TRACE(TRACE_EMERG, "error getting config! [user]");
if (config_get_value("pass", "DBMAIL", db_params.pass) < 0)
TRACE(TRACE_DEBUG, "error getting config! [pass]");
if (config_get_value("sqlport", "DBMAIL", port_string) < 0)
TRACE(TRACE_DEBUG, "error getting config! [sqlpost]");
if (config_get_value("sqlsocket", "DBMAIL", sock_string) < 0)
TRACE(TRACE_DEBUG, "error getting config! [sqlsocket]");
/* check if port_string holds a value */
if (strlen(port_string) != 0) {
errno = 0;
db_params.port =
(unsigned int) strtoul(port_string, NULL, 10);
if (errno == EINVAL || errno == ERANGE)
TRACE(TRACE_EMERG, "wrong value for sqlport in config file [%s]", strerror(errno));
} else
db_params.port = 0;
/* same for sock_string */
if (strlen(sock_string) != 0)
g_strlcpy(db_params.sock, sock_string, FIELDSIZE);
else
db_params.sock[0] = '\0';
} else {
/* expand ~ in db name to HOME env variable */
if (strncmp(db_params.dburi, "sqlite://~", 10) == 0) {
char *rest = index(db_params.dburi, '~');
char *homedir;
Field_T dburi;
if (strlen(rest) < 3)
TRACE(TRACE_EMERG, "invalid filename for sqlite database");
rest++;
if ((homedir = getenv ("HOME")) == NULL)
TRACE(TRACE_EMERG, "can't expand ~ in db name");
g_snprintf(dburi, FIELDSIZE, "sqlite://%s%s", homedir, rest);
g_strlcpy(db_params.dburi, dburi, FIELDSIZE);
}
}
if (config_get_value("authdriver", "DBMAIL", db_params.authdriver) < 0)
TRACE(TRACE_DEBUG, "missing config! [authdriver]");
if (config_get_value("sortdriver", "DBMAIL", db_params.sortdriver) < 0)
TRACE(TRACE_DEBUG, "error getting config! [sortdriver]");
if (config_get_value("serverid", "DBMAIL", serverid_string) < 0)
TRACE(TRACE_DEBUG, "error getting config! [serverid]");
if (config_get_value("encoding", "DBMAIL", db_params.encoding) < 0)
TRACE(TRACE_DEBUG, "error getting config! [encoding]");
if (config_get_value("table_prefix", "DBMAIL", db_params.pfx) < 0)
TRACE(TRACE_DEBUG, "error getting config! [table_prefix]");
if (config_get_value("max_db_connections", "DBMAIL", max_db_connections) < 0)
TRACE(TRACE_DEBUG, "error getting config! [max_db_connections]");
if (config_get_value("query_time_info", "DBMAIL", query_time) < 0)
TRACE(TRACE_DEBUG, "error getting config! [query_time_info]");
if (strlen(query_time) != 0)
db_params.query_time_info = (unsigned int) strtoul(query_time, NULL, 10);
else
db_params.query_time_info = 10;
if (config_get_value("query_time_notice", "DBMAIL", query_time) < 0)
TRACE(TRACE_DEBUG, "error getting config! [query_time_notice]");
if (strlen(query_time) != 0)
db_params.query_time_notice = (unsigned int) strtoul(query_time, NULL, 10);
else
db_params.query_time_notice = 20;
if (config_get_value("query_time_warning", "DBMAIL", query_time) < 0)
TRACE(TRACE_DEBUG, "error getting config! [query_time_warning]");
if (strlen(query_time) != 0)
db_params.query_time_warning = (unsigned int) strtoul(query_time, NULL, 10);
else
db_params.query_time_warning = 30;
if (config_get_value("query_timeout", "DBMAIL", query_time) < 0)
TRACE(TRACE_DEBUG, "error getting config! [query_timeout]");
if (strlen(query_time) != 0)
db_params.query_timeout = (unsigned int) strtoul(query_time, NULL, 10) * 1000;
else
db_params.query_timeout = 300000;
if (strcmp(db_params.pfx, "\"\"") == 0) {
/* FIXME: It appears that when the empty string is quoted
* that the quotes themselves are returned as the value. */
g_strlcpy(db_params.pfx, "", FIELDSIZE);
} else if (strlen(db_params.pfx) == 0) {
/* If it's not "" but is zero length, set the default. */
g_strlcpy(db_params.pfx, DEFAULT_DBPFX, FIELDSIZE);
}
/* serverid */
if (strlen(serverid_string) != 0) {
db_params.serverid = (unsigned int) strtol(serverid_string, NULL, 10);
if (errno == EINVAL || errno == ERANGE)
TRACE(TRACE_EMERG, "serverid invalid in config file");
} else {
db_params.serverid = 1;
}
/* max_db_connections */
if (strlen(max_db_connections) != 0) {
db_params.max_db_connections = (unsigned int) strtol(max_db_connections, NULL, 10);
if (errno == EINVAL || errno == ERANGE)
TRACE(TRACE_EMERG, "max_db_connnections invalid in config file");
} else {
db_params.max_db_connections = 10;
}
}
void config_get_timeout(ServerConfig_T *config, const char * const service)
{
Field_T val;
/* read items: TIMEOUT */
config_get_value("TIMEOUT", service, val);
if (strlen(val) == 0) {
TRACE(TRACE_DEBUG, "no value for TIMEOUT in config file");
config->timeout = 300;
} else if ((config->timeout = atoi(val)) <= 30)
TRACE(TRACE_EMERG, "value for TIMEOUT is invalid: [%d]", config->timeout);
TRACE(TRACE_DEBUG, "timeout [%d] seconds", config->timeout);
/* read items: LOGIN_TIMEOUT */
config_get_value("LOGIN_TIMEOUT", service, val);
if (strlen(val) == 0) {
TRACE(TRACE_DEBUG, "no value for TIMEOUT in config file");
config->login_timeout = 60;
} else if ((config->login_timeout = atoi(val)) <= 10)
TRACE(TRACE_EMERG, "value for TIMEOUT is invalid: [%d]", config->login_timeout);
TRACE(TRACE_DEBUG, "login_timeout [%d] seconds",
config->login_timeout);
}
void config_get_logfiles(ServerConfig_T *config, const char * const service)
{
Field_T val;
/* logfile */
config_get_value("logfile", service, val);
if (! strlen(val))
g_strlcpy(config->log,DEFAULT_LOG_FILE, FIELDSIZE);
else
g_strlcpy(config->log, val, FIELDSIZE);
assert(config->log);
/* errorlog */
config_get_value("errorlog", service, val);
if (! strlen(val))
g_strlcpy(config->error_log,DEFAULT_ERROR_LOG, FIELDSIZE);
else
g_strlcpy(config->error_log, val, FIELDSIZE);
assert(config->error_log);
/* pid directory */
config_get_value("pid_directory", service, val);
if (! strlen(val))
g_strlcpy(config->pid_dir, LOCALSTATEDIR, FIELDSIZE);
else
g_strlcpy(config->pid_dir, val, FIELDSIZE);
assert(config->pid_dir);
}
char * config_get_pidfile(ServerConfig_T *config, const char *name)
{
char *res;
GString *s;
res = g_build_filename(config->pid_dir, name, NULL);
s = g_string_new("");
g_string_printf(s, "%s%s", res, DEFAULT_PID_EXT);
g_free(res);
res = s->str;
g_string_free(s,FALSE);
return res;
}