3 /* Copyright (C) 2009 Trend Micro Inc.
6 * This program is a free software; you can redistribute it
7 * and/or modify it under the terms of the GNU General Public
8 * License (version 2) as published by the FSF - Free Software
13 /* Basic e-mailing operations */
17 #include "os_net/os_net.h"
21 /* Return codes (from SMTP server) */
22 #define VALIDBANNER "220"
23 #define VALIDMAIL "250"
24 #define VALIDDATA "354"
27 /* Default values use to connect */
28 #define SMTP_DEFAULT_PORT 25
29 #define HELOMSG "Helo notify.ossec.net\r\n"
30 #define MAILFROM "Mail From: <%s>\r\n"
31 #define RCPTTO "Rcpt To: <%s>\r\n"
32 #define DATAMSG "DATA\r\n"
33 #define FROM "From: OSSEC HIDS <%s>\r\n"
34 #define TO "To: <%s>\r\n"
35 #define CC "Cc: <%s>\r\n"
36 #define SUBJECT "Subject: %s\r\n"
37 #define ENDDATA "\r\n.\r\n"
38 #define QUITMSG "QUIT\r\n"
41 /* Error messages - Can be translated */
42 #define INTERNAL_ERROR "os_maild (1760): ERROR: Memory/configuration error"
43 #define BANNER_ERROR "os_sendmail(1762): WARN: Banner not received from server"
44 #define HELO_ERROR "os_sendmail(1763): WARN: Hello not accepted by server"
45 #define FROM_ERROR "os_sendmail(1764): WARN: Mail from not accepted by server"
46 #define TO_ERROR "os_sendmail(1765): WARN: RCPT TO not accepted by server - '%s'."
47 #define DATA_ERROR "os_sendmail(1766): WARN: DATA not accepted by server"
48 #define END_DATA_ERROR "os_sendmail(1767): WARN: End of DATA not accepted by server"
51 #define MAIL_DEBUG_FLAG 0
52 #define MAIL_DEBUG(x,y,z) if(MAIL_DEBUG_FLAG) merror(x,y,z)
58 int OS_SendCustomEmail(char **to, char *subject, char *smtpserver, char *from, FILE *fp, struct tm *p)
69 /* Connecting to the smtp server */
70 socket = OS_ConnectTCP(SMTP_DEFAULT_PORT, smtpserver);
77 /* Receiving the banner */
78 msg = OS_RecvTCP(socket, OS_SIZE_1024);
79 if((msg == NULL)||(!OS_Match(VALIDBANNER, msg)))
87 MAIL_DEBUG("DEBUG: Received banner: '%s' %s", msg, "");
92 /* Sending HELO message */
93 OS_SendTCP(socket,HELOMSG);
94 msg = OS_RecvTCP(socket, OS_SIZE_1024);
95 if((msg == NULL)||(!OS_Match(VALIDMAIL, msg)))
99 /* Ugly fix warning :) */
100 /* In some cases (with virus scans in the middle)
101 * we may get two banners. Check for that in here.
103 if(OS_Match(VALIDBANNER, msg))
108 msg = OS_RecvTCP(socket, OS_SIZE_1024);
109 if((msg == NULL)||(!OS_Match(VALIDMAIL, msg)))
111 merror("%s:%s",HELO_ERROR,msg!= NULL?msg:"null");
120 merror("%s:%s",HELO_ERROR,msg);
128 merror("%s:%s",HELO_ERROR,"null");
134 MAIL_DEBUG("DEBUG: Sent '%s', received: '%s'", HELOMSG, msg);
138 /* Building "Mail from" msg */
139 memset(snd_msg,'\0',128);
140 snprintf(snd_msg,127, MAILFROM, from);
141 OS_SendTCP(socket, snd_msg);
142 msg = OS_RecvTCP(socket, OS_SIZE_1024);
143 if((msg == NULL)||(!OS_Match(VALIDMAIL, msg)))
151 MAIL_DEBUG("DEBUG: Sent '%s', received: '%s'", snd_msg, msg);
155 /* Building "RCPT TO" msg */
158 memset(snd_msg,'\0',128);
159 snprintf(snd_msg,127,RCPTTO, to[i]);
160 OS_SendTCP(socket,snd_msg);
161 msg = OS_RecvTCP(socket, OS_SIZE_1024);
162 if((msg == NULL)||(!OS_Match(VALIDMAIL, msg)))
164 merror(TO_ERROR, to[i]);
170 MAIL_DEBUG("DEBUG: Sent '%s', received: '%s'", snd_msg, msg);
177 /* Sending the "DATA" msg */
178 OS_SendTCP(socket,DATAMSG);
179 msg = OS_RecvTCP(socket, OS_SIZE_1024);
180 if((msg == NULL)||(!OS_Match(VALIDDATA, msg)))
188 MAIL_DEBUG("DEBUG: Sent '%s', received: '%s'", DATAMSG, msg);
192 /* Building "From" and "To" in the e-mail header */
193 memset(snd_msg,'\0',128);
194 snprintf(snd_msg,127, TO, to[0]);
195 OS_SendTCP(socket, snd_msg);
197 memset(snd_msg,'\0',128);
198 snprintf(snd_msg,127, FROM, from);
199 OS_SendTCP(socket, snd_msg);
213 memset(snd_msg,'\0',128);
214 snprintf(snd_msg,127, TO, to[i]);
215 OS_SendTCP(socket,snd_msg);
223 memset(snd_msg,'\0',128);
226 /* Solaris doesn't have the "%z", so we set the timezone to 0. */
228 strftime(snd_msg, 127, "Date: %a, %d %b %Y %T -0000\r\n",p);
230 strftime(snd_msg, 127, "Date: %a, %d %b %Y %T %z\r\n",p);
233 OS_SendTCP(socket,snd_msg);
236 /* Sending subject */
237 memset(snd_msg, '\0', 128);
238 snprintf(snd_msg, 127, SUBJECT, subject);
240 OS_SendTCP(socket, snd_msg);
245 fseek(fp, 0, SEEK_SET);
246 while(fgets(buffer, 2048, fp) != NULL)
248 OS_SendTCP(socket,buffer);
252 /* Sending end of data \r\n.\r\n */
253 OS_SendTCP(socket,ENDDATA);
254 msg = OS_RecvTCP(socket, OS_SIZE_1024);
257 /* Checking msg in here, since it may be null */
262 /* quitting and closing socket */
263 OS_SendTCP(socket,QUITMSG);
264 msg = OS_RecvTCP(socket, OS_SIZE_1024);
269 memset(snd_msg,'\0',128);
272 /* Returning 0 (success) */