97d7ff9b719933c77739732a70547dd39872a247
[ossec-hids.git] / src / os_csyslogd / csyslogd.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  * License details at the LICENSE file included with OSSEC or 
12  * online at: http://www.ossec.net/en/licensing.html
13  */
14
15
16
17 #include "csyslogd.h"
18 #include "os_net/os_net.h"
19
20
21
22 /* OS_SyslogD: Monitor the alerts and sends them via syslog.
23  * Only return in case of error.
24  */
25 void OS_CSyslogD(SyslogConfig **syslog_config)
26 {
27     int s = 0;
28     time_t tm;     
29     struct tm *p;       
30
31     file_queue *fileq;
32     alert_data *al_data;
33
34
35     /* Getting currently time before starting */
36     tm = time(NULL);
37     p = localtime(&tm); 
38
39
40     /* Initating file queue - to read the alerts */
41     os_calloc(1, sizeof(file_queue), fileq);
42     Init_FileQueue(fileq, p, 0);
43
44
45     /* Connecting to syslog. */
46     s = 0;
47     while(syslog_config[s])
48     {
49         syslog_config[s]->socket = OS_ConnectUDP(syslog_config[s]->port,
50                                                  syslog_config[s]->server);
51         if(syslog_config[s]->socket < 0)
52         {
53             merror(CONNS_ERROR, ARGV0, syslog_config[s]->server);
54         }
55         else
56         {
57             merror("%s: INFO: Forwarding alerts via syslog to: '%s:%d'.", 
58                    ARGV0, syslog_config[s]->server, syslog_config[s]->port); 
59         }
60
61         s++;
62     }
63
64
65     
66     /* Infinite loop reading the alerts and inserting them. */
67     while(1)
68     {
69         tm = time(NULL);
70         p = localtime(&tm);
71
72
73         /* Get message if available (timeout of 5 seconds) */
74         al_data = Read_FileMon(fileq, p, 5);
75         if(!al_data)
76         {
77             continue;
78         }
79
80
81
82         /* Sending via syslog */
83         s = 0;
84         while(syslog_config[s])
85         {
86             OS_Alert_SendSyslog(al_data, syslog_config[s]);
87             s++;
88         }
89
90
91         /* Clearing the memory */
92         FreeAlertData(al_data);
93     }
94 }
95
96 /* EOF */