1 /* @(#) $Id: ./src/os_dbd/main.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
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
12 * License details at the LICENSE file included with OSSEC or
13 * online at: http://www.ossec.net/en/licensing.html
22 #define ARGV0 "ossec-dbd"
29 /* Prints information regarding enabled databases */
33 print_out("%s %s - %s", __ossec_name, __version, __author);
36 print_out("Compiled with MySQL support.");
40 print_out("Compiled with PostgreSQL support.");
43 #if !defined(UMYSQL) && !defined(UPOSTGRES)
44 print_out("Compiled without any Database support.");
48 print_out("%s",__license);
55 int main(int argc, char **argv)
57 int c, test_config = 0, run_foreground = 0;
60 /* Using MAILUSER (read only) */
61 char *dir = DEFAULTDIR;
62 char *user = MAILUSER;
63 char *group = GROUPGLOBAL;
64 char *cfg = DEFAULTCPATH;
67 /* Database Structure */
69 db_config.error_count = 0;
72 /* Setting the name */
76 while((c = getopt(argc, argv, "vVdhtfu:g:D:c:")) != -1){
95 ErrorExit("%s: -u needs an argument",ARGV0);
100 ErrorExit("%s: -g needs an argument",ARGV0);
105 ErrorExit("%s: -D needs an argument",ARGV0);
110 ErrorExit("%s: -c needs an argument",ARGV0);
124 /* Starting daemon */
125 debug1(STARTED_MSG, ARGV0);
128 /* Check if the user/group given are valid */
129 uid = Privsep_GetUser(user);
130 gid = Privsep_GetGroup(group);
131 if((uid < 0)||(gid < 0))
133 ErrorExit(USER_ERROR, ARGV0, user, group);
137 /* Reading configuration */
138 if((c = OS_ReadDBConf(test_config, cfg, &db_config)) < 0)
140 ErrorExit(CONFIG_ERROR, ARGV0, cfg);
144 /* Exit here if test config is set */
151 /* Going on daemon mode */
161 verbose("%s: Database not configured. Clean exit.", ARGV0);
166 /* Maybe disable this debug? */
167 debug1("%s: DEBUG: Connecting to '%s', using '%s', '%s', '%s', %d,'%s'.",
168 ARGV0, db_config.host, db_config.user,
169 db_config.pass, db_config.db,db_config.port,db_config.sock);
172 /* Setting config pointer */
173 osdb_setconfig(&db_config);
176 /* Getting maximum reconned attempts */
177 db_config.maxreconnect = getDefine_Int("dbd",
178 "reconnect_attempts", 1, 9999);
181 /* Connecting to the database */
183 while(c <= (db_config.maxreconnect * 10))
185 db_config.conn = osdb_connect(db_config.host, db_config.user,
186 db_config.pass, db_config.db,
187 db_config.port,db_config.sock);
189 /* If we are able to reconnect, keep going */
201 /* If after the maxreconnect attempts, it still didn't work, exit here. */
204 merror(DB_CONFIGERR, ARGV0);
205 ErrorExit(CONFIG_ERROR, ARGV0, cfg);
209 /* We must notify that we connected -- easy debugging */
210 verbose("%s: Connected to database '%s' at '%s'.",
211 ARGV0, db_config.db, db_config.host);
214 /* Privilege separation */
215 if(Privsep_SetGroup(gid) < 0)
216 ErrorExit(SETGID_ERROR,ARGV0,group);
220 if(Privsep_Chroot(dir) < 0)
221 ErrorExit(CHROOT_ERROR,ARGV0,dir);
228 /* Inserting server info into the db */
229 db_config.server_id = OS_Server_ReadInsertDB(&db_config);
230 if(db_config.server_id <= 0)
232 ErrorExit(CONFIG_ERROR, ARGV0, cfg);
236 /* Read rules and insert into the db */
237 if(OS_InsertRulesDB(&db_config) < 0)
239 ErrorExit(CONFIG_ERROR, ARGV0, cfg);
244 if(Privsep_SetUser(uid) < 0)
245 ErrorExit(SETUID_ERROR,ARGV0,user);
248 /* Basic start up completed. */
249 debug1(PRIVSEP_MSG,ARGV0,dir,user);
252 /* Signal manipulation */
256 /* Creating PID files */
257 if(CreatePID(ARGV0, getpid()) < 0)
258 ErrorExit(PID_ERROR,ARGV0);
261 /* Start up message */
262 verbose(STARTUP_MSG, ARGV0, (int)getpid());
265 /* the real daemon now */