/
main.c
245 lines (205 loc) · 6.7 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* Copyright (C) 2009 Trend Micro Inc.
* All rights reserved.
*
* This program is a free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
* License (version 2) as published by the FSF - Free Software
* Foundation.
*/
#include "shared.h"
#include "dbd.h"
#ifndef ARGV0
#define ARGV0 "ossec-dbd"
#endif
/* Prototypes */
static void print_db_info(void);
static void help_dbd(void) __attribute__((noreturn));
/* Print information regarding enabled databases */
static void print_db_info()
{
#ifdef UMYSQL_DATABASE_ENABLED
print_out(" Compiled with MySQL support");
#endif
#ifdef PGSQL_DATABASE_ENABLED
print_out(" Compiled with PostgreSQL support");
#endif
#if !defined(MYSQL_DATABASE_ENABLED) && !defined(PGSQL_DATABASE_ENABLED)
print_out(" Compiled without any database support");
#endif
}
/* Print help statement */
static void help_dbd()
{
print_header();
print_out(" %s: -[Vhdtfv] [-u user] [-g group] [-c config] [-D dir]", ARGV0);
print_out(" -V Version and license message");
print_out(" -h This help message");
print_out(" -d Execute in debug mode. This parameter");
print_out(" can be specified multiple times");
print_out(" to increase the debug level.");
print_out(" -t Test configuration");
print_out(" -f Run in foreground");
print_out(" -u <user> User to run as (default: %s)", MAILUSER);
print_out(" -g <group> Group to run as (default: %s)", GROUPGLOBAL);
print_out(" -c <config> Configuration file to use (default: %s)", DEFAULTCPATH);
print_out(" -D <dir> Directory to chroot into (default: %s)", DEFAULTDIR);
print_out(" ");
print_out(" Database Support:");
print_db_info();
print_out(" ");
exit(1);
}
int main(int argc, char **argv)
{
int c, test_config = 0, run_foreground = 0;
uid_t uid;
gid_t gid;
unsigned int d;
/* Use MAILUSER (read only) */
const char *dir = DEFAULTDIR;
const char *user = MAILUSER;
const char *group = GROUPGLOBAL;
const char *cfg = DEFAULTCPATH;
/* Database Structure */
DBConfig db_config;
db_config.error_count = 0;
/* Set the name */
OS_SetName(ARGV0);
while ((c = getopt(argc, argv, "Vdhtfu:g:D:c:")) != -1) {
switch (c) {
case 'V':
print_version();
break;
case 'h':
help_dbd();
break;
case 'd':
nowDebug();
break;
case 'f':
run_foreground = 1;
break;
case 'u':
if (!optarg) {
ErrorExit("%s: -u needs an argument", ARGV0);
}
user = optarg;
break;
case 'g':
if (!optarg) {
ErrorExit("%s: -g needs an argument", ARGV0);
}
group = optarg;
break;
case 'D':
if (!optarg) {
ErrorExit("%s: -D needs an argument", ARGV0);
}
dir = optarg;
break;
case 'c':
if (!optarg) {
ErrorExit("%s: -c needs an argument", ARGV0);
}
cfg = optarg;
break;
case 't':
test_config = 1;
break;
default:
help_dbd();
break;
}
}
/* Start daemon */
debug1(STARTED_MSG, ARGV0);
/* Check if the user/group given are valid */
uid = Privsep_GetUser(user);
gid = Privsep_GetGroup(group);
if (uid == (uid_t) - 1 || gid == (gid_t) - 1) {
ErrorExit(USER_ERROR, ARGV0, user, group);
}
/* Read configuration */
if ((c = OS_ReadDBConf(test_config, cfg, &db_config)) < 0) {
ErrorExit(CONFIG_ERROR, ARGV0, cfg);
}
/* Exit here if test config is set */
if (test_config) {
exit(0);
}
if (!run_foreground) {
/* Going on daemon mode */
nowDaemon();
goDaemon();
}
/* Not configured */
if (c == 0) {
verbose("%s: Database not configured. Clean exit.", ARGV0);
exit(0);
}
/* Maybe disable this debug? */
debug1("%s: DEBUG: Connecting to '%s', using '%s', '%s', '%s', %d,'%s'.",
ARGV0, db_config.host, db_config.user,
db_config.pass, db_config.db, db_config.port, db_config.sock);
/* Set config pointer */
osdb_setconfig(&db_config);
/* Get maximum reconnect attempts */
db_config.maxreconnect = (unsigned int) getDefine_Int("dbd",
"reconnect_attempts", 1, 9999);
/* Connect to the database */
d = 0;
while (d <= (db_config.maxreconnect * 10)) {
db_config.conn = osdb_connect(db_config.host, db_config.user,
db_config.pass, db_config.db,
db_config.port, db_config.sock);
/* If we are able to reconnect, keep going */
if (db_config.conn) {
break;
}
d++;
sleep(d * 60);
}
/* If after the maxreconnect attempts, it still didn't work, exit here */
if (!db_config.conn) {
merror(DB_CONFIGERR, ARGV0);
ErrorExit(CONFIG_ERROR, ARGV0, cfg);
}
/* We must notify that we connected -- easy debugging */
verbose("%s: Connected to database '%s' at '%s'.",
ARGV0, db_config.db, db_config.host);
/* Privilege separation */
if (Privsep_SetGroup(gid) < 0) {
ErrorExit(SETGID_ERROR, ARGV0, group, errno, strerror(errno));
}
/* chroot */
if (Privsep_Chroot(dir) < 0) {
ErrorExit(CHROOT_ERROR, ARGV0, dir, errno, strerror(errno));
}
/* Now in chroot */
nowChroot();
/* Insert server info into the db */
db_config.server_id = OS_Server_ReadInsertDB(&db_config);
if (db_config.server_id <= 0) {
ErrorExit(CONFIG_ERROR, ARGV0, cfg);
}
/* Read rules and insert into the db */
if (OS_InsertRulesDB(&db_config) < 0) {
ErrorExit(CONFIG_ERROR, ARGV0, cfg);
}
/* Change user */
if (Privsep_SetUser(uid) < 0) {
ErrorExit(SETUID_ERROR, ARGV0, user, errno, strerror(errno));
}
/* Basic start up completed */
debug1(PRIVSEP_MSG, ARGV0, dir, user);
/* Signal manipulation */
StartSIG(ARGV0);
/* Create PID files */
if (CreatePID(ARGV0, getpid()) < 0) {
ErrorExit(PID_ERROR, ARGV0);
}
/* Start up message */
verbose(STARTUP_MSG, ARGV0, (int)getpid());
/* The real daemon now */
OS_DBD(&db_config);
}