-
Notifications
You must be signed in to change notification settings - Fork 634
/
main.cpp
464 lines (385 loc) · 13.2 KB
/
main.cpp
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
*
* (C) 2013-22 - ntop.org
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include "ntop_includes.h"
extern "C" {
extern char* rrd_strversion(void);
};
AfterShutdownAction afterShutdownAction = after_shutdown_nop;
/* ******************************** */
void sighup(int sig) {
;
}
/* ******************************** */
#ifdef WIN32
BOOL WINAPI sigproc(DWORD sig)
#else
void sigproc(int sig)
#endif
{
static int called = 0;
if(called) {
ntop->getTrace()->traceEvent(TRACE_NORMAL, "Ok I am leaving now");
_exit(0);
} else {
ntop->getTrace()->traceEvent(TRACE_NORMAL, "Shutting down...");
called = 1;
ntop->getGlobals()->requestShutdown();
}
}
/* ******************************************* */
#ifdef WIN32
void initWinsock32() {
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 0);
err = WSAStartup( wVersionRequested, &wsaData );
if( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
printf("FATAL ERROR: unable to initialize Winsock 2.x.\n");
exit(-1);
}
}
/* ******************************** */
extern "C" {
int ntop_main(int argc, char *argv[])
#else
int main(int argc, char *argv[])
#endif
{
Prefs *prefs = NULL;
char *ifName;
int rc;
char *affinity;
int indexAffinity = 0;
char *core_id_s = NULL;
int core_id;
char path[2*MAX_PATH];
bool has_view_all = false;
FILE *fd;
ThreadedActivity *boot_activity;
#ifdef WIN32
initWinsock32();
#else
sqlite3_shutdown();
/* Multi-thread.
In this mode, SQLite can be safely used by multiple threads
provided that no single database connection is used simultaneously
in two or more threads.
*/
if(sqlite3_config(SQLITE_CONFIG_MULTITHREAD) != SQLITE_OK) {
ntop->getTrace()->traceEvent(TRACE_WARNING, "Unable to set SQLITE_CONFIG_MULTITHREAD for sqlite, exiting.");
exit(1);
}
sqlite3_initialize();
#endif
if((ntop = new(std::nothrow) Ntop(argv[0])) == NULL) _exit(0);
if((prefs = new(std::nothrow) Prefs(ntop)) == NULL) _exit(0);
#ifndef WIN32
if((argc >= 2) && (argv[1][0] != '-')) {
rc = prefs->loadFromFile(argv[1]);
if(argc > 2)
rc = prefs->loadFromCLI(argc, argv);
} else
#endif
rc = prefs->loadFromCLI(argc, argv);
if(rc < 0) return(-1);
/* Create the working dir before dropping the provileges, otherwise the
* user might not be able to create it. Utils::dropPrivileges will move
* the directory ownership to the user. */
Utils::mkdir_tree(ntop->get_working_dir());
#ifndef WIN32
ntop->lockNtopInstance();
#endif
ntop->registerPrefs(prefs, false);
prefs->reloadPrefsFromRedis();
if((boot_activity = new (std::nothrow) ThreadedActivity(BOOT_SCRIPT_PATH))) {
/* Don't call run() as by the time the script will be run the delete below will free the memory */
/* NOTE: preferences restore from file is handled here */
boot_activity->runSystemScript(time(NULL));
delete boot_activity;
}
prefs->registerNetworkInterfaces();
if(prefs->get_num_user_specified_interfaces() == 0) {
/* We add all interfaces available on this host */
prefs->add_default_interfaces();
}
prefs->validate();
#ifndef HAVE_NEDGE
if(ntop->getPrefs()->is_appliance()) {
/* Disabling user change for the time being as system configuration
* scripts currently require permissions. */
ntop->getTrace()->traceEvent(TRACE_WARNING, "Disabling user change in appliance mode");
ntop->getPrefs()->dont_change_user();
}
#endif
#ifndef WIN32
ntop->initPing();
#endif
if(prefs->daemonize_ntopng())
ntop->daemonize();
#ifndef HAVE_NEDGE
/* Force ZMQ interface creation */
ntop->broadcastIPSMessage(NULL);
#endif
#ifdef __linux__
/* Store number of CPUs before dropping privileges */
ntop->setNumCPUs(sysconf(_SC_NPROCESSORS_ONLN));
ntop->getTrace()->traceEvent(TRACE_INFO, "System has %d CPU cores", ntop->getNumCPUs());
#endif
affinity = prefs->get_cpu_affinity();
for(int i = 0; i < MAX_NUM_INTERFACE_IDS; i++) {
NetworkInterface *iface = NULL;
if((ifName = ntop->get_if_name(i)) == NULL
|| !strncmp(ifName, "view:", 5) /* Defer view interfaces init */)
continue;
try {
/* [ zmq-collector.lua@tcp://127.0.0.1:5556 ] */
#ifndef HAVE_NEDGE
if(!strcmp(ifName, "dummy")) {
iface = new (std::nothrow) DummyInterface();
} else if((strstr(ifName, "tcp://") || strstr(ifName, "ipc://"))) {
char *at = strchr(ifName, '@');
char *endpoint;
if(at != NULL)
endpoint = &at[1];
else
endpoint = ifName;
iface = new (std::nothrow) ZMQCollectorInterface(endpoint);
} else if(strstr(ifName, "syslog://")) {
iface = new (std::nothrow) SyslogCollectorInterface(ifName);
#if defined(HAVE_PF_RING) && (!defined(NTOPNG_EMBEDDED_EDITION)) && (!defined(__i686__)) && (!defined(__ARM_ARCH))
} else if(strstr(ifName, "zcflow:")) {
iface = new (std::nothrow) ZCCollectorInterface(ifName);
#endif
} else
#endif
{
iface = NULL;
#if defined(NTOPNG_PRO) && !defined(WIN32)
if(strncmp(ifName, "bridge:", 7) == 0) {
ntop->getTrace()->traceEvent(TRACE_WARNING, "\n");
ntop->getTrace()->traceEvent(TRACE_WARNING, "Inline/bridge capabilities have now been moved in ntopng Edge (nEdge)");
ntop->getTrace()->traceEvent(TRACE_WARNING, "For more information and free migration see:");
ntop->getTrace()->traceEvent(TRACE_WARNING, "https://www.ntop.org/support/faq/migration-of-ntopng-inline-pro-enterprises-licenses-to-ntopng-edge-nedge/");
ntop->getTrace()->traceEvent(TRACE_WARNING, "\n");
}
#endif
#if defined(HAVE_NEDGE)
if(iface == NULL && strncmp(ifName, "nf:", 3) == 0)
iface = new (std::nothrow) NetfilterInterface(ifName);
#endif
#ifdef HAVE_PF_RING
if((iface == NULL) && (!strstr(ifName, ".pcap"))) {
errno = 0;
iface = new (std::nothrow) PF_RINGInterface(ifName);
}
#endif
}
} catch(int err) {
ntop->getTrace()->traceEvent(TRACE_NORMAL, "Unable to open interface %s [%d]: %s. Falling back to pcap.",
ifName, err, err == EAFNOSUPPORT ? "PF_RING not loaded" : strerror(err));
if(iface) delete iface;
iface = NULL;
} catch(...) {
ntop->getTrace()->traceEvent(TRACE_NORMAL, "Unable to open interface %s. Falling back to pcap.", ifName);
if(iface) delete iface;
iface = NULL;
}
#ifndef HAVE_NEDGE
if(iface == NULL) {
try {
errno = 0;
iface = new PcapInterface(ifName, i);
} catch(int err) {
ntop->getTrace()->traceEvent(TRACE_ERROR, "Unable to open interface %s with pcap [%d]: %s",
ifName, err, strerror(err));
if(iface) delete iface;
iface = NULL;
}
}
#endif
if(iface) {
if(affinity != NULL) {
char *tmp;
if(indexAffinity == 0)
core_id_s = strtok_r(affinity, ",", &tmp);
else
core_id_s = strtok_r(NULL, ",", &tmp);
if(core_id_s != NULL)
core_id = atoi(core_id_s);
else
core_id = indexAffinity;
indexAffinity++;
iface->setCPUAffinity(core_id);
}
if(prefs->get_packet_filter())
iface->set_packet_filter(prefs->get_packet_filter());
ntop->registerInterface(iface);
}
} /* for */
/* Instantiated deferred view interfaces */
for(int i = 0; i < MAX_NUM_INTERFACE_IDS; i++) {
NetworkInterface *iface = NULL;
if((ifName = ntop->get_if_name(i)) == NULL || strncmp(ifName, "view:", 5))
continue;
if(!strcmp(ifName, "view:all")) {
/* Defer view:all interface after the other view interfaces */
has_view_all = true;
continue;
}
if((iface = new (std::nothrow) ViewInterface(ifName)))
ntop->registerInterface(iface);
}
if(has_view_all) {
NetworkInterface *iface = NULL;
if((iface = new (std::nothrow) ViewInterface("view:all")))
ntop->registerInterface(iface);
}
if(ntop->getFirstInterface() == NULL) {
#ifdef WIN32
ntop->getTrace()->traceEvent(TRACE_ERROR, "Please make sure you have installed the https://nmap.org/npcap/ driver");
#else
ntop->getTrace()->traceEvent(TRACE_ERROR, "Startup error: missing super-user privileges ?");
#endif
exit(0);
}
#ifndef WIN32
if(prefs->get_pid_path() != NULL) {
FILE *fd;
fd = fopen(prefs->get_pid_path(), "w");
if(fd != NULL) {
int n;
chmod(prefs->get_pid_path(), CONST_DEFAULT_FILE_MODE);
n = fprintf(fd, "%u\n", getpid());
fclose(fd);
if(n > 0) {
chmod(prefs->get_pid_path(), 0644);
ntop->getTrace()->traceEvent(TRACE_NORMAL, "PID stored in file %s",
prefs->get_pid_path());
} else
ntop->getTrace()->traceEvent(TRACE_ERROR, "The PID file %s is empty: is your disk full perhaps ?",
prefs->get_pid_path());
} else
ntop->getTrace()->traceEvent(TRACE_ERROR, "Unable to store PID in file %s: %s",
prefs->get_pid_path(), strerror(errno));
}
#endif
#ifndef HAVE_NEDGE
ntop->createExportInterface();
#endif
ntop->loadGeolocation();
ntop->loadMacManufacturers(prefs->get_docs_dir());
ntop->loadTrackers();
/* Register the HTTP server before dropping the privileges. This is required
* in order to possibly bind the HTTP server to privileged ports (< 1024) */
ntop->registerHTTPserver(new (std::nothrow) HTTPserver(prefs->get_docs_dir(), prefs->get_scripts_dir()));
/* Drop the privileges before initializing the network interfaces. This
* is necessary as the initialization may create files/directories, which
* should not be created as root. */
if(ntop->getPrefs()->do_change_user())
Utils::dropPrivileges();
/* initInterface writes DB data on disk, keep it after changing user */
for(int i = 0; i < MAX_NUM_INTERFACE_IDS; i++) {
NetworkInterface *iface;
if((iface = ntop->getInterface(i)) != NULL)
ntop->initInterface(iface);
}
/*
We have created the network interface and thus changed user. Let's now check
if we can write on the data directory
*/
snprintf(path, sizeof(path), "%s/.test", ntop->get_working_dir());
ntop->fixPath(path);
if((fd = fopen(path, "w")) == NULL) {
ntop->getTrace()->traceEvent(TRACE_ERROR,
"Unable to write on %s as '%s' [%s]: %s. Please specify a different directory (-d)",
ntop->get_working_dir(), prefs->get_user(), path, strerror(errno));
exit(EXIT_FAILURE);
} else {
chmod(path, CONST_DEFAULT_FILE_MODE);
fclose(fd); /* All right */
unlink(path);
}
if(prefs->is_log_to_file_enabled()
#ifndef WIN32
|| prefs->daemonize_ntopng()
#endif
) {
char path[2*MAX_PATH];
Utils::mkdir_tree(ntop->get_data_dir());
Utils::mkdir_tree(ntop->get_working_dir());
snprintf(path, sizeof(path), "%s/ntopng.log", ntop->get_working_dir() /* "C:\\Windows\\Temp" */);
ntop->fixPath(path);
ntop->registerLogFile(path);
ntop->getTrace()->traceEvent(TRACE_NORMAL, "Logging onto %s", path);
}
ntop->getTrace()->traceEvent(TRACE_NORMAL, "Working directory: %s",
ntop->get_working_dir());
ntop->getTrace()->traceEvent(TRACE_NORMAL, "Scripts/HTML pages directory: %s",
ntop->get_install_dir());
#ifdef WIN32
SetConsoleCtrlHandler(sigproc, TRUE);
#else
signal(SIGHUP, sighup);
signal(SIGINT, sigproc);
signal(SIGTERM, sigproc);
#endif
#if defined(WIN32) && defined(DEMO_WIN32)
ntop->getTrace()->traceEvent(TRACE_NORMAL, "-----------------------------------------------------------");
ntop->getTrace()->traceEvent(TRACE_WARNING, "This is a demo version of ntopng limited to %d packets", MAX_NUM_PACKETS);
ntop->getTrace()->traceEvent(TRACE_WARNING, "Please go to http://shop.ntop.org for getting the full version");
ntop->getTrace()->traceEvent(TRACE_NORMAL, "-----------------------------------------------------------");
#endif
/* This method returns after a shutdown has been requested.
runHousekeepingTasks() is performed within this method untile it has returned. */
ntop->start();
ntop->getTrace()->traceEvent(TRACE_NORMAL, "Terminating...");
/* Perform all the necessary cleanup and wait for other threads termination */
ntop->shutdownAll();
ntop->runShutdownTasks();
delete ntop;
ntop = NULL;
#ifdef __linux__
switch(afterShutdownAction) {
case after_shutdown_reboot:
Utils::deferredExec("systemctl start systemd-reboot");
break;
case after_shutdown_poweroff:
Utils::deferredExec("systemctl start systemd-poweroff");
break;
case after_shutdown_restart_self:
/* Returning 1 to force systemd to restart the service due
* to 'Unclean exit code'. A systemctl restart does not work here.
* Note: this requires 'Restart=on-failure' to work */
return(1);
break;
case after_shutdown_nop:
default:
break;
}
#endif
return(0);
}
#ifdef WIN32
}
#endif