From f91192c77eec33a27dea7fcd0d451823ef478529 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Thu, 2 Aug 2012 17:10:58 +0300 Subject: [PATCH] Log errors from plugins to server log. --- .../authorization/inetaccess/inetaccess.cpp | 13 ++++++-- .../plugins/capture/cap_nf/cap_nf.cpp | 33 +++++++++++++------ .../capture/divert_freebsd/divert_cap.cpp | 24 ++++++++++---- .../capture/ether_freebsd/ether_cap.cpp | 17 +++++++--- .../plugins/capture/ether_linux/ether_cap.cpp | 20 ++++++----- .../plugins/capture/ipq_linux/ipq_cap.cpp | 2 ++ .../configuration/rpcconfig/rpcconfig.cpp | 7 ++++ .../plugins/configuration/sgconfig/rsconf.cpp | 25 +++++++++----- .../configuration/sgconfig/stgconfig.cpp | 1 + .../stargazer/plugins/other/ping/ping.cpp | 1 + .../stargazer/plugins/other/radius/radius.cpp | 22 ++++++++++--- .../plugins/other/rscript/rscript.cpp | 18 ++++++---- .../stargazer/plugins/other/smux/smux.cpp | 3 ++ stglibs/logger.lib/include/stg/logger.h | 8 ++--- stglibs/logger.lib/logger.cpp | 6 ++-- 15 files changed, 142 insertions(+), 58 deletions(-) diff --git a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp index 89ddd09c..d3c1b960 100644 --- a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp +++ b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp @@ -393,6 +393,7 @@ if (!isRunningRun) { errorStr = "Cannot create thread."; printfd(__FILE__, "Cannot create recv thread\n"); + logger("Cannot create recv thread."); return -1; } } @@ -403,6 +404,7 @@ if (!isRunningRunTimeouter) { errorStr = "Cannot create thread."; printfd(__FILE__, "Cannot create timeouter thread\n"); + logger("Cannot create timeouter thread."); return -1; } } @@ -528,6 +530,7 @@ listenSocket = socket(AF_INET, SOCK_DGRAM, 0); if (listenSocket < 0) { errorStr = "Cannot create socket."; + logger("Cannot create a socket: %s", strerror(errno)); return -1; } @@ -538,6 +541,7 @@ listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0"); if (bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr)) < 0) { errorStr = "AUTH_IA: Bind failed."; + logger("Cannot bind the socket: %s", strerror(errno)); return -1; } @@ -571,6 +575,7 @@ if (dataLen <= 0) // Error if (errno != EINTR) { printfd(__FILE__, "recvfrom res=%d, error: '%s'\n", dataLen, strerror(errno)); + logger("recvfrom error: %s", strerror(errno)); return -1; } return 0; @@ -606,12 +611,14 @@ printfd(__FILE__, "User '%s' FOUND!\n", user->GetLogin().c_str()); if (user->GetProperty().disabled.Get()) { + logger("Cannont authorize '%s', user is disabled.", login); SendError(sip, sport, protoVer, "õÞÅÔÎÁÑ ÚÁÐÉÓØ ÚÁÂÌÏËÉÒÏ×ÁÎÁ"); return 0; } if (user->GetProperty().passive.Get()) { + logger("Cannont authorize '%s', user is passive.", login); SendError(sip, sport, protoVer, "õÞÅÔÎÁÑ ÚÁÐÉÓØ ÚÁÍÏÒÏÖÅÎÁ"); return 0; } @@ -758,7 +765,7 @@ if (it == ip2user.end()) userPtr->GetLogin().c_str(), inet_ntostring(sip).c_str(), login.c_str()); - logger("IP address already in use by user '%s'. IP %s, login: '%s'", + logger("IP address is already in use by user '%s'. IP %s, login: '%s'", userPtr->GetLogin().c_str(), inet_ntostring(sip).c_str(), login.c_str()); @@ -783,7 +790,7 @@ else if (user->GetID() != it->second.user->GetID()) it->second.user->GetLogin().c_str(), inet_ntostring(sip).c_str(), user->GetLogin().c_str()); - logger("IP address already in use by user '%s'. IP %s, login: '%s'", + logger("IP address is already in use by user '%s'. IP %s, login: '%s'", it->second.user->GetLogin().c_str(), inet_ntostring(sip).c_str(), user->GetLogin().c_str()); @@ -823,7 +830,7 @@ if (user->IsAuthorizedBy(this) && user->GetCurrIP() != sip) printfd(__FILE__, "Login %s already in use from ip %s. IP %s\n", login.c_str(), inet_ntostring(user->GetCurrIP()).c_str(), inet_ntostring(sip).c_str()); - logger("Login %s already in use from ip %s. IP %s", + logger("Login '%s' is already in use from ip %s. IP %s", login.c_str(), inet_ntostring(user->GetCurrIP()).c_str(), inet_ntostring(sip).c_str()); diff --git a/projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp b/projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp index 8c84885f..37af9720 100644 --- a/projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp +++ b/projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp @@ -118,6 +118,7 @@ if (portU > 0) runningUDP = false; CloseUDP(); errorStr = "Cannot create UDP thread"; + logger("Cannot create UDP thread."); printfd(__FILE__, "Error: Cannot create UDP thread\n"); return -1; } @@ -133,6 +134,7 @@ if (portT > 0) { runningTCP = false; CloseTCP(); + logger("Cannot create TCP thread."); errorStr = "Cannot create TCP thread"; printfd(__FILE__, "Error: Cannot create TCP thread\n"); return -1; @@ -161,10 +163,12 @@ if (portU && !stoppedUDP) if (pthread_kill(tidUDP, SIGUSR1)) { errorStr = "Error sending signal to UDP thread"; + logger("Error sending sugnal to UDP thread."); printfd(__FILE__, "Error: Error sending signal to UDP thread\n"); return -1; } printfd(__FILE__, "UDP thread NOT stopped\n"); + logger("Cannot stop UDP thread."); } } if (portT && !stoppedTCP) @@ -184,10 +188,12 @@ if (portT && !stoppedTCP) if (pthread_kill(tidTCP, SIGUSR1)) { errorStr = "Error sending signal to TCP thread"; + logger("Error sending signal to TCP thread."); printfd(__FILE__, "Error: Error sending signal to TCP thread\n"); return -1; } printfd(__FILE__, "TCP thread NOT stopped\n"); + logger("Cannot stop TCP thread."); } } return 0; @@ -200,6 +206,7 @@ sockUDP = socket(PF_INET, SOCK_DGRAM, 0); if (sockUDP <= 0) { errorStr = "Error opening UDP socket"; + logger("Cannot create UDP socket: %s", strerror(errno)); printfd(__FILE__, "Error: Error opening UDP socket\n"); return true; } @@ -209,6 +216,7 @@ sin.sin_addr.s_addr = inet_addr("0.0.0.0"); if (bind(sockUDP, (struct sockaddr *)&sin, sizeof(sin))) { errorStr = "Error binding UDP socket"; + logger("Cannot bind UDP socket: %s", strerror(errno)); printfd(__FILE__, "Error: Error binding UDP socket\n"); return true; } @@ -222,6 +230,7 @@ sockTCP = socket(PF_INET, SOCK_STREAM, 0); if (sockTCP <= 0) { errorStr = "Error opening TCP socket"; + logger("Cannot create TCP socket: %s", strerror(errno)); printfd(__FILE__, "Error: Error opening TCP socket\n"); return true; } @@ -231,12 +240,14 @@ sin.sin_addr.s_addr = inet_addr("0.0.0.0"); if (bind(sockTCP, (struct sockaddr *)&sin, sizeof(sin))) { errorStr = "Error binding TCP socket"; + logger("Cannot bind TCP socket: %s", strerror(errno)); printfd(__FILE__, "Error: Error binding TCP socket\n"); return true; } if (listen(sockTCP, 1)) { errorStr = "Error listening on TCP socket"; + logger("Cannot listen on TCP socket: %s", strerror(errno)); printfd(__FILE__, "Error: Error listening TCP socket\n"); return true; } @@ -268,6 +279,12 @@ while (cap->runningUDP) if (!cap->runningUDP) break; + if (res < 0) + { + cap->logger("recvfrom error: %s", strerror(errno)); + continue; + } + if (res == 0) // EOF { continue; @@ -317,11 +334,8 @@ while (cap->runningTCP) if (sd <= 0) { - if (errno != EINTR) - { - cap->errorStr = "Error accepting connection"; - printfd(__FILE__, "Error: Error accepting connection\n"); - } + if (sd < 0) + cap->logger("accept error: %s", strerror(errno)); continue; } @@ -332,6 +346,10 @@ while (cap->runningTCP) } res = recv(sd, buf, BUF_SIZE, MSG_WAITALL); + + if (res < 0) + cap->logger("recv error: %s", strerror(errno)); + close(sd); if (!cap->runningTCP) @@ -346,11 +364,6 @@ while (cap->runningTCP) // Need to check actual data length and wait all data to receive if (res < 24) { - if (errno != EINTR) - { - cap->errorStr = "Invalid data received"; - printfd(__FILE__, "Error: Invalid data received through TCP\n"); - } continue; } diff --git a/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp b/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp index c50b4974..6bbfd7e8 100644 --- a/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp +++ b/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp @@ -105,14 +105,15 @@ if (DivertCapOpen() < 0) nonstop = true; -if (pthread_create(&thread, NULL, Run, this) == 0) +if (pthread_create(&thread, NULL, Run, this)) { - return 0; + errorStr = "Cannot create thread."; + logger("Cannot create thread."); + printfd(__FILE__, "Cannot create thread\n"); + return -1; } -errorStr = "Cannot create thread."; -printfd(__FILE__, "Cannot create thread\n"); -return -1; +return 0; } //----------------------------------------------------------------------------- int DIVERT_CAP::Stop() @@ -141,6 +142,7 @@ if (isRunning) if (pthread_kill(thread, SIGINT)) { errorStr = "Cannot kill thread."; + logger("Cannot send signal to thread."); printfd(__FILE__, "Cannot kill thread\n"); return -1; } @@ -198,6 +200,7 @@ cddiv.sock = socket(PF_INET, SOCK_RAW, IPPROTO_DIVERT); if (cddiv.sock < 0) { errorStr = "Create divert socket error."; + logger("Cannot create a socket: %s", strerror(errno)); printfd(__FILE__, "Cannot create divert socket\n"); return -1; } @@ -215,6 +218,7 @@ ret = bind(cddiv.sock, (struct sockaddr *)&divAddr, sizeof(divAddr)); if (ret < 0) { errorStr = "Bind divert socket error."; + logger("Cannot bind the scoket: %s", strerror(errno)); printfd(__FILE__, "Cannot bind divert socket\n"); return -1; } @@ -253,7 +257,15 @@ if ((bytes = recvfrom (cddiv.sock, buf, BUFF_LEN, *iface = cddiv.iface; if (!disableForwarding) - sendto(cddiv.sock, buf, bytes, 0, (struct sockaddr*)&divertaddr, divertaddrSize); + { + if (sendto(cddiv.sock, buf, bytes, 0, (struct sockaddr*)&divertaddr, divertaddrSize) < 0) + logger("sendto error: %s", strerror(errno)); + } + } +else + { + if (bytes < 0) + logger("recvfrom error: %s", strerror(errno)); } return 0; diff --git a/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp b/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp index 4e9fa0ff..c217e22f 100644 --- a/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp +++ b/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp @@ -154,14 +154,15 @@ if (BPFCapOpen() < 0) nonstop = true; -if (pthread_create(&thread, NULL, Run, this) == 0) +if (pthread_create(&thread, NULL, Run, this)) { - return 0; + errorStr = "Cannot create thread."; + logger("Cannot create thread."); + printfd(__FILE__, "Cannot create thread\n"); + return -1; } -errorStr = "Cannot create thread."; -printfd(__FILE__, "Cannot create thread\n"); -return -1; +return 0; } //----------------------------------------------------------------------------- int BPF_CAP::Stop() @@ -191,6 +192,7 @@ if (isRunning) if (pthread_kill(thread, SIGINT)) { errorStr = "Cannot kill thread."; + logger("Cannot send signal to thread."); printfd(__FILE__, "Cannot kill thread\n"); return -1; } @@ -272,6 +274,7 @@ do if (bd->fd < 0) { errorStr = "Can't capture packets. Open bpf device for " + bd->iface + " error."; + logger("Cannot open device for interface '%s': %s", bd->iface, strerror(errno)); printfd(__FILE__, "Cannot open BPF device\n"); return -1; } @@ -281,6 +284,7 @@ strncpy(ifr.ifr_name, bd->iface.c_str(), sizeof(ifr.ifr_name)); if (ioctl(bd->fd, BIOCSBLEN, (caddr_t)&l) < 0) { errorStr = bd->iface + " BIOCSBLEN " + std::string(strerror(errno)); + logger("ioctl (BIOCSBLEN) error for interface '%s': %s", db->iface, strerror(errno)); printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str()); return -1; } @@ -288,6 +292,7 @@ if (ioctl(bd->fd, BIOCSBLEN, (caddr_t)&l) < 0) if (ioctl(bd->fd, BIOCSETIF, (caddr_t)&ifr) < 0) { errorStr = bd->iface + " BIOCSETIF " + std::string(strerror(errno)); + logger("ioctl (BIOCSETIF) error for interface '%s': %s", db->iface, strerror(errno)); printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str()); return -1; } @@ -295,6 +300,7 @@ if (ioctl(bd->fd, BIOCSETIF, (caddr_t)&ifr) < 0) if (ioctl(bd->fd, BIOCIMMEDIATE, &im) < 0) { errorStr = bd->iface + " BIOCIMMEDIATE " + std::string(strerror(errno)); + logger("ioctl (BIOCIMMEDIATE) error for interface '%s': %s", db->iface, strerror(errno)); printfd(__FILE__, "ioctl failed: '%s'\n", errorStr.c_str()); return -1; } @@ -336,6 +342,7 @@ if (bd->canRead) bd->r = read(bd->fd, bd->buffer, BUFF_LEN); if (bd->r < 0) { + logger("read error: %s", strerror(errno)); struct timespec ts = {0, 20000000}; nanosleep(&ts, NULL); return -1; diff --git a/projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp b/projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp index f620c802..69761722 100644 --- a/projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp +++ b/projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp @@ -99,12 +99,13 @@ nonstop = true; if (pthread_create(&thread, NULL, Run, this) == 0) { - return 0; + errorStr = "Cannot create thread."; + logger("Cannot create thread."); + printfd(__FILE__, "Cannot create thread\n"); + return -1; } -errorStr = "Cannot create thread."; -printfd(__FILE__, "Cannot create thread\n"); -return -1; +return 0; } //----------------------------------------------------------------------------- int ETHER_CAP::Stop() @@ -126,6 +127,7 @@ if (isRunning) if (pthread_kill(thread, SIGUSR1)) { errorStr = "Cannot kill thread."; + logger("Cannot send signal to thread."); return -1; } for (int i = 0; i < 25 && isRunning; ++i) @@ -136,6 +138,7 @@ if (isRunning) if (isRunning) { errorStr = "ETHER_CAP not stopped."; + logger("Cannot stop thread."); printfd(__FILE__, "Cannot stop thread\n"); return -1; } @@ -197,6 +200,8 @@ return NULL; int ETHER_CAP::EthCapOpen() { capSock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); +if (capSock < 0) + logger("Cannot create socket: %s", strerror(errno)); return capSock; } //----------------------------------------------------------------------------- @@ -220,12 +225,9 @@ addrLen = sizeof(addr); res = recvfrom(capSock, ((char*)buffer) + 2, blen, 0, (struct sockaddr *)&addr, (socklen_t*)&addrLen); -if (-1 == res) +if (res < 0) { - if (errno != EINTR) - { - printfd(__FILE__, "Error on recvfrom: '%s'\n", strerror(errno)); - } + logger("recvfrom error: %s", strerror(errno)); return ENODATA; } diff --git a/projects/stargazer/plugins/capture/ipq_linux/ipq_cap.cpp b/projects/stargazer/plugins/capture/ipq_linux/ipq_cap.cpp index 6cf5b0f9..2dcded07 100644 --- a/projects/stargazer/plugins/capture/ipq_linux/ipq_cap.cpp +++ b/projects/stargazer/plugins/capture/ipq_linux/ipq_cap.cpp @@ -160,6 +160,7 @@ ipq_h = ipq_create_handle(0, PF_INET); if (ipq_h == NULL) { ipq_destroy_handle(ipq_h); + logger("Cannot create IPQ handle."); errorStr = "Cannot create ipq handle!"; return -1; } @@ -167,6 +168,7 @@ int status = ipq_set_mode(ipq_h, IPQ_COPY_PACKET, PAYLOAD_LEN); if (status < 0) { ipq_destroy_handle(ipq_h); + logger("Cannot set IPQ_COPY_PACKET mode."); errorStr = "Cannot set IPQ_COPY_PACKET mode!"; return -1; } diff --git a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp index 682c5268..86072bcf 100644 --- a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp +++ b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp @@ -135,6 +135,7 @@ fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) { errorStr = "Failed to create socket"; + logger("Cannot create a socket: %s", strerror(errno)); printfd(__FILE__, "Failed to create listening socket: %s\n", strerror(errno)); return -1; } @@ -144,6 +145,7 @@ int flag = 1; if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag))) { errorStr = "Setsockopt failed."; + logger("setsockopt error: %s", strerror(errno)); printfd(__FILE__, "Setsockopt failed: %s\n", strerror(errno)); return -1; } @@ -155,6 +157,7 @@ addr.sin_addr.s_addr = inet_addr("0.0.0.0"); if (bind(fd, (struct sockaddr *)&addr, sizeof(addr))) { + logger("Cannot bind the socket: %s", strerror(errno)); errorStr = "Failed to bind socket"; printfd(__FILE__, "Failed to bind listening socket: %s\n", strerror(errno)); return -1; @@ -162,6 +165,7 @@ if (bind(fd, (struct sockaddr *)&addr, sizeof(addr))) if (listen(fd, 10)) { + logger("Cannot listen the socket: %s", strerror(errno)); errorStr = "Failed to listen socket"; printfd(__FILE__, "Failed to listen listening socket: %s\n", strerror(errno)); return -1; @@ -177,6 +181,7 @@ rpcServer = new xmlrpc_c::serverAbyss( if (pthread_create(&tid, NULL, Run, this)) { errorStr = "Failed to create RPC thread"; + logger("Cannot create RPC thread."); printfd(__FILE__, "Failed to crate RPC thread\n"); return -1; } @@ -196,6 +201,7 @@ for (int i = 0; i < 5 && !stopped; ++i) if (!stopped) { running = true; + logger("Cannot stop RPC thread."); printfd(__FILE__, "Failed to stop RPC thread\n"); errorStr = "Failed to stop RPC thread"; return -1; @@ -263,6 +269,7 @@ ADMIN * admin = NULL; if (!admins->Correct(login, password, &admin)) { + logger("Attempt to connect with invalid credentials. Login: %s", login.c_str()); return true; } diff --git a/projects/stargazer/plugins/configuration/sgconfig/rsconf.cpp b/projects/stargazer/plugins/configuration/sgconfig/rsconf.cpp index 5e699848..5b592cbc 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/rsconf.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/rsconf.cpp @@ -74,6 +74,7 @@ listenSocket = socket(PF_INET, SOCK_STREAM, 0); if (listenSocket < 0) { errorStr = "Create NET_CONFIGURATOR socket failed."; + logger("Cannot create a socket: %s", strerror(errno)); return -1; } @@ -86,6 +87,7 @@ int lng = 1; if (0 != setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &lng, 4)) { errorStr = "Setsockopt failed. " + string(strerror(errno)); + logger("setsockopt error: %s", strerror(errno)); return -1; } @@ -94,6 +96,7 @@ res = bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr)); if (res == -1) { errorStr = "Bind admin socket failed"; + logger("Cannot bind the socket: %s", strerror(errno)); return -1; } @@ -101,6 +104,7 @@ res = listen(listenSocket, 0); if (res == -1) { errorStr = "Listen admin socket failed"; + logger("Cannot listen the socket: %s", strerror(errno)); return -1; } @@ -147,16 +151,15 @@ while (nonstop) break; } - if (outerSocket == -1) + if (outerSocket < 0) { + logger("accept error: %s", strerror(errno)); printfd(__FILE__, "accept failed\n"); continue; } adminIP = *(unsigned int*)&(outerAddr.sin_addr); - printfd(__FILE__, "Connection accepted from %s\n", inet_ntostring(outerAddr.sin_addr.s_addr).c_str()); - if (state == confHdr) { if (RecvHdr(outerSocket) < 0) @@ -231,6 +234,7 @@ while (nonstop) { WriteLogAccessFailed(adminIP); } + printfd(__FILE__, "Successfull connection from %s\n", inet_ntostring(outerAddr.sin_addr.s_addr).c_str()); close(outerSocket); } } @@ -252,6 +256,8 @@ while (pos < stgHdrLen) int ret = recv(sock, &buf[pos], stgHdrLen - pos, 0); if (ret <= 0) { + if (ret < 0) + logger("recv error: %s", strerror(errno)); state = confHdr; return -1; } @@ -278,7 +284,7 @@ if (err) { if (send(sock, ERR_HEADER, sizeof(ERR_HEADER) - 1, 0) < 0) { - logger("send ERR_HEADER error in SendHdrAnswer."); + logger("send error: %s", strerror(errno)); return -1; } } @@ -286,7 +292,7 @@ else { if (send(sock, OK_HEADER, sizeof(OK_HEADER) - 1, 0) < 0) { - logger("send OK_HEADER error in SendHdrAnswer."); + logger("send error: %s", strerror(errno)); return -1; } } @@ -313,6 +319,7 @@ while (pos < ADM_LOGIN_LEN) { if (ret <= 0) { // Error in network + logger("recv error: %s", strerror(errno)); state = confHdr; return ENODATA; } @@ -363,6 +370,7 @@ while (pos < ADM_LOGIN_LEN) { // Network error printfd(__FILE__, "recv error: '%s'\n", strerror(errno)); + logger("recv error: %s", strerror(errno)); state = confHdr; return ENODATA; } @@ -408,7 +416,7 @@ if (err) { if (send(sock, ERR_LOGINS, sizeof(ERR_LOGINS) - 1, 0) < 0) { - logger("send ERR_LOGIN error in SendLoginAnswer."); + logger("send error: %s", strerror(errno)); return -1; } } @@ -416,7 +424,7 @@ else { if (send(sock, OK_LOGINS, sizeof(OK_LOGINS) - 1, 0) < 0) { - logger("send OK_LOGINS error in SendLoginSAnswer."); + logger("send error: %s", strerror(errno)); return -1; } } @@ -447,6 +455,7 @@ while (1) if (ret < 0) { // Network error + logger("recv error: %s", strerror(errno)); printfd(__FILE__, "recv error: '%s'\n", strerror(errno)); return -1; } @@ -537,6 +546,6 @@ answerList.push_back(s); //----------------------------------------------------------------------------- void CONFIGPROTO::WriteLogAccessFailed(uint32_t ip) { -logger("Admin's connect failed. IP %s", inet_ntostring(ip).c_str()); +logger("Admin's connection failed. IP %s", inet_ntostring(ip).c_str()); } //----------------------------------------------------------------------------- diff --git a/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp b/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp index 78c45d72..e3199047 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp @@ -104,6 +104,7 @@ if (pthread_create(&thread, NULL, Run, this)) { errorStr = "Cannot create thread."; printfd(__FILE__, "Cannot create thread\n"); + logger("Cannot create thread."); return -1; } errorStr = ""; diff --git a/projects/stargazer/plugins/other/ping/ping.cpp b/projects/stargazer/plugins/other/ping/ping.cpp index 00bb362d..7f47b7cf 100644 --- a/projects/stargazer/plugins/other/ping/ping.cpp +++ b/projects/stargazer/plugins/other/ping/ping.cpp @@ -108,6 +108,7 @@ pinger.Start(); if (pthread_create(&thread, NULL, Run, this)) { errorStr = "Cannot start thread."; + logger("Cannot create thread."); printfd(__FILE__, "Cannot start thread\n"); return -1; } diff --git a/projects/stargazer/plugins/other/radius/radius.cpp b/projects/stargazer/plugins/other/radius/radius.cpp index 6fb4d2f9..1c060b7f 100644 --- a/projects/stargazer/plugins/other/radius/radius.cpp +++ b/projects/stargazer/plugins/other/radius/radius.cpp @@ -157,6 +157,7 @@ sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { errorStr = "Cannot create socket."; + logger("Cannot create a socket: %s", strerror(errno)); printfd(__FILE__, "Cannot create socket\n"); return -1; } @@ -169,6 +170,7 @@ inAddr.sin_addr.s_addr = inet_addr("0.0.0.0"); if (bind(sock, (struct sockaddr*)&inAddr, sizeof(inAddr)) < 0) { errorStr = "RADIUS: Bind failed."; + logger("Cannot bind the socket: %s", strerror(errno)); printfd(__FILE__, "Cannot bind socket\n"); return -1; } @@ -203,6 +205,7 @@ if (!isRunning) if (pthread_create(&thread, NULL, Run, this)) { errorStr = "Cannot create thread."; + logger("Cannot create thread."); printfd(__FILE__, "Cannot create thread\n"); return -1; } @@ -290,14 +293,22 @@ int RADIUS::RecvData(RAD_PACKET * packet, struct sockaddr_in * outerAddr) int8_t buf[RAD_MAX_PACKET_LEN]; socklen_t outerAddrLen = sizeof(struct sockaddr_in); int dataLen = recvfrom(sock, buf, RAD_MAX_PACKET_LEN, 0, reinterpret_cast(outerAddr), &outerAddrLen); - if (dataLen > 0) { - Decrypt(&ctx, (char *)packet, (const char *)buf, dataLen / 8); - } + if (dataLen < 0) + { + logger("recvfrom error: %s", strerror(errno)); + return -1; + } + if (dataLen == 0) + return -1; + + Decrypt(&ctx, (char *)packet, (const char *)buf, dataLen / 8); + if (strncmp((char *)packet->magic, RAD_ID, RAD_MAGIC_LEN)) { printfd(__FILE__, "RADIUS::RecvData Error magic. Wanted: '%s', got: '%s'\n", RAD_ID, packet->magic); return -1; } + return 0; } //----------------------------------------------------------------------------- @@ -307,7 +318,10 @@ size_t len = sizeof(RAD_PACKET); char buf[1032]; Encrypt(&ctx, buf, (char *)&packet, len / 8); -return sendto(sock, buf, len, 0, reinterpret_cast(outerAddr), sizeof(struct sockaddr_in)); +int res = sendto(sock, buf, len, 0, reinterpret_cast(outerAddr), sizeof(struct sockaddr_in)); +if (res < 0) + logger("sendto error: %s", strerror(errno)); +return res; } //----------------------------------------------------------------------------- int RADIUS::ProcessData(RAD_PACKET * packet) diff --git a/projects/stargazer/plugins/other/rscript/rscript.cpp b/projects/stargazer/plugins/other/rscript/rscript.cpp index 4ce51d65..f7479da6 100644 --- a/projects/stargazer/plugins/other/rscript/rscript.cpp +++ b/projects/stargazer/plugins/other/rscript/rscript.cpp @@ -30,6 +30,8 @@ #include #include #include +#include +#include #include #include "stg/common.h" @@ -253,6 +255,7 @@ if (!isRunning) if (pthread_create(&thread, NULL, Run, this)) { errorStr = "Cannot create thread."; + logger("Cannot create thread."); printfd(__FILE__, "Cannot create thread\n"); return -1; } @@ -291,7 +294,10 @@ users->DelNotifierUserDel(&onDelUserNotifier); users->DelNotifierUserAdd(&onAddUserNotifier); if (isRunning) + { + logger("Cannot stop thread."); return -1; + } return 0; } @@ -303,6 +309,7 @@ NRMapParser nrMapParser; if (nrMapParser.ReadFile(rsSettings.GetMapFileName())) { errorStr = nrMapParser.GetErrorStr(); + logger("Map file reading error: %s", errorStr.c_str()); return -1; } @@ -328,6 +335,7 @@ sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { errorStr = "Cannot create socket."; + logger("Canot create a socket: %s", strerror(errno)); printfd(__FILE__, "Cannot create socket\n"); return true; } @@ -465,6 +473,9 @@ sendAddr.sin_addr.s_addr = routerIP; int res = sendto(sock, buffer, sizeof(buffer), 0, (struct sockaddr *)&sendAddr, sizeof(sendAddr)); +if (res < 0) + logger("sendto error: %s", strerror(errno)); + return (res != sizeof(buffer)); } //----------------------------------------------------------------------------- @@ -473,12 +484,7 @@ bool REMOTE_SCRIPT::GetUsers() USER_PTR u; int h = users->OpenSearch(); -if (!h) - { - errorStr = "users->OpenSearch() error."; - printfd(__FILE__, "OpenSearch() error\n"); - return true; - } +assert(h && "USERS::OpenSearch is always correct"); while (!users->SearchNext(h, &u)) { diff --git a/projects/stargazer/plugins/other/smux/smux.cpp b/projects/stargazer/plugins/other/smux/smux.cpp index c1352de2..bef6e462 100644 --- a/projects/stargazer/plugins/other/smux/smux.cpp +++ b/projects/stargazer/plugins/other/smux/smux.cpp @@ -205,6 +205,7 @@ if (!running) if (pthread_create(&thread, NULL, Runner, this)) { errorStr = "Cannot create thread."; + logger("Cannot create thread."); printfd(__FILE__, "Cannot create thread\n"); return -1; } @@ -316,6 +317,7 @@ sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { errorStr = "Cannot create socket."; + logger("Cannot create a socket: %s", strerror(errno)); printfd(__FILE__, "Cannot create socket\n"); return true; } @@ -329,6 +331,7 @@ addr.sin_addr.s_addr = smuxSettings.GetIP(); if (connect(sock, reinterpret_cast(&addr), sizeof(addr))) { errorStr = "Cannot connect."; + logger("Cannot connect the socket: %s", strerror(errno)); printfd(__FILE__, "Cannot connect. Message: '%s'\n", strerror(errno)); return true; } diff --git a/stglibs/logger.lib/include/stg/logger.h b/stglibs/logger.lib/include/stg/logger.h index 2c9c93fc..8cc25cad 100644 --- a/stglibs/logger.lib/include/stg/logger.h +++ b/stglibs/logger.lib/include/stg/logger.h @@ -31,17 +31,17 @@ friend class PLUGIN_LOGGER; public: ~STG_LOGGER(); void SetLogFileName(const std::string & fn); - void operator()(const char * fmt, ...); + void operator()(const char * fmt, ...) const; private: STG_LOGGER(); STG_LOGGER(const STG_LOGGER & rvalue); STG_LOGGER & operator=(const STG_LOGGER & rvalue); - const char * LogDate(time_t t); + const char * LogDate(time_t t) const; std::string fileName; - pthread_mutex_t mutex; + mutable pthread_mutex_t mutex; }; //----------------------------------------------------------------------------- class PLUGIN_LOGGER : private STG_LOGGER @@ -50,7 +50,7 @@ friend PLUGIN_LOGGER GetPluginLogger(const STG_LOGGER & logger, const std::strin public: PLUGIN_LOGGER(const PLUGIN_LOGGER & rhs); - void operator()(const char * fmt, ...); + void operator()(const char * fmt, ...) const; private: PLUGIN_LOGGER(const STG_LOGGER & logger, const std::string & pn); diff --git a/stglibs/logger.lib/logger.cpp b/stglibs/logger.lib/logger.cpp index 3bbd3367..14c09591 100644 --- a/stglibs/logger.lib/logger.cpp +++ b/stglibs/logger.lib/logger.cpp @@ -32,7 +32,7 @@ STG_LOGGER_LOCKER lock(&mutex); fileName = fn; } //----------------------------------------------------------------------------- -void STG_LOGGER::operator()(const char * fmt, ...) +void STG_LOGGER::operator()(const char * fmt, ...) const { STG_LOGGER_LOCKER lock(&mutex); @@ -74,7 +74,7 @@ else } } //----------------------------------------------------------------------------- -const char * STG_LOGGER::LogDate(time_t t) +const char * STG_LOGGER::LogDate(time_t t) const { static char s[32]; if (t == 0) @@ -107,7 +107,7 @@ PLUGIN_LOGGER::PLUGIN_LOGGER(const PLUGIN_LOGGER & rhs) SetLogFileName(fileName); } //----------------------------------------------------------------------------- -void PLUGIN_LOGGER::operator()(const char * fmt, ...) +void PLUGIN_LOGGER::operator()(const char * fmt, ...) const { char buff[2029]; -- 2.43.2