1 /* Copyright (C) 2009 Trend Micro Inc.
4 * This program is a free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License (version 2) as published by the FSF - Free Software
11 #include "config/config.h"
15 static int __DBSelectServer(const char *server, const DBConfig *db_config) __attribute__((nonnull));
16 static int __DBInsertServer(const char *server, const char *info, const DBConfig *db_config) __attribute__((nonnull));
19 static char __shost[512];
22 /* Select the server ID from the db
23 * Returns 0 if not found
25 static int __DBSelectServer(const char *server, const DBConfig *db_config)
28 char sql_query[OS_SIZE_1024];
30 memset(sql_query, '\0', OS_SIZE_1024);
33 snprintf(sql_query, OS_SIZE_1024 - 1,
35 "server WHERE hostname = '%s'",
38 result = osdb_query_select(db_config->conn, sql_query);
43 /* Inserts server in to the db */
44 static int __DBInsertServer(const char *server, const char *info, const DBConfig *db_config)
46 char sql_query[OS_SIZE_1024];
48 memset(sql_query, '\0', OS_SIZE_1024);
50 /* Check if the server is present */
51 snprintf(sql_query, OS_SIZE_1024 - 1,
52 "SELECT id from server where hostname = '%s'",
55 /* If not present, insert */
56 if (osdb_query_select(db_config->conn, sql_query) == 0) {
57 snprintf(sql_query, OS_SIZE_1024 - 1,
59 "server(last_contact, version, hostname, information) "
60 "VALUES ('%u', '%s', '%s', '%s')",
61 (unsigned int)time(0), __version, server, info);
63 if (!osdb_query_insert(db_config->conn, sql_query)) {
64 merror(DB_GENERROR, ARGV0);
68 /* If present, update it */
70 snprintf(sql_query, OS_SIZE_1024 - 1,
72 "last_contact='%u',version='%s',information='%s' "
73 "WHERE hostname = '%s'",
74 (unsigned int)time(0), __version, info, server);
76 if (!osdb_query_insert(db_config->conn, sql_query)) {
77 merror(DB_GENERROR, ARGV0);
84 /* Insert server info to the db
85 * Returns server ID or 0 on error
87 int OS_Server_ReadInsertDB(const DBConfig *db_config)
92 debug1("%s: DEBUG: entering OS_Server_ReadInsertDB()", ARGV0);
94 /* Get server hostname */
95 memset(__shost, '\0', 512);
96 if (gethostname(__shost, 512 - 1) != 0) {
97 merror("%s: Error: gethostname() failed", ARGV0);
101 /* Get system uname */
104 merror(MEM_ERROR, ARGV0, errno, strerror(errno));
109 osdb_escapestr(info);
110 osdb_escapestr(__shost);
113 __DBInsertServer(__shost, info, db_config);
116 server_id = __DBSelectServer(__shost, db_config);