1 /* Copyright (C) 2009 Trend Micro Inc.
4 * This program is a free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License (version 2) as published by the FSF - Free Software
11 #include "config/config.h"
13 #include "os_net/os_net.h"
16 static void help_monitord(void) __attribute__((noreturn));
19 /* Print help statement */
20 static void help_monitord()
23 print_out(" %s: -[Vhdtf] [-u user] [-g group] [-c config] [-D dir]", ARGV0);
24 print_out(" -V Version and license message");
25 print_out(" -h This help message");
26 print_out(" -d Execute in debug mode. This parameter");
27 print_out(" can be specified multiple times");
28 print_out(" to increase the debug level.");
29 print_out(" -t Test configuration");
30 print_out(" -f Run in foreground");
31 print_out(" -u <user> User to run as (default: %s)", USER);
32 print_out(" -g <group> Group to run as (default: %s)", GROUPGLOBAL);
33 print_out(" -c <config> Configuration file to use (default: %s)", DEFAULTCPATH);
34 print_out(" -D <dir> Directory to chroot into (default: %s)", DEFAULTDIR);
39 int main(int argc, char **argv)
41 int c, test_config = 0, run_foreground = 0;
44 const char *dir = DEFAULTDIR;
45 const char *user = USER;
46 const char *group = GROUPGLOBAL;
47 const char *cfg = DEFAULTCPATH;
49 /* Initialize global variables */
55 while ((c = getopt(argc, argv, "Vdhtfu:g:D:c:")) != -1) {
71 ErrorExit("%s: -u needs an argument", ARGV0);
77 ErrorExit("%s: -g needs an argument", ARGV0);
83 ErrorExit("%s: -D needs an argument", ARGV0);
89 ErrorExit("%s: -c needs an argument", ARGV0);
104 debug1(STARTED_MSG, ARGV0);
106 /*Check if the user/group given are valid */
107 uid = Privsep_GetUser(user);
108 gid = Privsep_GetGroup(group);
109 if (uid == (uid_t) - 1 || gid == (gid_t) - 1) {
110 ErrorExit(USER_ERROR, ARGV0, user, group);
113 /* Get config options */
114 mond.day_wait = (unsigned short) getDefine_Int("monitord", "day_wait", 5, 240);
115 mond.compress = (short) getDefine_Int("monitord", "compress", 0, 1);
116 mond.sign = (short) getDefine_Int("monitord", "sign", 0, 1);
117 mond.monitor_agents = (short) getDefine_Int("monitord", "monitor_agents", 0, 1);
118 mond.notify_time = getDefine_Int("monitord", "notify_time", 60, 3600);
120 mond.smtpserver = NULL;
121 mond.emailfrom = NULL;
122 mond.emailidsname = NULL;
126 if (ReadConfig(c, cfg, &mond, NULL) < 0) {
127 ErrorExit(CONFIG_ERROR, ARGV0, cfg);
130 /* If we have any reports configured, read smtp/emailfrom */
135 const char *(xml_smtp[]) = {"ossec_config", "global", "smtp_server", NULL};
136 const char *(xml_from[]) = {"ossec_config", "global", "email_from", NULL};
137 const char *(xml_idsname[]) = {"ossec_config", "global", "email_idsname", NULL};
139 if (OS_ReadXML(cfg, &xml) < 0) {
140 ErrorExit(CONFIG_ERROR, ARGV0, cfg);
143 tmpsmtp = OS_GetOneContentforElement(&xml, xml_smtp);
144 mond.emailfrom = OS_GetOneContentforElement(&xml, xml_from);
145 mond.emailidsname = OS_GetOneContentforElement(&xml, xml_idsname);
147 if (tmpsmtp && mond.emailfrom) {
148 mond.smtpserver = OS_GetHost(tmpsmtp, 5);
149 if (!mond.smtpserver) {
150 merror(INVALID_SMTP, ARGV0, tmpsmtp);
151 if (mond.emailfrom) {
152 free(mond.emailfrom);
154 mond.emailfrom = NULL;
155 merror("%s: Invalid SMTP server. Disabling email reports.", ARGV0);
161 if (mond.emailfrom) {
162 free(mond.emailfrom);
165 mond.emailfrom = NULL;
166 merror("%s: SMTP server or 'email from' missing. Disabling email reports.", ARGV0);
172 /* Exit here if test config is set */
177 if (!run_foreground) {
178 /* Going on daemon mode */
183 /* Privilege separation */
184 if (Privsep_SetGroup(gid) < 0) {
185 ErrorExit(SETGID_ERROR, ARGV0, group, errno, strerror(errno));
189 if (Privsep_Chroot(dir) < 0) {
190 ErrorExit(CHROOT_ERROR, ARGV0, dir, errno, strerror(errno));
196 if (Privsep_SetUser(uid) < 0) {
197 ErrorExit(SETUID_ERROR, ARGV0, user, errno, strerror(errno));
200 debug1(CHROOT_MSG, ARGV0, dir);
201 debug1(PRIVSEP_MSG, ARGV0, user);
203 /* Signal manipulation */
206 /* Create PID files */
207 if (CreatePID(ARGV0, getpid()) < 0) {
208 ErrorExit(PID_ERROR, ARGV0);
211 /* Start up message */
212 verbose(STARTUP_MSG, ARGV0, (int)getpid());
214 /* The real daemon now */