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 #include "util-atomic.h"
33 
34 #ifdef OS_WIN32
35 /* _getcwd(NULL, 0) mallocs per call and the callers have no ownership
36  * contract; the cwd does not change for the process lifetime. The cached
37  * value is the fast path; a first-call race is settled atomically and the
38  * loser frees its copy */
39 static SC_ATOMIC_DECLARE(char *, cached_cwd);
40 
41 static const char *ConfigGetCwdFallback(void)
42 {
43  const char *c = SC_ATOMIC_GET(cached_cwd);
44  if (c != NULL)
45  return c;
46  char *p = _getcwd(NULL, 0);
47  char *prev = NULL;
48  if (!SC_ATOMIC_CAS(&cached_cwd, prev, p)) {
49  free(p);
50  return SC_ATOMIC_GET(cached_cwd);
51  }
52  return p;
53 }
54 #endif /* OS_WIN32 */
55 
57 {
58  return SCConfSetFinal("default-log-dir", name) ? TM_ECODE_OK : TM_ECODE_FAILED;
59 }
60 
61 const char *SCConfigGetLogDirectory(void)
62 {
63  const char *log_dir = NULL;
64 
65  if (SCConfGetNonNull("default-log-dir", &log_dir) != 1) {
66 #ifdef OS_WIN32
67  log_dir = ConfigGetCwdFallback();
68  if (log_dir == NULL) {
69  log_dir = DEFAULT_LOG_DIR;
70  }
71 #else
72  log_dir = DEFAULT_LOG_DIR;
73 #endif /* OS_WIN32 */
74  }
75 
76  return log_dir;
77 }
78 
80 {
81  SCEnter();
82  SCStat buf;
83  if (SCStatFn(log_dir, &buf) != 0) {
85  }
87 }
88 
90 {
91  size_t name_len = strlen(name);
92  if (name_len == 0)
93  return TM_ECODE_OK;
94 
95  if (name_len > PATH_MAX) {
96  SCLogError("Too long name for data directory");
97  return TM_ECODE_FAILED;
98  }
99  size_t size = strlen(name) + 1;
100  char tmp[size];
101  strlcpy(tmp, name, size);
102  if (size > 2 && tmp[size - 2] == '/') // > 2 to allow just /
103  tmp[size - 2] = '\0';
104 
105  return SCConfSetFinal("default-data-dir", tmp) ? TM_ECODE_OK : TM_ECODE_FAILED;
106 }
107 
108 const char *ConfigGetDataDirectory(void)
109 {
110  const char *data_dir = NULL;
111 
112  if (SCConfGetNonNull("default-data-dir", &data_dir) != 1) {
113 #ifdef OS_WIN32
114  data_dir = ConfigGetCwdFallback();
115  if (data_dir == NULL) {
116  data_dir = DEFAULT_DATA_DIR;
117  }
118 #else
119  data_dir = DEFAULT_DATA_DIR;
120 #endif /* OS_WIN32 */
121  }
122 
123  SCLogDebug("returning '%s'", data_dir);
124  return data_dir;
125 }
126 
127 TmEcode ConfigCheckDataDirectory(const char *data_dir)
128 {
129  SCEnter();
130  SCStat buf;
131  if (SCStatFn(data_dir, &buf) != 0) {
133  }
135 }
136 
137 /**
138  * \brief Find the configuration node for a specific device.
139 
140  * Basically hunts through the list of maps for the first one with a
141  * key of "interface", and a value of the provided interface.
142  *
143  * \param node The node to start looking for the device
144  * configuration. Typically this would be something like the af-packet
145  * or pf-ring node.
146  *
147  * \param iface The name of the interface to find the config for.
148  */
149 SCConfNode *ConfFindDeviceConfig(SCConfNode *node, const char *iface)
150 {
151  SCConfNode *if_node, *item;
152  TAILQ_FOREACH(if_node, &node->head, next) {
153  TAILQ_FOREACH(item, &if_node->head, next) {
154  if (strcmp(item->name, "interface") == 0 &&
155  strcmp(item->val, iface) == 0) {
156  return if_node;
157  }
158  }
159  }
160 
161  return NULL;
162 }
163 
165 {
166  const char *value;
167 
168  if (SCConfGet("unix-command.enabled", &value) != 1) {
169  return 0;
170  }
171 
172  if (value == NULL) {
173  SCLogError("malformed value for unix-command.enabled: NULL");
174  return 0;
175  }
176 
177  if (!strcmp(value, "auto")) {
178 #ifdef OS_WIN32
179  return 0;
180 #else
181  if (!IsRunModeOffline(SCRunmodeGet())) {
182  SCLogInfo("Running in live mode, activating unix socket");
183  return 1;
184  } else {
185  return 0;
186  }
187 #endif
188  }
189 
190  return SCConfValIsTrue(value);
191 }
SCConfValIsTrue
int SCConfValIsTrue(const char *val)
Check if a value is true.
Definition: conf.c:577
IsRunModeOffline
bool IsRunModeOffline(enum SCRunModes run_mode_to_check)
Definition: runmodes.c:556
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:47
SCConfGet
int SCConfGet(const char *name, const char **vptr)
Retrieve the value of a configuration node.
Definition: conf.c:352
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:252
SC_ATOMIC_CAS
#define SC_ATOMIC_CAS(name, cmpval, newval)
atomic Compare and Switch
Definition: util-atomic.h:367
p
Packet * p
Definition: fuzz_dataset.c:30
ConfigGetDataDirectory
const char * ConfigGetDataDirectory(void)
Definition: util-conf.c:108
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:302
util-debug.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:284
SCConfGetNonNull
int SCConfGetNonNull(const char *name, const char **vptr)
Retrieve the non-null value of a configuration node.
Definition: conf.c:380
util-atomic.h
SC_ATOMIC_DECLARE
#define SC_ATOMIC_DECLARE(type, name)
wrapper for declaring atomic variables.
Definition: util-atomic.h:280
SCConfigGetLogDirectory
const char * SCConfigGetLogDirectory(void)
Definition: util-conf.c:61
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:80
ConfigSetDataDirectory
TmEcode ConfigSetDataDirectory(char *name)
Definition: util-conf.c:89
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:320
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:56
ConfUnixSocketIsEnable
int ConfUnixSocketIsEnable(void)
Definition: util-conf.c:164
ConfigCheckDataDirectory
TmEcode ConfigCheckDataDirectory(const char *data_dir)
Definition: util-conf.c:127
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:79
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:149
SCStatFn
#define SCStatFn(pathname, statbuf)
Definition: util-path.h:35
SC_ATOMIC_GET
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:375
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