1 /* @(#) $Id: ./src/os_dbd/db_op.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
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
12 * License details at the LICENSE file included with OSSEC or
13 * online at: http://www.ossec.net/en/licensing.html
17 /* Common lib for dealing with databases */
27 /* Using PostgreSQL */
35 DBConfig *db_config_pt = NULL;
39 /** void osdb_escapestr
40 * Escapes a null terminated string before inserting into the database.
41 * We built a white list of allowed characters at insert_map. Everything
42 * not allowed will become spaces.
44 void osdb_escapestr(char *str)
61 else if(insert_map[(unsigned char)*str] != '\001')
68 /* It can not end with \\ */
77 /** void osdb_checkerror()
78 * Checks for errors and handle it appropriately.
80 void osdb_checkerror()
83 if(!db_config_pt || db_config_pt->error_count > 20)
85 ErrorExit(DB_MAINERROR, ARGV0);
89 /* If error count is too large, we try to reconnect. */
90 if(db_config_pt->error_count > 0)
93 if(db_config_pt->conn)
95 osdb_close(db_config_pt->conn);
96 db_config_pt->conn = NULL;
99 while(i <= db_config_pt->maxreconnect)
101 merror(DB_ATTEMPT, ARGV0);
102 db_config_pt->conn = osdb_connect(db_config_pt->host,
109 /* If we were able to reconnect, keep going. */
110 if(db_config_pt->conn)
120 /* If we weren't able to connect, exit */
121 if(!db_config_pt->conn)
123 ErrorExit(DB_MAINERROR, ARGV0);
127 verbose("%s: Connected to database '%s' at '%s'.",
128 ARGV0, db_config_pt->db, db_config_pt->host);
134 /** void osdb_seterror()
135 * Sets the error counter.
139 db_config_pt->error_count++;
144 /** void osdb_setconfig(DBConfig *db_config)
145 * Creates an internal pointer to the db configuration.
147 void osdb_setconfig(DBConfig *db_config)
149 db_config_pt = db_config;
158 /* Create the database connection.
159 * Returns NULL on error
161 void *mysql_osdb_connect(char *host, char *user, char *pass, char *db,
162 int port, char *sock)
165 conn = mysql_init(NULL);
168 merror(DBINIT_ERROR, ARGV0);
173 /* If host is 127.0.0.1 or localhost, use tcp socket */
174 if((strcmp(host, "127.0.0.1") == 0) ||
175 (strcmp(host, "localhost") == 0))
179 mysql_options(conn, MYSQL_OPT_NAMED_PIPE, NULL);
183 unsigned int p_type = MYSQL_PROTOCOL_TCP;
184 mysql_options(conn, MYSQL_OPT_PROTOCOL, (char *)&p_type);
187 if(mysql_real_connect(conn, host, user, pass, db,
188 port, sock, 0) == NULL)
190 merror(DBCONN_ERROR, ARGV0, host, db, mysql_error(conn));
200 /* Closes the database connection.
202 void *mysql_osdb_close(void *db_conn)
204 merror(DB_CLOSING, ARGV0);
205 mysql_close(db_conn);
211 /** int mysql_osdb_query_insert(void *db_conn, char *query)
212 * Sends insert query to database.
214 int mysql_osdb_query_insert(void *db_conn, char *query)
216 if(mysql_query(db_conn, query) != 0)
218 /* failure; report error */
219 merror(DBQUERY_ERROR, ARGV0, query, mysql_error(db_conn));
229 /** int mysql_osdb_query_select(void *db_conn, char *query)
230 * Sends a select query to database. Returns the value of it.
231 * Returns 0 on error (not found).
233 int mysql_osdb_query_select(void *db_conn, char *query)
236 MYSQL_RES *result_data;
237 MYSQL_ROW result_row;
240 /* Sending the query. It can not fail. */
241 if(mysql_query(db_conn, query) != 0)
243 /* failure; report error */
244 merror(DBQUERY_ERROR, ARGV0, query, mysql_error(db_conn));
251 result_data = mysql_use_result(db_conn);
252 if(result_data == NULL)
254 /* failure; report error */
255 merror(DBQUERY_ERROR, ARGV0, query, mysql_error(db_conn));
261 /* Getting row. We only care about the first result. */
262 result_row = mysql_fetch_row(result_data);
263 if(result_row && (result_row[0] != NULL))
265 result_int = atoi(result_row[0]);
269 mysql_free_result(result_data);
275 /** End of MYSQL calls **/
280 /** PostGRES Calls **/
281 #if defined UPOSTGRES
284 /** void *postgresql_osdb_connect(char *host, char *user, char *pass, char *db)
285 * Create the PostgreSQL database connection.
286 * Return NULL on error
288 void *postgresql_osdb_connect(char *host, char *user, char *pass, char *db,
289 int port, char *sock)
294 conn = PQsetdbLogin(host, NULL, NULL, NULL, db, user, pass);
295 if(PQstatus(conn) == CONNECTION_BAD)
297 merror(DBCONN_ERROR, ARGV0, host, db, PQerrorMessage(conn));
307 /** void postgresql_osdb_close(void *db_conn)
308 * Terminates db connection.
310 void *postgresql_osdb_close(void *db_conn)
312 merror(DB_CLOSING, ARGV0);
319 /** int postgresql_osdb_query_insert(void *db_conn, char *query)
320 * Sends insert query to database.
322 int postgresql_osdb_query_insert(void *db_conn, char *query)
327 result = PQexec(db_conn,query);
330 merror(DBQUERY_ERROR, ARGV0, query, PQerrorMessage(db_conn));
336 if(PQresultStatus(result) != PGRES_COMMAND_OK)
338 merror(DBQUERY_ERROR, ARGV0, query, PQerrorMessage(db_conn));
351 /** int postgresql_osdb_query_select(void *db_conn, char *query)
352 * Sends a select query to database. Returns the value of it.
353 * Returns 0 on error (not found).
355 int postgresql_osdb_query_select(void *db_conn, char *query)
360 result = PQexec(db_conn,query);
363 merror(DBQUERY_ERROR, ARGV0, query, PQerrorMessage(db_conn));
368 if((PQresultStatus(result) == PGRES_TUPLES_OK))
370 if(PQntuples(result) == 1)
372 result_int = atoi(PQgetvalue(result,0,0));
377 merror(DBQUERY_ERROR, ARGV0, query, PQerrorMessage(db_conn));
389 /** End of PostGRES calls **/
394 /* Everything else when db is not defined. */
395 #if !defined(UPOSTGRES) && !defined(UMYSQL)
399 void *none_osdb_connect(char *host, char *user, char *pass, char *db,
400 int port, char *sock)
402 merror("%s: ERROR: Database support not enabled. Exiting.", ARGV0);
405 void *none_osdb_close(void *db_conn)
407 merror("%s: ERROR: Database support not enabled. Exiting.", ARGV0);
410 void *none_osdb_query_insert(void *db_conn, char *query)
412 merror("%s: ERROR: Database support not enabled. Exiting.", ARGV0);
415 void *none_osdb_query_select(void *db_conn, char *query)
417 merror("%s: ERROR: Database support not enabled. Exiting.", ARGV0);
424 /** End of not defined db calls **/