new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / os_dbd / server.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All rights reserved.
3  *
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
7  * Foundation
8  */
9
10 #include "dbd.h"
11 #include "config/config.h"
12 #include "rules_op.h"
13
14 /* Prototypes */
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));
17
18 /* System hostname */
19 static char __shost[512];
20
21
22 /* Select the server ID from the db
23  * Returns 0 if not found
24  */
25 static int __DBSelectServer(const char *server, const 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     /* Generate SQL */
33     snprintf(sql_query, OS_SIZE_1024 - 1,
34              "SELECT id FROM "
35              "server WHERE hostname = '%s'",
36              server);
37
38     result = osdb_query_select(db_config->conn, sql_query);
39
40     return (result);
41 }
42
43 /* Inserts server in to the db */
44 static int __DBInsertServer(const char *server, const char *info, const DBConfig *db_config)
45 {
46     char sql_query[OS_SIZE_1024];
47
48     memset(sql_query, '\0', OS_SIZE_1024);
49
50     /* Check if the server is present */
51     snprintf(sql_query, OS_SIZE_1024 - 1,
52              "SELECT id from server where hostname = '%s'",
53              server);
54
55     /* If not present, insert */
56     if (osdb_query_select(db_config->conn, sql_query) == 0) {
57         snprintf(sql_query, OS_SIZE_1024 - 1,
58                  "INSERT INTO "
59                  "server(last_contact, version, hostname, information) "
60                  "VALUES ('%u', '%s', '%s', '%s')",
61                  (unsigned int)time(0), __version, server, info);
62
63         if (!osdb_query_insert(db_config->conn, sql_query)) {
64             merror(DB_GENERROR, ARGV0);
65         }
66     }
67
68     /* If present, update it */
69     else {
70         snprintf(sql_query, OS_SIZE_1024 - 1,
71                  "UPDATE server SET "
72                  "last_contact='%u',version='%s',information='%s' "
73                  "WHERE hostname = '%s'",
74                  (unsigned int)time(0), __version, info, server);
75
76         if (!osdb_query_insert(db_config->conn, sql_query)) {
77             merror(DB_GENERROR, ARGV0);
78         }
79     }
80
81     return (0);
82 }
83
84 /* Insert server info to the db
85  * Returns server ID or 0 on error
86  */
87 int OS_Server_ReadInsertDB(const DBConfig *db_config)
88 {
89     int server_id = 0;
90     char *info;
91
92     debug1("%s: DEBUG: entering OS_Server_ReadInsertDB()", ARGV0);
93
94     /* Get server hostname */
95     memset(__shost, '\0', 512);
96     if (gethostname(__shost, 512 - 1) != 0) {
97         merror("%s: Error: gethostname() failed", ARGV0);
98         return (0);
99     }
100
101     /* Get system uname */
102     info = getuname();
103     if (!info) {
104         merror(MEM_ERROR, ARGV0, errno, strerror(errno));
105         return (0);
106     }
107
108     /* Escape strings */
109     osdb_escapestr(info);
110     osdb_escapestr(__shost);
111
112     /* Insert server */
113     __DBInsertServer(__shost, info, db_config);
114
115     /* Get server id */
116     server_id = __DBSelectServer(__shost, db_config);
117
118     free(info);
119
120     return (server_id);
121 }
122