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 *******************************************************************/
 
  29 #include <unistd.h> // close
 
  33 #include <cstdio> // snprintf
 
  35 #include "stg/blowfish.h"
 
  36 #include "configproto.h"
 
  57 //-----------------------------------------------------------------------------
 
  58 int CONFIGPROTO::Prepare()
 
  60 list<string> ansList; //óÀÄÁ ÂÕÄÅÔ ÐÏÍÅÝÅΠÏÔ×ÅÔ ÄÌÑ ÍÅÎÅÄÖÅÒÁ ËÌÉÅÎÔÏ×
 
  62 struct sockaddr_in listenAddr;
 
  64 sigset_t sigmask, oldmask;
 
  65 sigemptyset(&sigmask);
 
  66 sigaddset(&sigmask, SIGINT);
 
  67 sigaddset(&sigmask, SIGTERM);
 
  68 sigaddset(&sigmask, SIGUSR1);
 
  69 sigaddset(&sigmask, SIGHUP);
 
  70 pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
 
  72 listenSocket = socket(PF_INET, SOCK_STREAM, 0);
 
  76     errorStr = "Create NET_CONFIGURATOR socket failed.";
 
  77     logger("Cannot create a socket: %s", strerror(errno));
 
  81 listenAddr.sin_family = PF_INET;
 
  82 listenAddr.sin_port = htons(port);
 
  83 listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
 
  87 if (0 != setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &lng, 4))
 
  89     errorStr = "Setsockopt failed. " + string(strerror(errno));
 
  90     logger("setsockopt error: %s", strerror(errno));
 
  94 res = bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr));
 
  98     errorStr = "Bind admin socket failed";
 
  99     logger("Cannot bind the socket: %s", strerror(errno));
 
 103 res = listen(listenSocket, 0);
 
 106     errorStr = "Listen admin socket failed";
 
 107     logger("Cannot listen the socket: %s", strerror(errno));
 
 115 //-----------------------------------------------------------------------------
 
 116 int CONFIGPROTO::Stop()
 
 122 struct sockaddr_in  addr;
 
 124 addr.sin_family = PF_INET;
 
 125 addr.sin_port = htons(port);
 
 126 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
 
 128 addrLen = sizeof(addr);
 
 129 sock = socket(PF_INET, SOCK_STREAM, 0);
 
 130 connect(sock, (sockaddr*)&addr, addrLen);
 
 135 //-----------------------------------------------------------------------------
 
 136 void CONFIGPROTO::Run()
 
 143     struct sockaddr_in outerAddr;
 
 144     socklen_t outerAddrLen(sizeof(outerAddr));
 
 145     int outerSocket = accept(listenSocket,
 
 146                              (struct sockaddr*)(&outerAddr),
 
 156         logger("accept error: %s", strerror(errno));
 
 157         printfd(__FILE__, "accept failed\n");
 
 161     adminIP = *(unsigned int*)&(outerAddr.sin_addr);
 
 163     if (state == confHdr)
 
 165         if (RecvHdr(outerSocket) < 0)
 
 170         if (state == confLogin)
 
 172             if (SendHdrAnswer(outerSocket, ans_ok) < 0)
 
 177             if (RecvLogin(outerSocket) < 0)
 
 182             if (state == confLoginCipher)
 
 184                 if (SendLoginAnswer(outerSocket) < 0)
 
 189                 if (RecvLoginS(outerSocket) < 0)
 
 194                 if (state == confData)
 
 196                     if (SendLoginSAnswer(outerSocket, ans_ok) < 0)
 
 201                     if (RecvData(outerSocket) < 0)
 
 210                     if (SendLoginSAnswer(outerSocket, ans_err) < 0)
 
 215                     WriteLogAccessFailed(adminIP);
 
 220                 WriteLogAccessFailed(adminIP);
 
 225             WriteLogAccessFailed(adminIP);
 
 226             if (SendHdrAnswer(outerSocket, ans_err) < 0)
 
 235         WriteLogAccessFailed(adminIP);
 
 237     printfd(__FILE__, "Successfull connection from %s\n", inet_ntostring(outerAddr.sin_addr.s_addr).c_str());
 
 241 //-----------------------------------------------------------------------------
 
 242 int CONFIGPROTO::RecvHdr(int sock)
 
 244 char buf[sizeof(STG_HEADER)];
 
 245 memset(buf, 0, sizeof(STG_HEADER));
 
 246 size_t stgHdrLen = sizeof(STG_HEADER) - 1; // Without 0-char
 
 248 while (pos < stgHdrLen)
 
 250     if (!WaitPackets(sock))
 
 253         SendError("Bad request");
 
 256     int ret = recv(sock, &buf[pos], stgHdrLen - pos, 0);
 
 260             logger("recv error: %s", strerror(errno));
 
 267 if (0 == strncmp(buf, STG_HEADER, strlen(STG_HEADER)))
 
 274     SendError("Bad request");
 
 280 //-----------------------------------------------------------------------------
 
 281 int CONFIGPROTO::SendHdrAnswer(int sock, int err)
 
 285     if (send(sock, ERR_HEADER, sizeof(ERR_HEADER) - 1, 0) < 0)
 
 287         logger("send error: %s", strerror(errno));
 
 293     if (send(sock, OK_HEADER, sizeof(OK_HEADER) - 1, 0) < 0)
 
 295         logger("send error: %s", strerror(errno));
 
 302 //-----------------------------------------------------------------------------
 
 303 int CONFIGPROTO::RecvLogin(int sock)
 
 305 char login[ADM_LOGIN_LEN + 1];
 
 307 memset(login, 0, ADM_LOGIN_LEN + 1);
 
 310 while (pos < ADM_LOGIN_LEN) {
 
 311     if (!WaitPackets(sock))
 
 317     int ret = recv(sock, &login[pos], ADM_LOGIN_LEN - pos, 0);
 
 322         logger("recv error: %s", strerror(errno));
 
 330 if (admins->Find(login, &currAdmin))
 
 337 currAdmin->SetIP(adminIP);
 
 339 state = confLoginCipher;
 
 342 //-----------------------------------------------------------------------------
 
 343 int CONFIGPROTO::SendLoginAnswer(int sock)
 
 345 if (send(sock, OK_LOGIN, sizeof(OK_LOGIN) - 1, 0) < 0)
 
 347     logger("Send OK_LOGIN error in SendLoginAnswer.");
 
 352 //-----------------------------------------------------------------------------
 
 353 int CONFIGPROTO::RecvLoginS(int sock)
 
 355 char loginS[ADM_LOGIN_LEN + 1];
 
 356 memset(loginS, 0, ADM_LOGIN_LEN + 1);
 
 359 while (pos < ADM_LOGIN_LEN)
 
 361     if (!WaitPackets(sock))
 
 367     int ret = recv(sock, &loginS[pos], ADM_LOGIN_LEN - pos, 0);
 
 372         printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
 
 373         logger("recv error: %s", strerror(errno));
 
 381 if (currAdmin->GetLogin().empty())
 
 388 EnDecodeInit(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
 
 390 char login[ADM_LOGIN_LEN + 1];
 
 391 for (size_t i = 0; i < ADM_LOGIN_LEN / 8; i++)
 
 393     DecodeString(login + i * 8, loginS + i * 8, &ctx);
 
 396 if (currAdmin == admins->GetNoAdmin())
 
 398     // If there are no admins registered in the system - give access with any password
 
 403 if (strncmp(currAdmin->GetLogin().c_str(), login, ADM_LOGIN_LEN) != 0)
 
 410 adminPassword = currAdmin->GetPassword();
 
 413 //-----------------------------------------------------------------------------
 
 414 int CONFIGPROTO::SendLoginSAnswer(int sock, int err)
 
 418     if (send(sock, ERR_LOGINS, sizeof(ERR_LOGINS) - 1, 0) < 0)
 
 420         logger("send error: %s", strerror(errno));
 
 426     if (send(sock, OK_LOGINS, sizeof(OK_LOGINS) - 1, 0) < 0)
 
 428         logger("send error: %s", strerror(errno));
 
 434 //-----------------------------------------------------------------------------
 
 435 int CONFIGPROTO::RecvData(int sock)
 
 440 EnDecodeInit(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
 
 447     while (pos < sizeof(bufferS))
 
 449         if (!WaitPackets(sock))
 
 455         int ret = recv(sock, &bufferS[pos], sizeof(bufferS) - pos, 0);
 
 459             logger("recv error: %s", strerror(errno));
 
 460             printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
 
 476     DecodeString(buffer, bufferS, &ctx);
 
 477     requestList.push_back(std::string(buffer, pos));
 
 479     if (done || memchr(buffer, 0, pos) != NULL)
 
 484             SendError("Bad command");
 
 486         return SendDataAnswer(sock);
 
 491 //-----------------------------------------------------------------------------
 
 492 int CONFIGPROTO::SendDataAnswer(int sock)
 
 494 list<string>::iterator li;
 
 495 li = answerList.begin();
 
 504 EnDecodeInit(adminPassword.c_str(), ADM_PASSWD_LEN, &ctx);
 
 506 while (li != answerList.end())
 
 508     while ((*li).c_str()[k])
 
 510         buff[n % 8] = (*li).c_str()[k];
 
 516             EncodeString(buffS, buff, &ctx);
 
 517             int ret = send(sock, buffS, 8, 0);
 
 528 if (answerList.empty()) {
 
 533 EncodeString(buffS, buff, &ctx);
 
 537 return send(sock, buffS, 8, 0);
 
 539 //-----------------------------------------------------------------------------
 
 540 void CONFIGPROTO::SendError(const char * text)
 
 544 snprintf(s, 255, "<Error value=\"%s\"/>", text);
 
 545 answerList.push_back(s);
 
 547 //-----------------------------------------------------------------------------
 
 548 void CONFIGPROTO::WriteLogAccessFailed(uint32_t ip)
 
 550 logger("Admin's connection failed. IP %s", inet_ntostring(ip).c_str());
 
 552 //-----------------------------------------------------------------------------