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;
205 /* Getting currently time before starting */
208 thishour = p->tm_hour;
211 /* Init file queue */
213 i |= CRALERT_MAIL_SET;
214 os_calloc(1, sizeof(file_queue), fileq);
215 Init_FileQueue(fileq, p, i);
218 /* Creating the list */
219 OS_CreateMailList(MAIL_LIST_SIZE);
222 /* Setting default timeout */
223 mail_timeout = DEFAULT_TIMEOUT;
226 /* Clearing global vars */
227 _g_subject_level = 0;
228 memset(_g_subject, '\0', SUBJECT_SIZE +2);
237 /* SMS messages are sent without delay */
246 merror("%s: Fork failed. cause: %d - %s", ARGV0, errno, strerror(errno));
247 merror(FORK_ERROR, ARGV0);
253 if(OS_Sendsms(mail, p, msg_sms) < 0)
254 merror(SNDMAIL_ERROR, ARGV0, mail->smtpserver);
260 /* Freeing sms structure */
261 FreeMailMsg(msg_sms);
265 /* Increasing child count */
270 /* If mail_timeout == NEXTMAIL_TIMEOUT, we will try to get
271 * more messages, before sending anything
273 if((mail_timeout == NEXTMAIL_TIMEOUT) && (p->tm_hour == thishour))
275 /* getting more messages */
279 /* Hour changed. Send all supressed mails */
280 else if(((mailtosend < mail->maxperhour) && (mailtosend != 0))||
281 ((p->tm_hour != thishour) && (childcount < MAXCHILDPROCESS)))
286 /* Checking if we have anything to sent */
287 mailmsg = OS_CheckLastMail();
290 /* dont fork in here */
297 merror("%s: Fork failed. cause: %d - %s", ARGV0, errno, strerror(errno));
298 merror(FORK_ERROR, ARGV0);
304 if(OS_Sendmail(mail, p) < 0)
305 merror(SNDMAIL_ERROR,ARGV0,mail->smtpserver);
310 /* Cleaning the memory */
311 mailmsg = OS_PopLastMail();
315 mailmsg = OS_PopLastMail();
319 /* Increasing child count */
323 /* Clearing global vars */
324 _g_subject[0] = '\0';
325 _g_subject[SUBJECT_SIZE -1] = '\0';
326 _g_subject_level = 0;
329 /* Cleaning up set values */
333 while(mail->gran_to[i] != NULL)
335 if(s_msg && mail->gran_set[i] == DONOTGROUP)
337 mail->gran_set[i] = FULL_FORMAT;
341 mail->gran_set[i] = 0;
348 /* If we sent everything */
349 if(p->tm_hour != thishour)
351 thishour = p->tm_hour;
357 /* Saved message for the do_not_group option.
361 /* We need to set the remaining do no group to
367 while(mail->gran_to[i] != NULL)
369 if(mail->gran_set[i] == DONOTGROUP)
371 mail->gran_set[i] = FULL_FORMAT;
377 OS_AddMailtoList(s_msg);
385 /* Receive message from queue */
386 if((msg = OS_RecvMailQ(fileq, p, mail, &msg_sms)) != NULL)
388 /* If the e-mail priority is do_not_group, we first will
389 * flush all previous entries and then send it.
390 * We use s_msg to hold the pointer to the message
393 if(mail->priority == DONOTGROUP)
399 OS_AddMailtoList(msg);
403 /* Change timeout to see if any new message is coming shortly */
406 /* If priority is set, send email now */
409 mail_timeout = DEFAULT_TIMEOUT;
411 /* If do_not_group is set, we do not increase the
412 * list count in here.
414 if(mail->priority != DONOTGROUP)
422 mail_timeout = NEXTMAIL_TIMEOUT;
427 /* Send message by itself */
433 if(mail_timeout == NEXTMAIL_TIMEOUT)
437 /* Default timeout */
438 mail_timeout = DEFAULT_TIMEOUT;
443 /* Waiting for the childs .. */
448 wp = waitpid((pid_t) -1, &p_status, WNOHANG);
451 merror(WAITPID_ERROR, ARGV0);
455 /* if = 0, we still need to wait for the child process */
462 merror(CHLDWAIT_ERROR,ARGV0,p_status);
463 merror(SNDMAIL_ERROR,ARGV0,mail->smtpserver);
469 /* Too many errors */
472 merror(TOOMANY_WAIT_ERROR,ARGV0);
473 merror(SNDMAIL_ERROR,ARGV0,mail->smtpserver);