1 /* @(#) $Id: ./src/os_maild/maild.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
12 * License details at the LICENSE file included with OSSEC or
13 * online at: http://www.ossec.net/en/licensing.html
22 #define ARGV0 "ossec-maild"
27 #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);
79 ErrorExit("%s: -c needs an argument",ARGV0);
93 debug1(STARTED_MSG,ARGV0);
95 /*Check if the user/group given are valid */
96 uid = Privsep_GetUser(user);
97 gid = Privsep_GetGroup(group);
98 if((uid < 0)||(gid < 0))
99 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",
123 mail.geoip = getDefine_Int("maild",
129 /* Exit here if test config is set */
141 /* Privilege separation */
142 if(Privsep_SetGroup(gid) < 0)
143 ErrorExit(SETGID_ERROR,ARGV0,group);
147 if(Privsep_Chroot(dir) < 0)
148 ErrorExit(CHROOT_ERROR,ARGV0,dir);
155 if(Privsep_SetUser(uid) < 0)
156 ErrorExit(SETUID_ERROR,ARGV0,user);
159 debug1(PRIVSEP_MSG,ARGV0,dir,user);
163 /* Signal manipulation */
168 /* Creating PID files */
169 if(CreatePID(ARGV0, getpid()) < 0)
170 ErrorExit(PID_ERROR, ARGV0);
173 /* Start up message */
174 verbose(STARTUP_MSG, ARGV0, (int)getpid());
177 /* the real daemon now */
183 /* OS_Run: Read the queue and send the appropriate alerts.
184 * not supposed to return..
186 void OS_Run(MailConfig *mail)
189 MailMsg *s_msg = NULL;
190 MailMsg *msg_sms = NULL;
206 /* Getting currently time before starting */
210 thishour = p->tm_hour;
213 /* Init file queue */
215 i |= CRALERT_MAIL_SET;
216 os_calloc(1, sizeof(file_queue), fileq);
217 Init_FileQueue(fileq, p, i);
220 /* Creating the list */
221 OS_CreateMailList(MAIL_LIST_SIZE);
224 /* Setting default timeout */
225 mail_timeout = DEFAULT_TIMEOUT;
228 /* Clearing global vars */
229 _g_subject_level = 0;
230 memset(_g_subject, '\0', SUBJECT_SIZE +2);
239 /* SMS messages are sent without delay */
248 merror("%s: Fork failed. cause: %d - %s", ARGV0, errno, strerror(errno));
249 merror(FORK_ERROR, ARGV0);
255 if(OS_Sendsms(mail, p, msg_sms) < 0)
256 merror(SNDMAIL_ERROR, ARGV0, mail->smtpserver);
262 /* Freeing sms structure */
263 FreeMailMsg(msg_sms);
267 /* Increasing child count */
272 /* If mail_timeout == NEXTMAIL_TIMEOUT, we will try to get
273 * more messages, before sending anything
275 if((mail_timeout == NEXTMAIL_TIMEOUT) && (p->tm_hour == thishour))
277 /* getting more messages */
281 /* Hour changed. Send all supressed mails */
282 else if(((mailtosend < mail->maxperhour) && (mailtosend != 0))||
283 ((p->tm_hour != thishour) && (childcount < MAXCHILDPROCESS)))
288 /* Checking if we have anything to sent */
289 mailmsg = OS_CheckLastMail();
292 /* dont fork in here */
299 merror("%s: Fork failed. cause: %d - %s", ARGV0, errno, strerror(errno));
300 merror(FORK_ERROR, ARGV0);
306 if(OS_Sendmail(mail, p) < 0)
307 merror(SNDMAIL_ERROR,ARGV0,mail->smtpserver);
312 /* Cleaning the memory */
313 mailmsg = OS_PopLastMail();
317 mailmsg = OS_PopLastMail();
321 /* Increasing child count */
325 /* Clearing global vars */
326 _g_subject[0] = '\0';
327 _g_subject[SUBJECT_SIZE -1] = '\0';
328 _g_subject_level = 0;
331 /* Cleaning up set values */
335 while(mail->gran_to[i] != NULL)
337 if(s_msg && mail->gran_set[i] == DONOTGROUP)
339 mail->gran_set[i] = FULL_FORMAT;
343 mail->gran_set[i] = 0;
350 /* If we sent everything */
351 if(p->tm_hour != thishour)
353 thishour = p->tm_hour;
359 /* Saved message for the do_not_group option.
363 /* We need to set the remaining do no group to
369 while(mail->gran_to[i] != NULL)
371 if(mail->gran_set[i] == DONOTGROUP)
373 mail->gran_set[i] = FULL_FORMAT;
379 OS_AddMailtoList(s_msg);
387 /* Receive message from queue */
388 if((msg = OS_RecvMailQ(fileq, p, mail, &msg_sms)) != NULL)
390 /* If the e-mail priority is do_not_group, we first will
391 * flush all previous entries and then send it.
392 * We use s_msg to hold the pointer to the message
395 if(mail->priority == DONOTGROUP)
401 OS_AddMailtoList(msg);
405 /* Change timeout to see if any new message is coming shortly */
408 /* If priority is set, send email now */
411 mail_timeout = DEFAULT_TIMEOUT;
413 /* If do_not_group is set, we do not increase the
414 * list count in here.
416 if(mail->priority != DONOTGROUP)
424 mail_timeout = NEXTMAIL_TIMEOUT;
429 /* Send message by itself */
435 if(mail_timeout == NEXTMAIL_TIMEOUT)
439 /* Default timeout */
440 mail_timeout = DEFAULT_TIMEOUT;
445 /* Waiting for the childs .. */
450 wp = waitpid((pid_t) -1, &p_status, WNOHANG);
453 merror(WAITPID_ERROR, ARGV0);
457 /* if = 0, we still need to wait for the child process */
464 merror(CHLDWAIT_ERROR,ARGV0,p_status);
465 merror(SNDMAIL_ERROR,ARGV0,mail->smtpserver);
471 /* Too many errors */
474 merror(TOOMANY_WAIT_ERROR,ARGV0);
475 merror(SNDMAIL_ERROR,ARGV0,mail->smtpserver);