Imported Upstream version 2.7
[ossec-hids.git] / src / monitord / main.c
index 0eee48b..a4329cd 100755 (executable)
@@ -1,17 +1,20 @@
-/* @(#) $Id: main.c,v 1.9 2009/11/18 19:07:40 dcid Exp $ */
+/* @(#) $Id: ./src/monitord/main.c, 2011/09/08 dcid Exp $
+ */
 
 /* Copyright (C) 2009 Trend Micro Inc.
  * All rights reserved.
  *
  * This program is a free software; you can redistribute it
  * and/or modify it under the terms of the GNU General Public
- * License (version 3) as published by the FSF - Free Software
+ * License (version 2) as published by the FSF - Free Software
  * Foundation
  */
 
 
 #include "shared.h"
+#include "config/config.h"
 #include "monitord.h"
+#include "os_net/os_net.h"
 
 
 int main(int argc, char **argv)
@@ -28,7 +31,7 @@ int main(int argc, char **argv)
 
     /* Setting the name */
     OS_SetName(ARGV0);
-        
+
 
     while((c = getopt(argc, argv, "Vdhtfu:g:D:c:")) != -1){
         switch(c){
@@ -58,13 +61,14 @@ int main(int argc, char **argv)
                 if(!optarg)
                     ErrorExit("%s: -D needs an argument",ARGV0);
                 dir=optarg;
+                break;
             case 'c':
                 if(!optarg)
                     ErrorExit("%s: -c needs an argument",ARGV0);
                 cfg = optarg;
                 break;
             case 't':
-                test_config = 1;    
+                test_config = 1;
                 break;
             default:
                 help(ARGV0);
@@ -95,26 +99,77 @@ int main(int argc, char **argv)
     mond.monitor_agents = getDefine_Int("monitord","monitor_agents",0,1);
 
     mond.agents = NULL;
+    mond.smtpserver = NULL;
+    mond.emailfrom = NULL;
+
+
+    c = 0;
+    c|= CREPORTS;
+    if(ReadConfig(c, cfg, &mond, NULL) < 0)
+    {
+        ErrorExit(CONFIG_ERROR, ARGV0, cfg);
+    }
+
+    /* If we have any reports configured, read smtp/emailfrom */
+    if(mond.reports)
+    {
+        OS_XML xml;
+        char *tmpsmtp;
+
+        char *(xml_smtp[])={"ossec_config", "global", "smtp_server", NULL};
+        char *(xml_from[])={"ossec_config", "global", "email_from", NULL};
+
+        if(OS_ReadXML(cfg, &xml) < 0)
+        {
+            ErrorExit(CONFIG_ERROR, ARGV0, cfg);
+        }
+
+        tmpsmtp = OS_GetOneContentforElement(&xml,xml_smtp);
+        mond.emailfrom = OS_GetOneContentforElement(&xml,xml_from);
+
+        if(tmpsmtp && mond.emailfrom)
+        {
+            mond.smtpserver = OS_GetHost(tmpsmtp, 5);
+            if(!mond.smtpserver)
+            {
+                merror(INVALID_SMTP, ARGV0, tmpsmtp);
+                if(mond.emailfrom) free(mond.emailfrom);
+                mond.emailfrom = NULL;
+                merror("%s: Invalid SMTP server.  Disabling email reports.", ARGV0);
+            }
+        }
+        else
+        {
+            if(tmpsmtp) free(tmpsmtp);
+            if(mond.emailfrom) free(mond.emailfrom);
+
+            mond.emailfrom = NULL;
+
+            merror("%s: SMTP server or 'email from' missing. Disabling email reports.", ARGV0);
+        }
+
+        OS_ClearXML(&xml);
+    }
 
 
     /* Exit here if test config is set */
     if(test_config)
         exit(0);
 
-        
-    if (!run_foreground) 
+
+    if (!run_foreground)
     {
         /* Going on daemon mode */
         nowDaemon();
         goDaemon();
     }
 
-    
+
     /* Privilege separation */ 
     if(Privsep_SetGroup(gid) < 0)
         ErrorExit(SETGID_ERROR,ARGV0,group);
 
-    
+
     /* chrooting */
     if(Privsep_Chroot(dir) < 0)
         ErrorExit(CHROOT_ERROR,ARGV0,dir);
@@ -122,8 +177,8 @@ int main(int argc, char **argv)
     nowChroot();
 
 
-    
-    /* Changing user */        
+
+    /* Changing user */
     if(Privsep_SetUser(uid) < 0)
         ErrorExit(SETUID_ERROR,ARGV0,user);
 
@@ -135,16 +190,16 @@ int main(int argc, char **argv)
     /* Signal manipulation */
     StartSIG(ARGV0);
 
-    
+
 
     /* Creating PID files */
     if(CreatePID(ARGV0, getpid()) < 0)
         ErrorExit(PID_ERROR,ARGV0);
 
-    
+
     /* Start up message */
     verbose(STARTUP_MSG, ARGV0, (int)getpid());
-    
+
 
     /* the real daemon now */  
     Monitord();