"Fossies" - the Fresh Open Source Software Archive

Member "netxms-3.1.300/src/server/core/admin.cpp" (7 Jan 2020, 5768 Bytes) of package /linux/misc/netxms-3.1.300.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "admin.cpp" see the Fossies "Dox" file reference documentation.

    1 /* 
    2 ** NetXMS - Network Management System
    3 ** Copyright (C) 2003-2019 Victor Kirhenshtein
    4 **
    5 ** This program is free software; you can redistribute it and/or modify
    6 ** it under the terms of the GNU General Public License as published by
    7 ** the Free Software Foundation; either version 2 of the License, or
    8 ** (at your option) any later version.
    9 **
   10 ** This program is distributed in the hope that it will be useful,
   11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13 ** GNU General Public License for more details.
   14 **
   15 ** You should have received a copy of the GNU General Public License
   16 ** along with this program; if not, write to the Free Software
   17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   18 **
   19 ** File: admin.cpp
   20 **
   21 **/
   22 
   23 #include "nxcore.h"
   24 #include <local_admin.h>
   25 
   26 /**
   27  * Max message size
   28  */
   29 #define MAX_MSG_SIZE       65536
   30 
   31 /**
   32  * DB password set condition
   33  */
   34 extern Condition g_dbPasswordReady;
   35 
   36 /**
   37  * Request processing thread
   38  */
   39 static THREAD_RESULT THREAD_CALL ProcessingThread(void *pArg)
   40 {
   41    SOCKET sock = CAST_FROM_POINTER(pArg, SOCKET);
   42 
   43    SocketConsole console(sock);
   44    SocketMessageReceiver receiver(sock, 4096, MAX_MSG_SIZE);
   45    while(true)
   46    {
   47       MessageReceiverResult result;
   48       NXCPMessage *request = receiver.readMessage(INFINITE, &result);
   49 
   50       // Receive error
   51       if (request == NULL)
   52       {
   53          if (result == MSGRECV_CLOSED)
   54             nxlog_debug(5, _T("LocalServerConsole: connection closed"));
   55          else
   56             nxlog_debug(5, _T("LocalServerConsole: message receiving error (%s)"), AbstractMessageReceiver::resultToText(result));
   57          break;
   58       }
   59 
   60       if (IsShutdownInProgress())
   61       {
   62          delete request;
   63          break;
   64       }
   65 
   66       if (request->getCode() == CMD_ADM_REQUEST)
   67       {
   68          TCHAR command[256];
   69          request->getFieldAsString(VID_COMMAND, command, 256);
   70 
   71          int exitCode = ProcessConsoleCommand(command, &console);
   72          switch(exitCode)
   73          {
   74             case CMD_EXIT_SHUTDOWN:
   75                InitiateShutdown();
   76                break;
   77             case CMD_EXIT_CLOSE_SESSION:
   78                delete request;
   79                goto close_session;
   80             default:
   81                break;
   82          }
   83       }
   84       else if (request->getCode() == CMD_SET_DB_PASSWORD)
   85       {
   86          request->getFieldAsString(VID_PASSWORD, g_szDbPassword, MAX_PASSWORD);
   87          DecryptPassword(g_szDbLogin, g_szDbPassword, g_szDbPassword, MAX_PASSWORD);
   88          g_dbPasswordReady.set();
   89       }
   90 
   91       NXCPMessage response(CMD_REQUEST_COMPLETED, request->getId());
   92       NXCP_MESSAGE *rawMsgOut = response.serialize();
   93         SendEx(sock, rawMsgOut, ntohl(rawMsgOut->size), 0, console.getMutex());
   94       MemFree(rawMsgOut);
   95       delete request;
   96    }
   97 
   98 close_session:
   99    shutdown(sock, 2);
  100    closesocket(sock);
  101    return THREAD_OK;
  102 }
  103 
  104 /**
  105  * Local administrative interface listener thread
  106  */
  107 THREAD_RESULT THREAD_CALL LocalAdminListener(void *pArg)
  108 {
  109    ThreadSetName("DebugConsole");
  110 
  111    SOCKET sock, sockClient;
  112    struct sockaddr_in servAddr;
  113    int errorCount = 0;
  114    socklen_t iSize;
  115 
  116    // Create socket
  117    if ((sock = CreateSocket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
  118    {
  119       TCHAR buffer[1024];
  120       nxlog_write(NXLOG_ERROR, _T("Unable to create socket for local admin interface (%s)"), GetLastSocketErrorText(buffer, 1024));
  121       return THREAD_OK;
  122    }
  123 
  124     SetSocketExclusiveAddrUse(sock);
  125     SetSocketReuseFlag(sock);
  126 #ifndef _WIN32
  127    fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC);
  128 #endif
  129 
  130    // Fill in local address structure
  131    memset(&servAddr, 0, sizeof(struct sockaddr_in));
  132    servAddr.sin_family = AF_INET;
  133    servAddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  134    servAddr.sin_port = htons(LOCAL_ADMIN_PORT);
  135 
  136    // Bind socket
  137    if (bind(sock, (struct sockaddr *)&servAddr, sizeof(struct sockaddr_in)) != 0)
  138    {
  139       TCHAR buffer[1024];
  140       nxlog_write(NXLOG_ERROR, _T("Unable to bind socket for local admin interface (%s)"), GetLastSocketErrorText(buffer, 1024));
  141       closesocket(sock);
  142       return THREAD_OK;
  143    }
  144 
  145    // Set up queue
  146    listen(sock, SOMAXCONN);
  147 
  148    nxlog_debug(1, _T("Local administration interface listener started"));
  149 
  150    // Wait for connection requests
  151    while(!IsShutdownInProgress())
  152    {
  153       SocketPoller sp;
  154       sp.add(sock);
  155       int pollError = sp.poll(2000);
  156       if (pollError <= 0)
  157       {
  158          if (pollError < 0)
  159          {
  160             if (SleepAndCheckForShutdown(30))
  161                break;
  162          }
  163          continue;
  164       }
  165 
  166       iSize = sizeof(struct sockaddr_in);
  167       if ((sockClient = accept(sock, (struct sockaddr *)&servAddr, &iSize)) == -1)
  168       {
  169          if (IsShutdownInProgress())
  170          {
  171             closesocket(sockClient);
  172             break;
  173          }
  174 #ifdef _WIN32
  175          int error = WSAGetLastError();
  176          if (error != WSAEINTR)
  177 #else
  178          int error = errno;
  179          if (error != EINTR)
  180 #endif
  181          {
  182             TCHAR buffer[1024];
  183             nxlog_write(NXLOG_ERROR, _T("Unable to accept incoming connection (%s)"), GetLastSocketErrorText(buffer, 1024));
  184          }
  185          errorCount++;
  186          if (errorCount > 1000)
  187          {
  188             nxlog_write(NXLOG_WARNING, _T("Too many consecutive errors on accept() call"));
  189             errorCount = 0;
  190          }
  191          ThreadSleepMs(500);
  192          continue;
  193       }
  194 
  195       errorCount = 0;     // Reset consecutive errors counter
  196 
  197       // Create new session structure and threads
  198       ThreadCreate(ProcessingThread, 0, CAST_TO_POINTER(sockClient, void *));
  199    }
  200 
  201    closesocket(sock);
  202    nxlog_debug(1, _T("Local administration interface listener stopped"));
  203    return THREAD_OK;
  204 }