1 /* @(#) $Id: os_maild_client.c,v 1.22 2009/06/24 17:06:31 dcid Exp $ */
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 3) as published by the FSF - Free Software
19 * Receive a Message on the Mail queue
20 * v0,2: Using the new file-queue.
22 MailMsg *OS_RecvMailQ(file_queue *fileq, struct tm *p,
23 MailConfig *Mail, MailMsg **msg_sms)
25 int i = 0, body_size = OS_MAXSTR -3, log_size, sms_set = 0,donotgroup = 0;
26 char logs[OS_MAXSTR + 1];
35 /* Get message if available */
36 al_data = Read_FileMon(fileq, p, mail_timeout);
41 /* If e-mail came correctly, generate the e-mail body/subject */
42 os_calloc(1,sizeof(MailMsg), mail);
43 os_calloc(BODY_SIZE, sizeof(char), mail->body);
44 os_calloc(SUBJECT_SIZE, sizeof(char), mail->subject);
47 /* Generating the logs */
49 logs[OS_MAXSTR] = '\0';
51 while(al_data->log[i])
53 log_size = strlen(al_data->log[i]) + 4;
55 /* If size left is small than the size of the log, stop it */
56 if(body_size <= log_size)
61 strncat(logs, al_data->log[i], body_size);
62 strncat(logs, "\r\n", body_size);
63 body_size -= log_size;
69 subject_host = strchr(al_data->location, '>');
76 /* We have two subject options - full and normal */
77 if(Mail->subject_full)
79 /* Option for a clean full subject (without ossec in the name) */
81 snprintf(mail->subject, SUBJECT_SIZE -1, MAIL_SUBJECT_FULL2,
86 snprintf(mail->subject, SUBJECT_SIZE -1, MAIL_SUBJECT_FULL,
94 snprintf(mail->subject, SUBJECT_SIZE -1, MAIL_SUBJECT,
100 /* fixing subject back */
108 snprintf(mail->body, BODY_SIZE -1, MAIL_BODY,
117 /* Checking for granular email configs */
121 while(Mail->gran_to[i] != NULL)
125 /* Looking if location is set */
126 if(Mail->gran_location[i])
128 if(OSMatch_Execute(al_data->location,
129 strlen(al_data->location),
130 Mail->gran_location[i]))
141 /* Looking for the level */
142 if(Mail->gran_level[i])
144 if(al_data->level >= Mail->gran_level[i])
156 /* Looking for rule id */
160 while(Mail->gran_id[i][id_i] != 0)
162 if(Mail->gran_id[i][id_i] == al_data->rule)
169 /* If we found, id is going to be a valid rule */
170 if(Mail->gran_id[i][id_i])
182 /* Looking for the group */
183 if(Mail->gran_group[i])
185 if(OSMatch_Execute(al_data->group,
186 strlen(al_data->group),
187 Mail->gran_group[i]))
199 /* If we got in here, it is because everything
200 * matched. Set this e-mail to be used.
204 if(Mail->gran_format[i] == SMS_FORMAT)
206 Mail->gran_set[i] = SMS_FORMAT;
208 /* Setting the SMS flag */
214 if(Mail->gran_format[i] == FORWARD_NOW)
217 Mail->gran_set[i] = FULL_FORMAT;
219 else if(Mail->gran_format[i] == DONOTGROUP)
221 Mail->priority = DONOTGROUP;
222 Mail->gran_set[i] = DONOTGROUP;
227 Mail->gran_set[i] = FULL_FORMAT;
236 /* If DONOTGROUP is set, we can't assign the new subject */
239 /* Getting highest level for alert */
240 if(_g_subject[0] != '\0')
242 if(_g_subject_level < al_data->level)
244 strncpy(_g_subject, mail->subject, SUBJECT_SIZE);
245 _g_subject_level = al_data->level;
250 strncpy(_g_subject, mail->subject, SUBJECT_SIZE);
251 _g_subject_level = al_data->level;
256 /* If sms is set, create the sms output */
259 MailMsg *msg_sms_tmp;
261 /* Allocate memory for sms */
262 os_calloc(1,sizeof(MailMsg), msg_sms_tmp);
263 os_calloc(BODY_SIZE, sizeof(char), msg_sms_tmp->body);
264 os_calloc(SUBJECT_SIZE, sizeof(char), msg_sms_tmp->subject);
266 snprintf(msg_sms_tmp->subject, SUBJECT_SIZE -1, SMS_SUBJECT,
272 strncpy(msg_sms_tmp->body, logs, 128);
273 msg_sms_tmp->body[127] = '\0';
275 /* Assigning msg_sms */
276 *msg_sms = msg_sms_tmp;
281 /* Clearing the memory */
282 FreeAlertData(al_data);