1 /* @(#) $Id: ./src/os_maild/sendcustomemail.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
14 /* Basic e-mailing operations */
18 #include "os_net/os_net.h"
22 /* Return codes (from SMTP server) */
23 #define VALIDBANNER "220"
24 #define VALIDMAIL "250"
25 #define VALIDDATA "354"
28 /* Default values use to connect */
29 #define SMTP_DEFAULT_PORT 25
30 #define HELOMSG "Helo notify.ossec.net\r\n"
31 #define MAILFROM "Mail From: <%s>\r\n"
32 #define RCPTTO "Rcpt To: <%s>\r\n"
33 #define DATAMSG "DATA\r\n"
34 #define FROM "From: OSSEC HIDS <%s>\r\n"
35 #define TO "To: <%s>\r\n"
36 #define CC "Cc: <%s>\r\n"
37 #define SUBJECT "Subject: %s\r\n"
38 #define ENDHEADER "\r\n"
39 #define ENDDATA "\r\n.\r\n"
40 #define QUITMSG "QUIT\r\n"
41 #define XHEADER "X-IDS-OSSEC: %s\r\n"
44 /* Error messages - Can be translated */
45 #define INTERNAL_ERROR "os_maild (1760): ERROR: Memory/configuration error"
46 #define BANNER_ERROR "os_sendmail(1762): WARN: Banner not received from server"
47 #define HELO_ERROR "os_sendmail(1763): WARN: Hello not accepted by server"
48 #define FROM_ERROR "os_sendmail(1764): WARN: Mail from not accepted by server"
49 #define TO_ERROR "os_sendmail(1765): WARN: RCPT TO not accepted by server - '%s'."
50 #define DATA_ERROR "os_sendmail(1766): WARN: DATA not accepted by server"
51 #define END_DATA_ERROR "os_sendmail(1767): WARN: End of DATA not accepted by server"
54 #define MAIL_DEBUG_FLAG 0
55 #define MAIL_DEBUG(x,y,z) if(MAIL_DEBUG_FLAG) merror(x,y,z)
61 int OS_SendCustomEmail(char **to, char *subject, char *smtpserver, char *from, char *idsname, FILE *fp, struct tm *p)
72 /* Connecting to the smtp server */
73 socket = OS_ConnectTCP(SMTP_DEFAULT_PORT, smtpserver, 0);
80 /* Receiving the banner */
81 msg = OS_RecvTCP(socket, OS_SIZE_1024);
82 if((msg == NULL)||(!OS_Match(VALIDBANNER, msg)))
90 MAIL_DEBUG("DEBUG: Received banner: '%s' %s", msg, "");
95 /* Sending HELO message */
96 OS_SendTCP(socket,HELOMSG);
97 msg = OS_RecvTCP(socket, OS_SIZE_1024);
98 if((msg == NULL)||(!OS_Match(VALIDMAIL, msg)))
102 /* Ugly fix warning :) */
103 /* In some cases (with virus scans in the middle)
104 * we may get two banners. Check for that in here.
106 if(OS_Match(VALIDBANNER, msg))
111 msg = OS_RecvTCP(socket, OS_SIZE_1024);
112 if((msg == NULL)||(!OS_Match(VALIDMAIL, msg)))
114 merror("%s:%s",HELO_ERROR,msg!= NULL?msg:"null");
123 merror("%s:%s",HELO_ERROR,msg);
131 merror("%s:%s",HELO_ERROR,"null");
137 MAIL_DEBUG("DEBUG: Sent '%s', received: '%s'", HELOMSG, msg);
141 /* Building "Mail from" msg */
142 memset(snd_msg,'\0',128);
143 snprintf(snd_msg,127, MAILFROM, from);
144 OS_SendTCP(socket, snd_msg);
145 msg = OS_RecvTCP(socket, OS_SIZE_1024);
146 if((msg == NULL)||(!OS_Match(VALIDMAIL, msg)))
154 MAIL_DEBUG("DEBUG: Sent '%s', received: '%s'", snd_msg, msg);
158 /* Building "RCPT TO" msg */
161 memset(snd_msg,'\0',128);
162 snprintf(snd_msg,127,RCPTTO, to[i]);
163 OS_SendTCP(socket,snd_msg);
164 msg = OS_RecvTCP(socket, OS_SIZE_1024);
165 if((msg == NULL)||(!OS_Match(VALIDMAIL, msg)))
167 merror(TO_ERROR, to[i]);
173 MAIL_DEBUG("DEBUG: Sent '%s', received: '%s'", snd_msg, msg);
180 /* Sending the "DATA" msg */
181 OS_SendTCP(socket,DATAMSG);
182 msg = OS_RecvTCP(socket, OS_SIZE_1024);
183 if((msg == NULL)||(!OS_Match(VALIDDATA, msg)))
191 MAIL_DEBUG("DEBUG: Sent '%s', received: '%s'", DATAMSG, msg);
195 /* Building "From" and "To" in the e-mail header */
196 memset(snd_msg,'\0',128);
197 snprintf(snd_msg,127, TO, to[0]);
198 OS_SendTCP(socket, snd_msg);
200 memset(snd_msg,'\0',128);
201 snprintf(snd_msg,127, FROM, from);
202 OS_SendTCP(socket, snd_msg);
216 memset(snd_msg,'\0',128);
217 snprintf(snd_msg,127, TO, to[i]);
218 OS_SendTCP(socket,snd_msg);
226 memset(snd_msg,'\0',128);
229 /* Solaris doesn't have the "%z", so we set the timezone to 0. */
231 strftime(snd_msg, 127, "Date: %a, %d %b %Y %T -0000\r\n",p);
233 strftime(snd_msg, 127, "Date: %a, %d %b %Y %T %z\r\n",p);
236 OS_SendTCP(socket,snd_msg);
240 /* Sending server name header */
241 memset(snd_msg,'\0',128);
242 snprintf(snd_msg,127, XHEADER, idsname);
243 OS_SendTCP(socket, snd_msg);
246 /* Sending subject */
247 memset(snd_msg, '\0', 128);
248 snprintf(snd_msg, 127, SUBJECT, subject);
250 OS_SendTCP(socket, snd_msg);
252 OS_SendTCP(socket,ENDHEADER);
256 fseek(fp, 0, SEEK_SET);
257 while(fgets(buffer, 2048, fp) != NULL)
259 OS_SendTCP(socket,buffer);
263 /* Sending end of data \r\n.\r\n */
264 OS_SendTCP(socket,ENDDATA);
265 msg = OS_RecvTCP(socket, OS_SIZE_1024);
268 /* Checking msg in here, since it may be null */
273 /* quitting and closing socket */
274 OS_SendTCP(socket,QUITMSG);
275 msg = OS_RecvTCP(socket, OS_SIZE_1024);
280 memset(snd_msg,'\0',128);
283 /* Returning 0 (success) */