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