1 /* @(#) $Id: ./src/os_maild/sendmail.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"
20 #include "mail_list.h"
23 /* Return codes (from SMTP server) */
24 #define VALIDBANNER "220"
25 #define VALIDMAIL "250"
26 #define VALIDDATA "354"
29 /* Default values use to connect */
30 #define SMTP_DEFAULT_PORT 25
31 #define HELOMSG "Helo notify.ossec.net\r\n"
32 #define MAILFROM "Mail From: <%s>\r\n"
33 #define RCPTTO "Rcpt To: <%s>\r\n"
34 #define DATAMSG "DATA\r\n"
35 #define FROM "From: OSSEC HIDS <%s>\r\n"
36 #define TO "To: <%s>\r\n"
37 #define CC "Cc: <%s>\r\n"
38 #define SUBJECT "Subject: %s\r\n"
39 #define ENDDATA "\r\n.\r\n"
40 #define QUITMSG "QUIT\r\n"
43 /* Error messages - Can be translated */
44 #define INTERNAL_ERROR "os_maild (1760): ERROR: Memory/configuration error"
45 #define BANNER_ERROR "os_sendmail(1762): WARN: Banner not received from server"
46 #define HELO_ERROR "os_sendmail(1763): WARN: Hello not accepted by server"
47 #define FROM_ERROR "os_sendmail(1764): WARN: Mail from not accepted by server"
48 #define TO_ERROR "os_sendmail(1765): WARN: RCPT TO not accepted by server - '%s'."
49 #define DATA_ERROR "os_sendmail(1766): WARN: DATA not accepted by server"
50 #define END_DATA_ERROR "os_sendmail(1767): WARN: End of DATA not accepted by server"
53 #define MAIL_DEBUG_FLAG 0
54 #define MAIL_DEBUG(x,y,z) if(MAIL_DEBUG_FLAG) merror(x,y,z)
59 int OS_Sendsms(MailConfig *mail, struct tm *p, MailMsg *sms_msg)
61 int socket, i = 0, final_to_sz;
67 /* Connecting to the smtp server */
68 socket = OS_ConnectTCP(SMTP_DEFAULT_PORT, mail->smtpserver, 0);
75 /* Receiving the banner */
76 msg = OS_RecvTCP(socket, OS_SIZE_1024);
77 if((msg == NULL)||(!OS_Match(VALIDBANNER, msg)))
85 MAIL_DEBUG("DEBUG: Received banner: '%s' %s", msg, "");
90 /* Sending HELO message */
91 OS_SendTCP(socket,HELOMSG);
92 msg = OS_RecvTCP(socket, OS_SIZE_1024);
93 if((msg == NULL)||(!OS_Match(VALIDMAIL, msg)))
97 /* Ugly fix warning :) */
98 /* In some cases (with virus scans in the middle)
99 * we may get two banners. Check for that in here.
101 if(OS_Match(VALIDBANNER, msg))
106 msg = OS_RecvTCP(socket, OS_SIZE_1024);
107 if((msg == NULL)||(!OS_Match(VALIDMAIL, msg)))
109 merror("%s:%s",HELO_ERROR,msg!= NULL?msg:"null");
118 merror("%s:%s",HELO_ERROR,msg);
126 merror("%s:%s",HELO_ERROR,"null");
132 MAIL_DEBUG("DEBUG: Sent '%s', received: '%s'", HELOMSG, msg);
136 /* Building "Mail from" msg */
137 memset(snd_msg,'\0',128);
138 snprintf(snd_msg,127, MAILFROM, mail->from);
139 OS_SendTCP(socket, snd_msg);
140 msg = OS_RecvTCP(socket, OS_SIZE_1024);
141 if((msg == NULL)||(!OS_Match(VALIDMAIL, msg)))
149 MAIL_DEBUG("DEBUG: Sent '%s', received: '%s'", snd_msg, msg);
153 /* Additional RCPT to */
155 final_to_sz = sizeof(final_to) -2;
160 while(mail->gran_to[i] != NULL)
162 if(mail->gran_set[i] != SMS_FORMAT)
168 memset(snd_msg,'\0',128);
169 snprintf(snd_msg,127, RCPTTO, mail->gran_to[i]);
170 OS_SendTCP(socket, snd_msg);
171 msg = OS_RecvTCP(socket, OS_SIZE_1024);
172 if((msg == NULL)||(!OS_Match(VALIDMAIL, msg)))
174 merror(TO_ERROR, mail->gran_to[i]);
180 MAIL_DEBUG("DEBUG: Sent '%s', received: '%s'", snd_msg, msg);
184 /* Creating header for to */
185 memset(snd_msg,'\0',128);
186 snprintf(snd_msg,127, TO, mail->gran_to[i]);
187 strncat(final_to, snd_msg, final_to_sz);
188 final_to_sz -= strlen(snd_msg) +2;
196 /* Sending the "DATA" msg */
197 OS_SendTCP(socket,DATAMSG);
198 msg = OS_RecvTCP(socket, OS_SIZE_1024);
199 if((msg == NULL)||(!OS_Match(VALIDDATA, msg)))
207 MAIL_DEBUG("DEBUG: Sent '%s', received: '%s'", DATAMSG, msg);
211 /* Building "From" and "To" in the e-mail header */
212 OS_SendTCP(socket, final_to);
215 memset(snd_msg,'\0',128);
216 snprintf(snd_msg,127, FROM, mail->from);
217 OS_SendTCP(socket, snd_msg);
221 memset(snd_msg,'\0',128);
224 /* Solaris doesn't have the "%z", so we set the timezone to 0. */
226 strftime(snd_msg, 127, "Date: %a, %d %b %Y %T -0000\r\n",p);
228 strftime(snd_msg, 127, "Date: %a, %d %b %Y %T %z\r\n",p);
231 OS_SendTCP(socket,snd_msg);
234 /* Sending subject */
235 memset(snd_msg,'\0',128);
236 snprintf(snd_msg, 127, SUBJECT, sms_msg->subject);
237 OS_SendTCP(socket,snd_msg);
242 OS_SendTCP(socket, sms_msg->body);
245 /* Sending end of data \r\n.\r\n */
246 OS_SendTCP(socket,ENDDATA);
247 msg = OS_RecvTCP(socket, OS_SIZE_1024);
248 if(mail->strict_checking && ((msg == NULL)||(!OS_Match(VALIDMAIL, msg))))
250 merror(END_DATA_ERROR);
256 /* Checking msg in here, since it may be null */
261 /* quitting and closing socket */
262 OS_SendTCP(socket,QUITMSG);
263 msg = OS_RecvTCP(socket, OS_SIZE_1024);
268 memset(snd_msg,'\0',128);
271 /* Returning 0 (success) */
279 /* OS_Sendmail v0.1: 2005/03/18
281 int OS_Sendmail(MailConfig *mail, struct tm *p)
286 char additional_to[512];
290 additional_to[0] = '\0';
292 /* If there is no sms message, we attempt to get from the
295 mailmsg = OS_PopLastMail();
299 merror("%s: No email to be sent. Inconsistent state.",ARGV0);
303 /* Connecting to the smtp server */
304 socket = OS_ConnectTCP(SMTP_DEFAULT_PORT, mail->smtpserver, 0);
311 /* Receiving the banner */
312 msg = OS_RecvTCP(socket, OS_SIZE_1024);
313 if((msg == NULL)||(!OS_Match(VALIDBANNER, msg)))
315 merror(BANNER_ERROR);
321 MAIL_DEBUG("DEBUG: Received banner: '%s' %s", msg, "");
326 /* Sending HELO message */
327 OS_SendTCP(socket,HELOMSG);
328 msg = OS_RecvTCP(socket, OS_SIZE_1024);
329 if((msg == NULL)||(!OS_Match(VALIDMAIL, msg)))
333 /* Ugly fix warning :) */
334 /* In some cases (with virus scans in the middle)
335 * we may get two banners. Check for that in here.
337 if(OS_Match(VALIDBANNER, msg))
342 msg = OS_RecvTCP(socket, OS_SIZE_1024);
343 if((msg == NULL)||(!OS_Match(VALIDMAIL, msg)))
345 merror("%s:%s",HELO_ERROR,msg!= NULL?msg:"null");
354 merror("%s:%s",HELO_ERROR,msg);
362 merror("%s:%s",HELO_ERROR,"null");
368 MAIL_DEBUG("DEBUG: Sent '%s', received: '%s'", HELOMSG, msg);
372 /* Building "Mail from" msg */
373 memset(snd_msg,'\0',128);
374 snprintf(snd_msg,127, MAILFROM, mail->from);
375 OS_SendTCP(socket, snd_msg);
376 msg = OS_RecvTCP(socket, OS_SIZE_1024);
377 if((msg == NULL)||(!OS_Match(VALIDMAIL, msg)))
385 MAIL_DEBUG("DEBUG: Sent '%s', received: '%s'", snd_msg, msg);
389 /* Building "RCPT TO" msg */
392 if(mail->to[i] == NULL)
396 merror(INTERNAL_ERROR);
402 memset(snd_msg,'\0',128);
403 snprintf(snd_msg,127,RCPTTO, mail->to[i++]);
404 OS_SendTCP(socket,snd_msg);
405 msg = OS_RecvTCP(socket, OS_SIZE_1024);
406 if((msg == NULL)||(!OS_Match(VALIDMAIL, msg)))
408 merror(TO_ERROR, mail->to[i -1]);
414 MAIL_DEBUG("DEBUG: Sent '%s', received: '%s'", snd_msg, msg);
419 /* Additional RCPT to */
423 while(mail->gran_to[i] != NULL)
425 if(mail->gran_set[i] != FULL_FORMAT)
431 memset(snd_msg,'\0',128);
432 snprintf(snd_msg,127,RCPTTO, mail->gran_to[i]);
433 OS_SendTCP(socket,snd_msg);
434 msg = OS_RecvTCP(socket, OS_SIZE_1024);
435 if((msg == NULL)||(!OS_Match(VALIDMAIL, msg)))
437 merror(TO_ERROR, mail->gran_to[i]);
445 MAIL_DEBUG("DEBUG: Sent '%s', received: '%s'", snd_msg, msg);
453 /* Sending the "DATA" msg */
454 OS_SendTCP(socket,DATAMSG);
455 msg = OS_RecvTCP(socket, OS_SIZE_1024);
456 if((msg == NULL)||(!OS_Match(VALIDDATA, msg)))
464 MAIL_DEBUG("DEBUG: Sent '%s', received: '%s'", DATAMSG, msg);
468 /* Building "From" and "To" in the e-mail header */
469 memset(snd_msg,'\0',128);
470 snprintf(snd_msg,127, TO, mail->to[0]);
471 OS_SendTCP(socket, snd_msg);
473 memset(snd_msg,'\0',128);
474 snprintf(snd_msg,127, FROM, mail->from);
475 OS_SendTCP(socket, snd_msg);
484 if(mail->to[i] == NULL)
489 memset(snd_msg,'\0',128);
490 snprintf(snd_msg,127, TO, mail->to[i]);
491 OS_SendTCP(socket,snd_msg);
498 /* More CCs - from granular options */
502 while(mail->gran_to[i] != NULL)
504 if(mail->gran_set[i] != FULL_FORMAT)
510 memset(snd_msg,'\0',128);
511 snprintf(snd_msg,127, TO, mail->gran_to[i]);
512 OS_SendTCP(socket, snd_msg);
520 memset(snd_msg,'\0',128);
523 /* Solaris doesn't have the "%z", so we set the timezone to 0. */
525 strftime(snd_msg, 127, "Date: %a, %d %b %Y %T -0000\r\n",p);
527 strftime(snd_msg, 127, "Date: %a, %d %b %Y %T %z\r\n",p);
530 OS_SendTCP(socket,snd_msg);
533 /* Sending subject */
534 memset(snd_msg,'\0',128);
537 /* Checking if global subject is available */
538 if((_g_subject_level != 0) && (_g_subject[0] != '\0'))
540 snprintf(snd_msg, 127, SUBJECT, _g_subject);
542 /* Clearing global values */
543 _g_subject[0] = '\0';
544 _g_subject_level = 0;
548 snprintf(snd_msg, 127, SUBJECT, mailmsg->mail->subject);
550 OS_SendTCP(socket,snd_msg);
556 /* Sending multiple emails together if we have to */
559 OS_SendTCP(socket, mailmsg->mail->body);
560 mailmsg = OS_PopLastMail();
564 /* Sending end of data \r\n.\r\n */
565 OS_SendTCP(socket,ENDDATA);
566 msg = OS_RecvTCP(socket, OS_SIZE_1024);
567 if(mail->strict_checking && ((msg == NULL)||(!OS_Match(VALIDMAIL, msg))))
569 merror(END_DATA_ERROR);
575 /* Checking msg in here, since it may be null */
580 /* quitting and closing socket */
581 OS_SendTCP(socket,QUITMSG);
582 msg = OS_RecvTCP(socket, OS_SIZE_1024);
587 memset(snd_msg,'\0',128);
590 /* Returning 0 (success) */