Imported Upstream version 2.3
[ossec-hids.git] / src / monitord / main.c
1 /* @(#) $Id: main.c,v 1.9 2009/11/18 19:07:40 dcid Exp $ */
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 3) as published by the FSF - Free Software
9  * Foundation
10  */
11
12
13 #include "shared.h"
14 #include "monitord.h"
15
16
17 int main(int argc, char **argv)
18 {
19     int c, test_config = 0, run_foreground = 0;
20     int uid=0,gid=0;
21     char *dir  = DEFAULTDIR;
22     char *user = USER;
23     char *group = GROUPGLOBAL;
24     char *cfg = DEFAULTCPATH;
25
26     /* Initializing global variables */
27     mond.a_queue = 0;
28
29     /* Setting the name */
30     OS_SetName(ARGV0);
31         
32
33     while((c = getopt(argc, argv, "Vdhtfu:g:D:c:")) != -1){
34         switch(c){
35             case 'V':
36                 print_version();
37                 break;
38             case 'h':
39                 help(ARGV0);
40                 break;
41             case 'd':
42                 nowDebug();
43                 break;
44             case 'f':
45                 run_foreground = 1;
46                 break;
47             case 'u':
48                 if(!optarg)
49                     ErrorExit("%s: -u needs an argument",ARGV0);
50                 user=optarg;
51                 break;
52             case 'g':
53                 if(!optarg)
54                     ErrorExit("%s: -g needs an argument",ARGV0);
55                 group=optarg;
56                 break;
57             case 'D':
58                 if(!optarg)
59                     ErrorExit("%s: -D needs an argument",ARGV0);
60                 dir=optarg;
61             case 'c':
62                 if(!optarg)
63                     ErrorExit("%s: -c needs an argument",ARGV0);
64                 cfg = optarg;
65                 break;
66             case 't':
67                 test_config = 1;    
68                 break;
69             default:
70                 help(ARGV0);
71                 break;
72         }
73
74     }
75
76     /* Starting daemon */
77     debug1(STARTED_MSG,ARGV0);
78
79     /*Check if the user/group given are valid */
80     uid = Privsep_GetUser(user);
81     gid = Privsep_GetGroup(group);
82     if((uid < 0)||(gid < 0))
83         ErrorExit(USER_ERROR,ARGV0,user,group);
84
85
86     /* Getting config options */
87     mond.day_wait = getDefine_Int("monitord",
88                                   "day_wait",
89                                   5,240);
90     mond.compress = getDefine_Int("monitord",
91                                   "compress",
92                                   0,1);
93     mond.sign = getDefine_Int("monitord","sign",0,1);
94
95     mond.monitor_agents = getDefine_Int("monitord","monitor_agents",0,1);
96
97     mond.agents = NULL;
98
99
100     /* Exit here if test config is set */
101     if(test_config)
102         exit(0);
103
104         
105     if (!run_foreground) 
106     {
107         /* Going on daemon mode */
108         nowDaemon();
109         goDaemon();
110     }
111
112     
113     /* Privilege separation */  
114     if(Privsep_SetGroup(gid) < 0)
115         ErrorExit(SETGID_ERROR,ARGV0,group);
116
117     
118     /* chrooting */
119     if(Privsep_Chroot(dir) < 0)
120         ErrorExit(CHROOT_ERROR,ARGV0,dir);
121
122     nowChroot();
123
124
125     
126     /* Changing user */        
127     if(Privsep_SetUser(uid) < 0)
128         ErrorExit(SETUID_ERROR,ARGV0,user);
129
130
131     debug1(PRIVSEP_MSG,ARGV0,dir,user);
132
133
134
135     /* Signal manipulation */
136     StartSIG(ARGV0);
137
138     
139
140     /* Creating PID files */
141     if(CreatePID(ARGV0, getpid()) < 0)
142         ErrorExit(PID_ERROR,ARGV0);
143
144     
145     /* Start up message */
146     verbose(STARTUP_MSG, ARGV0, (int)getpid());
147     
148
149     /* the real daemon now */   
150     Monitord();
151     exit(0);
152 }
153
154
155 /* EOF */