-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigValidation.cpp
More file actions
280 lines (261 loc) · 8.89 KB
/
Copy pathConfigValidation.cpp
File metadata and controls
280 lines (261 loc) · 8.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* ConfigValidation.cpp
*
* Configuration validation implementation for PoE-Honeypot
*
* GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
* https://github.com/Xorlent/PoE-Honeypot
*/
#include <Arduino.h>
#include "Config.h"
#include "ConfigValidation.h"
// Validate configuration at startup
// Returns false if critical errors found
bool validateConfiguration() {
bool hasErrors = false;
bool hasWarnings = false;
Serial.println("=== Configuration Validation ===");
// Validate hostname
size_t hostnameLen = strlen((const char*)hostName);
if (hostnameLen == 0) {
Serial.println("ERROR: hostName is empty");
hasErrors = true;
} else if (hostnameLen > 255) {
Serial.println("ERROR: hostName exceeds 255 characters");
hasErrors = true;
} else {
// Check for invalid characters (spaces, CRLF, control chars)
for (size_t i = 0; i < hostnameLen; i++) {
if (hostName[i] == ' ') {
Serial.println("ERROR: hostName contains spaces (not allowed per RFC 3164)");
hasErrors = true;
break;
} else if (hostName[i] == '\r' || hostName[i] == '\n') {
Serial.println("ERROR: hostName contains CR/LF");
hasErrors = true;
break;
} else if (hostName[i] < 32 || hostName[i] > 126) {
Serial.println("ERROR: hostName contains non-printable characters");
hasErrors = true;
break;
}
}
}
// Validate SMTP if enabled
if (USE_SMTP) {
Serial.println("SMTP mode enabled, validating email configuration...");
// Validate SMTP server IP
if (smtpServer[0] == 0 && smtpServer[1] == 0 &&
smtpServer[2] == 0 && smtpServer[3] == 0) {
Serial.println("ERROR: smtpServer IP is 0.0.0.0 (invalid)");
hasErrors = true;
}
// Validate SMTP port
if (smtpPort == 0) {
Serial.println("ERROR: smtpPort is 0 (invalid)");
hasErrors = true;
}
// Validate email addresses
if (smtpFromAddr != NULL) {
size_t fromLen = strlen(smtpFromAddr);
if (fromLen == 0) {
Serial.println("ERROR: smtpFromAddr is empty");
hasErrors = true;
} else {
for (size_t i = 0; i < fromLen; i++) {
if (smtpFromAddr[i] == '\r' || smtpFromAddr[i] == '\n') {
Serial.println("ERROR: smtpFromAddr contains CR/LF");
hasErrors = true;
break;
}
}
// Basic email format check
if (strchr(smtpFromAddr, '@') == NULL) {
Serial.println("WARNING: smtpFromAddr missing '@' symbol");
hasWarnings = true;
}
if (strchr(smtpFromAddr, '.') == NULL) {
Serial.println("WARNING: smtpFromAddr missing '.' character");
hasWarnings = true;
}
}
} else {
Serial.println("ERROR: smtpFromAddr is NULL");
hasErrors = true;
}
if (smtpToAddr != NULL) {
size_t toLen = strlen(smtpToAddr);
if (toLen == 0) {
Serial.println("ERROR: smtpToAddr is empty");
hasErrors = true;
} else {
for (size_t i = 0; i < toLen; i++) {
if (smtpToAddr[i] == '\r' || smtpToAddr[i] == '\n') {
Serial.println("ERROR: smtpToAddr contains CR/LF");
hasErrors = true;
break;
}
}
// Basic email format check
if (strchr(smtpToAddr, '@') == NULL) {
Serial.println("WARNING: smtpToAddr missing '@' symbol");
hasWarnings = true;
}
if (strchr(smtpToAddr, '.') == NULL) {
Serial.println("WARNING: smtpToAddr missing '.' character");
hasWarnings = true;
}
}
} else {
Serial.println("ERROR: smtpToAddr is NULL");
hasErrors = true;
}
} else {
// Validate syslog configuration
if (syslogSvr[0] == 0 && syslogSvr[1] == 0 &&
syslogSvr[2] == 0 && syslogSvr[3] == 0) {
Serial.println("ERROR: syslogSvr IP is 0.0.0.0 (invalid)");
hasErrors = true;
}
if (syslogPort == 0) {
Serial.println("ERROR: syslogPort is 0 (invalid)");
hasErrors = true;
}
}
// Validate network config
if (ip[0] == 0 && ip[1] == 0 && ip[2] == 0 && ip[3] == 0) {
Serial.println("ERROR: Local IP is 0.0.0.0 (invalid)");
hasErrors = true;
}
if (gateway[0] == 0 && gateway[1] == 0 && gateway[2] == 0 && gateway[3] == 0) {
Serial.println("WARNING: Gateway IP is 0.0.0.0");
hasWarnings = true;
}
// Validate holdoff values
if (TCP_HOLDOFF_SECONDS > 600) {
Serial.println("WARNING: TCP_HOLDOFF_SECONDS excessively large (may miss events)");
hasWarnings = true;
}
if (UDP_HOLDOFF_SECONDS > 600) {
Serial.println("WARNING: UDP_HOLDOFF_SECONDS excessively large (may miss events)");
hasWarnings = true;
}
if (ICMP_HOLDOFF_SECONDS > 600) {
Serial.println("WARNING: ICMP_HOLDOFF_SECONDS excessively large (may miss events)");
hasWarnings = true;
}
// Validate MAX_TRACKED_IPS
if (MAX_TRACKED_IPS == 0) {
Serial.println("ERROR: MAX_TRACKED_IPS is 0 (holdoff will not work)");
hasErrors = true;
} else if (MAX_TRACKED_IPS > 255) {
Serial.println("WARNING: MAX_TRACKED_IPS is very large (may consume excessive memory)");
hasWarnings = true;
}
// Validate port counts
if (honeypotNumPorts == 0) {
Serial.println("WARNING: No TCP ports configured for monitoring");
hasWarnings = true;
}
if (MONITOR_UDP && honeypotNumUDPPorts == 0) {
Serial.println("WARNING: UDP monitoring enabled but no ports configured");
hasWarnings = true;
}
if (MONITOR_ICMP && honeypotNumICMPTypes == 0) {
Serial.println("WARNING: ICMP monitoring enabled but no types configured");
hasWarnings = true;
}
// Validate TCP port service names
for (int i = 0; i < honeypotNumPorts; i++) {
if (honeypotTCPPorts[i].service == NULL) {
Serial.print("WARNING: TCP port ");
Serial.print(honeypotTCPPorts[i].port);
Serial.println(" has NULL service name (will log as 'unknown')");
hasWarnings = true;
continue;
}
size_t svcLen = strlen(honeypotTCPPorts[i].service);
if (svcLen > 64) {
Serial.print("ERROR: TCP port ");
Serial.print(honeypotTCPPorts[i].port);
Serial.println(" service name exceeds 64 characters");
hasErrors = true;
}
for (size_t j = 0; j < svcLen; j++) {
if (honeypotTCPPorts[i].service[j] == '\r' || honeypotTCPPorts[i].service[j] == '\n') {
Serial.print("ERROR: TCP port ");
Serial.print(honeypotTCPPorts[i].port);
Serial.println(" service name contains CR/LF");
hasErrors = true;
break;
}
}
}
// Validate UDP port service names
if (MONITOR_UDP) {
for (int i = 0; i < honeypotNumUDPPorts; i++) {
if (honeypotUDPPorts[i].service == NULL) {
Serial.print("WARNING: UDP port ");
Serial.print(honeypotUDPPorts[i].port);
Serial.println(" has NULL service name (will log as 'unknown')");
hasWarnings = true;
continue;
}
size_t svcLen = strlen(honeypotUDPPorts[i].service);
if (svcLen > 64) {
Serial.print("ERROR: UDP port ");
Serial.print(honeypotUDPPorts[i].port);
Serial.println(" service name exceeds 64 characters");
hasErrors = true;
}
for (size_t j = 0; j < svcLen; j++) {
if (honeypotUDPPorts[i].service[j] == '\r' || honeypotUDPPorts[i].service[j] == '\n') {
Serial.print("ERROR: UDP port ");
Serial.print(honeypotUDPPorts[i].port);
Serial.println(" service name contains CR/LF");
hasErrors = true;
break;
}
}
}
}
// Validate ICMP type names
if (MONITOR_ICMP) {
for (int i = 0; i < honeypotNumICMPTypes; i++) {
if (honeypotICMPTypes[i].name == NULL) {
Serial.print("WARNING: ICMP type ");
Serial.print(honeypotICMPTypes[i].type);
Serial.println(" has NULL name (will log as 'unknown')");
hasWarnings = true;
continue;
}
size_t nameLen = strlen(honeypotICMPTypes[i].name);
if (nameLen > 64) {
Serial.print("ERROR: ICMP type ");
Serial.print(honeypotICMPTypes[i].type);
Serial.println(" name exceeds 64 characters");
hasErrors = true;
}
for (size_t j = 0; j < nameLen; j++) {
if (honeypotICMPTypes[i].name[j] == '\r' || honeypotICMPTypes[i].name[j] == '\n') {
Serial.print("ERROR: ICMP type ");
Serial.print(honeypotICMPTypes[i].type);
Serial.println(" name contains CR/LF");
hasErrors = true;
break;
}
}
}
}
// Print summary
Serial.println("=== Validation Complete ===");
if (hasErrors) {
Serial.println("CRITICAL ERRORS FOUND - Please fix Config.h");
Serial.println("Device will not start until errors are resolved.");
return false;
} else if (hasWarnings) {
Serial.println("Warnings found - review Config.h (continuing anyway)");
}
Serial.println();
return true;
}