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 "conf.h"
27 #include "runmodes.h"
28 #include "util-conf.h"
29 
30 TmEcode ConfigSetLogDirectory(const char *name)
31 {
32  return ConfSetFinal("default-log-dir", name) ? TM_ECODE_OK : TM_ECODE_FAILED;
33 }
34 
35 const char *ConfigGetLogDirectory()
36 {
37  const char *log_dir = NULL;
38 
39  if (ConfGet("default-log-dir", &log_dir) != 1) {
40 #ifdef OS_WIN32
41  log_dir = _getcwd(NULL, 0);
42  if (log_dir == NULL) {
43  log_dir = DEFAULT_LOG_DIR;
44  }
45 #else
46  log_dir = DEFAULT_LOG_DIR;
47 #endif /* OS_WIN32 */
48  }
49 
50  return log_dir;
51 }
52 
54 {
55  SCEnter();
56 #ifdef OS_WIN32
57  struct _stat buf;
58  if (_stat(log_dir, &buf) != 0) {
59 #else
60  struct stat buf;
61  if (stat(log_dir, &buf) != 0) {
62 #endif /* OS_WIN32 */
64  }
66 }
67 
69 {
70  if (strlen(name) == 0)
71  return TM_ECODE_OK;
72 
73  size_t size = strlen(name) + 1;
74  char tmp[size];
75  strlcpy(tmp, name, size);
76  if (size > 2 && tmp[size - 2] == '/') // > 2 to allow just /
77  tmp[size - 2] = '\0';
78 
79  return ConfSetFinal("default-data-dir", tmp) ? TM_ECODE_OK : TM_ECODE_FAILED;
80 }
81 
83 {
84  const char *data_dir = NULL;
85 
86  if (ConfGet("default-data-dir", &data_dir) != 1) {
87 #ifdef OS_WIN32
88  data_dir = _getcwd(NULL, 0);
89  if (data_dir == NULL) {
90  data_dir = DEFAULT_DATA_DIR;
91  }
92 #else
93  data_dir = DEFAULT_DATA_DIR;
94 #endif /* OS_WIN32 */
95  }
96 
97  SCLogDebug("returning '%s'", data_dir);
98  return data_dir;
99 }
100 
101 TmEcode ConfigCheckDataDirectory(const char *data_dir)
102 {
103  SCEnter();
104 #ifdef OS_WIN32
105  struct _stat buf;
106  if (_stat(data_dir, &buf) != 0) {
107 #else
108  struct stat buf;
109  if (stat(data_dir, &buf) != 0) {
110 #endif /* OS_WIN32 */
112  }
114 }
115 
116 /**
117  * \brief Find the configuration node for a specific device.
118 
119  * Basically hunts through the list of maps for the first one with a
120  * key of "interface", and a value of the provided interface.
121  *
122  * \param node The node to start looking for the device
123  * configuration. Typically this would be something like the af-packet
124  * or pf-ring node.
125  *
126  * \param iface The name of the interface to find the config for.
127  */
128 ConfNode *ConfFindDeviceConfig(ConfNode *node, const char *iface)
129 {
130  ConfNode *if_node, *item;
131  TAILQ_FOREACH(if_node, &node->head, next) {
132  TAILQ_FOREACH(item, &if_node->head, next) {
133  if (strcmp(item->name, "interface") == 0 &&
134  strcmp(item->val, iface) == 0) {
135  return if_node;
136  }
137  }
138  }
139 
140  return NULL;
141 }
142 
144 {
145  const char *value;
146 
147  if (ConfGet("unix-command.enabled", &value) != 1) {
148  return 0;
149  }
150 
151  if (value == NULL) {
152  SCLogError(SC_ERR_INVALID_YAML_CONF_ENTRY, "malformed value for unix-command.enabled: NULL");
153  return 0;
154  }
155 
156  if (!strcmp(value, "auto")) {
157 #ifdef OS_WIN32
158  return 0;
159 #else
161  SCLogInfo("Running in live mode, activating unix socket");
162  return 1;
163  } else {
164  return 0;
165  }
166 #endif
167  }
168 
169  return ConfValIsTrue(value);
170 }
ConfNode_::val
char * val
Definition: conf.h:34
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:350
ConfSetFinal
int ConfSetFinal(const char *name, const char *val)
Set a final configuration value.
Definition: conf.c:298
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:81
IsRunModeOffline
bool IsRunModeOffline(enum RunModes run_mode_to_check)
Definition: runmodes.c:518
ConfValIsTrue
int ConfValIsTrue(const char *val)
Check if a value is true.
Definition: conf.c:565
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:80
DEFAULT_LOG_DIR
#define DEFAULT_LOG_DIR
Definition: conf.h:54
strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
ConfGet
int ConfGet(const char *name, const char **vptr)
Retrieve the value of a configuration node.
Definition: conf.c:330
ConfFindDeviceConfig
ConfNode * ConfFindDeviceConfig(ConfNode *node, const char *iface)
Find the configuration node for a specific device.
Definition: util-conf.c:128
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:79
ConfigSetDataDirectory
TmEcode ConfigSetDataDirectory(char *name)
Definition: util-conf.c:68
runmodes.h
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:217
SC_ERR_INVALID_YAML_CONF_ENTRY
@ SC_ERR_INVALID_YAML_CONF_ENTRY
Definition: util-error.h:169
util-conf.h
suricata-common.h
ConfNode_::name
char * name
Definition: conf.h:33
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
DEFAULT_DATA_DIR
#define DEFAULT_DATA_DIR
Definition: conf.h:55
RunmodeGetCurrent
int RunmodeGetCurrent(void)
Definition: suricata.c:272
ConfigSetLogDirectory
TmEcode ConfigSetLogDirectory(const char *name)
Definition: util-conf.c:30
ConfUnixSocketIsEnable
int ConfUnixSocketIsEnable(void)
Definition: util-conf.c:143
ConfigCheckDataDirectory
TmEcode ConfigCheckDataDirectory(const char *data_dir)
Definition: util-conf.c:101
ConfigGetLogDirectory
const char * ConfigGetLogDirectory()
Definition: util-conf.c:35
ConfNode_
Definition: conf.h:32
ConfigCheckLogDirectoryExists
TmEcode ConfigCheckLogDirectoryExists(const char *log_dir)
Definition: util-conf.c:53
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
ConfigGetDataDirectory
const char * ConfigGetDataDirectory()
Definition: util-conf.c:82