2  *    This program is free software; you can redistribute it and/or modify
 
   3  *    it under the terms of the GNU General Public License as published by
 
   4  *    the Free Software Foundation; either version 2 of the License, or
 
   5  *    (at your option) any later version.
 
   7  *    This program is distributed in the hope that it will be useful,
 
   8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
   9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  10  *    GNU General Public License for more details.
 
  12  *    You should have received a copy of the GNU General Public License
 
  13  *    along with this program; if not, write to the Free Software
 
  14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
  17 /*******************************************************************
 
  19 *    DESCRIPTION: æÁÊÌ Ó ÏÓÎÏ×ÎÙÍÉ ÆÕÎËÃÉÑÍÉ ÄÌÑ ÓÅÔÅ×ÏÇÏ ÏÂÍÅÎÁ ÄÁÎÎÙÍÉ
 
  20 *    Ó ÍÅÎÅÄÖÅÒÏÍ ËÌÉÅÎÔÏ×. ðÒÉÅÍ, ÐÅÒÅÄÁÞÁ É ÛÉÆÒÏ×ÁÎÉÅ ÓÏÏÂÝÅÎÉÊ.
 
  22 *    AUTHOR: Boris Mikhailenko <stg34@stargazer.dp.ua>
 
  25 *    $Date: 2010/10/04 20:24:54 $
 
  27 *******************************************************************/
 
  31 #include <cstdio> // snprintf
 
  33 #include "configproto.h"
 
  55 //-----------------------------------------------------------------------------
 
  56 int CONFIGPROTO::Prepare()
 
  58 list<string> ansList; //óÀÄÁ ÂÕÄÅÔ ÐÏÍÅÝÅΠÏÔ×ÅÔ ÄÌÑ ÍÅÎÅÄÖÅÒÁ ËÌÉÅÎÔÏ×
 
  60 struct sockaddr_in listenAddr;
 
  62 sigset_t sigmask, oldmask;
 
  63 sigemptyset(&sigmask);
 
  64 sigaddset(&sigmask, SIGINT);
 
  65 sigaddset(&sigmask, SIGTERM);
 
  66 sigaddset(&sigmask, SIGUSR1);
 
  67 sigaddset(&sigmask, SIGHUP);
 
  68 pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
 
  70 listenSocket = socket(PF_INET, SOCK_STREAM, 0);
 
  74     errorStr = "Create NET_CONFIGURATOR socket failed.";
 
  78 listenAddr.sin_family = PF_INET;
 
  79 listenAddr.sin_port = htons(port);
 
  80 listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
 
  84 if (0 != setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &lng, 4))
 
  86     errorStr = "Setsockopt failed. " + string(strerror(errno));
 
  90 res = bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr));
 
  94     errorStr = "Bind admin socket failed";
 
  98 res = listen(listenSocket, 0);
 
 101     errorStr = "Listen admin socket failed";
 
 109 //-----------------------------------------------------------------------------
 
 110 int CONFIGPROTO::Stop()
 
 116 struct sockaddr_in  addr;
 
 118 addr.sin_family = PF_INET;
 
 119 addr.sin_port = htons(port);
 
 120 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
 
 122 addrLen = sizeof(addr);
 
 123 sock = socket(PF_INET, SOCK_STREAM, 0);
 
 124 connect(sock, (sockaddr*)&addr, addrLen);
 
 129 //-----------------------------------------------------------------------------
 
 130 // æÕÎËÃÉÑ ÏÂÝÅÎÉÑ Ó ËÏÎÆÉÇÕÒÁÔÏÒÏÍ
 
 131 void * CONFIGPROTO::Run(void * a)
 
 134  * Function Name:ReciveSendConf
 
 135  * Parameters: void * a ÕËÁÚÁÔÅÌØ ÎÁ ÜËÚÅÍÐÌÑÒ ËÌÁÓÓÁ CONFIGPROTO
 
 136  * Description: üÔÁ ÆÕÎËÃÉÑ ÏÂÅÓÐÅÞÉ×ÁÅÔ ÓÅÔÅ×ÏÅ ×ÚÁÉÍÏÄÅÊÓÔ×ÉÅ
 
 137  *  Ó íÅÎÅÄÖÅÒÏÍ ëÌÉÅÎÔÏ×. ÷ ÅÅ ÚÁÄÁÞÉ ×ÈÏÄÉÔ: ÐÒÉÅÍ ÚÁÐÒÏÓÏ× ÐÏ TCP/IP
 
 138  *  ÉÈ ÒÁÓÛÉÆÒÏ×ËÁ, ÐÅÒÅÄÁÞÁ ÐÒÉÎÑÔÙÈ ÄÁÎÎÙÈ ÁÎÁÌÉÚÁÔÏÒÕ É ÏÔÐÒÁ×ËÁ ÏÔ×ÅÔÁ ÎÁÚÁÄ.
 
 139  * Returns: ×ÏÚ×ÒÁÝÁÅÔ NULL
 
 142 CONFIGPROTO * cp = (CONFIGPROTO*)a;
 
 148     struct sockaddr_in outerAddr;
 
 149     socklen_t outerAddrLen(sizeof(outerAddr));
 
 150     int outerSocket = accept(cp->listenSocket,
 
 151                              (struct sockaddr*)(&outerAddr),
 
 159     if (outerSocket == -1)
 
 161         printfd(__FILE__, "accept failed\n");
 
 166     cp->adminIP = *(unsigned int*)&(outerAddr.sin_addr);
 
 168     printfd(__FILE__, "Connection accepted from %s\n", inet_ntostring(outerAddr.sin_addr.s_addr).c_str());
 
 170     if (cp->state == confHdr)
 
 172         if (cp->RecvHdr(outerSocket) < 0)
 
 177         if (cp->state == confLogin)
 
 179             if (cp->SendHdrAnswer(outerSocket, ans_ok) < 0)
 
 185             if (cp->RecvLogin(outerSocket) < 0)
 
 191             if (cp->state == confLoginCipher)
 
 193                 if (cp->SendLoginAnswer(outerSocket, ans_ok) < 0)
 
 198                 if (cp->RecvLoginS(outerSocket) < 0)
 
 204                 if (cp->state == confData)
 
 206                     if (cp->SendLoginSAnswer(outerSocket, ans_ok) < 0)
 
 211                     if (cp->RecvData(outerSocket) < 0)
 
 220                     if (cp->SendLoginSAnswer(outerSocket, ans_err) < 0)
 
 225                     cp->WriteLogAccessFailed(cp->adminIP);
 
 230                 cp->WriteLogAccessFailed(cp->adminIP);
 
 235             cp->WriteLogAccessFailed(cp->adminIP);
 
 236             if (cp->SendHdrAnswer(outerSocket, ans_err) < 0)
 
 245         cp->WriteLogAccessFailed(cp->adminIP);
 
 252 //-----------------------------------------------------------------------------
 
 253 int CONFIGPROTO::RecvHdr(int sock)
 
 255 char buf[sizeof(STG_HEADER)];
 
 256 memset(buf, 0, sizeof(STG_HEADER));
 
 258 int stgHdrLen = strlen(STG_HEADER);
 
 259 for (int i = 0; i < stgHdrLen; i++)
 
 261     ret = recv(sock, &buf[i], 1, 0);
 
 269 if (0 == strncmp(buf, STG_HEADER, strlen(STG_HEADER)))
 
 276     SendError("Bad request");
 
 282 //-----------------------------------------------------------------------------
 
 283 int CONFIGPROTO::SendHdrAnswer(int sock, int err)
 
 289     ret = send(sock, ERR_HEADER, sizeof(ERR_HEADER)-1, 0);
 
 292         WriteServLog("send ERR_HEADER error in SendHdrAnswer.");
 
 298     ret = send(sock, OK_HEADER, sizeof(OK_HEADER)-1, 0);
 
 301         WriteServLog("send OK_HEADER error in SendHdrAnswer.");
 
 308 //-----------------------------------------------------------------------------
 
 309 int CONFIGPROTO::RecvLogin(int sock)
 
 311 char login[ADM_LOGIN_LEN+1];
 
 314 memset(login, 0, ADM_LOGIN_LEN + 1);
 
 316 ret = recv(sock, login, ADM_LOGIN_LEN, 0);
 
 326 if (ret < ADM_LOGIN_LEN)
 
 334 if (admins->FindAdmin(login, &currAdmin))
 
 341 currAdmin.SetAdminIP(adminIP);
 
 343 state = confLoginCipher;
 
 346 //-----------------------------------------------------------------------------
 
 347 int CONFIGPROTO::SendLoginAnswer(int sock, int)
 
 351 ret = send(sock, OK_LOGIN, sizeof(OK_LOGIN)-1, 0);
 
 354     WriteServLog("Send OK_LOGIN error in SendLoginAnswer.");
 
 359 //-----------------------------------------------------------------------------
 
 360 int CONFIGPROTO::RecvLoginS(int sock)
 
 362 char loginS[ADM_LOGIN_LEN + 1];
 
 363 char login[ADM_LOGIN_LEN + 1];
 
 366 memset(loginS, 0, ADM_LOGIN_LEN + 1);
 
 370 while (total < ADM_LOGIN_LEN)
 
 372     ret = recv(sock, &loginS[total], ADM_LOGIN_LEN - total, 0);
 
 377         printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
 
 386 if (currAdmin.GetLogin() == "")
 
 392 EnDecodeInit(currAdmin.GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
 
 394 for (int i = 0; i < ADM_LOGIN_LEN/8; i++)
 
 396     DecodeString(login + i*8, loginS + i*8, &ctx);
 
 399 if (currAdmin == admins->GetNoAdmin())
 
 401     // If there are no admins registered in the system - give access with any password
 
 406 if (strncmp(currAdmin.GetLogin().c_str(), login, ADM_LOGIN_LEN) != 0)
 
 415 //-----------------------------------------------------------------------------
 
 416 int CONFIGPROTO::SendLoginSAnswer(int sock, int err)
 
 422     ret = send(sock, ERR_LOGINS, sizeof(ERR_LOGINS)-1, 0);
 
 425         WriteServLog("send ERR_LOGIN error in SendLoginAnswer.");
 
 431     ret = send(sock, OK_LOGINS, sizeof(OK_LOGINS)-1, 0);
 
 434         WriteServLog("send OK_LOGINS error in SendLoginSAnswer.");
 
 440 //-----------------------------------------------------------------------------
 
 441 int CONFIGPROTO::RecvData(int sock)
 
 452 EnDecodeInit(currAdmin.GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
 
 460         ret = recv(sock, &bufferS[total], 8 - total, 0);
 
 470             if (memchr(buffer, 0, ret) != NULL)
 
 480     DecodeString(buffer, bufferS, &ctx);
 
 481     requestList.push_back(std::string(buffer, total));
 
 483     if (done || memchr(buffer, 0, total) != NULL)
 
 488             SendError("Bad command");
 
 490         return SendDataAnswer(sock);
 
 495 //-----------------------------------------------------------------------------
 
 496 int CONFIGPROTO::SendDataAnswer(int sock)
 
 498 list<string>::iterator li;
 
 499 li = answerList.begin();
 
 509 EnDecodeInit(currAdmin.GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
 
 511 while (li != answerList.end())
 
 513     while ((*li).c_str()[k])
 
 515         buff[n%8] = (*li).c_str()[k];
 
 521             EncodeString(buffS, buff, &ctx);
 
 522             ret = send(sock, buffS, 8, 0);
 
 533 if (answerList.empty()) {
 
 538 EncodeString(buffS, buff, &ctx);
 
 542 return send(sock, buffS, 8, 0);
 
 544 //-----------------------------------------------------------------------------
 
 545 void CONFIGPROTO::SendError(const char * text)
 
 549 snprintf(s, 255, "<Error value=\"%s\"/>", text);
 
 550 answerList.push_back(s);
 
 552 //-----------------------------------------------------------------------------
 
 553 void CONFIGPROTO::WriteLogAccessFailed(uint32_t ip)
 
 555 WriteServLog("Admin's connect failed. IP %s", inet_ntostring(ip).c_str());
 
 557 //-----------------------------------------------------------------------------