1 /* @(#) $Id: ./src/os_csyslogd/csyslogd.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
21 #include "os_net/os_net.h"
24 /* OS_SyslogD: Monitor the alerts and sends them via syslog.
25 * Only return in case of error.
27 void OS_CSyslogD(SyslogConfig **syslog_config)
38 /* Getting currently time before starting */
43 /* Initating file queue - to read the alerts */
44 os_calloc(1, sizeof(file_queue), fileq);
45 while( (Init_FileQueue(fileq, p, 0) ) < 0 ) {
47 if( tries > OS_CSYSLOGD_MAX_TRIES ) {
48 merror("%s: ERROR: Could not open queue after %d tries, exiting!",
55 debug1("%s: INFO: File queue connected.", ARGV0 );
58 /* Connecting to syslog. */
60 while(syslog_config[s])
62 syslog_config[s]->socket = OS_ConnectUDP(syslog_config[s]->port,
63 syslog_config[s]->server, 0);
64 if(syslog_config[s]->socket < 0)
66 merror(CONNS_ERROR, ARGV0, syslog_config[s]->server);
70 merror("%s: INFO: Forwarding alerts via syslog to: '%s:%d'.",
71 ARGV0, syslog_config[s]->server, syslog_config[s]->port);
79 /* Infinite loop reading the alerts and inserting them. */
86 /* Get message if available (timeout of 5 seconds) */
87 al_data = Read_FileMon(fileq, p, 5);
95 /* Sending via syslog */
97 while(syslog_config[s])
99 OS_Alert_SendSyslog(al_data, syslog_config[s]);
104 /* Clearing the memory */
105 FreeAlertData(al_data);
109 /* Format Field for output */
110 int field_add_string(char *dest, int size, const char *format, const char *value ) {
111 char buffer[OS_SIZE_2048];
113 int dest_sz = size - strlen(dest);
116 // Not enough room in the buffer
122 ((value[0] != '(') && (value[1] != 'n') && (value[2] != 'o')) ||
123 ((value[0] != '(') && (value[1] != 'u') && (value[2] != 'n')) ||
124 ((value[0] != 'u') && (value[1] != 'n') && (value[4] != 'k'))
127 len = snprintf(buffer, sizeof(buffer) - dest_sz - 1, format, value);
128 strncat(dest, buffer, dest_sz);
134 /* Add a field, but truncate if too long */
135 int field_add_truncated(char *dest, int size, const char *format, const char *value, int fmt_size ) {
136 char buffer[OS_SIZE_2048];
138 int available_sz = size - strlen(dest);
139 int total_sz = strlen(value) + strlen(format) - fmt_size;
140 int field_sz = available_sz - strlen(format) + fmt_size;
143 char trailer[] = "...";
144 char *truncated = NULL;
146 if(available_sz <= 0 ) {
147 // Not enough room in the buffer
153 ((value[0] != '(') && (value[1] != 'n') && (value[2] != 'o')) ||
154 ((value[0] != '(') && (value[1] != 'u') && (value[2] != 'n')) ||
155 ((value[0] != 'u') && (value[1] != 'n') && (value[4] != 'k'))
159 if( (truncated=malloc(field_sz + 1)) != NULL ) {
160 if( total_sz > available_sz ) {
161 // Truncate and add a trailer
162 os_substr(truncated, value, 0, field_sz - strlen(trailer));
163 strcat(truncated, trailer);
166 strncpy(truncated,value,field_sz);
169 len = snprintf(buffer, available_sz, format, truncated);
170 strncat(dest, buffer, available_sz);
177 // Free the temporary pointer
183 /* Handle integers in the second position */
184 int field_add_int(char *dest, int size, const char *format, const int value ) {
187 int dest_sz = size - strlen(dest);
190 // Not enough room in the buffer
195 len = snprintf(buffer, sizeof(buffer), format, value);
196 strncat(dest, buffer, dest_sz);