novi upstream verzije 2.8.3
[ossec-hids.git] / src / os_maild / sendmail.c
index 5b0fd6f..d937a15 100755 (executable)
 #define TO                         "To: <%s>\r\n"
 #define CC                         "Cc: <%s>\r\n"
 #define SUBJECT                        "Subject: %s\r\n"
+#define ENDHEADER               "\r\n"
 #define ENDDATA                        "\r\n.\r\n"
 #define QUITMSG                "QUIT\r\n"
+#define XHEADER                "X-IDS-OSSEC: %s\r\n"
 
 
 /* Error messages - Can be translated */
@@ -64,7 +66,7 @@ int OS_Sendsms(MailConfig *mail, struct tm *p, MailMsg *sms_msg)
     char final_to[512];
 
 
-    /* Connecting to the smtp server */        
+    /* Connecting to the smtp server */
     socket = OS_ConnectTCP(SMTP_DEFAULT_PORT, mail->smtpserver, 0);
     if(socket < 0)
     {
@@ -80,7 +82,7 @@ int OS_Sendsms(MailConfig *mail, struct tm *p, MailMsg *sms_msg)
         if(msg)
             free(msg);
         close(socket);
-        return(OS_INVALID);    
+        return(OS_INVALID);
     }
     MAIL_DEBUG("DEBUG: Received banner: '%s' %s", msg, "");
     free(msg);
@@ -130,7 +132,7 @@ int OS_Sendsms(MailConfig *mail, struct tm *p, MailMsg *sms_msg)
     }
 
     MAIL_DEBUG("DEBUG: Sent '%s', received: '%s'", HELOMSG, msg);
-    free(msg); 
+    free(msg);
 
 
     /* Building "Mail from" msg */
@@ -144,10 +146,10 @@ int OS_Sendsms(MailConfig *mail, struct tm *p, MailMsg *sms_msg)
         if(msg)
             free(msg);
         close(socket);
-        return(OS_INVALID);    
+        return(OS_INVALID);
     }
     MAIL_DEBUG("DEBUG: Sent '%s', received: '%s'", snd_msg, msg);
-    free(msg); 
+    free(msg);
 
 
     /* Additional RCPT to */
@@ -202,7 +204,7 @@ int OS_Sendsms(MailConfig *mail, struct tm *p, MailMsg *sms_msg)
         if(msg)
             free(msg);
         close(socket);
-        return(OS_INVALID);    
+        return(OS_INVALID);
     }
     MAIL_DEBUG("DEBUG: Sent '%s', received: '%s'", DATAMSG, msg);
     free(msg);
@@ -236,6 +238,7 @@ int OS_Sendsms(MailConfig *mail, struct tm *p, MailMsg *sms_msg)
     snprintf(snd_msg, 127, SUBJECT, sms_msg->subject);
     OS_SendTCP(socket,snd_msg);
 
+    OS_SendTCP(socket,ENDHEADER);
 
 
     /* Sending body */
@@ -243,7 +246,7 @@ int OS_Sendsms(MailConfig *mail, struct tm *p, MailMsg *sms_msg)
 
 
     /* Sending end of data \r\n.\r\n */
-    OS_SendTCP(socket,ENDDATA);        
+    OS_SendTCP(socket,ENDDATA);
     msg = OS_RecvTCP(socket, OS_SIZE_1024);
     if(mail->strict_checking && ((msg == NULL)||(!OS_Match(VALIDMAIL, msg))))
     {
@@ -251,7 +254,7 @@ int OS_Sendsms(MailConfig *mail, struct tm *p, MailMsg *sms_msg)
         if(msg)
             free(msg);
         close(socket);
-        return(OS_INVALID);    
+        return(OS_INVALID);
     }
     /* Checking msg in here, since it may be null */
     if(msg)
@@ -265,7 +268,7 @@ int OS_Sendsms(MailConfig *mail, struct tm *p, MailMsg *sms_msg)
     if(msg)
         free(msg);
 
-    memset(snd_msg,'\0',128);  
+    memset(snd_msg,'\0',128);
 
 
     /* Returning 0 (success) */
@@ -283,13 +286,10 @@ int OS_Sendmail(MailConfig *mail, struct tm *p)
     int socket,i=0;
     char *msg;
     char snd_msg[128];
-    char additional_to[512];
 
     MailNode *mailmsg;
 
-    additional_to[0] = '\0';
-
-    /* If there is no sms message, we attempt to get from the
+   /* If there is no sms message, we attempt to get from the
      * email list.
      */
     mailmsg = OS_PopLastMail();
@@ -300,7 +300,7 @@ int OS_Sendmail(MailConfig *mail, struct tm *p)
     }
 
 
-    /* Connecting to the smtp server */        
+    /* Connecting to the smtp server */
     socket = OS_ConnectTCP(SMTP_DEFAULT_PORT, mail->smtpserver, 0);
     if(socket < 0)
     {
@@ -316,7 +316,7 @@ int OS_Sendmail(MailConfig *mail, struct tm *p)
         if(msg)
             free(msg);
         close(socket);
-        return(OS_INVALID);    
+        return(OS_INVALID);
     }
     MAIL_DEBUG("DEBUG: Received banner: '%s' %s", msg, "");
     free(msg);
@@ -366,7 +366,7 @@ int OS_Sendmail(MailConfig *mail, struct tm *p)
     }
 
     MAIL_DEBUG("DEBUG: Sent '%s', received: '%s'", HELOMSG, msg);
-    free(msg); 
+    free(msg);
 
 
     /* Building "Mail from" msg */
@@ -380,10 +380,10 @@ int OS_Sendmail(MailConfig *mail, struct tm *p)
         if(msg)
             free(msg);
         close(socket);
-        return(OS_INVALID);    
+        return(OS_INVALID);
     }
     MAIL_DEBUG("DEBUG: Sent '%s', received: '%s'", snd_msg, msg);
-    free(msg); 
+    free(msg);
 
 
     /* Building "RCPT TO" msg */
@@ -409,7 +409,7 @@ int OS_Sendmail(MailConfig *mail, struct tm *p)
             if(msg)
                 free(msg);
             close(socket);
-            return(OS_INVALID);        
+            return(OS_INVALID);
         }
         MAIL_DEBUG("DEBUG: Sent '%s', received: '%s'", snd_msg, msg);
         free(msg);
@@ -459,7 +459,7 @@ int OS_Sendmail(MailConfig *mail, struct tm *p)
         if(msg)
             free(msg);
         close(socket);
-        return(OS_INVALID);    
+        return(OS_INVALID);
     }
     MAIL_DEBUG("DEBUG: Sent '%s', received: '%s'", DATAMSG, msg);
     free(msg);
@@ -529,6 +529,13 @@ int OS_Sendmail(MailConfig *mail, struct tm *p)
 
     OS_SendTCP(socket,snd_msg);
 
+    if(mail->idsname)
+    {
+        /* Sending server name header */
+        memset(snd_msg,'\0',128);
+        snprintf(snd_msg,127, XHEADER, mail->idsname);
+        OS_SendTCP(socket, snd_msg);
+    }
 
     /* Sending subject */
     memset(snd_msg,'\0',128);
@@ -537,7 +544,7 @@ int OS_Sendmail(MailConfig *mail, struct tm *p)
     /* Checking if global subject is available */
     if((_g_subject_level != 0) && (_g_subject[0] != '\0'))
     {
-        snprintf(snd_msg, 127, SUBJECT, _g_subject);   
+        snprintf(snd_msg, 127, SUBJECT, _g_subject);
 
         /* Clearing global values */
         _g_subject[0] = '\0';
@@ -549,6 +556,7 @@ int OS_Sendmail(MailConfig *mail, struct tm *p)
     }
     OS_SendTCP(socket,snd_msg);
 
+    OS_SendTCP(socket,ENDHEADER);
 
 
     /* Sending body */
@@ -562,7 +570,7 @@ int OS_Sendmail(MailConfig *mail, struct tm *p)
 
 
     /* Sending end of data \r\n.\r\n */
-    OS_SendTCP(socket,ENDDATA);        
+    OS_SendTCP(socket,ENDDATA);
     msg = OS_RecvTCP(socket, OS_SIZE_1024);
     if(mail->strict_checking && ((msg == NULL)||(!OS_Match(VALIDMAIL, msg))))
     {
@@ -570,7 +578,7 @@ int OS_Sendmail(MailConfig *mail, struct tm *p)
         if(msg)
             free(msg);
         close(socket);
-        return(OS_INVALID);    
+        return(OS_INVALID);
     }
     /* Checking msg in here, since it may be null */
     if(msg)
@@ -584,7 +592,7 @@ int OS_Sendmail(MailConfig *mail, struct tm *p)
     if(msg)
         free(msg);
 
-    memset(snd_msg,'\0',128);  
+    memset(snd_msg,'\0',128);
 
 
     /* Returning 0 (success) */