suricata
util-conf.c
Go to the documentation of this file.
1 /* Copyright (C) 2013 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Eric Leblond <eric@regit.org>
22  *
23  */
24 
25 #include "suricata-common.h"
26 #include "config.h"
27 #include "conf.h"
28 #include "runmodes.h"
29 #include "util-conf.h"
30 
32 {
33  return ConfSetFinal("default-log-dir", name) ? TM_ECODE_OK : TM_ECODE_FAILED;
34 }
35 
36 const char *ConfigGetLogDirectory()
37 {
38  const char *log_dir = NULL;
39 
40  if (ConfGet("default-log-dir", &log_dir) != 1) {
41 #ifdef OS_WIN32
42  log_dir = _getcwd(NULL, 0);
43  if (log_dir == NULL) {
44  log_dir = DEFAULT_LOG_DIR;
45  }
46 #else
47  log_dir = DEFAULT_LOG_DIR;
48 #endif /* OS_WIN32 */
49  }
50 
51  return log_dir;
52 }
53 
55 {
56  SCEnter();
57 #ifdef OS_WIN32
58  struct _stat buf;
59  if (_stat(log_dir, &buf) != 0) {
60 #else
61  struct stat buf;
62  if (stat(log_dir, &buf) != 0) {
63 #endif /* OS_WIN32 */
65  }
67 }
68 
70 {
71  if (strlen(name) == 0)
72  return TM_ECODE_OK;
73 
74  size_t size = strlen(name) + 1;
75  char tmp[size];
76  strlcpy(tmp, name, size);
77  if (tmp[size - 2] == '/')
78  tmp[size - 2] = '\0';
79 
80  return ConfSetFinal("default-data-dir", tmp) ? TM_ECODE_OK : TM_ECODE_FAILED;
81 }
82 
84 {
85  const char *data_dir = NULL;
86 
87  if (ConfGet("default-data-dir", &data_dir) != 1) {
88 #ifdef OS_WIN32
89  data_dir = _getcwd(NULL, 0);
90  if (data_dir == NULL) {
91  data_dir = DEFAULT_DATA_DIR;
92  }
93 #else
94  data_dir = DEFAULT_DATA_DIR;
95 #endif /* OS_WIN32 */
96  }
97 
98  SCLogNotice("returning '%s'", data_dir);
99  return data_dir;
100 }
101 
102 TmEcode ConfigCheckDataDirectory(const char *data_dir)
103 {
104  SCEnter();
105 #ifdef OS_WIN32
106  struct _stat buf;
107  if (_stat(data_dir, &buf) != 0) {
108 #else
109  struct stat buf;
110  if (stat(data_dir, &buf) != 0) {
111 #endif /* OS_WIN32 */
113  }
115 }
116 
117 /**
118  * \brief Find the configuration node for a specific device.
119 
120  * Basically hunts through the list of maps for the first one with a
121  * key of "interface", and a value of the provided interface.
122  *
123  * \param node The node to start looking for the device
124  * configuration. Typically this would be something like the af-packet
125  * or pf-ring node.
126  *
127  * \param iface The name of the interface to find the config for.
128  */
129 ConfNode *ConfFindDeviceConfig(ConfNode *node, const char *iface)
130 {
131  ConfNode *if_node, *item;
132  TAILQ_FOREACH(if_node, &node->head, next) {
133  TAILQ_FOREACH(item, &if_node->head, next) {
134  if (strcmp(item->name, "interface") == 0 &&
135  strcmp(item->val, iface) == 0) {
136  return if_node;
137  }
138  }
139  }
140 
141  return NULL;
142 }
143 
145 {
146  const char *value;
147 
148  if (ConfGet("unix-command.enabled", &value) != 1) {
149  return 0;
150  }
151 
152  if (value == NULL) {
153  SCLogError(SC_ERR_INVALID_YAML_CONF_ENTRY, "malformed value for unix-command.enabled: NULL");
154  return 0;
155  }
156 
157  if (!strcmp(value, "auto")) {
158 #ifdef OS_WIN32
159  return 0;
160 #else
162  SCLogInfo("Running in live mode, activating unix socket");
163  return 1;
164  } else {
165  return 0;
166  }
167 #endif
168  }
169 
170  return ConfValIsTrue(value);
171 }
TmEcode ConfigSetLogDirectory(char *name)
Definition: util-conf.c:31
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:350
struct HtpBodyChunk_ * next
TmEcode ConfigCheckDataDirectory(const char *data_dir)
Definition: util-conf.c:102
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
#define DEFAULT_LOG_DIR
Definition: conf.h:54
TmEcode ConfigCheckLogDirectoryExists(const char *log_dir)
Definition: util-conf.c:54
bool IsRunModeOffline(enum RunModes run_mode_to_check)
Definition: runmodes.c:504
int ConfUnixSocketIsEnable(void)
Definition: util-conf.c:144
char * val
Definition: conf.h:34
int ConfGet(const char *name, const char **vptr)
Retrieve the value of a configuration node.
Definition: conf.c:331
ConfNode * ConfFindDeviceConfig(ConfNode *node, const char *iface)
Find the configuration node for a specific device.
Definition: util-conf.c:129
#define DEFAULT_DATA_DIR
Definition: conf.h:55
const char * ConfigGetDataDirectory()
Definition: util-conf.c:83
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
#define SCEnter(...)
Definition: util-debug.h:337
int ConfSetFinal(const char *name, const char *val)
Set a final configuration value.
Definition: conf.c:299
#define SCReturnInt(x)
Definition: util-debug.h:341
int ConfValIsTrue(const char *val)
Check if a value is true.
Definition: conf.c:566
Definition: conf.h:32
TmEcode ConfigSetDataDirectory(char *name)
Definition: util-conf.c:69
const char * ConfigGetLogDirectory()
Definition: util-conf.c:36
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:254
#define SCLogNotice(...)
Macro used to log NOTICE messages.
Definition: util-debug.h:269
char * name
Definition: conf.h:33
int RunmodeGetCurrent(void)
Definition: suricata.c:275