Imported Upstream version 2.5.1
[ossec-hids.git] / src / monitord / 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
12
13 #include "shared.h"
14 #include "config/config.h"
15 #include "monitord.h"
16 #include "os_net/os_net.h"
17
18
19 int main(int argc, char **argv)
20 {
21     int c, test_config = 0, run_foreground = 0;
22     int uid=0,gid=0;
23     char *dir  = DEFAULTDIR;
24     char *user = USER;
25     char *group = GROUPGLOBAL;
26     char *cfg = DEFAULTCPATH;
27
28     /* Initializing global variables */
29     mond.a_queue = 0;
30
31     /* Setting the name */
32     OS_SetName(ARGV0);
33         
34
35     while((c = getopt(argc, argv, "Vdhtfu:g:D:c:")) != -1){
36         switch(c){
37             case 'V':
38                 print_version();
39                 break;
40             case 'h':
41                 help(ARGV0);
42                 break;
43             case 'd':
44                 nowDebug();
45                 break;
46             case 'f':
47                 run_foreground = 1;
48                 break;
49             case 'u':
50                 if(!optarg)
51                     ErrorExit("%s: -u needs an argument",ARGV0);
52                 user=optarg;
53                 break;
54             case 'g':
55                 if(!optarg)
56                     ErrorExit("%s: -g needs an argument",ARGV0);
57                 group=optarg;
58                 break;
59             case 'D':
60                 if(!optarg)
61                     ErrorExit("%s: -D needs an argument",ARGV0);
62                 dir=optarg;
63             case 'c':
64                 if(!optarg)
65                     ErrorExit("%s: -c needs an argument",ARGV0);
66                 cfg = optarg;
67                 break;
68             case 't':
69                 test_config = 1;    
70                 break;
71             default:
72                 help(ARGV0);
73                 break;
74         }
75
76     }
77
78     /* Starting daemon */
79     debug1(STARTED_MSG,ARGV0);
80
81     /*Check if the user/group given are valid */
82     uid = Privsep_GetUser(user);
83     gid = Privsep_GetGroup(group);
84     if((uid < 0)||(gid < 0))
85         ErrorExit(USER_ERROR,ARGV0,user,group);
86
87
88     /* Getting config options */
89     mond.day_wait = getDefine_Int("monitord",
90                                   "day_wait",
91                                   5,240);
92     mond.compress = getDefine_Int("monitord",
93                                   "compress",
94                                   0,1);
95     mond.sign = getDefine_Int("monitord","sign",0,1);
96
97     mond.monitor_agents = getDefine_Int("monitord","monitor_agents",0,1);
98
99     mond.agents = NULL;
100     mond.smtpserver = NULL;
101     mond.emailfrom = NULL;
102
103
104     c = 0;
105     c|= CREPORTS;
106     if(ReadConfig(c, cfg, &mond, NULL) < 0)
107     {
108         ErrorExit(CONFIG_ERROR, ARGV0, cfg);
109     }
110
111     /* If we have any reports configured, read smtp/emailfrom */
112     if(mond.reports)
113     {
114         OS_XML xml;
115         char *tmpsmtp;
116
117         char *(xml_smtp[])={"ossec_config", "global", "smtp_server", NULL};
118         char *(xml_from[])={"ossec_config", "global", "email_from", NULL};
119
120         if(OS_ReadXML(cfg, &xml) < 0)
121         {
122             ErrorExit(CONFIG_ERROR, ARGV0, cfg);
123         }
124
125         tmpsmtp = OS_GetOneContentforElement(&xml,xml_smtp);
126         mond.emailfrom = OS_GetOneContentforElement(&xml,xml_from);
127
128         if(tmpsmtp && mond.emailfrom)
129         {
130             mond.smtpserver = OS_GetHost(tmpsmtp, 5);
131             if(!mond.smtpserver)
132             {
133                 merror(INVALID_SMTP, ARGV0, tmpsmtp);
134                 if(mond.emailfrom) free(mond.emailfrom);
135                 mond.emailfrom = NULL;
136                 merror("%s: Invalid SMTP server.  Disabling email reports.", ARGV0);
137             }
138         }
139         else
140         {
141             if(tmpsmtp) free(tmpsmtp);
142             if(mond.emailfrom) free(mond.emailfrom);
143
144             mond.emailfrom = NULL;
145
146             merror("%s: SMTP server or 'email from' missing. Disabling email reports.", ARGV0);
147         }
148
149         OS_ClearXML(&xml);
150     }
151
152
153     /* Exit here if test config is set */
154     if(test_config)
155         exit(0);
156
157         
158     if (!run_foreground) 
159     {
160         /* Going on daemon mode */
161         nowDaemon();
162         goDaemon();
163     }
164
165     
166     /* Privilege separation */  
167     if(Privsep_SetGroup(gid) < 0)
168         ErrorExit(SETGID_ERROR,ARGV0,group);
169
170     
171     /* chrooting */
172     if(Privsep_Chroot(dir) < 0)
173         ErrorExit(CHROOT_ERROR,ARGV0,dir);
174
175     nowChroot();
176
177
178     
179     /* Changing user */        
180     if(Privsep_SetUser(uid) < 0)
181         ErrorExit(SETUID_ERROR,ARGV0,user);
182
183
184     debug1(PRIVSEP_MSG,ARGV0,dir,user);
185
186
187
188     /* Signal manipulation */
189     StartSIG(ARGV0);
190
191     
192
193     /* Creating PID files */
194     if(CreatePID(ARGV0, getpid()) < 0)
195         ErrorExit(PID_ERROR,ARGV0);
196
197     
198     /* Start up message */
199     verbose(STARTUP_MSG, ARGV0, (int)getpid());
200     
201
202     /* the real daemon now */   
203     Monitord();
204     exit(0);
205 }
206
207
208 /* EOF */