3 /* Copyright (C) 2009 Trend Micro Inc.
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
11 * License details at the LICENSE file included with OSSEC or
12 * online at: http://www.ossec.net/en/licensing.html
21 #define ARGV0 "ossec-dbd"
28 /* Prints information regarding enabled databases */
32 print_out("%s %s - %s", __name, __version, __author);
35 print_out("Compiled with MySQL support.");
39 print_out("Compiled with PostgreSQL support.");
42 #if !defined(UMYSQL) && !defined(UPOSTGRES)
43 print_out("Compiled without any Database support.");
47 print_out("%s",__license);
54 int main(int argc, char **argv)
56 int c, test_config = 0, run_foreground = 0;
59 /* Using MAILUSER (read only) */
60 char *dir = DEFAULTDIR;
61 char *user = MAILUSER;
62 char *group = GROUPGLOBAL;
63 char *cfg = DEFAULTCPATH;
66 /* Database Structure */
68 db_config.error_count = 0;
71 /* Setting the name */
75 while((c = getopt(argc, argv, "vVdhtfu:g:D:c:")) != -1){
94 ErrorExit("%s: -u needs an argument",ARGV0);
99 ErrorExit("%s: -g needs an argument",ARGV0);
104 ErrorExit("%s: -D needs an argument",ARGV0);
108 ErrorExit("%s: -c needs an argument",ARGV0);
122 /* Starting daemon */
123 debug1(STARTED_MSG, ARGV0);
126 /* Check if the user/group given are valid */
127 uid = Privsep_GetUser(user);
128 gid = Privsep_GetGroup(group);
129 if((uid < 0)||(gid < 0))
131 ErrorExit(USER_ERROR, ARGV0, user, group);
135 /* Reading configuration */
136 if((c = OS_ReadDBConf(test_config, cfg, &db_config)) < 0)
138 ErrorExit(CONFIG_ERROR, ARGV0, cfg);
142 /* Exit here if test config is set */
149 /* Going on daemon mode */
159 verbose("%s: Database not configured. Clean exit.", ARGV0);
164 /* Maybe disable this debug? */
165 debug1("%s: DEBUG: Connecting to '%s', using '%s', '%s', '%s', %d,'%s'.",
166 ARGV0, db_config.host, db_config.user,
167 db_config.pass, db_config.db,db_config.port,db_config.sock);
170 /* Setting config pointer */
171 osdb_setconfig(&db_config);
174 /* Getting maximum reconned attempts */
175 db_config.maxreconnect = getDefine_Int("dbd",
176 "reconnect_attempts", 1, 9999);
179 /* Connecting to the database */
181 while(c <= (db_config.maxreconnect * 10))
183 db_config.conn = osdb_connect(db_config.host, db_config.user,
184 db_config.pass, db_config.db,
185 db_config.port,db_config.sock);
187 /* If we are able to reconnect, keep going */
199 /* If after the maxreconnect attempts, it still didn't work, exit here. */
202 merror(DB_CONFIGERR, ARGV0);
203 ErrorExit(CONFIG_ERROR, ARGV0, cfg);
207 /* We must notify that we connected -- easy debugging */
208 verbose("%s: Connected to database '%s' at '%s'.",
209 ARGV0, db_config.db, db_config.host);
212 /* Privilege separation */
213 if(Privsep_SetGroup(gid) < 0)
214 ErrorExit(SETGID_ERROR,ARGV0,group);
218 if(Privsep_Chroot(dir) < 0)
219 ErrorExit(CHROOT_ERROR,ARGV0,dir);
226 /* Inserting server info into the db */
227 db_config.server_id = OS_Server_ReadInsertDB(&db_config);
228 if(db_config.server_id <= 0)
230 ErrorExit(CONFIG_ERROR, ARGV0, cfg);
234 /* Read rules and insert into the db */
235 if(OS_InsertRulesDB(&db_config) < 0)
237 ErrorExit(CONFIG_ERROR, ARGV0, cfg);
242 if(Privsep_SetUser(uid) < 0)
243 ErrorExit(SETUID_ERROR,ARGV0,user);
246 /* Basic start up completed. */
247 debug1(PRIVSEP_MSG,ARGV0,dir,user);
250 /* Signal manipulation */
254 /* Creating PID files */
255 if(CreatePID(ARGV0, getpid()) < 0)
256 ErrorExit(PID_ERROR,ARGV0);
259 /* Start up message */
260 verbose(STARTUP_MSG, ARGV0, (int)getpid());
263 /* the real daemon now */