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;
104 mond.emailidsname = NULL;
109 if(ReadConfig(c, cfg, &mond, NULL) < 0)
111 ErrorExit(CONFIG_ERROR, ARGV0, cfg);
114 /* If we have any reports configured, read smtp/emailfrom */
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};
124 if(OS_ReadXML(cfg, &xml) < 0)
126 ErrorExit(CONFIG_ERROR, ARGV0, cfg);
129 tmpsmtp = OS_GetOneContentforElement(&xml,xml_smtp);
130 mond.emailfrom = OS_GetOneContentforElement(&xml,xml_from);
131 mond.emailidsname = OS_GetOneContentforElement(&xml,xml_idsname);
133 if(tmpsmtp && mond.emailfrom)
135 mond.smtpserver = OS_GetHost(tmpsmtp, 5);
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);
146 if(tmpsmtp) free(tmpsmtp);
147 if(mond.emailfrom) free(mond.emailfrom);
149 mond.emailfrom = NULL;
151 merror("%s: SMTP server or 'email from' missing. Disabling email reports.", ARGV0);
158 /* Exit here if test config is set */
165 /* Going on daemon mode */
171 /* Privilege separation */
172 if(Privsep_SetGroup(gid) < 0)
173 ErrorExit(SETGID_ERROR,ARGV0,group);
177 if(Privsep_Chroot(dir) < 0)
178 ErrorExit(CHROOT_ERROR,ARGV0,dir);
185 if(Privsep_SetUser(uid) < 0)
186 ErrorExit(SETUID_ERROR,ARGV0,user);
189 debug1(PRIVSEP_MSG,ARGV0,dir,user);
193 /* Signal manipulation */
198 /* Creating PID files */
199 if(CreatePID(ARGV0, getpid()) < 0)
200 ErrorExit(PID_ERROR,ARGV0);
203 /* Start up message */
204 verbose(STARTUP_MSG, ARGV0, (int)getpid());
207 /* the real daemon now */