Imported Upstream version 2.7
[ossec-hids.git] / src / os_dbd / server.c
1 /* @(#) $Id: ./src/os_dbd/server.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All rights reserved.
6  *
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
10  * Foundation
11  *
12  * License details at the LICENSE file included with OSSEC or
13  * online at: http://www.ossec.net/en/licensing.html
14  */
15
16
17 #include "dbd.h"
18 #include "config/config.h"
19 #include "rules_op.h"
20
21
22 /** int __DBSelectServer(char *server, DBConfig *db_config)
23  * Selects the server ID from the db.
24  * Returns 0 if not found.
25  */
26 int __DBSelectServer(char *server, DBConfig *db_config)
27 {
28     int result = 0;
29     char sql_query[OS_SIZE_1024];
30
31     memset(sql_query, '\0', OS_SIZE_1024);
32
33
34     /* Generating SQL */
35     snprintf(sql_query, OS_SIZE_1024 -1,
36             "SELECT id FROM "
37             "server WHERE hostname = '%s'",
38             server);
39
40
41     /* Checking return code. */
42     result = osdb_query_select(db_config->conn, sql_query);
43
44     return(result);
45 }
46
47
48 /** int __DBInsertServer(char *server, char *info, DBConfig *db_config)
49  * Inserts server in to the db.
50  */
51 int __DBInsertServer(char *server, char *info, DBConfig *db_config)
52 {
53     char sql_query[OS_SIZE_1024];
54
55     memset(sql_query, '\0', OS_SIZE_1024);
56
57     /* Checking if the server is present */
58     snprintf(sql_query, OS_SIZE_1024 -1,
59             "SELECT id from server where hostname = '%s'",
60             server);
61
62     /* If not present, we insert */
63     if(osdb_query_select(db_config->conn, sql_query) == 0)
64     {
65         snprintf(sql_query, OS_SIZE_1024 -1,
66                 "INSERT INTO "
67                 "server(last_contact, version, hostname, information) "
68                 "VALUES ('%u', '%s', '%s', '%s')",
69                 (unsigned int)time(0), __version, server, info);
70
71         /* Checking return code. */
72         if(!osdb_query_insert(db_config->conn, sql_query))
73         {
74             merror(DB_GENERROR, ARGV0);
75         }
76     }
77
78     /* If it is, we update it */
79     else
80     {
81
82         snprintf(sql_query, OS_SIZE_1024 -1,
83                 "UPDATE server SET "
84                 "last_contact='%u',version='%s',information='%s' "
85                 "WHERE hostname = '%s'",
86                 (unsigned int)time(0), __version, info, server);
87
88         /* Checking return code. */
89         if(!osdb_query_insert(db_config->conn, sql_query))
90         {
91             merror(DB_GENERROR, ARGV0);
92         }
93     }
94
95     return(0);
96 }
97
98
99
100 /** int OS_Server_ReadInsertDB(void *db_config)
101  * Insert server info to the db.
102  * Returns server ID or 0 on error.
103  */
104 int OS_Server_ReadInsertDB(void *db_config)
105 {
106     int server_id = 0;
107     char *info;
108
109
110     debug1("%s: DEBUG: entering OS_Server_ReadInsertDB()", ARGV0);
111
112
113     /* Getting servers hostname */
114     memset(__shost, '\0', 512);
115     if(gethostname(__shost, 512 -1) != 0)
116     {
117         merror("%s: Error: gethostname() failed", ARGV0);
118         return(0);
119     }
120
121
122     /* Getting system uname */
123     info = getuname();
124     if(!info)
125     {
126         merror(MEM_ERROR, ARGV0);
127         return(0);
128     }
129
130
131     /* Escaping strings */
132     osdb_escapestr(info);
133     osdb_escapestr(__shost);
134
135
136     /* Inserting server */
137     __DBInsertServer(__shost, info, (DBConfig *)db_config);
138
139
140     /* Getting server id */
141     server_id = __DBSelectServer(__shost, (DBConfig *)db_config);
142
143
144     return(server_id);
145 }
146
147
148 /* EOF */