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
11 * License details at the LICENSE file included with OSSEC or
12 * online at: http://www.ossec.net/en/licensing.html
21 #define ARGV0 "ossec-maild"
26 #include "mail_list.h"
29 void OS_Run(MailConfig *mail);
31 int main(int argc, char **argv)
33 int c, test_config = 0,run_foreground = 0;
35 char *dir = DEFAULTDIR;
36 char *user = MAILUSER;
37 char *group = GROUPGLOBAL;
38 char *cfg = DEFAULTCPATH;
44 /* Setting the name */
48 while((c = getopt(argc, argv, "Vdhtfu:g:D:c:")) != -1){
64 ErrorExit("%s: -u needs an argument",ARGV0);
69 ErrorExit("%s: -g needs an argument",ARGV0);
74 ErrorExit("%s: -D needs an argument",ARGV0);
78 ErrorExit("%s: -c needs an argument",ARGV0);
92 debug1(STARTED_MSG,ARGV0);
94 /*Check if the user/group given are valid */
95 uid = Privsep_GetUser(user);
96 gid = Privsep_GetGroup(group);
97 if((uid < 0)||(gid < 0))
98 ErrorExit(USER_ERROR,ARGV0,user,group);
101 /* Reading configuration */
102 if(MailConf(test_config, cfg, &mail) < 0)
103 ErrorExit(CONFIG_ERROR, ARGV0, cfg);
106 /* Reading internal options */
107 mail.strict_checking = getDefine_Int("maild",
112 mail.groupping = getDefine_Int("maild",
116 /* Getting subject type */
117 mail.subject_full = getDefine_Int("maild",
122 /* Exit here if test config is set */
134 /* Privilege separation */
135 if(Privsep_SetGroup(gid) < 0)
136 ErrorExit(SETGID_ERROR,ARGV0,group);
140 if(Privsep_Chroot(dir) < 0)
141 ErrorExit(CHROOT_ERROR,ARGV0,dir);
148 if(Privsep_SetUser(uid) < 0)
149 ErrorExit(SETUID_ERROR,ARGV0,user);
152 debug1(PRIVSEP_MSG,ARGV0,dir,user);
156 /* Signal manipulation */
161 /* Creating PID files */
162 if(CreatePID(ARGV0, getpid()) < 0)
163 ErrorExit(PID_ERROR, ARGV0);
166 /* Start up message */
167 verbose(STARTUP_MSG, ARGV0, (int)getpid());
170 /* the real daemon now */
176 /* OS_Run: Read the queue and send the appropriate alerts.
177 * not supposed to return..
179 void OS_Run(MailConfig *mail)
182 MailMsg *s_msg = NULL;
183 MailMsg *msg_sms = NULL;
199 /* Getting currently time before starting */
203 thishour = p->tm_hour;
206 /* Init file queue */
208 i |= CRALERT_MAIL_SET;
209 os_calloc(1, sizeof(file_queue), fileq);
210 Init_FileQueue(fileq, p, i);
213 /* Creating the list */
214 OS_CreateMailList(MAIL_LIST_SIZE);
217 /* Setting default timeout */
218 mail_timeout = DEFAULT_TIMEOUT;
221 /* Clearing global vars */
222 _g_subject_level = 0;
223 memset(_g_subject, '\0', SUBJECT_SIZE +2);
232 /* SMS messages are sent without delay */
241 merror("%s: Fork failed. cause: %d - %s", ARGV0, errno, strerror(errno));
242 merror(FORK_ERROR, ARGV0);
248 if(OS_Sendsms(mail, p, msg_sms) < 0)
249 merror(SNDMAIL_ERROR, ARGV0, mail->smtpserver);
255 /* Freeing sms structure */
256 FreeMailMsg(msg_sms);
260 /* Increasing child count */
265 /* If mail_timeout == NEXTMAIL_TIMEOUT, we will try to get
266 * more messages, before sending anything
268 if((mail_timeout == NEXTMAIL_TIMEOUT) && (p->tm_hour == thishour))
270 /* getting more messages */
274 /* Hour changed. Send all supressed mails */
275 else if(((mailtosend < mail->maxperhour) && (mailtosend != 0))||
276 ((p->tm_hour != thishour) && (childcount < MAXCHILDPROCESS)))
281 /* Checking if we have anything to sent */
282 mailmsg = OS_CheckLastMail();
285 /* dont fork in here */
292 merror("%s: Fork failed. cause: %d - %s", ARGV0, errno, strerror(errno));
293 merror(FORK_ERROR, ARGV0);
299 if(OS_Sendmail(mail, p) < 0)
300 merror(SNDMAIL_ERROR,ARGV0,mail->smtpserver);
305 /* Cleaning the memory */
306 mailmsg = OS_PopLastMail();
310 mailmsg = OS_PopLastMail();
314 /* Increasing child count */
318 /* Clearing global vars */
319 _g_subject[0] = '\0';
320 _g_subject[SUBJECT_SIZE -1] = '\0';
321 _g_subject_level = 0;
324 /* Cleaning up set values */
328 while(mail->gran_to[i] != NULL)
330 if(s_msg && mail->gran_set[i] == DONOTGROUP)
332 mail->gran_set[i] = FULL_FORMAT;
336 mail->gran_set[i] = 0;
343 /* If we sent everything */
344 if(p->tm_hour != thishour)
346 thishour = p->tm_hour;
352 /* Saved message for the do_not_group option.
356 /* We need to set the remaining do no group to
362 while(mail->gran_to[i] != NULL)
364 if(mail->gran_set[i] == DONOTGROUP)
366 mail->gran_set[i] = FULL_FORMAT;
372 OS_AddMailtoList(s_msg);
380 /* Receive message from queue */
381 if((msg = OS_RecvMailQ(fileq, p, mail, &msg_sms)) != NULL)
383 /* If the e-mail priority is do_not_group, we first will
384 * flush all previous entries and then send it.
385 * We use s_msg to hold the pointer to the message
388 if(mail->priority == DONOTGROUP)
394 OS_AddMailtoList(msg);
398 /* Change timeout to see if any new message is coming shortly */
401 /* If priority is set, send email now */
404 mail_timeout = DEFAULT_TIMEOUT;
406 /* If do_not_group is set, we do not increase the
407 * list count in here.
409 if(mail->priority != DONOTGROUP)
417 mail_timeout = NEXTMAIL_TIMEOUT;
422 /* Send message by itself */
428 if(mail_timeout == NEXTMAIL_TIMEOUT)
432 /* Default timeout */
433 mail_timeout = DEFAULT_TIMEOUT;
438 /* Waiting for the childs .. */
443 wp = waitpid((pid_t) -1, &p_status, WNOHANG);
446 merror(WAITPID_ERROR, ARGV0);
450 /* if = 0, we still need to wait for the child process */
457 merror(SNDMAIL_ERROR,ARGV0,mail->smtpserver);
463 /* Too many errors */
466 merror(SNDMAIL_ERROR,ARGV0,mail->smtpserver);