| 1 |
// http://e2guardian.org/ |
| 2 |
// Released under the GPL v2, with the OpenSSL exception described in the README file. |
| 3 |
|
| 4 |
// INCLUDES |
| 5 |
|
| 6 |
#ifdef HAVE_CONFIG_H |
| 7 |
#include "e2config.h" |
| 8 |
#endif |
| 9 |
|
| 10 |
#include <sstream> |
| 11 |
#include <cstdlib> |
| 12 |
#include <csignal> |
| 13 |
#include <ctime> |
| 14 |
#include <sys/stat.h> |
| 15 |
#include <pwd.h> |
| 16 |
#include <cerrno> |
| 17 |
#include <unistd.h> |
| 18 |
#include <fcntl.h> |
| 19 |
#include <fstream> |
| 20 |
#include <sys/time.h> |
| 21 |
#include <sys/poll.h> |
| 22 |
|
| 23 |
// LINUX ONLY FEATURE |
| 24 |
//#ifdef HAVE_SYS_EPOLL_H |
| 25 |
//#include <sys/epoll.h> |
| 26 |
//#endif |
| 27 |
|
| 28 |
#include <istream> |
| 29 |
#include <map> |
| 30 |
#include <memory> |
| 31 |
#include <vector> |
| 32 |
#include <atomic> |
| 33 |
#include <sys/types.h> |
| 34 |
#include <sys/wait.h> |
| 35 |
#include <sys/select.h> |
| 36 |
|
| 37 |
#include <openssl/ssl.h> |
| 38 |
#include <openssl/err.h> |
| 39 |
#include <openssl/conf.h> |
| 40 |
|
| 41 |
#include "FatController.hpp" |
| 42 |
#include "ConnectionHandler.hpp" |
| 43 |
#include "DynamicURLList.hpp" |
| 44 |
#include "DynamicIPList.hpp" |
| 45 |
#include "String.hpp" |
| 46 |
#include "SocketArray.hpp" |
| 47 |
#include "UDSocket.hpp" |
| 48 |
#include "SysV.hpp" |
| 49 |
#include "Queue.hpp" |
| 50 |
#include "OptionContainer.hpp" |
| 51 |
#include "Logger.hpp" |
| 52 |
|
| 53 |
#ifdef VALGRD |
| 54 |
extern "C" |
| 55 |
{ |
| 56 |
static void* |
| 57 |
execute_native_thread_routine(void* __p) |
| 58 |
{ |
| 59 |
std::thread::_Impl_base* __t = static_cast<std::thread::_Impl_base*>(__p); |
| 60 |
std::thread::__shared_base_type __local; |
| 61 |
__local.swap(__t->_M_this_ptr); |
| 62 |
|
| 63 |
__try |
| 64 |
{ |
| 65 |
__t->_M_run(); |
| 66 |
} |
| 67 |
__catch(const __cxxabiv1::__forced_unwind&) |
| 68 |
{ |
| 69 |
__throw_exception_again; |
| 70 |
} |
| 71 |
__catch(...) |
| 72 |
{ |
| 73 |
std::terminate(); |
| 74 |
} |
| 75 |
|
| 76 |
return nullptr; |
| 77 |
} |
| 78 |
} // extern "C" |
| 79 |
|
| 80 |
|
| 81 |
void |
| 82 |
std::thread::_M_start_thread(__shared_base_type __b) |
| 83 |
{ |
| 84 |
if (!__gthread_active_p()) |
| 85 |
#if __cpp_exceptions |
| 86 |
throw system_error(make_error_code(errc::operation_not_permitted), |
| 87 |
"Enable multithreading to use std::thread"); |
| 88 |
#else |
| 89 |
__throw_system_error(int(errc::operation_not_permitted)); |
| 90 |
#endif |
| 91 |
|
| 92 |
_M_start_thread(std::move(__b), nullptr); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
void |
| 97 |
std::thread::_M_start_thread(__shared_base_type __b, void (*)()) |
| 98 |
{ |
| 99 |
auto ptr = __b.get(); |
| 100 |
ptr->_M_this_ptr = std::move(__b); |
| 101 |
int __e = __gthread_create(&_M_id._M_thread, |
| 102 |
&execute_native_thread_routine, ptr); |
| 103 |
if (__e) |
| 104 |
{ |
| 105 |
ptr->_M_this_ptr.reset(); |
| 106 |
__throw_system_error(__e); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
#endif |
| 111 |
|
| 112 |
// GLOBALS |
| 113 |
|
| 114 |
std::atomic<bool> ttg; |
| 115 |
std::atomic<bool> e2logger_ttg; |
| 116 |
std::atomic<bool> gentlereload; |
| 117 |
std::atomic<bool> reloadconfig; |
| 118 |
std::atomic<int> reload_cnt; |
| 119 |
std::atomic<bool> rotate_access; |
| 120 |
std::atomic<bool> rotate_request; |
| 121 |
std::atomic<bool> rotate_dstat; |
| 122 |
|
| 123 |
extern std::atomic<bool> g_is_starting; |
| 124 |
|
| 125 |
extern OptionContainer o; |
| 126 |
extern bool is_daemonised; |
| 127 |
|
| 128 |
pid_t master_pid = 0; |
| 129 |
|
| 130 |
void stat_rec::clear() { |
| 131 |
conx = 0; |
| 132 |
reqs = 0; |
| 133 |
}; |
| 134 |
|
| 135 |
void stat_rec::start(bool firsttime = true) { |
| 136 |
|
| 137 |
if (firsttime) { |
| 138 |
clear(); |
| 139 |
start_int = time(NULL); |
| 140 |
end_int = start_int + o.dstat.dstat_interval; |
| 141 |
maxusedfd = 0; |
| 142 |
} |
| 143 |
|
| 144 |
if (o.dstat.dstat_log_flag) { |
| 145 |
// now opened by Logger |
| 146 |
//mode_t old_umask; |
| 147 |
//old_umask = umask(S_IWGRP | S_IWOTH); |
| 148 |
//fs = fopen(o.dstat_location.c_str(), "a"); |
| 149 |
std::string outmess; |
| 150 |
if (o.dstat.stats_human_readable) { |
| 151 |
outmess = "time httpw busy httpwQ logQ conx conx/s reqs reqs/s maxfd LCcnt"; |
| 152 |
} else { |
| 153 |
outmess = "time httpw busy httpwQ logQ conx conx/s reqs reqs/s maxfd LCcnt"; |
| 154 |
} |
| 155 |
E2LOGGER_dstatslog(outmess); |
| 156 |
e2logger.flush(LoggerSource::dstatslog); |
| 157 |
}; |
| 158 |
}; |
| 159 |
|
| 160 |
void stat_rec::reset() { |
| 161 |
time_t now = time(NULL); |
| 162 |
int bc = busychildren; |
| 163 |
long period = now - start_int; |
| 164 |
long cnx = (long) conx; |
| 165 |
long rqx = (long) reqs; |
| 166 |
int mfd = maxusedfd; |
| 167 |
int LC = o.LC_cnt; |
| 168 |
// clear and reset stats now so that stats are less likely to be missed |
| 169 |
clear(); |
| 170 |
if ((end_int + o.dstat.dstat_interval) > now) |
| 171 |
start_int = end_int; |
| 172 |
else |
| 173 |
start_int = now; |
| 174 |
|
| 175 |
end_int = start_int + o.dstat.dstat_interval; |
| 176 |
|
| 177 |
if (rotate_dstat) { |
| 178 |
e2logger.rotate(LoggerSource::dstatslog); |
| 179 |
start(false); // output header |
| 180 |
rotate_dstat = false; |
| 181 |
} |
| 182 |
|
| 183 |
char outbuff[201]; |
| 184 |
|
| 185 |
long cps = cnx / period; |
| 186 |
long rqs = rqx / period; |
| 187 |
if (o.dstat.stats_human_readable) { |
| 188 |
struct tm *timeinfo; |
| 189 |
time(&now); |
| 190 |
timeinfo = localtime(&now); |
| 191 |
char buffer[50]; |
| 192 |
strftime(buffer, 50, "%Y-%m-%d %H:%M", timeinfo); |
| 193 |
snprintf(outbuff, 200, "%s %d %d %d %d %ld %ld %ld %ld %d %d", buffer, |
| 194 |
o.proc.http_workers, |
| 195 |
bc, o.http_worker_Q.size(), o.log.log_Q->size(), cnx, cps, rqx, rqs, mfd, LC); |
| 196 |
} else { |
| 197 |
snprintf(outbuff, 200, "%ld %d %d %d %d %ld %ld %ld %ld %d %d", now, |
| 198 |
o.proc.http_workers, |
| 199 |
bc, o.http_worker_Q.size(), o.log.log_Q->size(), cnx, cps, rqx, rqs, mfd, LC); |
| 200 |
} |
| 201 |
std::string outs(outbuff); |
| 202 |
E2LOGGER_dstatslog(outs); |
| 203 |
e2logger.flush(LoggerSource::dstatslog); |
| 204 |
|
| 205 |
//fflush(fs); |
| 206 |
}; |
| 207 |
|
| 208 |
void stat_rec::close() { |
| 209 |
if (fs != NULL) fclose(fs); |
| 210 |
}; |
| 211 |
|
| 212 |
|
| 213 |
//int cache_erroring; // num cache errors reported by children |
| 214 |
//int restart_cnt = 0; |
| 215 |
//int restart_numchildren; // numchildren at time of gentle restart |
| 216 |
//int hup_index; |
| 217 |
//int gentle_to_hup = 0; |
| 218 |
|
| 219 |
//bool gentle_in_progress = false; |
| 220 |
int top_child_fds; // cross platform maxchildren position in children array |
| 221 |
int failurecount; |
| 222 |
int serversocketcount; |
| 223 |
SocketArray serversockets; // the sockets we will listen on for connections |
| 224 |
Socket *peersock(NULL); // the socket which will contain the connection |
| 225 |
|
| 226 |
//String peersockip; // which will contain the connection ip |
| 227 |
|
| 228 |
|
| 229 |
void monitor_flag_set(bool action) { |
| 230 |
|
| 231 |
String fulink = o.monitor.monitor_flag_prefix; |
| 232 |
String ftouch = fulink; |
| 233 |
if (action) { |
| 234 |
fulink += "paused"; |
| 235 |
ftouch += "running"; |
| 236 |
} else { |
| 237 |
fulink += "running"; |
| 238 |
ftouch += "paused"; |
| 239 |
} |
| 240 |
|
| 241 |
//mode_t old_umask; |
| 242 |
//old_umask = umask(S_IWOTH); |
| 243 |
umask(S_IWOTH); |
| 244 |
FILE *fs = fopen(ftouch.c_str(), "w"); |
| 245 |
if (!fs) { |
| 246 |
E2LOGGER_error("Unable to open monitor_flag ", ftouch, " for writing"); |
| 247 |
o.monitor.monitor_flag_flag = false; |
| 248 |
} |
| 249 |
fclose(fs); |
| 250 |
if (unlink(fulink.c_str()) == -1) { |
| 251 |
E2LOGGER_error("Unable to unlink monitor_flag ", fulink, " error: ", strerror(errno)); |
| 252 |
} |
| 253 |
return; |
| 254 |
} |
| 255 |
|
| 256 |
stat_rec dstat; |
| 257 |
stat_rec *dystat = &dstat; |
| 258 |
|
| 259 |
// DECLARATIONS |
| 260 |
|
| 261 |
// Signal handlers |
| 262 |
extern "C" { |
| 263 |
//void sig_chld(int signo); |
| 264 |
//void sig_term(int signo); // This is so we can kill our children |
| 265 |
//void sig_termsafe(int signo); // This is so we can kill our children safer |
| 266 |
//void sig_hup(int signo); // This is so we know if we should re-read our config. |
| 267 |
//void sig_usr1(int signo); // This is so we know if we should re-read our config but not kill current connections |
| 268 |
//void sig_childterm(int signo); |
| 269 |
//#ifdef ENABLE_SEGV_BACKTRACE |
| 270 |
//void sig_segv(int signo, siginfo_t *info, void *secret); // Generate a backtrace on segfault |
| 271 |
//#endif |
| 272 |
} |
| 273 |
|
| 274 |
// logging & URL cache processes |
| 275 |
void log_listener(Queue<std::string> *log_Q, bool is_RQlog); |
| 276 |
|
| 277 |
// fork off into background |
| 278 |
bool daemonise(int pidfilefd); |
| 279 |
// create specified amount of child threads |
| 280 |
|
| 281 |
//void handle_connections(int tindex); // needs changing to be threadish |
| 282 |
|
| 283 |
// setuid() to proxy user (not just seteuid()) - used by child processes & logger/URL cache for security & resource usage reasons |
| 284 |
bool drop_priv_completely(); |
| 285 |
|
| 286 |
// IMPLEMENTATION |
| 287 |
|
| 288 |
// completely drop our privs - i.e. setuid, not just seteuid |
| 289 |
bool drop_priv_completely() { |
| 290 |
// This is done to solve the problem where the total processes for the |
| 291 |
// uid rather than euid is taken for RLIMIT_NPROC and so can't fork() |
| 292 |
// as many as expected. |
| 293 |
// It is also more secure. |
| 294 |
// |
| 295 |
// Suggested fix by Lawrence Manning Tue 25th February 2003 |
| 296 |
// |
| 297 |
|
| 298 |
int rc = seteuid(o.proc.root_user); // need to be root again to drop properly |
| 299 |
if (rc == -1) { |
| 300 |
E2LOGGER_error("Unable to seteuid(suid)"); |
| 301 |
return false; // setuid failed for some reason so exit with error |
| 302 |
} |
| 303 |
rc = setuid(o.proc.proxy_user); |
| 304 |
if (rc == -1) { |
| 305 |
E2LOGGER_error("Unable to setuid()"); |
| 306 |
return false; // setuid failed for some reason so exit with error |
| 307 |
} |
| 308 |
return true; |
| 309 |
} |
| 310 |
|
| 311 |
// Fork ourselves off into the background |
| 312 |
bool daemonise(int pidfileid) { |
| 313 |
|
| 314 |
int nullfd = -1; |
| 315 |
if ((nullfd = open("/dev/null", O_WRONLY, 0)) == -1) { |
| 316 |
E2LOGGER_error("Couldn't open /dev/null"); |
| 317 |
return false; |
| 318 |
} |
| 319 |
|
| 320 |
pid_t pid; |
| 321 |
if ((pid = fork()) < 0) { |
| 322 |
// Error!! |
| 323 |
close(nullfd); |
| 324 |
return false; |
| 325 |
} else if (pid != 0) {// parent goes...A |
| 326 |
master_pid = pid; |
| 327 |
if (nullfd != -1) { |
| 328 |
close(nullfd); |
| 329 |
} |
| 330 |
int rc = sysv_writepidfile(pidfileid, master_pid); |
| 331 |
if (rc != 0) { |
| 332 |
E2LOGGER_error("Error writing to the e2guardian.pid file: ", strerror(errno)); |
| 333 |
} |
| 334 |
// bye-bye |
| 335 |
exit(0); |
| 336 |
} |
| 337 |
|
| 338 |
// child continues |
| 339 |
dup2(nullfd, 0); // stdin |
| 340 |
dup2(nullfd, 1); // stdout |
| 341 |
dup2(nullfd, 2); // stderr |
| 342 |
close(nullfd); |
| 343 |
|
| 344 |
setsid(); // become session leader |
| 345 |
//int dummy = chdir("/"); // change working directory |
| 346 |
if (chdir("/") != 0) {// change working directory |
| 347 |
E2LOGGER_error(" Can't change / directory !"); |
| 348 |
return false; |
| 349 |
} |
| 350 |
umask(0); // clear our file mode creation mask |
| 351 |
umask(S_IWGRP | S_IWOTH); // set to mor sensible setting?? |
| 352 |
|
| 353 |
is_daemonised = true; |
| 354 |
|
| 355 |
return true; |
| 356 |
} |
| 357 |
|
| 358 |
// * |
| 359 |
// * |
| 360 |
// * worker thread code |
| 361 |
// * |
| 362 |
// * |
| 363 |
|
| 364 |
// handle any connections received by this thread |
| 365 |
void handle_connections(int tindex) { |
| 366 |
(thread_id = "hw") += std::to_string(tindex) += ": "; |
| 367 |
|
| 368 |
try { |
| 369 |
while (!ttg) { // extra loop in order to delete and create ConnentionHandler on new lists or error |
| 370 |
ConnectionHandler h; // the class that handles the connections |
| 371 |
String ip; |
| 372 |
|
| 373 |
while (!ttg) { |
| 374 |
DEBUG_debug(" waiting connection on http_worker_Q "); |
| 375 |
LQ_rec rec = o.http_worker_Q.pop(); |
| 376 |
Socket *peersock = rec.sock; |
| 377 |
DEBUG_debug(" popped connection from http_worker_Q"); |
| 378 |
if (ttg) break; |
| 379 |
|
| 380 |
String peersockip = peersock->getPeerIP(); |
| 381 |
if (peersock->getFD() < 0 || peersockip.length() < 7) { |
| 382 |
// if (o.conn.logconerror) |
| 383 |
E2LOGGER_info("Error accepting. (Ignorable)"); |
| 384 |
continue; |
| 385 |
} |
| 386 |
++dystat->busychildren; |
| 387 |
++dystat->conx; |
| 388 |
#ifdef DEBUG_LOW |
| 389 |
int rc = h.handlePeer(*peersock, peersockip, dystat, rec.ct_type); // deal with the connection |
| 390 |
DEBUG_debug("handle_peer returned: ", rc); |
| 391 |
#else |
| 392 |
h.handlePeer(*peersock, peersockip, dystat, rec.ct_type); // deal with the connection |
| 393 |
#endif |
| 394 |
|
| 395 |
--dystat->busychildren; |
| 396 |
if(peersock != nullptr) delete peersock; |
| 397 |
break; |
| 398 |
}; |
| 399 |
}; |
| 400 |
} catch (...) { |
| 401 |
E2LOGGER_error("worker thread caught unexpected exception - exiting"); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
// * |
| 407 |
// * |
| 408 |
// * end of worker thread code |
| 409 |
// * |
| 410 |
// * |
| 411 |
|
| 412 |
|
| 413 |
#ifdef REMOVE_IN_55 |
| 414 |
void tell_monitor(bool active) //may not be needed |
| 415 |
{ |
| 416 |
|
| 417 |
String buff(o.monitor_helper); |
| 418 |
String buff1; |
| 419 |
|
| 420 |
if (active) |
| 421 |
buff1 = " start"; |
| 422 |
else |
| 423 |
buff1 = " stop"; |
| 424 |
|
| 425 |
E2LOGGER_error("Monitorhelper called: ", buff, buff1); |
| 426 |
pid_t childid; |
| 427 |
childid = fork(); |
| 428 |
|
| 429 |
if (childid == -1) { |
| 430 |
E2LOGGER_error("Unable to fork to tell monitorhelper error: ", strerror(errno)); |
| 431 |
return; |
| 432 |
}; |
| 433 |
|
| 434 |
if (childid == 0) { // Am the child |
| 435 |
int rc = seteuid(o.proc.root_user); |
| 436 |
if (rc != -1) { |
| 437 |
int systemreturn = execl(buff.c_str(), buff.c_str(), buff1.c_str(), |
| 438 |
(char *) NULL); // should not return from call |
| 439 |
if (systemreturn == -1) { |
| 440 |
E2LOGGER_error("Unable to exec: ", buff, buff1, " : errno ", errno, " ", strerror(errno)); |
| 441 |
exit(0); |
| 442 |
} |
| 443 |
} else { |
| 444 |
E2LOGGER_error("Unable to set uid root"); |
| 445 |
exit(0); |
| 446 |
} |
| 447 |
}; |
| 448 |
|
| 449 |
if (childid > 0) { // Am the parent |
| 450 |
int rc; |
| 451 |
int status; |
| 452 |
rc = waitpid(childid, &status, 0); |
| 453 |
if (rc == -1) { |
| 454 |
E2LOGGER_error("Wait for monitorhelper returned : errno ", strerror(errno)); |
| 455 |
return; |
| 456 |
}; |
| 457 |
if (WIFEXITED(status)) { |
| 458 |
return; |
| 459 |
} else { |
| 460 |
E2LOGGER_error("Monitorhelper exited abnormally"); |
| 461 |
return; |
| 462 |
}; |
| 463 |
}; |
| 464 |
}; |
| 465 |
#endif |
| 466 |
|
| 467 |
#ifdef NOTDEF |
| 468 |
void wait_for_proxy() |
| 469 |
{ |
| 470 |
Socket proxysock; |
| 471 |
int rc; |
| 472 |
|
| 473 |
try { |
| 474 |
// ...connect to proxy |
| 475 |
rc = proxysock.connect(o.proxy_ip, o.proxy_port); |
| 476 |
if (!rc) { |
| 477 |
proxysock.close(); |
| 478 |
//cache_erroring = 0; |
| 479 |
return; |
| 480 |
} |
| 481 |
if (errno == EINTR) { |
| 482 |
return; |
| 483 |
} |
| 484 |
} catch (std::exception &e) { |
| 485 |
DEBUG_debug(" -exception while creating proxysock: ", e.what()); |
| 486 |
} |
| 487 |
E2LOGGER_error("Proxy is not responding - Waiting for proxy to respond"); |
| 488 |
if (o.monitor_flag_flag) |
| 489 |
monitor_flag_set(false); |
| 490 |
if (o.monitor_helper_flag) |
| 491 |
tell_monitor(false); |
| 492 |
int wait_time = 1; |
| 493 |
//int report_interval = 600; // report every 10 mins to log |
| 494 |
int cnt_down = o.proxy_failure_log_interval; |
| 495 |
while (true) { |
| 496 |
rc = proxysock.connect(o.proxy_ip, o.proxy_port); |
| 497 |
if (!rc) { |
| 498 |
proxysock.close(); |
| 499 |
//cache_erroring = 0; |
| 500 |
E2LOGGER_error("Proxy now responding - resuming after %d seconds", wait_time); |
| 501 |
if (o.monitor_flag_flag) |
| 502 |
monitor_flag_set(true); |
| 503 |
if (o.monitor_helper_flag) |
| 504 |
tell_monitor(true); |
| 505 |
return; |
| 506 |
} else { |
| 507 |
if (ttg) |
| 508 |
return; |
| 509 |
wait_time++; |
| 510 |
cnt_down--; |
| 511 |
if (cnt_down < 1) { |
| 512 |
E2LOGGER_error("Proxy not responding - still waiting after %d seconds", wait_time); |
| 513 |
cnt_down = o.proxy_failure_log_interval; |
| 514 |
} |
| 515 |
std::this_thread::sleep_for(std::chrono::milliseconds(1000)); |
| 516 |
} |
| 517 |
} |
| 518 |
} |
| 519 |
#endif |
| 520 |
|
| 521 |
|
| 522 |
// * |
| 523 |
// * |
| 524 |
// * logger, IP list and URL cache main loops |
| 525 |
// * |
| 526 |
// * |
| 527 |
|
| 528 |
void log_listener(Queue<std::string> *log_Q, bool is_RQlog) { |
| 529 |
if (is_RQlog) |
| 530 |
thread_id = "RQlog: "; |
| 531 |
else |
| 532 |
thread_id = "log: "; |
| 533 |
|
| 534 |
try { |
| 535 |
DEBUG_trace("log listener started"); |
| 536 |
|
| 537 |
#ifdef ENABLE_EMAIL // no longer needed - replace with some sort of external script reading alert log?? |
| 538 |
// Email notification patch by J. Gauthier |
| 539 |
std::map<std::string, int> violation_map; |
| 540 |
std::map<std::string, int> timestamp_map; |
| 541 |
std::map<std::string, std::string> vbody_map; |
| 542 |
|
| 543 |
int curv_tmp, stamp_tmp, byuser; |
| 544 |
#endif |
| 545 |
|
| 546 |
//String where, what, how; |
| 547 |
std::string cr("\n"); |
| 548 |
|
| 549 |
std::string where, what, how, cat, clienthost, from, who, mimetype, useragent, ssize, sweight, params, message_no; |
| 550 |
std::string stype, postdata, flags, searchterms; |
| 551 |
int port = 80, isnaughty = 0, isexception = 0, issemiexception = 0, code = 200, naughtytype = 0; |
| 552 |
int do_access_log = 0, do_alert_log =0; |
| 553 |
int cachehit = 0, wasinfected = 0, wasscanned = 0, filtergroup = 0; |
| 554 |
long tv_sec = 0, tv_usec = 0, endtv_sec = 0, endtv_usec = 0; |
| 555 |
int contentmodified = 0, urlmodified = 0, headermodified = 0; |
| 556 |
int headeradded = 0; |
| 557 |
|
| 558 |
String server(""); |
| 559 |
// Get server name - only needed for formats 5 & 7 |
| 560 |
if ((o.log.log_file_format == 5) || (o.log.log_file_format == 7)) { |
| 561 |
server = o.net.server_name; |
| 562 |
} |
| 563 |
|
| 564 |
std::string semiexception_word = o.language_list.getTranslation(51); |
| 565 |
semiexception_word = "*" + semiexception_word + "* "; |
| 566 |
std::string exception_word = o.language_list.getTranslation(51); |
| 567 |
exception_word = "*" + exception_word + "* "; |
| 568 |
std::string denied_word = o.language_list.getTranslation(52); |
| 569 |
denied_word = "*" + denied_word; |
| 570 |
std::string infected_word = o.language_list.getTranslation(53); |
| 571 |
infected_word = "*" + infected_word + "* "; |
| 572 |
std::string scanned_word = o.language_list.getTranslation(54); |
| 573 |
scanned_word = "*" + scanned_word + "* "; |
| 574 |
std::string contentmod_word = o.language_list.getTranslation(55); |
| 575 |
contentmod_word = "*" + contentmod_word + "* "; |
| 576 |
std::string urlmod_word = o.language_list.getTranslation(56); |
| 577 |
urlmod_word = "*" + urlmod_word + "* "; |
| 578 |
std::string headermod_word = o.language_list.getTranslation(57); |
| 579 |
headermod_word = "*" + headermod_word + "* "; |
| 580 |
std::string headeradd_word = o.language_list.getTranslation(58); |
| 581 |
headeradd_word = "*" + headeradd_word + "* "; |
| 582 |
std::string neterr_word = o.language_list.getTranslation(59); |
| 583 |
neterr_word = "*" + neterr_word + "* "; |
| 584 |
std::string blank_str; |
| 585 |
|
| 586 |
if (o.log.use_dash_for_blanks) |
| 587 |
blank_str = "-"; |
| 588 |
else |
| 589 |
blank_str = ""; |
| 590 |
|
| 591 |
|
| 592 |
while (!e2logger_ttg) { // loop, essentially, for ever |
| 593 |
std::string loglines; |
| 594 |
loglines.append(log_Q->pop()); // get logdata from queue |
| 595 |
if (e2logger_ttg) break; |
| 596 |
if (is_RQlog && rotate_request) { |
| 597 |
e2logger.rotate(LoggerSource::requestlog); |
| 598 |
rotate_request = false; |
| 599 |
} else if (!is_RQlog && rotate_access) { |
| 600 |
e2logger.rotate(LoggerSource::accesslog); |
| 601 |
rotate_access = false; |
| 602 |
} |
| 603 |
DEBUG_debug("received a log request"); |
| 604 |
|
| 605 |
// Formatting code migration from ConnectionHandler |
| 606 |
// and email notification code based on patch provided |
| 607 |
// by J. Gauthier |
| 608 |
|
| 609 |
// read in the various parts of the log string |
| 610 |
bool error = true; |
| 611 |
int itemcount = 0; |
| 612 |
//char * dup = strdup(loglines.c_str()); |
| 613 |
//const char *delim = "\n"; |
| 614 |
std::istringstream iss(loglines); |
| 615 |
std::string logline; |
| 616 |
std::shared_ptr <LOptionContainer> ldl; |
| 617 |
ldl = o.currentLists(); |
| 618 |
|
| 619 |
while (std::getline(iss, logline)) { |
| 620 |
// Loop around reading in data, because we might have huge URLs |
| 621 |
std::string s; |
| 622 |
|
| 623 |
if (o.log.use_dash_for_blanks && logline == "") { |
| 624 |
s = "-"; |
| 625 |
} else if (!o.log.use_dash_for_blanks && logline == "-") { |
| 626 |
s = ""; |
| 627 |
} else { |
| 628 |
s = logline; |
| 629 |
} |
| 630 |
|
| 631 |
switch (itemcount) { |
| 632 |
case 0: |
| 633 |
isexception = atoi(logline.c_str()); |
| 634 |
break; |
| 635 |
case 1: |
| 636 |
cat = s; |
| 637 |
break; |
| 638 |
case 2: |
| 639 |
isnaughty = atoi(logline.c_str()); |
| 640 |
break; |
| 641 |
case 3: |
| 642 |
naughtytype = atoi(logline.c_str()); |
| 643 |
break; |
| 644 |
case 4: |
| 645 |
sweight = s; |
| 646 |
break; |
| 647 |
case 5: |
| 648 |
where = s; |
| 649 |
break; |
| 650 |
case 6: |
| 651 |
what = s; |
| 652 |
break; |
| 653 |
case 7: |
| 654 |
how = s; |
| 655 |
break; |
| 656 |
case 8: |
| 657 |
who = s; |
| 658 |
break; |
| 659 |
case 9: |
| 660 |
from = s; |
| 661 |
break; |
| 662 |
case 10: |
| 663 |
port = atoi(logline.c_str()); |
| 664 |
break; |
| 665 |
case 11: |
| 666 |
wasscanned = atoi(logline.c_str()); |
| 667 |
break; |
| 668 |
case 12: |
| 669 |
wasinfected = atoi(logline.c_str()); |
| 670 |
break; |
| 671 |
case 13: |
| 672 |
contentmodified = atoi(logline.c_str()); |
| 673 |
break; |
| 674 |
case 14: |
| 675 |
urlmodified = atoi(logline.c_str()); |
| 676 |
break; |
| 677 |
case 15: |
| 678 |
headermodified = atoi(logline.c_str()); |
| 679 |
break; |
| 680 |
case 16: |
| 681 |
ssize = s; |
| 682 |
break; |
| 683 |
case 17: |
| 684 |
filtergroup = atoi(logline.c_str()); |
| 685 |
if (filtergroup < 0 || filtergroup > o.filter.numfg) filtergroup = 0; |
| 686 |
break; |
| 687 |
case 18: |
| 688 |
code = atoi(logline.c_str()); |
| 689 |
break; |
| 690 |
case 19: |
| 691 |
cachehit = atoi(logline.c_str()); |
| 692 |
break; |
| 693 |
case 20: |
| 694 |
mimetype = s; |
| 695 |
break; |
| 696 |
case 21: |
| 697 |
tv_sec = atol(logline.c_str()); |
| 698 |
break; |
| 699 |
case 22: |
| 700 |
tv_usec = atol(logline.c_str()); |
| 701 |
break; |
| 702 |
case 23: |
| 703 |
endtv_sec = atol(logline.c_str()); |
| 704 |
break; |
| 705 |
case 24: |
| 706 |
endtv_usec = atol(logline.c_str()); |
| 707 |
break; |
| 708 |
case 25: |
| 709 |
clienthost = s; |
| 710 |
break; |
| 711 |
case 26: |
| 712 |
useragent = s; |
| 713 |
break; |
| 714 |
case 27: |
| 715 |
params = s; |
| 716 |
break; |
| 717 |
case 28: |
| 718 |
postdata = s; |
| 719 |
break; |
| 720 |
case 29: |
| 721 |
message_no = s; |
| 722 |
break; |
| 723 |
case 30: |
| 724 |
headeradded = atoi(logline.c_str()); |
| 725 |
break; |
| 726 |
case 31: |
| 727 |
flags = s; |
| 728 |
break; |
| 729 |
case 32: |
| 730 |
searchterms = s; |
| 731 |
break; |
| 732 |
case 33: |
| 733 |
do_access_log = atoi(logline.c_str()); |
| 734 |
break; |
| 735 |
case 34: |
| 736 |
do_alert_log = atoi(logline.c_str()); |
| 737 |
break; |
| 738 |
case 35: |
| 739 |
issemiexception = atoi(logline.c_str()); |
| 740 |
error = false; |
| 741 |
break; |
| 742 |
} |
| 743 |
itemcount++; |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
// don't build the log line if we couldn't read all the component parts |
| 748 |
if (error) { |
| 749 |
E2LOGGER_error("Error in logline ", itemcount, " ", loglines); |
| 750 |
continue; |
| 751 |
} |
| 752 |
|
| 753 |
// Start building the log line |
| 754 |
|
| 755 |
if (port != 0 && port != 80) { |
| 756 |
// put port numbers of non-standard HTTP requests into the logged URL |
| 757 |
String newwhere(where); |
| 758 |
if (newwhere.after("://").contains("/")) { |
| 759 |
String proto, host, path; |
| 760 |
proto = newwhere.before("://"); |
| 761 |
host = newwhere.after("://"); |
| 762 |
path = host.after("/"); |
| 763 |
host = host.before("/"); |
| 764 |
newwhere = proto; |
| 765 |
newwhere += "://"; |
| 766 |
newwhere += host; |
| 767 |
newwhere += ":"; |
| 768 |
newwhere += String((int) port); |
| 769 |
newwhere += "/"; |
| 770 |
newwhere += path; |
| 771 |
where = newwhere; |
| 772 |
} else { |
| 773 |
where += ":"; |
| 774 |
where += String((int) port); |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
bool neterr = false; |
| 779 |
|
| 780 |
// stamp log entries so they stand out/can be searched |
| 781 |
switch (naughtytype) { |
| 782 |
case 1: |
| 783 |
stype = "-POST"; |
| 784 |
break; |
| 785 |
case 2: |
| 786 |
stype = "-PARAMS"; |
| 787 |
break; |
| 788 |
case 3: |
| 789 |
neterr = true; |
| 790 |
break; |
| 791 |
default: |
| 792 |
stype.clear(); |
| 793 |
} |
| 794 |
|
| 795 |
if (isnaughty) { |
| 796 |
if (neterr) |
| 797 |
what = neterr_word + what; |
| 798 |
else |
| 799 |
what = denied_word + stype + "* " + what; |
| 800 |
} else if (isexception && (o.log.log_exception_hits == 2)) { |
| 801 |
if (issemiexception) { |
| 802 |
what = semiexception_word + what; |
| 803 |
} else { |
| 804 |
what = exception_word + what; |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
if (wasinfected) |
| 809 |
what = infected_word + stype + "* " + what; |
| 810 |
else if (wasscanned) |
| 811 |
what = scanned_word + what; |
| 812 |
|
| 813 |
if (contentmodified) { |
| 814 |
what = contentmod_word + what; |
| 815 |
} |
| 816 |
if (urlmodified) { |
| 817 |
what = urlmod_word + what; |
| 818 |
} |
| 819 |
if (headermodified) { |
| 820 |
what = headermod_word + what; |
| 821 |
} |
| 822 |
if (headeradded) { |
| 823 |
what = headeradd_word + what; |
| 824 |
} |
| 825 |
|
| 826 |
std::string builtline, year, month, day, hour, min, sec, when, vbody, utime; |
| 827 |
|
| 828 |
// create a string representation of UNIX timestamp if desired |
| 829 |
if (o.log.log_timestamp || (o.log.log_file_format == 3) |
| 830 |
|| (o.log.log_file_format > 4)) { |
| 831 |
String temp((int) (endtv_usec / 1000)); |
| 832 |
while (temp.length() < 3) { |
| 833 |
temp = "0" + temp; |
| 834 |
} |
| 835 |
if (temp.length() > 3) { |
| 836 |
temp = "999"; |
| 837 |
} |
| 838 |
utime = temp; |
| 839 |
utime = "." + utime; |
| 840 |
utime = String((int) endtv_sec) + utime; |
| 841 |
} |
| 842 |
|
| 843 |
|
| 844 |
if ((o.log.log_file_format <= 2) || (o.log.log_file_format == 4)) { |
| 845 |
// "when" not used in format 3, and not if logging timestamps instead in formats 5-8 |
| 846 |
//time_t now = time(NULL); |
| 847 |
time_t now = endtv_sec; |
| 848 |
char date[32]; |
| 849 |
struct tm *tm = localtime(&now); |
| 850 |
strftime(date, sizeof date, "%Y.%m.%d %H:%M:%S", tm); |
| 851 |
when = date; |
| 852 |
// append timestamp if desired |
| 853 |
if (o.log.log_timestamp) |
| 854 |
when += " " + utime; |
| 855 |
} |
| 856 |
|
| 857 |
// blank out IP, hostname and username if desired |
| 858 |
if (o.log.anonymise_logs) { |
| 859 |
who = ""; |
| 860 |
from = "0.0.0.0"; |
| 861 |
clienthost.clear(); |
| 862 |
} else if ((clienthost == blank_str) || (clienthost == "DNSERROR")) { |
| 863 |
clienthost = from; |
| 864 |
} |
| 865 |
|
| 866 |
String groupname; |
| 867 |
String stringcode(code); |
| 868 |
String stringgroup(filtergroup + 1); |
| 869 |
|
| 870 |
if (is_RQlog) { |
| 871 |
groupname = ""; |
| 872 |
} else { |
| 873 |
if (stringcode == "407") { |
| 874 |
groupname = "negotiate_identification"; |
| 875 |
} else { |
| 876 |
groupname = ldl->fg[filtergroup]->name; |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
switch (o.log.log_file_format) { |
| 881 |
case 4: |
| 882 |
builtline = when + "\t" + who + "\t" + from + "\t" + where + "\t" + what + "\t" + how |
| 883 |
+ "\t" + ssize + "\t" + sweight + "\t" + cat + "\t" + stringgroup + "\t" |
| 884 |
+ stringcode + "\t" + mimetype + "\t" + clienthost + "\t" + groupname |
| 885 |
#ifdef SG_LOGFORMAT |
| 886 |
+ "\t" + useragent + "\t\t" + o.log.logid_1 + "\t" + o.log.prod_id + "\t" |
| 887 |
+ params + "\t" + o.log.logid_2 + "\t" + postdata; |
| 888 |
#else |
| 889 |
+ "\t" + useragent + "\t" + params + "\t" + o.log.logid_1 + "\t" + o.log.logid_2 + "\t" + |
| 890 |
postdata; |
| 891 |
#endif |
| 892 |
break; |
| 893 |
case 3: { |
| 894 |
// as certain bits of info are logged in format 3, their creation is best done here, not in all cases. |
| 895 |
std::string duration, hier, hitmiss; |
| 896 |
long durationsecs, durationusecs; |
| 897 |
durationsecs = (endtv_sec - tv_sec); |
| 898 |
durationusecs = endtv_usec - tv_usec; |
| 899 |
durationusecs = (durationusecs / 1000) + durationsecs * 1000; |
| 900 |
String temp((int) durationusecs); |
| 901 |
while (temp.length() < 6) { |
| 902 |
temp = " " + temp; |
| 903 |
} |
| 904 |
duration = temp; |
| 905 |
|
| 906 |
if (code == 403) { |
| 907 |
hitmiss = "TCP_DENIED/403"; |
| 908 |
} else { |
| 909 |
if (cachehit) { |
| 910 |
hitmiss = "TCP_HIT/"; |
| 911 |
hitmiss.append(stringcode); |
| 912 |
} else { |
| 913 |
hitmiss = "TCP_MISS/"; |
| 914 |
hitmiss.append(stringcode); |
| 915 |
} |
| 916 |
} |
| 917 |
hier = "DEFAULT_PARENT/"; |
| 918 |
hier += o.net.proxy_ip; |
| 919 |
builtline = |
| 920 |
utime + " " + duration + " " + ((clienthost.length() > 0) ? clienthost : from) + " " + |
| 921 |
hitmiss + |
| 922 |
" " + ssize + " " |
| 923 |
+ how + " " + where + " " + who + " " + hier + " " + mimetype; |
| 924 |
break; |
| 925 |
} |
| 926 |
case 2: |
| 927 |
builtline = |
| 928 |
"\"" + when + "\",\"" + who + "\",\"" + from + "\",\"" + where + "\",\"" + what + "\",\"" |
| 929 |
+ how + "\",\"" + ssize + "\",\"" + sweight + "\",\"" + cat + "\",\"" + stringgroup + |
| 930 |
"\",\"" |
| 931 |
+ stringcode + "\",\"" + mimetype + "\",\"" + clienthost + "\",\"" + |
| 932 |
groupname + "\",\"" |
| 933 |
+ useragent + "\",\"" + params + "\",\"" + o.log.logid_1 + "\",\"" + o.log.logid_2 + "\",\"" + |
| 934 |
postdata + "\""; |
| 935 |
break; |
| 936 |
case 1: |
| 937 |
builtline = when + " " + who + " " + from + " " + where + " " + what + " " |
| 938 |
+ how + " " + ssize + " " + sweight + " " + cat + " " + stringgroup + " " |
| 939 |
+ stringcode + " " + mimetype + " " + clienthost + " " + groupname + " " |
| 940 |
+ useragent + " " + params + " " + o.log.logid_1 + " " + o.log.logid_2 + " " + postdata; |
| 941 |
break; |
| 942 |
case 5: |
| 943 |
case 6: |
| 944 |
case 7: |
| 945 |
case 8: |
| 946 |
default: |
| 947 |
std::string duration; |
| 948 |
long durationsecs, durationusecs; |
| 949 |
durationsecs = (endtv_sec - tv_sec); |
| 950 |
durationusecs = endtv_usec - tv_usec; |
| 951 |
durationusecs = (durationusecs / 1000) + durationsecs * 1000; |
| 952 |
String temp((int) durationusecs); |
| 953 |
duration = temp; |
| 954 |
|
| 955 |
builtline = utime + "\t" |
| 956 |
+ server + "\t" |
| 957 |
+ who + "\t"; |
| 958 |
if (o.log.log_client_host_and_ip) { |
| 959 |
builtline += from + "\t"; |
| 960 |
builtline += clienthost + "\t"; |
| 961 |
} else { |
| 962 |
if (clienthost.length() > 2) |
| 963 |
builtline += clienthost + "\t"; |
| 964 |
else |
| 965 |
builtline += from + "\t"; |
| 966 |
} |
| 967 |
builtline += where + "\t" |
| 968 |
+ how + "\t" |
| 969 |
+ stringcode + "\t" |
| 970 |
+ ssize + "\t" |
| 971 |
+ mimetype + "\t" |
| 972 |
+ (o.log.log_user_agent ? useragent : blank_str) + "\t" |
| 973 |
+ blank_str + "\t" // squid result code |
| 974 |
+ duration + "\t" |
| 975 |
+ blank_str + "\t" // squid peer code |
| 976 |
+ message_no + "\t" // dg message no |
| 977 |
+ what + "\t" |
| 978 |
+ sweight + "\t" |
| 979 |
+ cat + "\t" |
| 980 |
+ groupname + "\t" |
| 981 |
+ stringgroup; |
| 982 |
} |
| 983 |
if (o.log.log_file_format > 6) { |
| 984 |
builtline += "\t"; |
| 985 |
builtline += searchterms; |
| 986 |
builtline += "\t"; |
| 987 |
builtline += flags; |
| 988 |
} |
| 989 |
|
| 990 |
// Send to Log |
| 991 |
DEBUG_trace("Now sending to Log"); |
| 992 |
if (is_RQlog) { |
| 993 |
E2LOGGER_requestlog(builtline); |
| 994 |
} else { |
| 995 |
if(do_access_log) { |
| 996 |
E2LOGGER_accesslog(builtline); |
| 997 |
} |
| 998 |
if(do_alert_log) { |
| 999 |
E2LOGGER_alertlog(builtline); |
| 1000 |
} |
| 1001 |
E2LOGGER_responselog(builtline); // Will only work if responselog is enabled. |
| 1002 |
} |
| 1003 |
|
| 1004 |
|
| 1005 |
#ifdef ENABLE_EMAIL |
| 1006 |
// do the notification work here, but fork for speed |
| 1007 |
if (ldl->fg[filtergroup]->use_smtp == true) { |
| 1008 |
|
| 1009 |
// run through the gambit to find out of we're sending notification |
| 1010 |
// because if we're not.. then fork()ing is a waste of time. |
| 1011 |
|
| 1012 |
// virus |
| 1013 |
if ((wasscanned && wasinfected) && (ldl->fg[filtergroup]->notifyav)) { |
| 1014 |
// Use a double fork to ensure child processes are reaped adequately. |
| 1015 |
pid_t smtppid; |
| 1016 |
if ((smtppid = fork()) != 0) { |
| 1017 |
// Parent immediately waits for first child |
| 1018 |
waitpid(smtppid, NULL, 0); |
| 1019 |
} else { |
| 1020 |
// First child forks off the *real* process, but immediately exits itself |
| 1021 |
if (fork() == 0) { |
| 1022 |
// Second child - do stuff |
| 1023 |
setsid(); |
| 1024 |
FILE *mail = popen(o.mailer.c_str(), "w"); |
| 1025 |
if (mail == NULL) { |
| 1026 |
E2LOGGER_error("Unable to contact defined mailer."); |
| 1027 |
} else { |
| 1028 |
fprintf(mail, "To: %s\n", ldl->fg[filtergroup]->avadmin.c_str()); |
| 1029 |
fprintf(mail, "From: %s\n", ldl->fg[filtergroup]->mailfrom.c_str()); |
| 1030 |
fprintf(mail, "Subject: %s\n", ldl->fg[filtergroup]->avsubject.c_str()); |
| 1031 |
fprintf(mail, "A virus was detected by e2guardian.\n\n"); |
| 1032 |
fprintf(mail, "%-10s%s\n", "Data/Time:", when.c_str()); |
| 1033 |
if (who != blank_str) |
| 1034 |
fprintf(mail, "%-10s%s\n", "User:", who.c_str()); |
| 1035 |
fprintf(mail, "%-10s%s (%s)\n", "From:", from.c_str(), ((clienthost.length() > 0) ? clienthost.c_str() : blank_str.c_str())); |
| 1036 |
fprintf(mail, "%-10s%s\n", "Where:", where.c_str()); |
| 1037 |
// specifically, the virus name comes after message 1100 ("Virus or bad content detected.") |
| 1038 |
String swhat(what); |
| 1039 |
fprintf(mail, "%-10s%s\n", "Why:", swhat.after(o.language_list.getTranslation(1100).c_str()).toCharArray()); |
| 1040 |
fprintf(mail, "%-10s%s\n", "Method:", how.c_str()); |
| 1041 |
fprintf(mail, "%-10s%s\n", "Size:", ssize.c_str()); |
| 1042 |
fprintf(mail, "%-10s%s\n", "Weight:", sweight.c_str()); |
| 1043 |
if (cat.c_str() != NULL) |
| 1044 |
fprintf(mail, "%-10s%s\n", "Category:", cat.c_str()); |
| 1045 |
fprintf(mail, "%-10s%s\n", "Mime type:", mimetype.c_str()); |
| 1046 |
fprintf(mail, "%-10s%s\n", "Group:", ldl->fg[filtergroup]->name.c_str()); |
| 1047 |
fprintf(mail, "%-10s%s\n", "HTTP resp:", stringcode.c_str()); |
| 1048 |
|
| 1049 |
pclose(mail); |
| 1050 |
} |
| 1051 |
// Second child exits |
| 1052 |
_exit(0); |
| 1053 |
} |
| 1054 |
// First child exits |
| 1055 |
_exit(0); |
| 1056 |
} |
| 1057 |
} |
| 1058 |
|
| 1059 |
// naughty OR virus |
| 1060 |
else if ((isnaughty || (wasscanned && wasinfected)) && (ldl->fg[filtergroup]->notifycontent)) { |
| 1061 |
byuser = ldl->fg[filtergroup]->byuser; |
| 1062 |
|
| 1063 |
// if no violations so far by this user/group, |
| 1064 |
// reset threshold counters |
| 1065 |
if (byuser) { |
| 1066 |
if (!violation_map[who]) { |
| 1067 |
// set the time of the first violation |
| 1068 |
timestamp_map[who] = time(0); |
| 1069 |
vbody_map[who] = ""; |
| 1070 |
} |
| 1071 |
} else if (!ldl->fg[filtergroup]->current_violations) { |
| 1072 |
// set the time of the first violation |
| 1073 |
ldl->fg[filtergroup]->threshold_stamp = time(0); |
| 1074 |
ldl->fg[filtergroup]->violationbody = ""; |
| 1075 |
} |
| 1076 |
|
| 1077 |
// increase per-user or per-group violation count |
| 1078 |
if (byuser) |
| 1079 |
violation_map[who]++; |
| 1080 |
else |
| 1081 |
ldl->fg[filtergroup]->current_violations++; |
| 1082 |
|
| 1083 |
// construct email report |
| 1084 |
char *vbody_temp = new char[8192]; |
| 1085 |
sprintf(vbody_temp, "%-10s%s\n", "Data/Time:", when.c_str()); |
| 1086 |
vbody += vbody_temp; |
| 1087 |
|
| 1088 |
if ((!byuser) && (who != blank_str)) { |
| 1089 |
sprintf(vbody_temp, "%-10s%s\n", "User:", who.c_str()); |
| 1090 |
vbody += vbody_temp; |
| 1091 |
} |
| 1092 |
sprintf(vbody_temp, "%-10s%s (%s)\n", "From:", from.c_str(), ((clienthost.length() > 0) ? clienthost.c_str() : blank_str.c_str())); |
| 1093 |
vbody += vbody_temp; |
| 1094 |
sprintf(vbody_temp, "%-10s%s\n", "Where:", where.c_str()); |
| 1095 |
vbody += vbody_temp; |
| 1096 |
sprintf(vbody_temp, "%-10s%s\n", "Why:", what.c_str()); |
| 1097 |
vbody += vbody_temp; |
| 1098 |
sprintf(vbody_temp, "%-10s%s\n", "Method:", how.c_str()); |
| 1099 |
vbody += vbody_temp; |
| 1100 |
sprintf(vbody_temp, "%-10s%s\n", "Size:", ssize.c_str()); |
| 1101 |
vbody += vbody_temp; |
| 1102 |
sprintf(vbody_temp, "%-10s%s\n", "Weight:", sweight.c_str()); |
| 1103 |
vbody += vbody_temp; |
| 1104 |
if (cat.c_str() != NULL) { |
| 1105 |
sprintf(vbody_temp, "%-10s%s\n", "Category:", cat.c_str()); |
| 1106 |
vbody += vbody_temp; |
| 1107 |
} |
| 1108 |
sprintf(vbody_temp, "%-10s%s\n", "Mime type:", mimetype.c_str()); |
| 1109 |
vbody += vbody_temp; |
| 1110 |
sprintf(vbody_temp, "%-10s%s\n", "Group:", ldl->fg[filtergroup]->name.c_str()); |
| 1111 |
vbody += vbody_temp; |
| 1112 |
sprintf(vbody_temp, "%-10s%s\n\n", "HTTP resp:", stringcode.c_str()); |
| 1113 |
vbody += vbody_temp; |
| 1114 |
delete[] vbody_temp; |
| 1115 |
|
| 1116 |
// store the report with the group/user |
| 1117 |
if (byuser) { |
| 1118 |
vbody_map[who] += vbody; |
| 1119 |
curv_tmp = violation_map[who]; |
| 1120 |
stamp_tmp = timestamp_map[who]; |
| 1121 |
} else { |
| 1122 |
ldl->fg[filtergroup]->violationbody += vbody; |
| 1123 |
curv_tmp = ldl->fg[filtergroup]->current_violations; |
| 1124 |
stamp_tmp = ldl->fg[filtergroup]->threshold_stamp; |
| 1125 |
} |
| 1126 |
|
| 1127 |
// if threshold exceeded, send mail |
| 1128 |
if (curv_tmp >= ldl->fg[filtergroup]->violations) { |
| 1129 |
if ((ldl->fg[filtergroup]->threshold == 0) || ((time(0) - stamp_tmp) <= ldl->fg[filtergroup]->threshold)) { |
| 1130 |
// Use a double fork to ensure child processes are reaped adequately. |
| 1131 |
pid_t smtppid; |
| 1132 |
if ((smtppid = fork()) != 0) { |
| 1133 |
// Parent immediately waits for first child |
| 1134 |
waitpid(smtppid, NULL, 0); |
| 1135 |
} else { |
| 1136 |
// First child forks off the *real* process, but immediately exits itself |
| 1137 |
if (fork() == 0) { |
| 1138 |
// Second child - do stuff |
| 1139 |
setsid(); |
| 1140 |
FILE *mail = popen(o.mailer.c_str(), "w"); |
| 1141 |
if (mail == NULL) { |
| 1142 |
E2LOGGER_error("Unable to contact defined mailer."); |
| 1143 |
} else { |
| 1144 |
fprintf(mail, "To: %s\n", ldl->fg[filtergroup]->contentadmin.c_str()); |
| 1145 |
fprintf(mail, "From: %s\n", ldl->fg[filtergroup]->mailfrom.c_str()); |
| 1146 |
|
| 1147 |
if (byuser) |
| 1148 |
fprintf(mail, "Subject: %s (%s)\n", ldl->fg[filtergroup]->contentsubject.c_str(), who.c_str()); |
| 1149 |
else |
| 1150 |
fprintf(mail, "Subject: %s\n", ldl->fg[filtergroup]->contentsubject.c_str()); |
| 1151 |
|
| 1152 |
fprintf(mail, "%i violation%s ha%s occurred within %i seconds.\n", |
| 1153 |
curv_tmp, |
| 1154 |
(curv_tmp == 1) ? "" : "s", |
| 1155 |
(curv_tmp == 1) ? "s" : "ve", |
| 1156 |
ldl->fg[filtergroup]->threshold); |
| 1157 |
|
| 1158 |
fprintf(mail, "%s\n\n", "This exceeds the notification threshold."); |
| 1159 |
if (byuser) |
| 1160 |
fprintf(mail, "%s", vbody_map[who].c_str()); |
| 1161 |
else |
| 1162 |
fprintf(mail, "%s", ldl->fg[filtergroup]->violationbody.c_str()); |
| 1163 |
pclose(mail); |
| 1164 |
} |
| 1165 |
// Second child exits |
| 1166 |
_exit(0); |
| 1167 |
} |
| 1168 |
// First child exits |
| 1169 |
_exit(0); |
| 1170 |
} |
| 1171 |
} |
| 1172 |
if (byuser) |
| 1173 |
violation_map[who] = 0; |
| 1174 |
else |
| 1175 |
ldl->fg[filtergroup]->current_violations = 0; |
| 1176 |
} |
| 1177 |
} // end naughty OR virus |
| 1178 |
} // end usesmtp |
| 1179 |
#endif |
| 1180 |
|
| 1181 |
continue; // go back to listening |
| 1182 |
} |
| 1183 |
if (!e2logger_ttg) |
| 1184 |
DEBUG_debug("log_listener exiting with error"); |
| 1185 |
|
| 1186 |
} catch (...) { |
| 1187 |
E2LOGGER_error("log_listener caught unexpected exception - exiting"); |
| 1188 |
} |
| 1189 |
if (!e2logger_ttg) { |
| 1190 |
E2LOGGER_error("log_listener exiting with error"); |
| 1191 |
} else if (o.conn.logconerror) { |
| 1192 |
E2LOGGER_error("log_listener exiting"); |
| 1193 |
} |
| 1194 |
|
| 1195 |
return; // It is only possible to reach here with an error |
| 1196 |
} |
| 1197 |
|
| 1198 |
void accept_connections(int index) // thread to listen on a single listening socket |
| 1199 |
{ |
| 1200 |
try { |
| 1201 |
unsigned int ct_type = serversockets.getType(index); |
| 1202 |
int errorcount = 0; |
| 1203 |
thread_id = "listen_"; |
| 1204 |
thread_id += std::to_string(index); |
| 1205 |
thread_id += "_"; |
| 1206 |
switch (ct_type) { |
| 1207 |
case CT_PROXY: |
| 1208 |
thread_id += "proxy: "; |
| 1209 |
break; |
| 1210 |
case CT_PROXY_TLS: |
| 1211 |
thread_id += "tls_proxy: "; |
| 1212 |
break; |
| 1213 |
case CT_ICAP: |
| 1214 |
thread_id += "icap: "; |
| 1215 |
break; |
| 1216 |
case CT_THTTPS: |
| 1217 |
thread_id += "thttps: "; |
| 1218 |
break; |
| 1219 |
|
| 1220 |
} |
| 1221 |
thread_id += std::to_string(ct_type); |
| 1222 |
thread_id += ": "; |
| 1223 |
while ((errorcount < 30) && !ttg) { |
| 1224 |
Socket *peersock = serversockets[index]->accept(); |
| 1225 |
int err = serversockets[index]->getErrno(); |
| 1226 |
if (err == 0 && peersock != NULL && peersock->getFD() > -1) { |
| 1227 |
if (ttg) { |
| 1228 |
delete peersock; |
| 1229 |
break; |
| 1230 |
} |
| 1231 |
DEBUG_debug("got connection from accept"); |
| 1232 |
|
| 1233 |
if (peersock->getFD() > dstat.maxusedfd) dstat.maxusedfd = peersock->getFD(); |
| 1234 |
errorcount = 0; |
| 1235 |
LQ_rec rec; |
| 1236 |
rec.sock = peersock; |
| 1237 |
rec.ct_type = ct_type; |
| 1238 |
o.http_worker_Q.push(rec); |
| 1239 |
|
| 1240 |
DEBUG_debug("pushed connection to http_worker_Q"); |
| 1241 |
} else { |
| 1242 |
if (ttg) { |
| 1243 |
if (peersock != nullptr) delete peersock; |
| 1244 |
break; |
| 1245 |
} |
| 1246 |
E2LOGGER_error("Error on accept: errorcount ", String(errorcount), " errno: ", String(err)); |
| 1247 |
|
| 1248 |
++errorcount; |
| 1249 |
std::this_thread::sleep_for(std::chrono::milliseconds(50)); |
| 1250 |
} |
| 1251 |
}; |
| 1252 |
if (!ttg) |
| 1253 |
E2LOGGER_error("Error count on accept exceeds 30"); |
| 1254 |
serversockets[index]->close(); |
| 1255 |
} catch (...) { |
| 1256 |
E2LOGGER_error("listener thread caught unexpected exception exiting"); |
| 1257 |
} |
| 1258 |
if (o.conn.logconerror) { |
| 1259 |
E2LOGGER_info("listener thread exiting"); |
| 1260 |
} |
| 1261 |
} |
| 1262 |
|
| 1263 |
// * |
| 1264 |
// * |
| 1265 |
// * end logger, IP list and URL cache code |
| 1266 |
// * |
| 1267 |
// * |
| 1268 |
|
| 1269 |
// Does lots and lots of things - creates url cache & logger threads, creates child threads for connection handling, does tidying up on exit |
| 1270 |
// also handles the various signalling options e2g supports (reload config, flush cache, kill all processes etc.) |
| 1271 |
int fc_controlit() // |
| 1272 |
{ |
| 1273 |
int rc; |
| 1274 |
bool is_starting = true; |
| 1275 |
ttg = false; |
| 1276 |
e2logger_ttg = false; |
| 1277 |
reloadconfig = false; |
| 1278 |
gentlereload = false; |
| 1279 |
reload_cnt = 0; |
| 1280 |
rotate_request = false; |
| 1281 |
rotate_access = false; |
| 1282 |
rotate_dstat = false; |
| 1283 |
|
| 1284 |
o.lm.garbageCollect(); |
| 1285 |
thread_id = "master: "; |
| 1286 |
|
| 1287 |
// allocate & create our server sockets |
| 1288 |
if (o.net.filter_ip.size() > 0) { |
| 1289 |
serversocketcount = o.net.filter_ip.size() * o.net.filter_ports.size(); |
| 1290 |
if (!o.net.TLS_filter_ports.empty()) { |
| 1291 |
serversocketcount += (o.net.filter_ip.size() * o.net.TLS_filter_ports.size()); |
| 1292 |
} |
| 1293 |
} else { |
| 1294 |
serversocketcount = o.net.filter_ports.size() + o.net.TLS_filter_ports.size(); |
| 1295 |
} |
| 1296 |
|
| 1297 |
int serversocktopproxy = serversocketcount; |
| 1298 |
|
| 1299 |
if (o.net.transparenthttps_port > 0) |
| 1300 |
++serversocketcount; |
| 1301 |
if (o.net.icap_port > 0) |
| 1302 |
++serversocketcount; |
| 1303 |
|
| 1304 |
serversockets.reset(serversocketcount); |
| 1305 |
int *serversockfds = serversockets.getFDAll(); |
| 1306 |
//std::thread *listen_treads[serversocketcount]; |
| 1307 |
for (int i = 0; i < serversocketcount; i++) { |
| 1308 |
// if the socket fd is not +ve then the socket creation failed |
| 1309 |
if (serversockfds[i] < 0) { |
| 1310 |
E2LOGGER_error("Error creating server socket ", String(i)); |
| 1311 |
delete[] serversockfds; |
| 1312 |
return 1; |
| 1313 |
} |
| 1314 |
} |
| 1315 |
|
| 1316 |
DEBUG_trace("seteuiding for low port binding/pidfile creation"); |
| 1317 |
if (!o.proc.become_root_user()) { |
| 1318 |
E2LOGGER_error("Unable to seteuid() to bind filter port."); |
| 1319 |
delete[] serversockfds; |
| 1320 |
return 1; |
| 1321 |
} |
| 1322 |
|
| 1323 |
// we have to open/create as root before drop privs |
| 1324 |
int pidfilefd = sysv_openpidfile(o.proc.pid_filename); |
| 1325 |
if (pidfilefd < 0) { |
| 1326 |
E2LOGGER_error("Error creating/opening pid file."); |
| 1327 |
delete[] serversockfds; |
| 1328 |
return 1; |
| 1329 |
} |
| 1330 |
|
| 1331 |
int ss_index = 0; |
| 1332 |
// we expect to find a valid filter ip 0 specified in conf if multiple IPs are in use. |
| 1333 |
if (o.net.filter_ip[0].length() > 6) { |
| 1334 |
if (serversockets.bindAll(o.net.filter_ip, o.net.filter_ports,ss_index,CT_PROXY)) { |
| 1335 |
E2LOGGER_error("Error binding HTTP proxy server socket (is something else running on the filter port and ip?"); |
| 1336 |
close(pidfilefd); |
| 1337 |
delete[] serversockfds; |
| 1338 |
return 1; |
| 1339 |
} |
| 1340 |
if (!o.net.TLS_filter_ports.empty()) { |
| 1341 |
if (serversockets.bindAll(o.net.filter_ip, o.net.TLS_filter_ports, ss_index, CT_PROXY_TLS)) { |
| 1342 |
E2LOGGER_error( |
| 1343 |
"Error binding TLS proxy server socket (is something else running on the filter port and ip?)"); |
| 1344 |
close(pidfilefd); |
| 1345 |
delete[] serversockfds; |
| 1346 |
return 1; |
| 1347 |
} |
| 1348 |
} |
| 1349 |
} else { |
| 1350 |
// listen/bind to a port (or ports) on any interface |
| 1351 |
if (serversockets.bindSingleM(o.net.filter_ports, ss_index, CT_PROXY)) { |
| 1352 |
E2LOGGER_error("Error binding HTTP proxy server sockets: (", strerror(errno), ")"); |
| 1353 |
close(pidfilefd); |
| 1354 |
delete[] serversockfds; |
| 1355 |
return 1; |
| 1356 |
} |
| 1357 |
if (serversockets.bindSingleM(o.net.TLS_filter_ports, ss_index, CT_PROXY_TLS)) { |
| 1358 |
E2LOGGER_error("Error binding TLS proxy server sockets: (", strerror(errno), ")"); |
| 1359 |
close(pidfilefd); |
| 1360 |
delete[] serversockfds; |
| 1361 |
return 1; |
| 1362 |
} |
| 1363 |
} |
| 1364 |
|
| 1365 |
if (o.net.transparenthttps_port > 0) { |
| 1366 |
if (serversockets.bindSingle(serversocktopproxy++, o.net.transparenthttps_port, CT_THTTPS)) { |
| 1367 |
E2LOGGER_error("Error binding server thttps socket: (", strerror(errno), ")"); |
| 1368 |
close(pidfilefd); |
| 1369 |
delete[] serversockfds; |
| 1370 |
return 1; |
| 1371 |
} |
| 1372 |
}; |
| 1373 |
|
| 1374 |
if (o.net.icap_port > 0) { |
| 1375 |
if (serversockets.bindSingle(serversocktopproxy, o.net.icap_port, CT_ICAP)) { |
| 1376 |
E2LOGGER_error("Error binding server icap socket: (", strerror(errno), ")"); |
| 1377 |
close(pidfilefd); |
| 1378 |
delete[] serversockfds; |
| 1379 |
return 1; |
| 1380 |
} |
| 1381 |
}; |
| 1382 |
|
| 1383 |
// Made unconditional for same reasons as above |
| 1384 |
//if (needdrop) |
| 1385 |
#ifdef HAVE_SETREUID |
| 1386 |
rc = setreuid((uid_t)-1, o.proc.proxy_user); |
| 1387 |
#else |
| 1388 |
rc = seteuid(o.proc.proxy_user); // become low priv again |
| 1389 |
#endif |
| 1390 |
if (rc == -1) { |
| 1391 |
E2LOGGER_error("%sUnable to re-seteuid()"); |
| 1392 |
close(pidfilefd); |
| 1393 |
delete[] serversockfds; |
| 1394 |
return 1; // seteuid failed for some reason so exit with error |
| 1395 |
} |
| 1396 |
|
| 1397 |
if (serversockets.listenAll(256)) { // set it to listen mode with a kernel |
| 1398 |
// queue of 256 backlog connections |
| 1399 |
E2LOGGER_error("Error listening to server socket"); |
| 1400 |
close(pidfilefd); |
| 1401 |
delete[] serversockfds; |
| 1402 |
return 1; |
| 1403 |
} |
| 1404 |
|
| 1405 |
#ifndef DEBUG_LOW // remain in foreground when in low debug mode |
| 1406 |
if (!(is_daemonised||o.proc.no_daemon)) { |
| 1407 |
if (!daemonise(pidfilefd)) { |
| 1408 |
// detached daemon |
| 1409 |
E2LOGGER_error("Error daemonising"); |
| 1410 |
close(pidfilefd); |
| 1411 |
delete[] serversockfds; |
| 1412 |
return 1; |
| 1413 |
} |
| 1414 |
} |
| 1415 |
#endif |
| 1416 |
|
| 1417 |
//init open ssl |
| 1418 |
SSL_load_error_strings(); |
| 1419 |
OpenSSL_add_all_algorithms(); |
| 1420 |
OpenSSL_add_all_digests(); |
| 1421 |
if (o.cert.use_openssl_conf) { |
| 1422 |
if (o.cert.have_openssl_conf) { |
| 1423 |
if (CONF_modules_load_file(o.cert.openssl_conf_path.c_str(), nullptr, 0) != 1) { |
| 1424 |
E2LOGGER_error("Error reading openssl config file ", o.cert.openssl_conf_path.c_str()); |
| 1425 |
return false; |
| 1426 |
} |
| 1427 |
} else { |
| 1428 |
if (CONF_modules_load_file(nullptr, nullptr, 0) != 1) { |
| 1429 |
E2LOGGER_error("Error reading default openssl config files"); |
| 1430 |
return false; |
| 1431 |
} |
| 1432 |
} |
| 1433 |
} |
| 1434 |
SSL_library_init(); |
| 1435 |
|
| 1436 |
// only done now if not daemonised as pidfile must be written from parent when forking - required so that systemd will pick up master pid from pidfile |
| 1437 |
if (!is_daemonised) { |
| 1438 |
rc = sysv_writepidfile(pidfilefd, 0); // also closes the fd |
| 1439 |
if (rc != 0) { |
| 1440 |
E2LOGGER_error("Error writing to the e2guardian.pid file: ", strerror(errno)); |
| 1441 |
delete[] serversockfds; |
| 1442 |
return false; |
| 1443 |
} |
| 1444 |
} |
| 1445 |
|
| 1446 |
// We are now a daemon so all errors need to go in the syslog, rather |
| 1447 |
// than being reported on screen as we've detached from the console and |
| 1448 |
// trying to write to stdout will not be nice. |
| 1449 |
|
| 1450 |
g_is_starting = false; |
| 1451 |
|
| 1452 |
struct sigaction sa; |
| 1453 |
memset(&sa, 0, sizeof(sa)); |
| 1454 |
|
| 1455 |
sigset_t signal_set; |
| 1456 |
sigemptyset(&signal_set); |
| 1457 |
sigaddset(&signal_set, SIGHUP); |
| 1458 |
sigaddset(&signal_set, SIGPIPE); |
| 1459 |
sigaddset(&signal_set, SIGTERM); |
| 1460 |
sigaddset(&signal_set, SIGUSR1); |
| 1461 |
|
| 1462 |
#ifdef __OpenBSD__ |
| 1463 |
// OpenBSD does not support posix sig_timed_wait, so have to use timer and SIGALRM |
| 1464 |
// set up timer for main loop |
| 1465 |
struct itimerval timeout; |
| 1466 |
timeout.it_interval.tv_sec = 0; |
| 1467 |
timeout.it_interval.tv_usec = (suseconds_t) 0; |
| 1468 |
timeout.it_value.tv_usec = (suseconds_t) 0; |
| 1469 |
sigaddset(&signal_set, SIGALRM); |
| 1470 |
#else |
| 1471 |
struct timespec timeout; |
| 1472 |
timeout.tv_sec = 0; |
| 1473 |
timeout.tv_nsec = (long) 0; |
| 1474 |
#endif |
| 1475 |
int stat; |
| 1476 |
stat = pthread_sigmask(SIG_BLOCK, &signal_set, NULL); |
| 1477 |
if (stat != 0) { |
| 1478 |
E2LOGGER_error("Error setting sigmask"); |
| 1479 |
return 1; |
| 1480 |
} |
| 1481 |
|
| 1482 |
DEBUG_trace("sig handlers done"); |
| 1483 |
|
| 1484 |
|
| 1485 |
// Now start creating threads so main thread can just handle signals, list reloads and stats |
| 1486 |
// This removes need for select and/or epoll greatly simplifying the code |
| 1487 |
// Threads are created for logger, a separate thread for each listening port |
| 1488 |
// and an array of worker threads to deal with the work. |
| 1489 |
//if (!o.no_logger) { |
| 1490 |
if (e2logger.isEnabled(LoggerSource::accesslog)) { |
| 1491 |
std::thread log_thread(log_listener, o.log.log_Q, false); |
| 1492 |
log_thread.detach(); |
| 1493 |
DEBUG_trace("log_listener thread created"); |
| 1494 |
} |
| 1495 |
|
| 1496 |
//if(o.log_requests) { |
| 1497 |
if (e2logger.isEnabled(LoggerSource::requestlog)) { |
| 1498 |
std::thread RQlog_thread(log_listener, o.log.RQlog_Q, true); |
| 1499 |
RQlog_thread.detach(); |
| 1500 |
DEBUG_trace("RQlog_listener thread created"); |
| 1501 |
} |
| 1502 |
|
| 1503 |
|
| 1504 |
dystat->busychildren = 0; // to keep count of our children |
| 1505 |
|
| 1506 |
// worker thread generation |
| 1507 |
std::vector <std::thread> http_wt; |
| 1508 |
http_wt.reserve(o.proc.http_workers); |
| 1509 |
|
| 1510 |
int i; |
| 1511 |
for (i = 0; i < o.proc.http_workers; i++) { |
| 1512 |
http_wt.push_back(std::thread(handle_connections, i)); |
| 1513 |
} |
| 1514 |
for (auto &i : http_wt) { |
| 1515 |
i.detach(); |
| 1516 |
} |
| 1517 |
|
| 1518 |
DEBUG_trace("http_worker threads created"); |
| 1519 |
|
| 1520 |
// set listener threads going |
| 1521 |
std::vector <std::thread> listen_threads; |
| 1522 |
listen_threads.reserve(serversocketcount); |
| 1523 |
for (int i = 0; i < serversocketcount; i++) { |
| 1524 |
listen_threads.push_back(std::thread(accept_connections, i)); |
| 1525 |
} |
| 1526 |
for (auto &i : listen_threads) { |
| 1527 |
i.detach(); |
| 1528 |
} |
| 1529 |
|
| 1530 |
DEBUG_trace("listen threads created"); |
| 1531 |
|
| 1532 |
// I am the main thread here onwards. |
| 1533 |
DEBUG_trace("Master thread created threads"); |
| 1534 |
|
| 1535 |
time_t tmaxspare; |
| 1536 |
|
| 1537 |
time(&tmaxspare); |
| 1538 |
|
| 1539 |
failurecount = 0; // as we don't exit on an error with select() |
| 1540 |
// due to the fact that these errors do happen |
| 1541 |
// every so often on a fully working, but busy |
| 1542 |
// system, we just watch for too many errors |
| 1543 |
// consecutivly. |
| 1544 |
|
| 1545 |
is_starting = true; |
| 1546 |
|
| 1547 |
if (reloadconfig) { |
| 1548 |
E2LOGGER_info("Reconfiguring E2guardian: done"); |
| 1549 |
} else { |
| 1550 |
E2LOGGER_info("Started successfully."); |
| 1551 |
dystat->start(true); |
| 1552 |
} |
| 1553 |
reloadconfig = false; |
| 1554 |
|
| 1555 |
if (is_starting) { |
| 1556 |
if (o.monitor.monitor_flag_flag) |
| 1557 |
monitor_flag_set(true); |
| 1558 |
// if (o.monitor_helper_flag) { |
| 1559 |
// tell_monitor(true); |
| 1560 |
// } |
| 1561 |
is_starting = false; |
| 1562 |
} |
| 1563 |
|
| 1564 |
while (failurecount < 30 && !ttg && !reloadconfig) { |
| 1565 |
|
| 1566 |
// loop, essentially, for ever until 30 |
| 1567 |
// consecutive errors in which case something |
| 1568 |
// is badly wrong. |
| 1569 |
// OR, its timetogo - got a sigterm |
| 1570 |
// OR, we need to exit to reread config |
| 1571 |
if (gentlereload) { |
| 1572 |
DEBUG_trace("gentle reload activated"); |
| 1573 |
|
| 1574 |
E2LOGGER_info("Reconfiguring E2guardian: gentle reload starting"); |
| 1575 |
if (o.createLists(++reload_cnt)) { |
| 1576 |
E2LOGGER_info("Reconfiguring E2guardian: gentle reload completed"); |
| 1577 |
} else { |
| 1578 |
reload_cnt--; |
| 1579 |
E2LOGGER_info("%sReconfiguring E2guardian: gentle reload failed"); |
| 1580 |
} |
| 1581 |
|
| 1582 |
gentlereload = false; |
| 1583 |
continue; // OK to continue even if gentle failed - just continue to use previous lists |
| 1584 |
} |
| 1585 |
#ifdef __OpenBSD__ |
| 1586 |
// OpenBSD does not support posix sig_timed_wait, so have to use timer and SIGALRM |
| 1587 |
timeout.it_value.tv_sec = 5; |
| 1588 |
setitimer(ITIMER_REAL, &timeout, NULL); |
| 1589 |
int rsig; |
| 1590 |
rc = sigwait(&signal_set, &rsig); |
| 1591 |
if (rc < 0) { |
| 1592 |
if (errno != EAGAIN) { |
| 1593 |
E2LOGGER_info("Unexpected error from sigtimedwait(): ", String(errno), " ", strerror(errno)); |
| 1594 |
} |
| 1595 |
} else { |
| 1596 |
if (rsig == SIGUSR1) { |
| 1597 |
rotate_access = true; |
| 1598 |
rotate_request = true; |
| 1599 |
rotate_dstat = true; |
| 1600 |
} |
| 1601 |
if (rsig == SIGTERM) |
| 1602 |
ttg = true; |
| 1603 |
if (rsig == SIGHUP) |
| 1604 |
gentlereload = true; |
| 1605 |
if (rsig != SIGALRM) { |
| 1606 |
// unset alarm |
| 1607 |
timeout.it_value.tv_sec = 0; |
| 1608 |
//timer_settime(timerid,0,&timeout, NULL); |
| 1609 |
setitimer(ITIMER_REAL, &timeout, NULL); |
| 1610 |
|
| 1611 |
DEBUG_debug("signal:", String(rc)); |
| 1612 |
if (o.conn.logconerror) { |
| 1613 |
E2LOGGER_info("sigtimedwait() signal recd:", String(rsig) ); |
| 1614 |
} |
| 1615 |
} |
| 1616 |
} |
| 1617 |
#else |
| 1618 |
// other posix compliant platforms |
| 1619 |
timeout.tv_sec = 5; |
| 1620 |
rc = sigtimedwait(&signal_set, NULL, &timeout); |
| 1621 |
if (rc < 0) { |
| 1622 |
if (errno != EAGAIN) { |
| 1623 |
E2LOGGER_info("Unexpected error from sigtimedwait():", String(errno), " ", strerror(errno)); |
| 1624 |
} |
| 1625 |
} else { |
| 1626 |
if (rc == SIGUSR1) { |
| 1627 |
rotate_access = true; |
| 1628 |
rotate_request = true; |
| 1629 |
rotate_dstat = true; |
| 1630 |
} |
| 1631 |
if (rc == SIGTERM) |
| 1632 |
ttg = true; |
| 1633 |
if (rc == SIGHUP) |
| 1634 |
gentlereload = true; |
| 1635 |
|
| 1636 |
DEBUG_debug("signal: ", String(rc)); |
| 1637 |
if (o.conn.logconerror) { |
| 1638 |
E2LOGGER_info("ssigtimedwait() signal recd:", String(rc)); |
| 1639 |
} |
| 1640 |
} |
| 1641 |
#endif // end __OpenBSD__ else |
| 1642 |
|
| 1643 |
int q_size = o.http_worker_Q.size(); |
| 1644 |
DEBUG_debug("busychildren:", String(dystat->busychildren), |
| 1645 |
" worker Q size:", q_size); |
| 1646 |
if (o.dstat.dstat_log_flag) { |
| 1647 |
if (q_size > 10) { |
| 1648 |
E2LOGGER_info("Warning: all ", o.proc.http_workers, " http_worker threads are busy and ", |
| 1649 |
q_size, " connections are waiting in the queue."); |
| 1650 |
} |
| 1651 |
} else { |
| 1652 |
int busy_child = dystat->busychildren; |
| 1653 |
if (busy_child > (o.proc.http_workers - 10)) |
| 1654 |
E2LOGGER_info("Warning system is full : max httpworkers: ", o.proc.http_workers, " Used: ", |
| 1655 |
busy_child); |
| 1656 |
} |
| 1657 |
|
| 1658 |
// if (is_starting) |
| 1659 |
|
| 1660 |
time_t now = time(NULL); |
| 1661 |
|
| 1662 |
|
| 1663 |
if (o.dstat.dstat_log_flag && (now >= dystat->end_int)) |
| 1664 |
dystat->reset(); |
| 1665 |
} |
| 1666 |
|
| 1667 |
|
| 1668 |
// tidy-up |
| 1669 |
|
| 1670 |
sigfillset(&signal_set); |
| 1671 |
pthread_sigmask(SIG_BLOCK, &signal_set, NULL); |
| 1672 |
|
| 1673 |
E2LOGGER_info("Stopping"); |
| 1674 |
|
| 1675 |
if (o.monitor.monitor_flag_flag) |
| 1676 |
monitor_flag_set(false); |
| 1677 |
// if (o.monitor_helper_flag) |
| 1678 |
// tell_monitor(false); // tell monitor that we are not accepting any more connections |
| 1679 |
|
| 1680 |
if (o.conn.logconerror) { |
| 1681 |
E2LOGGER_info("sending null socket to http_workers to stop them"); |
| 1682 |
} |
| 1683 |
Socket *NS = NULL; |
| 1684 |
LQ_rec rec; |
| 1685 |
rec.sock = NS; |
| 1686 |
rec.ct_type = CT_PROXY; |
| 1687 |
for (i = 0; i < o.proc.http_workers; i++) { |
| 1688 |
o.http_worker_Q.push(rec); |
| 1689 |
} |
| 1690 |
// dystat->reset(); // remove this line for production version |
| 1691 |
|
| 1692 |
//std::this_thread::sleep_for(std::chrono::milliseconds(2000)); |
| 1693 |
//E2LOGGER_info("2nd wait complete"); |
| 1694 |
e2logger_ttg = true; |
| 1695 |
std::string nullstr(""); |
| 1696 |
o.log.log_Q->push(nullstr); |
| 1697 |
//if (o.log_requests) { |
| 1698 |
if (e2logger.isEnabled(LoggerSource::requestlog)) { |
| 1699 |
o.log.RQlog_Q->push(nullstr); |
| 1700 |
} |
| 1701 |
|
| 1702 |
if (o.conn.logconerror) { |
| 1703 |
E2LOGGER_info("stopping any remaining connections"); |
| 1704 |
} |
| 1705 |
serversockets.self_connect(); // stop accepting connections |
| 1706 |
if (o.conn.logconerror) { |
| 1707 |
E2LOGGER_info("connections stopped"); |
| 1708 |
} |
| 1709 |
|
| 1710 |
std::this_thread::sleep_for(std::chrono::milliseconds(2000)); |
| 1711 |
|
| 1712 |
if (o.dstat.dstat_log_flag) dystat->close(); |
| 1713 |
|
| 1714 |
delete[] serversockfds; |
| 1715 |
|
| 1716 |
if (o.conn.logconerror) { |
| 1717 |
E2LOGGER_info("Main thread exiting."); |
| 1718 |
} |
| 1719 |
return 0; |
| 1720 |
} |