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