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