izmjene licence
[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     mond.emailidsname = NULL;
105
106
107     c = 0;
108     c|= CREPORTS;
109     if(ReadConfig(c, cfg, &mond, NULL) < 0)
110     {
111         ErrorExit(CONFIG_ERROR, ARGV0, cfg);
112     }
113
114     /* If we have any reports configured, read smtp/emailfrom */
115     if(mond.reports)
116     {
117         OS_XML xml;
118         char *tmpsmtp;
119
120         const char *(xml_smtp[])={"ossec_config", "global", "smtp_server", NULL};
121         const char *(xml_from[])={"ossec_config", "global", "email_from", NULL};
122         const char *(xml_idsname[])={"ossec_config", "global", "email_idsname", NULL};
123
124         if(OS_ReadXML(cfg, &xml) < 0)
125         {
126             ErrorExit(CONFIG_ERROR, ARGV0, cfg);
127         }
128
129         tmpsmtp = OS_GetOneContentforElement(&xml,xml_smtp);
130         mond.emailfrom = OS_GetOneContentforElement(&xml,xml_from);
131         mond.emailidsname = OS_GetOneContentforElement(&xml,xml_idsname);
132
133         if(tmpsmtp && mond.emailfrom)
134         {
135             mond.smtpserver = OS_GetHost(tmpsmtp, 5);
136             if(!mond.smtpserver)
137             {
138                 merror(INVALID_SMTP, ARGV0, tmpsmtp);
139                 if(mond.emailfrom) free(mond.emailfrom);
140                 mond.emailfrom = NULL;
141                 merror("%s: Invalid SMTP server.  Disabling email reports.", ARGV0);
142             }
143         }
144         else
145         {
146             if(tmpsmtp) free(tmpsmtp);
147             if(mond.emailfrom) free(mond.emailfrom);
148
149             mond.emailfrom = NULL;
150
151             merror("%s: SMTP server or 'email from' missing. Disabling email reports.", ARGV0);
152         }
153
154         OS_ClearXML(&xml);
155     }
156
157
158     /* Exit here if test config is set */
159     if(test_config)
160         exit(0);
161
162
163     if (!run_foreground)
164     {
165         /* Going on daemon mode */
166         nowDaemon();
167         goDaemon();
168     }
169
170
171     /* Privilege separation */
172     if(Privsep_SetGroup(gid) < 0)
173         ErrorExit(SETGID_ERROR,ARGV0,group);
174
175
176     /* chrooting */
177     if(Privsep_Chroot(dir) < 0)
178         ErrorExit(CHROOT_ERROR,ARGV0,dir);
179
180     nowChroot();
181
182
183
184     /* Changing user */
185     if(Privsep_SetUser(uid) < 0)
186         ErrorExit(SETUID_ERROR,ARGV0,user);
187
188
189     debug1(PRIVSEP_MSG,ARGV0,dir,user);
190
191
192
193     /* Signal manipulation */
194     StartSIG(ARGV0);
195
196
197
198     /* Creating PID files */
199     if(CreatePID(ARGV0, getpid()) < 0)
200         ErrorExit(PID_ERROR,ARGV0);
201
202
203     /* Start up message */
204     verbose(STARTUP_MSG, ARGV0, (int)getpid());
205
206
207     /* the real daemon now */
208     Monitord();
209     exit(0);
210 }
211
212
213 /* EOF */