Imported Upstream version 2.7
[ossec-hids.git] / src / os_dbd / config.c
1 /* @(#) $Id: ./src/os_dbd/config.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All rights reserved.
6  *
7  * This program is a free software; you can redistribute it
8  * and/or modify it under the terms of the GNU General Public
9  * License (version 2) as published by the FSF - Free Software
10  * Foundation.
11  *
12  * License details at the LICENSE file included with OSSEC or
13  * online at: http://www.ossec.net/en/licensing.html
14  */
15
16
17 #include "dbd.h"
18 #include "config/global-config.h"
19 #include "config/config.h"
20
21
22 /** int OS_ReadDBConf(int test_config, char *cfgfile, DBConfig *db_config)
23  * Reads database configuration.
24  */
25 int OS_ReadDBConf(int test_config, char *cfgfile, DBConfig *db_config)
26 {
27     int modules = 0;
28     _Config *tmp_config;
29
30
31     /* Modules for the configuration */
32     modules|= CDBD;
33     modules|= CRULES;
34
35
36     /* Allocating config just to get the rules. */
37     os_calloc(1, sizeof(_Config), tmp_config);
38
39
40     /* Clearing configuration variables */
41     tmp_config->includes = NULL;
42     db_config->includes = NULL;
43     db_config->host = NULL;
44     db_config->user = NULL;
45     db_config->pass = NULL;
46     db_config->db = NULL;
47     db_config->port = 0;
48     db_config->sock = NULL;
49     db_config->db_type = 0;
50     db_config->maxreconnect = 0;
51
52
53     /* Reading configuration */
54     if(ReadConfig(modules, cfgfile, tmp_config, db_config) < 0)
55         return(OS_INVALID);
56
57
58     /* Here, we assign the rules to db_config and free the rest
59      * of the Config.
60      */
61     db_config->includes = tmp_config->includes;
62     free(tmp_config);
63
64
65     /* Checking if dbd isn't supposed to run. */
66     if(!db_config->host &&
67        !db_config->user &&
68        !db_config->pass &&
69        !db_config->db &&
70        !db_config->sock &&
71        !db_config->port &&
72        !db_config->db_type)
73     {
74         return(0);
75     }
76
77
78     /* Checking for a valid config. */
79     if(!db_config->host ||
80        !db_config->user ||
81        !db_config->pass ||
82        !db_config->db ||
83        !db_config->db_type)
84     {
85         merror(DB_MISS_CONFIG, ARGV0);
86         return(OS_INVALID);
87     }
88
89     osdb_connect = NULL;
90
91     /* Assigning the proper location for the function calls */
92     #ifdef UMYSQL
93     if(db_config->db_type == MYSQLDB)
94     {
95         osdb_connect = mysql_osdb_connect;
96         osdb_query_insert = mysql_osdb_query_insert;
97         osdb_query_select = mysql_osdb_query_select;
98         osdb_close = mysql_osdb_close;
99     }
100     #endif
101
102     #ifdef UPOSTGRES
103     if(db_config->db_type == POSTGDB)
104     {
105         osdb_connect = postgresql_osdb_connect;
106         osdb_query_insert = postgresql_osdb_query_insert;
107         osdb_query_select = postgresql_osdb_query_select;
108         osdb_close = postgresql_osdb_close;
109     }
110     #endif
111
112
113
114     /* Checking for config errros (moving from config.c).
115      */
116     if(db_config->db_type == MYSQLDB)
117     {
118         #ifndef UMYSQL
119         merror(DB_COMPILED, ARGV0, "mysql");
120         return(OS_INVALID);
121         #endif
122     }
123     else if(db_config->db_type == POSTGDB)
124     {
125         #ifndef UPOSTGRES
126         merror(DB_COMPILED, ARGV0, "postgresql");
127         return(OS_INVALID);
128         #endif
129     }
130
131
132     if(osdb_connect == NULL)
133     {
134         merror("%s: Invalid DB configuration (Internal error?). ", ARGV0);
135         return(OS_INVALID);
136     }
137
138     return(1);
139 }
140
141 /* EOF */