/src/config/csyslogd-config.c

https://gitlab.com/Blueprint-Marketing/ossec-hids · C · 208 lines · 156 code · 29 blank · 23 comment · 71 complexity · 3f31b868f2a65ccd61d9cc21159424a2 MD5 · raw file

  1. /* Copyright (C) 2009 Trend Micro Inc.
  2. * All right reserved.
  3. *
  4. * This program is a free software; you can redistribute it
  5. * and/or modify it under the terms of the GNU General Public
  6. * License (version 2) as published by the FSF - Free Software
  7. * Foundation
  8. */
  9. #include "csyslogd-config.h"
  10. #include "config.h"
  11. int Read_CSyslog(XML_NODE node, void *config, __attribute__((unused)) void *config2)
  12. {
  13. unsigned int i = 0, s = 0;
  14. /* XML definitions */
  15. const char *xml_syslog_server = "server";
  16. const char *xml_syslog_port = "port";
  17. const char *xml_syslog_format = "format";
  18. const char *xml_syslog_level = "level";
  19. const char *xml_syslog_id = "rule_id";
  20. const char *xml_syslog_group = "group";
  21. const char *xml_syslog_location = "location";
  22. const char *xml_syslog_use_fqdn = "use_fqdn";
  23. struct SyslogConfig_holder *config_holder = (struct SyslogConfig_holder *)config;
  24. SyslogConfig **syslog_config = config_holder->data;
  25. if (syslog_config) {
  26. while (syslog_config[s]) {
  27. s++;
  28. }
  29. }
  30. /* Allocate the memory for the config */
  31. os_realloc(syslog_config, (s + 2) * sizeof(SyslogConfig *), syslog_config);
  32. os_calloc(1, sizeof(SyslogConfig), syslog_config[s]);
  33. syslog_config[s + 1] = NULL;
  34. /* Zero the elements */
  35. syslog_config[s]->server = NULL;
  36. syslog_config[s]->rule_id = NULL;
  37. syslog_config[s]->group = NULL;
  38. syslog_config[s]->location = NULL;
  39. syslog_config[s]->level = 0;
  40. syslog_config[s]->port = 514;
  41. syslog_config[s]->format = DEFAULT_CSYSLOG;
  42. syslog_config[s]->use_fqdn = 0;
  43. /* local 0 facility (16) + severity 4 - warning. --default */
  44. syslog_config[s]->priority = (16 * 8) + 4;
  45. while (node[i]) {
  46. if (!node[i]->element) {
  47. merror(XML_ELEMNULL, __local_name);
  48. goto fail;
  49. } else if (!node[i]->content) {
  50. merror(XML_VALUENULL, __local_name, node[i]->element);
  51. goto fail;
  52. } else if (strcmp(node[i]->element, xml_syslog_level) == 0) {
  53. if (!OS_StrIsNum(node[i]->content)) {
  54. merror(XML_VALUEERR, __local_name, node[i]->element, node[i]->content);
  55. goto fail;
  56. }
  57. syslog_config[s]->level = (unsigned int) atoi(node[i]->content);
  58. } else if (strcmp(node[i]->element, xml_syslog_port) == 0) {
  59. if (!OS_StrIsNum(node[i]->content)) {
  60. merror(XML_VALUEERR, __local_name, node[i]->element, node[i]->content);
  61. goto fail;
  62. }
  63. syslog_config[s]->port = (unsigned int) atoi(node[i]->content);
  64. } else if (strcmp(node[i]->element, xml_syslog_server) == 0) {
  65. os_strdup(node[i]->content, syslog_config[s]->server);
  66. } else if (strcmp(node[i]->element, xml_syslog_id) == 0) {
  67. unsigned int r_id = 0;
  68. char *str_pt = node[i]->content;
  69. while (*str_pt != '\0') {
  70. /* We allow spaces in between */
  71. if (*str_pt == ' ') {
  72. str_pt++;
  73. continue;
  74. }
  75. /* If is digit, we get the value
  76. * and search for the next digit
  77. * available
  78. */
  79. else if (isdigit((int)*str_pt)) {
  80. unsigned int id_i = 0;
  81. r_id = (unsigned int) atoi(str_pt);
  82. debug1("%s: DEBUG: Adding '%d' to syslog alerting",
  83. __local_name, r_id);
  84. if (syslog_config[s]->rule_id) {
  85. while (syslog_config[s]->rule_id[id_i]) {
  86. id_i++;
  87. }
  88. }
  89. os_realloc(syslog_config[s]->rule_id,
  90. (id_i + 2) * sizeof(unsigned int),
  91. syslog_config[s]->rule_id);
  92. syslog_config[s]->rule_id[id_i + 1] = 0;
  93. syslog_config[s]->rule_id[id_i] = r_id;
  94. str_pt = strchr(str_pt, ',');
  95. if (str_pt) {
  96. str_pt++;
  97. } else {
  98. break;
  99. }
  100. }
  101. /* Check for duplicate commas */
  102. else if (*str_pt == ',') {
  103. str_pt++;
  104. continue;
  105. }
  106. else {
  107. break;
  108. }
  109. }
  110. } else if (strcmp(node[i]->element, xml_syslog_format) == 0) {
  111. if (strcmp(node[i]->content, "default") == 0) {
  112. /* Default is full format */
  113. } else if (strcmp(node[i]->content, "cef") == 0) {
  114. /* Enable the CEF format */
  115. syslog_config[s]->format = CEF_CSYSLOG;
  116. } else if (strcmp(node[i]->content, "json") == 0) {
  117. /* Enable the JSON format */
  118. syslog_config[s]->format = JSON_CSYSLOG;
  119. } else if (strcmp(node[i]->content, "splunk") == 0) {
  120. /* Enable the Splunk Key/Value format */
  121. syslog_config[s]->format = SPLUNK_CSYSLOG;
  122. } else {
  123. merror(XML_VALUEERR, __local_name, node[i]->element, node[i]->content);
  124. goto fail;
  125. }
  126. } else if (strcmp(node[i]->element, xml_syslog_location) == 0) {
  127. os_calloc(1, sizeof(OSMatch), syslog_config[s]->location);
  128. if (!OSMatch_Compile(node[i]->content,
  129. syslog_config[s]->location, 0)) {
  130. merror(REGEX_COMPILE, __local_name, node[i]->content,
  131. syslog_config[s]->location->error);
  132. goto fail;
  133. }
  134. } else if (strcmp(node[i]->element, xml_syslog_use_fqdn) == 0) {
  135. if (strcmp(node[i]->content, "yes") == 0) {
  136. syslog_config[s]->use_fqdn = 1;
  137. } else if (strcmp(node[i]->content, "no") == 0) {
  138. syslog_config[s]->use_fqdn = 0;
  139. } else {
  140. merror(XML_VALUEERR, __local_name, node[i]->element, node[i]->content);
  141. goto fail;
  142. }
  143. } else if (strcmp(node[i]->element, xml_syslog_group) == 0) {
  144. os_calloc(1, sizeof(OSMatch), syslog_config[s]->group);
  145. if (!OSMatch_Compile(node[i]->content,
  146. syslog_config[s]->group, 0)) {
  147. merror(REGEX_COMPILE, __local_name, node[i]->content,
  148. syslog_config[s]->group->error);
  149. goto fail;
  150. }
  151. } else {
  152. merror(XML_INVELEM, __local_name, node[i]->element);
  153. goto fail;
  154. }
  155. i++;
  156. }
  157. /* We must have at least one entry set */
  158. if (!syslog_config[s]->server) {
  159. merror(XML_INV_CSYSLOG, __local_name);
  160. goto fail;
  161. }
  162. config_holder->data = syslog_config;
  163. return (0);
  164. fail:
  165. i = 0;
  166. while (syslog_config[i]) {
  167. free(syslog_config[i]->server);
  168. if (syslog_config[i]->group) {
  169. OSMatch_FreePattern(syslog_config[i]->group);
  170. }
  171. if (syslog_config[i]->location) {
  172. OSMatch_FreePattern(syslog_config[i]->location);
  173. }
  174. free(syslog_config[i]->rule_id);
  175. ++i;
  176. }
  177. free(syslog_config);
  178. return (OS_INVALID);
  179. }