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