1 /* @(#) $Id: ./src/monitord/main.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
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
15 #include "config/config.h"
17 #include "os_net/os_net.h"
20 int main(int argc, char **argv)
22 int c, test_config = 0, run_foreground = 0;
24 char *dir = DEFAULTDIR;
26 char *group = GROUPGLOBAL;
27 char *cfg = DEFAULTCPATH;
29 /* Initializing global variables */
32 /* Setting the name */
36 while((c = getopt(argc, argv, "Vdhtfu:g:D:c:")) != -1){
52 ErrorExit("%s: -u needs an argument",ARGV0);
57 ErrorExit("%s: -g needs an argument",ARGV0);
62 ErrorExit("%s: -D needs an argument",ARGV0);
67 ErrorExit("%s: -c needs an argument",ARGV0);
81 debug1(STARTED_MSG,ARGV0);
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);
90 /* Getting config options */
91 mond.day_wait = getDefine_Int("monitord",
94 mond.compress = getDefine_Int("monitord",
97 mond.sign = getDefine_Int("monitord","sign",0,1);
99 mond.monitor_agents = getDefine_Int("monitord","monitor_agents",0,1);
102 mond.smtpserver = NULL;
103 mond.emailfrom = NULL;
108 if(ReadConfig(c, cfg, &mond, NULL) < 0)
110 ErrorExit(CONFIG_ERROR, ARGV0, cfg);
113 /* If we have any reports configured, read smtp/emailfrom */
119 char *(xml_smtp[])={"ossec_config", "global", "smtp_server", NULL};
120 char *(xml_from[])={"ossec_config", "global", "email_from", NULL};
122 if(OS_ReadXML(cfg, &xml) < 0)
124 ErrorExit(CONFIG_ERROR, ARGV0, cfg);
127 tmpsmtp = OS_GetOneContentforElement(&xml,xml_smtp);
128 mond.emailfrom = OS_GetOneContentforElement(&xml,xml_from);
130 if(tmpsmtp && mond.emailfrom)
132 mond.smtpserver = OS_GetHost(tmpsmtp, 5);
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);
143 if(tmpsmtp) free(tmpsmtp);
144 if(mond.emailfrom) free(mond.emailfrom);
146 mond.emailfrom = NULL;
148 merror("%s: SMTP server or 'email from' missing. Disabling email reports.", ARGV0);
155 /* Exit here if test config is set */
162 /* Going on daemon mode */
168 /* Privilege separation */
169 if(Privsep_SetGroup(gid) < 0)
170 ErrorExit(SETGID_ERROR,ARGV0,group);
174 if(Privsep_Chroot(dir) < 0)
175 ErrorExit(CHROOT_ERROR,ARGV0,dir);
182 if(Privsep_SetUser(uid) < 0)
183 ErrorExit(SETUID_ERROR,ARGV0,user);
186 debug1(PRIVSEP_MSG,ARGV0,dir,user);
190 /* Signal manipulation */
195 /* Creating PID files */
196 if(CreatePID(ARGV0, getpid()) < 0)
197 ErrorExit(PID_ERROR,ARGV0);
200 /* Start up message */
201 verbose(STARTUP_MSG, ARGV0, (int)getpid());
204 /* the real daemon now */