Imported Upstream version 2.7
[ossec-hids.git] / src / os_dbd / main.c
1 /* @(#) $Id: ./src/os_dbd/main.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All rights reserved.
6  *
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
10  * Foundation.
11  *
12  * License details at the LICENSE file included with OSSEC or
13  * online at: http://www.ossec.net/en/licensing.html
14  */
15
16
17 #ifndef DBD
18    #define DBD
19 #endif
20
21 #ifndef ARGV0
22    #define ARGV0 "ossec-dbd"
23 #endif
24
25 #include "shared.h"
26 #include "dbd.h"
27
28
29 /* Prints information regarding enabled databases */
30 void db_info()
31 {
32     print_out(" ");
33     print_out("%s %s - %s", __name, __version, __author);
34
35     #ifdef UMYSQL
36     print_out("Compiled with MySQL support.");
37     #endif
38
39     #ifdef UPOSTGRES
40     print_out("Compiled with PostgreSQL support.");
41     #endif
42
43     #if !defined(UMYSQL) && !defined(UPOSTGRES)
44     print_out("Compiled without any Database support.");
45     #endif
46
47     print_out(" ");
48     print_out("%s",__license);
49
50     exit(1);
51 }
52
53
54
55 int main(int argc, char **argv)
56 {
57     int c, test_config = 0, run_foreground = 0;
58     int uid = 0,gid = 0;
59
60     /* Using MAILUSER (read only) */
61     char *dir  = DEFAULTDIR;
62     char *user = MAILUSER;
63     char *group = GROUPGLOBAL;
64     char *cfg = DEFAULTCPATH;
65
66
67     /* Database Structure */
68     DBConfig db_config;
69     db_config.error_count = 0;
70
71
72     /* Setting the name */
73     OS_SetName(ARGV0);
74
75
76     while((c = getopt(argc, argv, "vVdhtfu:g:D:c:")) != -1){
77         switch(c){
78             case 'V':
79                 db_info();
80                 break;
81             case 'v':
82                 db_info();
83                 break;
84             case 'h':
85                 help(ARGV0);
86                 break;
87             case 'd':
88                 nowDebug();
89                 break;
90             case 'f':
91                 run_foreground = 1;
92                 break;
93             case 'u':
94                 if(!optarg)
95                     ErrorExit("%s: -u needs an argument",ARGV0);
96                 user=optarg;
97                 break;
98             case 'g':
99                 if(!optarg)
100                     ErrorExit("%s: -g needs an argument",ARGV0);
101                 group=optarg;
102                 break;
103             case 'D':
104                 if(!optarg)
105                     ErrorExit("%s: -D needs an argument",ARGV0);
106                 dir=optarg;
107                 break;
108             case 'c':
109                 if(!optarg)
110                     ErrorExit("%s: -c needs an argument",ARGV0);
111                 cfg = optarg;
112                 break;
113             case 't':
114                 test_config = 1;
115                 break;
116             default:
117                 help(ARGV0);
118                 break;
119         }
120
121     }
122
123
124     /* Starting daemon */
125     debug1(STARTED_MSG, ARGV0);
126
127
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))
132     {
133         ErrorExit(USER_ERROR, ARGV0, user, group);
134     }
135
136
137     /* Reading configuration */
138     if((c = OS_ReadDBConf(test_config, cfg, &db_config)) < 0)
139     {
140         ErrorExit(CONFIG_ERROR, ARGV0, cfg);
141     }
142
143
144     /* Exit here if test config is set */
145     if(test_config)
146         exit(0);
147
148
149     if(!run_foreground)
150     {
151         /* Going on daemon mode */
152         nowDaemon();
153         goDaemon();
154     }
155
156
157
158     /* Not configured */
159     if(c == 0)
160     {
161         verbose("%s: Database not configured. Clean exit.", ARGV0);
162         exit(0);
163     }
164
165
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);
170
171
172     /* Setting config pointer */
173     osdb_setconfig(&db_config);
174
175
176     /* Getting maximum reconned attempts */
177     db_config.maxreconnect = getDefine_Int("dbd",
178                                            "reconnect_attempts", 1, 9999);
179
180
181     /* Connecting to the database */
182     c = 0;
183     while(c <= (db_config.maxreconnect * 10))
184     {
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);
188
189         /* If we are able to reconnect, keep going */
190         if(db_config.conn)
191         {
192             break;
193         }
194
195         c++;
196         sleep(c * 60);
197
198     }
199
200
201     /* If after the maxreconnect attempts, it still didn't work, exit here. */
202     if(!db_config.conn)
203     {
204         merror(DB_CONFIGERR, ARGV0);
205         ErrorExit(CONFIG_ERROR, ARGV0, cfg);
206     }
207
208
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);
212
213
214     /* Privilege separation */  
215     if(Privsep_SetGroup(gid) < 0)
216         ErrorExit(SETGID_ERROR,ARGV0,group);
217
218
219     /* chrooting */
220     if(Privsep_Chroot(dir) < 0)
221         ErrorExit(CHROOT_ERROR,ARGV0,dir);
222
223
224     /* Now on chroot */
225     nowChroot();
226
227
228     /* Inserting server info into the db */
229     db_config.server_id = OS_Server_ReadInsertDB(&db_config);
230     if(db_config.server_id <= 0)
231     {
232         ErrorExit(CONFIG_ERROR, ARGV0, cfg);
233     }
234
235
236     /* Read rules and insert into the db */
237     if(OS_InsertRulesDB(&db_config) < 0)
238     {
239         ErrorExit(CONFIG_ERROR, ARGV0, cfg);
240     }
241
242
243     /* Changing user */
244     if(Privsep_SetUser(uid) < 0)
245         ErrorExit(SETUID_ERROR,ARGV0,user);
246
247
248     /* Basic start up completed. */
249     debug1(PRIVSEP_MSG,ARGV0,dir,user);
250
251
252     /* Signal manipulation */
253     StartSIG(ARGV0);
254
255
256     /* Creating PID files */
257     if(CreatePID(ARGV0, getpid()) < 0)
258         ErrorExit(PID_ERROR,ARGV0);
259
260
261     /* Start up message */
262     verbose(STARTUP_MSG, ARGV0, (int)getpid());
263
264
265     /* the real daemon now */   
266     OS_DBD(&db_config);
267     exit(0);
268 }
269
270
271 /* EOF */