Imported Upstream version 2.5.1
[ossec-hids.git] / src / os_maild / os_maild_client.c
1 /* @(#) $Id$ */
2
3 /* Copyright (C) 2009 Trend Micro Inc.
4  * All rights reserved.
5  *
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
9  * Foundation
10  */
11
12
13 #include "shared.h"
14 #include "maild.h"
15
16
17 /* OS_RecvMailQ, 
18  * v0.1, 2005/03/15
19  * Receive a Message on the Mail queue
20  * v0,2: Using the new file-queue.
21  */
22 MailMsg *OS_RecvMailQ(file_queue *fileq, struct tm *p, 
23                       MailConfig *Mail, MailMsg **msg_sms)
24 {
25     int i = 0, body_size = OS_MAXSTR -3, log_size, sms_set = 0,donotgroup = 0;
26     char logs[OS_MAXSTR + 1];
27     char *subject_host;
28     
29     MailMsg *mail;
30     alert_data *al_data;
31
32     Mail->priority = 0;
33
34
35     /* Get message if available */
36     al_data = Read_FileMon(fileq, p, mail_timeout);
37     if(!al_data)
38         return(NULL);
39
40
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);
45
46
47     /* Generating the logs */
48     logs[0] = '\0';
49     logs[OS_MAXSTR] = '\0';
50     
51     while(al_data->log[i])
52     {
53         log_size = strlen(al_data->log[i]) + 4;
54         
55         /* If size left is small than the size of the log, stop it */
56         if(body_size <= log_size)
57         {
58             break;
59         }
60         
61         strncat(logs, al_data->log[i], body_size);
62         strncat(logs, "\r\n", body_size);
63         body_size -= log_size;
64         i++;
65     }
66
67
68     /* Subject */
69     subject_host = strchr(al_data->location, '>');
70     if(subject_host)
71     {
72         subject_host--;
73         *subject_host = '\0';
74     }
75
76     /* We have two subject options - full and normal */
77     if(Mail->subject_full)
78     {
79         /* Option for a clean full subject (without ossec in the name) */
80         #ifdef CLEANFULL
81         snprintf(mail->subject, SUBJECT_SIZE -1, MAIL_SUBJECT_FULL2, 
82                                 al_data->level,
83                                 al_data->comment,
84                                 al_data->location);
85         #else
86         snprintf(mail->subject, SUBJECT_SIZE -1, MAIL_SUBJECT_FULL, 
87                                 al_data->location,
88                                 al_data->level,
89                                 al_data->comment);
90         #endif
91     }
92     else
93     {
94         snprintf(mail->subject, SUBJECT_SIZE -1, MAIL_SUBJECT, 
95                                              al_data->location,
96                                              al_data->level);
97     }
98
99     
100     /* fixing subject back */
101     if(subject_host)
102     {
103         *subject_host = '-';
104     }
105
106     
107     /* Body */
108     snprintf(mail->body, BODY_SIZE -1, MAIL_BODY,
109             al_data->date,
110             al_data->location,
111             al_data->rule,
112             al_data->level,
113             al_data->comment,
114             logs);
115
116
117     /* Checking for granular email configs */
118     if(Mail->gran_to)
119     {
120         i = 0;
121         while(Mail->gran_to[i] != NULL)
122         {
123             int gr_set = 0;
124             
125             /* Looking if location is set */
126             if(Mail->gran_location[i])
127             {
128                 if(OSMatch_Execute(al_data->location,
129                                    strlen(al_data->location),
130                                    Mail->gran_location[i]))
131                 {
132                     gr_set = 1;
133                 }
134                 else
135                 {
136                     i++;
137                     continue;
138                 }
139             }
140             
141             /* Looking for the level */
142             if(Mail->gran_level[i])
143             {
144                 if(al_data->level >= Mail->gran_level[i])
145                 {
146                     gr_set = 1;
147                 }
148                 else
149                 {
150                     i++;
151                     continue;
152                 }
153             }
154
155
156             /* Looking for rule id */
157             if(Mail->gran_id[i])
158             {
159                 int id_i = 0;
160                 while(Mail->gran_id[i][id_i] != 0)
161                 {
162                     if(Mail->gran_id[i][id_i] == al_data->rule)
163                     {
164                         break;
165                     }
166                     id_i++;
167                 }
168
169                 /* If we found, id is going to be a valid rule */
170                 if(Mail->gran_id[i][id_i])
171                 {
172                     gr_set = 1;
173                 }
174                 else
175                 {
176                     i++;
177                     continue;
178                 }
179             }
180             
181
182             /* Looking for the group */
183             if(Mail->gran_group[i])
184             {
185                 if(OSMatch_Execute(al_data->group,
186                                    strlen(al_data->group),
187                                    Mail->gran_group[i]))
188                 {
189                     gr_set = 1;
190                 }
191                 else
192                 {
193                     i++;
194                     continue;
195                 }
196             }
197
198
199             /* If we got in here, it is because everything
200              * matched. Set this e-mail to be used.
201              */
202             if(gr_set)
203             {
204                 if(Mail->gran_format[i] == SMS_FORMAT)
205                 {
206                     Mail->gran_set[i] = SMS_FORMAT;
207
208                     /* Setting the SMS flag */
209                     sms_set = 1;
210                 }
211                 else
212                 {
213                     /* Options */
214                     if(Mail->gran_format[i] == FORWARD_NOW)
215                     {
216                         Mail->priority = 1;
217                         Mail->gran_set[i] = FULL_FORMAT;
218                     }
219                     else if(Mail->gran_format[i] == DONOTGROUP)
220                     {
221                         Mail->priority = DONOTGROUP;
222                         Mail->gran_set[i] = DONOTGROUP;
223                         donotgroup = 1;
224                     }
225                     else
226                     {
227                         Mail->gran_set[i] = FULL_FORMAT;
228                     }
229                 }
230             }
231             i++;
232         }
233     }
234
235
236     /* If DONOTGROUP is set, we can't assign the new subject */
237     if(!donotgroup)
238     {
239         /* Getting highest level for alert */
240         if(_g_subject[0] != '\0')
241         {
242             if(_g_subject_level < al_data->level)
243             {
244                 strncpy(_g_subject, mail->subject, SUBJECT_SIZE);
245                 _g_subject_level = al_data->level;
246             }
247         }
248         else
249         {
250             strncpy(_g_subject, mail->subject, SUBJECT_SIZE);
251             _g_subject_level = al_data->level;
252         }
253     }
254     
255     
256     /* If sms is set, create the sms output */
257     if(sms_set)
258     {
259         MailMsg *msg_sms_tmp;
260         
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);
265
266         snprintf(msg_sms_tmp->subject, SUBJECT_SIZE -1, SMS_SUBJECT,
267                                       al_data->level,
268                                       al_data->rule,
269                                       al_data->comment);
270
271
272         strncpy(msg_sms_tmp->body, logs, 128);
273         msg_sms_tmp->body[127] = '\0';
274         
275         /* Assigning msg_sms */
276         *msg_sms = msg_sms_tmp;
277     }
278     
279     
280     
281     /* Clearing the memory */
282     FreeAlertData(al_data);
283
284     
285     return(mail);
286
287 }
288 /* EOF */