/src/suricata7/src/util-conf.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
33 | | TmEcode ConfigSetLogDirectory(const char *name) |
34 | 71 | { |
35 | 71 | return ConfSetFinal("default-log-dir", name) ? TM_ECODE_OK : TM_ECODE_FAILED; |
36 | 71 | } |
37 | | |
38 | | const char *ConfigGetLogDirectory(void) |
39 | 45 | { |
40 | 45 | const char *log_dir = NULL; |
41 | | |
42 | 45 | if (ConfGet("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 | 0 | log_dir = DEFAULT_LOG_DIR; |
50 | 0 | #endif /* OS_WIN32 */ |
51 | 0 | } |
52 | | |
53 | 45 | return log_dir; |
54 | 45 | } |
55 | | |
56 | | TmEcode ConfigCheckLogDirectoryExists(const char *log_dir) |
57 | 71 | { |
58 | 71 | SCEnter(); |
59 | 71 | SCStat buf; |
60 | 71 | if (SCStatFn(log_dir, &buf) != 0) { |
61 | 0 | SCReturnInt(TM_ECODE_FAILED); |
62 | 0 | } |
63 | 71 | SCReturnInt(TM_ECODE_OK); |
64 | 71 | } |
65 | | |
66 | | TmEcode ConfigSetDataDirectory(char *name) |
67 | 0 | { |
68 | 0 | if (strlen(name) == 0) |
69 | 0 | return TM_ECODE_OK; |
70 | | |
71 | 0 | size_t size = strlen(name) + 1; |
72 | 0 | char tmp[size]; |
73 | 0 | strlcpy(tmp, name, size); |
74 | 0 | if (size > 2 && tmp[size - 2] == '/') // > 2 to allow just / |
75 | 0 | tmp[size - 2] = '\0'; |
76 | |
|
77 | 0 | return ConfSetFinal("default-data-dir", tmp) ? TM_ECODE_OK : TM_ECODE_FAILED; |
78 | 0 | } |
79 | | |
80 | | const char *ConfigGetDataDirectory(void) |
81 | 2.65k | { |
82 | 2.65k | const char *data_dir = NULL; |
83 | | |
84 | 2.65k | if (ConfGet("default-data-dir", &data_dir) != 1) { |
85 | | #ifdef OS_WIN32 |
86 | | data_dir = _getcwd(NULL, 0); |
87 | | if (data_dir == NULL) { |
88 | | data_dir = DEFAULT_DATA_DIR; |
89 | | } |
90 | | #else |
91 | 2.65k | data_dir = DEFAULT_DATA_DIR; |
92 | 2.65k | #endif /* OS_WIN32 */ |
93 | 2.65k | } |
94 | | |
95 | 2.65k | SCLogDebug("returning '%s'", data_dir); |
96 | 2.65k | return data_dir; |
97 | 2.65k | } |
98 | | |
99 | | TmEcode ConfigCheckDataDirectory(const char *data_dir) |
100 | 0 | { |
101 | 0 | SCEnter(); |
102 | 0 | SCStat buf; |
103 | 0 | if (SCStatFn(data_dir, &buf) != 0) { |
104 | 0 | SCReturnInt(TM_ECODE_FAILED); |
105 | 0 | } |
106 | 0 | SCReturnInt(TM_ECODE_OK); |
107 | 0 | } |
108 | | |
109 | | /** |
110 | | * \brief Find the configuration node for a specific device. |
111 | | |
112 | | * Basically hunts through the list of maps for the first one with a |
113 | | * key of "interface", and a value of the provided interface. |
114 | | * |
115 | | * \param node The node to start looking for the device |
116 | | * configuration. Typically this would be something like the af-packet |
117 | | * or pf-ring node. |
118 | | * |
119 | | * \param iface The name of the interface to find the config for. |
120 | | */ |
121 | | ConfNode *ConfFindDeviceConfig(ConfNode *node, const char *iface) |
122 | 0 | { |
123 | 0 | ConfNode *if_node, *item; |
124 | 0 | TAILQ_FOREACH(if_node, &node->head, next) { |
125 | 0 | TAILQ_FOREACH(item, &if_node->head, next) { |
126 | 0 | if (strcmp(item->name, "interface") == 0 && |
127 | 0 | strcmp(item->val, iface) == 0) { |
128 | 0 | return if_node; |
129 | 0 | } |
130 | 0 | } |
131 | 0 | } |
132 | | |
133 | 0 | return NULL; |
134 | 0 | } |
135 | | |
136 | | int ConfUnixSocketIsEnable(void) |
137 | 4 | { |
138 | 4 | const char *value; |
139 | | |
140 | 4 | if (ConfGet("unix-command.enabled", &value) != 1) { |
141 | 4 | return 0; |
142 | 4 | } |
143 | | |
144 | 0 | if (value == NULL) { |
145 | 0 | SCLogError("malformed value for unix-command.enabled: NULL"); |
146 | 0 | return 0; |
147 | 0 | } |
148 | | |
149 | 0 | if (!strcmp(value, "auto")) { |
150 | | #ifdef OS_WIN32 |
151 | | return 0; |
152 | | #else |
153 | 0 | if (!IsRunModeOffline(RunmodeGetCurrent())) { |
154 | 0 | SCLogInfo("Running in live mode, activating unix socket"); |
155 | 0 | return 1; |
156 | 0 | } else { |
157 | 0 | return 0; |
158 | 0 | } |
159 | 0 | #endif |
160 | 0 | } |
161 | | |
162 | 0 | return ConfValIsTrue(value); |
163 | 0 | } |