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