1 /* @(#) $Id: server.c,v 1.7 2009/06/24 17:06:30 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
17 #include "config/config.h"
21 /** int __DBSelectServer(char *server, DBConfig *db_config)
22 * Selects the server ID from the db.
23 * Returns 0 if not found.
25 int __DBSelectServer(char *server, DBConfig *db_config)
28 char sql_query[OS_SIZE_1024];
30 memset(sql_query, '\0', OS_SIZE_1024);
34 snprintf(sql_query, OS_SIZE_1024 -1,
36 "server WHERE hostname = '%s'",
40 /* Checking return code. */
41 result = osdb_query_select(db_config->conn, sql_query);
47 /** int __DBInsertServer(char *server, char *info, DBConfig *db_config)
48 * Inserts server in to the db.
50 int __DBInsertServer(char *server, char *info, DBConfig *db_config)
52 char sql_query[OS_SIZE_1024];
54 memset(sql_query, '\0', OS_SIZE_1024);
56 /* Checking if the server is present */
57 snprintf(sql_query, OS_SIZE_1024 -1,
58 "SELECT id from server where hostname = '%s'",
61 /* If not present, we insert */
62 if(osdb_query_select(db_config->conn, sql_query) == 0)
64 snprintf(sql_query, OS_SIZE_1024 -1,
66 "server(last_contact, version, hostname, information) "
67 "VALUES ('%u', '%s', '%s', '%s')",
68 (unsigned int)time(0), __version, server, info);
70 /* Checking return code. */
71 if(!osdb_query_insert(db_config->conn, sql_query))
73 merror(DB_GENERROR, ARGV0);
77 /* If it is, we update it */
81 snprintf(sql_query, OS_SIZE_1024 -1,
83 "last_contact='%u',version='%s',information='%s' "
84 "WHERE hostname = '%s'",
85 (unsigned int)time(0), __version, info, server);
87 /* Checking return code. */
88 if(!osdb_query_insert(db_config->conn, sql_query))
90 merror(DB_GENERROR, ARGV0);
99 /** int OS_Server_ReadInsertDB(void *db_config)
100 * Insert server info to the db.
101 * Returns server ID or 0 on error.
103 int OS_Server_ReadInsertDB(void *db_config)
109 debug1("%s: DEBUG: entering OS_Server_ReadInsertDB()", ARGV0);
112 /* Getting servers hostname */
113 memset(__shost, '\0', 512);
114 if(gethostname(__shost, 512 -1) != 0)
116 merror("%s: Error: gethostname() failed", ARGV0);
121 /* Getting system uname */
125 merror(MEM_ERROR, ARGV0);
130 /* Escaping strings */
131 osdb_escapestr(info);
132 osdb_escapestr(__shost);
135 /* Inserting server */
136 __DBInsertServer(__shost, info, (DBConfig *)db_config);
139 /* Getting server id */
140 server_id = __DBSelectServer(__shost, (DBConfig *)db_config);