1 /* @(#) $Id: db_op.c,v 1.17 2009/12/01 01:49:55 dcid Exp $ */
3 /* Copyright (C) 2009 Trend Micro Inc.
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 3) as published by the FSF - Free Software
11 * License details at the LICENSE file included with OSSEC or
12 * online at: http://www.ossec.net/en/licensing.html
16 /* Common lib for dealing with databases */
26 /* Using PostgreSQL */
34 DBConfig *db_config_pt = NULL;
38 /** void osdb_escapestr
39 * Escapes a null terminated string before inserting into the database.
40 * We built a white list of allowed characters at insert_map. Everything
41 * not allowed will become spaces.
43 void osdb_escapestr(char *str)
60 else if(insert_map[(unsigned char)*str] != '\001')
67 /* It can not end with \\ */
76 /** void osdb_checkerror()
77 * Checks for errors and handle it appropriately.
79 void osdb_checkerror()
82 if(!db_config_pt || db_config_pt->error_count > 20)
84 ErrorExit(DB_MAINERROR, ARGV0);
88 /* If error count is too large, we try to reconnect. */
89 if(db_config_pt->error_count > 0)
92 if(db_config_pt->conn)
94 osdb_close(db_config_pt->conn);
95 db_config_pt->conn = NULL;
98 while(i <= db_config_pt->maxreconnect)
100 merror(DB_ATTEMPT, ARGV0);
101 db_config_pt->conn = osdb_connect(db_config_pt->host,
108 /* If we were able to reconnect, keep going. */
109 if(db_config_pt->conn)
119 /* If we weren't able to connect, exit */
120 if(!db_config_pt->conn)
122 ErrorExit(DB_MAINERROR, ARGV0);
126 verbose("%s: Connected to database '%s' at '%s'.",
127 ARGV0, db_config_pt->db, db_config_pt->host);
133 /** void osdb_seterror()
134 * Sets the error counter.
138 db_config_pt->error_count++;
143 /** void osdb_setconfig(DBConfig *db_config)
144 * Creates an internal pointer to the db configuration.
146 void osdb_setconfig(DBConfig *db_config)
148 db_config_pt = db_config;
157 /* Create the database connection.
158 * Returns NULL on error
160 void *mysql_osdb_connect(char *host, char *user, char *pass, char *db,
161 int port, char *sock)
164 conn = mysql_init(NULL);
167 merror(DBINIT_ERROR, ARGV0);
172 /* If host is 127.0.0.1 or localhost, use tcp socket */
173 if((strcmp(host, "127.0.0.1") == 0) ||
174 (strcmp(host, "localhost") == 0))
178 mysql_options(conn, MYSQL_OPT_NAMED_PIPE, NULL);
182 unsigned int p_type = MYSQL_PROTOCOL_TCP;
183 mysql_options(conn, MYSQL_OPT_PROTOCOL, (char *)&p_type);
186 if(mysql_real_connect(conn, host, user, pass, db,
187 port, sock, 0) == NULL)
189 merror(DBCONN_ERROR, ARGV0, host, db, mysql_error(conn));
199 /* Closes the database connection.
201 void *mysql_osdb_close(void *db_conn)
203 merror(DB_CLOSING, ARGV0);
204 mysql_close(db_conn);
210 /** int mysql_osdb_query_insert(void *db_conn, char *query)
211 * Sends insert query to database.
213 int mysql_osdb_query_insert(void *db_conn, char *query)
215 if(mysql_query(db_conn, query) != 0)
217 /* failure; report error */
218 merror(DBQUERY_ERROR, ARGV0, query, mysql_error(db_conn));
228 /** int mysql_osdb_query_select(void *db_conn, char *query)
229 * Sends a select query to database. Returns the value of it.
230 * Returns 0 on error (not found).
232 int mysql_osdb_query_select(void *db_conn, char *query)
235 MYSQL_RES *result_data;
236 MYSQL_ROW result_row;
239 /* Sending the query. It can not fail. */
240 if(mysql_query(db_conn, query) != 0)
242 /* failure; report error */
243 merror(DBQUERY_ERROR, ARGV0, query, mysql_error(db_conn));
250 result_data = mysql_use_result(db_conn);
251 if(result_data == NULL)
253 /* failure; report error */
254 merror(DBQUERY_ERROR, ARGV0, query, mysql_error(db_conn));
260 /* Getting row. We only care about the first result. */
261 result_row = mysql_fetch_row(result_data);
262 if(result_row && (result_row[0] != NULL))
264 result_int = atoi(result_row[0]);
268 mysql_free_result(result_data);
274 /** End of MYSQL calls **/
279 /** PostGRES Calls **/
280 #if defined UPOSTGRES
283 /** void *postgresql_osdb_connect(char *host, char *user, char *pass, char *db)
284 * Create the PostgreSQL database connection.
285 * Return NULL on error
287 void *postgresql_osdb_connect(char *host, char *user, char *pass, char *db,
288 int port, char *sock)
293 conn = PQsetdbLogin(host, NULL, NULL, NULL, db, user, pass);
294 if(PQstatus(conn) == CONNECTION_BAD)
296 merror(DBCONN_ERROR, ARGV0, host, db, PQerrorMessage(conn));
306 /** void postgresql_osdb_close(void *db_conn)
307 * Terminates db connection.
309 void *postgresql_osdb_close(void *db_conn)
311 merror(DB_CLOSING, ARGV0);
318 /** int postgresql_osdb_query_insert(void *db_conn, char *query)
319 * Sends insert query to database.
321 int postgresql_osdb_query_insert(void *db_conn, char *query)
326 result = PQexec(db_conn,query);
329 merror(DBQUERY_ERROR, ARGV0, query, PQerrorMessage(db_conn));
335 if(PQresultStatus(result) != PGRES_COMMAND_OK)
337 merror(DBQUERY_ERROR, ARGV0, query, PQerrorMessage(db_conn));
350 /** int postgresql_osdb_query_select(void *db_conn, char *query)
351 * Sends a select query to database. Returns the value of it.
352 * Returns 0 on error (not found).
354 int postgresql_osdb_query_select(void *db_conn, char *query)
359 result = PQexec(db_conn,query);
362 merror(DBQUERY_ERROR, ARGV0, query, PQerrorMessage(db_conn));
367 if((PQresultStatus(result) == PGRES_TUPLES_OK))
369 if(PQntuples(result) == 1)
371 result_int = atoi(PQgetvalue(result,0,0));
376 merror(DBQUERY_ERROR, ARGV0, query, PQerrorMessage(db_conn));
388 /** End of PostGRES calls **/
393 /* Everything else when db is not defined. */
394 #if !defined(UPOSTGRES) && !defined(UMYSQL)
398 void *none_osdb_connect(char *host, char *user, char *pass, char *db,
399 int port, char *sock)
404 /* Just to avoid warnings. */
405 tmp = host; tmp = user; tmp = pass; tmp = db;
408 merror("%s: ERROR: Database support not enabled. Exiting.", ARGV0);
411 void *none_osdb_close(void *db_conn)
416 merror("%s: ERROR: Database support not enabled. Exiting.", ARGV0);
419 void *none_osdb_query_insert(void *db_conn, char *query)
423 tmp = db_conn; tmp = query;
425 merror("%s: ERROR: Database support not enabled. Exiting.", ARGV0);
428 void *none_osdb_query_select(void *db_conn, char *query)
432 tmp = db_conn; tmp = query;
434 merror("%s: ERROR: Database support not enabled. Exiting.", ARGV0);
441 /** End of not defined db calls **/