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