new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / os_dbd / config.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All rights reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation.
8  */
9
10 #include "dbd.h"
11 #include "config/global-config.h"
12 #include "config/config.h"
13
14
15 /* Read database configuration */
16 int OS_ReadDBConf(__attribute__((unused)) int test_config, const char *cfgfile, DBConfig *db_config)
17 {
18     int modules = 0;
19     _Config *tmp_config;
20
21     /* Modules for the configuration */
22     modules |= CDBD;
23     modules |= CRULES;
24
25     /* Allocate config just to get the rules */
26     os_calloc(1, sizeof(_Config), tmp_config);
27
28     /* Clear configuration variables */
29     tmp_config->includes = NULL;
30     db_config->includes = NULL;
31     db_config->host = NULL;
32     db_config->user = NULL;
33     db_config->pass = NULL;
34     db_config->db = NULL;
35     db_config->port = 0;
36     db_config->sock = NULL;
37     db_config->db_type = 0;
38     db_config->maxreconnect = 0;
39
40     /* Read configuration */
41     if (ReadConfig(modules, cfgfile, tmp_config, db_config) < 0) {
42         return (OS_INVALID);
43     }
44
45     /* Assign the rules to db_config and free the rest of the Config */
46     db_config->includes = tmp_config->includes;
47     free(tmp_config);
48
49     /* Check if dbd isn't supposed to run */
50     if (!db_config->host &&
51             !db_config->user &&
52             !db_config->pass &&
53             !db_config->db &&
54             !db_config->sock &&
55             !db_config->port &&
56             !db_config->db_type) {
57         return (0);
58     }
59
60     /* Check for a valid config */
61     if (!db_config->host ||
62             !db_config->user ||
63             !db_config->pass ||
64             !db_config->db ||
65             !db_config->db_type) {
66         merror(DB_MISS_CONFIG, ARGV0);
67         return (OS_INVALID);
68     }
69
70     osdb_connect = NULL;
71
72     /* Assign the proper location for the function calls */
73
74 #ifdef MYSQL_DATABASE_ENABLED
75     if (db_config->db_type == MYSQLDB) {
76         osdb_connect = mysql_osdb_connect;
77         osdb_query_insert = mysql_osdb_query_insert;
78         osdb_query_select = mysql_osdb_query_select;
79         osdb_close = mysql_osdb_close;
80     }
81 #endif
82
83 #ifdef PGSQL_DATABASE_ENABLED
84     if (db_config->db_type == POSTGDB) {
85         osdb_connect = postgresql_osdb_connect;
86         osdb_query_insert = postgresql_osdb_query_insert;
87         osdb_query_select = postgresql_osdb_query_select;
88         osdb_close = postgresql_osdb_close;
89     }
90 #endif
91
92     /* Check for config errors */
93     if (db_config->db_type == MYSQLDB) {
94 #ifndef MYSQL_DATABASE_ENABLED
95         merror(DB_COMPILED, ARGV0, "mysql");
96         return (OS_INVALID);
97 #endif
98     } else if (db_config->db_type == POSTGDB) {
99 #ifndef PGSQL_DATABASE_ENABLED
100         merror(DB_COMPILED, ARGV0, "postgresql");
101         return (OS_INVALID);
102 #endif
103     }
104
105     if (osdb_connect == NULL) {
106         merror("%s: Invalid DB configuration (Internal error?). ", ARGV0);
107         return (OS_INVALID);
108     }
109
110     return (1);
111 }
112