1 /* @(#) $Id: ./src/os_dbd/server.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
18 #include "config/config.h"
22 /** int __DBSelectServer(char *server, DBConfig *db_config)
23 * Selects the server ID from the db.
24 * Returns 0 if not found.
26 int __DBSelectServer(char *server, DBConfig *db_config)
29 char sql_query[OS_SIZE_1024];
31 memset(sql_query, '\0', OS_SIZE_1024);
35 snprintf(sql_query, OS_SIZE_1024 -1,
37 "server WHERE hostname = '%s'",
41 /* Checking return code. */
42 result = osdb_query_select(db_config->conn, sql_query);
48 /** int __DBInsertServer(char *server, char *info, DBConfig *db_config)
49 * Inserts server in to the db.
51 int __DBInsertServer(char *server, char *info, DBConfig *db_config)
53 char sql_query[OS_SIZE_1024];
55 memset(sql_query, '\0', OS_SIZE_1024);
57 /* Checking if the server is present */
58 snprintf(sql_query, OS_SIZE_1024 -1,
59 "SELECT id from server where hostname = '%s'",
62 /* If not present, we insert */
63 if(osdb_query_select(db_config->conn, sql_query) == 0)
65 snprintf(sql_query, OS_SIZE_1024 -1,
67 "server(last_contact, version, hostname, information) "
68 "VALUES ('%u', '%s', '%s', '%s')",
69 (unsigned int)time(0), __version, server, info);
71 /* Checking return code. */
72 if(!osdb_query_insert(db_config->conn, sql_query))
74 merror(DB_GENERROR, ARGV0);
78 /* If it is, we update it */
82 snprintf(sql_query, OS_SIZE_1024 -1,
84 "last_contact='%u',version='%s',information='%s' "
85 "WHERE hostname = '%s'",
86 (unsigned int)time(0), __version, info, server);
88 /* Checking return code. */
89 if(!osdb_query_insert(db_config->conn, sql_query))
91 merror(DB_GENERROR, ARGV0);
100 /** int OS_Server_ReadInsertDB(void *db_config)
101 * Insert server info to the db.
102 * Returns server ID or 0 on error.
104 int OS_Server_ReadInsertDB(void *db_config)
110 debug1("%s: DEBUG: entering OS_Server_ReadInsertDB()", ARGV0);
113 /* Getting servers hostname */
114 memset(__shost, '\0', 512);
115 if(gethostname(__shost, 512 -1) != 0)
117 merror("%s: Error: gethostname() failed", ARGV0);
122 /* Getting system uname */
126 merror(MEM_ERROR, ARGV0);
131 /* Escaping strings */
132 osdb_escapestr(info);
133 osdb_escapestr(__shost);
136 /* Inserting server */
137 __DBInsertServer(__shost, info, (DBConfig *)db_config);
140 /* Getting server id */
141 server_id = __DBSelectServer(__shost, (DBConfig *)db_config);