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 "configproto.h"
31 #include "stg/blowfish.h"
35 #include <cstdio> // snprintf
37 #include <unistd.h> // close
58 //-----------------------------------------------------------------------------
59 int CONFIGPROTO::Prepare()
61 std::list<std::string> ansList; //óÀÄÁ ÂÕÄÅÔ ÐÏÍÅÝÅÎ ÏÔ×ÅÔ ÄÌÑ ÍÅÎÅÄÖÅÒÁ ËÌÉÅÎÔÏ×
63 struct sockaddr_in listenAddr;
65 sigset_t sigmask, oldmask;
66 sigemptyset(&sigmask);
67 sigaddset(&sigmask, SIGINT);
68 sigaddset(&sigmask, SIGTERM);
69 sigaddset(&sigmask, SIGUSR1);
70 sigaddset(&sigmask, SIGHUP);
71 pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
73 listenSocket = socket(PF_INET, SOCK_STREAM, 0);
77 errorStr = "Create NET_CONFIGURATOR socket failed.";
78 logger("Cannot create a socket: %s", strerror(errno));
82 listenAddr.sin_family = PF_INET;
83 listenAddr.sin_port = htons(port);
84 listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
88 if (0 != setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &lng, 4))
90 errorStr = "Setsockopt failed. " + std::string(strerror(errno));
91 logger("setsockopt error: %s", strerror(errno));
95 res = bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr));
99 errorStr = "Bind admin socket failed";
100 logger("Cannot bind the socket: %s", strerror(errno));
104 res = listen(listenSocket, 0);
107 errorStr = "Listen admin socket failed";
108 logger("Cannot listen the socket: %s", strerror(errno));
116 //-----------------------------------------------------------------------------
117 int CONFIGPROTO::Stop()
123 struct sockaddr_in addr;
125 addr.sin_family = PF_INET;
126 addr.sin_port = htons(port);
127 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
129 addrLen = sizeof(addr);
130 sock = socket(PF_INET, SOCK_STREAM, 0);
131 connect(sock, (sockaddr*)&addr, addrLen);
136 //-----------------------------------------------------------------------------
137 void CONFIGPROTO::Run()
144 struct sockaddr_in outerAddr;
145 socklen_t outerAddrLen(sizeof(outerAddr));
146 int outerSocket = accept(listenSocket,
147 (struct sockaddr*)(&outerAddr),
155 logger("accept error: %s", strerror(errno));
156 printfd(__FILE__, "accept failed\n");
160 adminIP = *(unsigned int*)&(outerAddr.sin_addr);
162 if (state == confHdr)
164 if (RecvHdr(outerSocket) < 0)
169 if (state == confLogin)
171 if (SendHdrAnswer(outerSocket, ans_ok) < 0)
176 if (RecvLogin(outerSocket) < 0)
181 if (state == confLoginCipher)
183 if (SendLoginAnswer(outerSocket) < 0)
188 if (RecvLoginS(outerSocket) < 0)
193 if (state == confData)
195 if (SendLoginSAnswer(outerSocket, ans_ok) < 0)
200 if (RecvData(outerSocket) < 0)
209 if (SendLoginSAnswer(outerSocket, ans_err) < 0)
214 WriteLogAccessFailed(adminIP);
219 WriteLogAccessFailed(adminIP);
224 WriteLogAccessFailed(adminIP);
225 if (SendHdrAnswer(outerSocket, ans_err) < 0)
234 WriteLogAccessFailed(adminIP);
236 printfd(__FILE__, "Successfull connection from %s\n", inet_ntostring(outerAddr.sin_addr.s_addr).c_str());
240 //-----------------------------------------------------------------------------
241 int CONFIGPROTO::RecvHdr(int sock)
243 char buf[sizeof(STG_HEADER)];
244 memset(buf, 0, sizeof(STG_HEADER));
245 size_t stgHdrLen = sizeof(STG_HEADER) - 1; // Without 0-char
247 while (pos < stgHdrLen)
249 if (!WaitPackets(sock))
252 SendError("Bad request");
255 ssize_t ret = recv(sock, &buf[pos], static_cast<int>(stgHdrLen) - static_cast<int>(pos), 0);
259 logger("recv error: %s", strerror(errno));
266 if (0 == strncmp(buf, STG_HEADER, strlen(STG_HEADER)))
273 SendError("Bad request");
279 //-----------------------------------------------------------------------------
280 int CONFIGPROTO::SendHdrAnswer(int sock, int err)
284 if (send(sock, ERR_HEADER, sizeof(ERR_HEADER) - 1, 0) < 0)
286 logger("send error: %s", strerror(errno));
292 if (send(sock, OK_HEADER, sizeof(OK_HEADER) - 1, 0) < 0)
294 logger("send error: %s", strerror(errno));
301 //-----------------------------------------------------------------------------
302 int CONFIGPROTO::RecvLogin(int sock)
304 char login[ADM_LOGIN_LEN + 1];
306 memset(login, 0, ADM_LOGIN_LEN + 1);
309 while (pos < ADM_LOGIN_LEN) {
310 if (!WaitPackets(sock))
316 ssize_t ret = recv(sock, &login[pos], ADM_LOGIN_LEN - static_cast<int>(pos), 0);
321 logger("recv error: %s", strerror(errno));
329 if (admins->Find(login, &currAdmin))
336 currAdmin->SetIP(adminIP);
338 state = confLoginCipher;
341 //-----------------------------------------------------------------------------
342 int CONFIGPROTO::SendLoginAnswer(int sock)
344 if (send(sock, OK_LOGIN, sizeof(OK_LOGIN) - 1, 0) < 0)
346 logger("Send OK_LOGIN error in SendLoginAnswer.");
351 //-----------------------------------------------------------------------------
352 int CONFIGPROTO::RecvLoginS(int sock)
354 char loginS[ADM_LOGIN_LEN + 1];
355 memset(loginS, 0, ADM_LOGIN_LEN + 1);
358 while (pos < ADM_LOGIN_LEN)
360 if (!WaitPackets(sock))
366 ssize_t ret = recv(sock, &loginS[pos], ADM_LOGIN_LEN - static_cast<int>(pos), 0);
371 printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
372 logger("recv error: %s", strerror(errno));
380 if (currAdmin->GetLogin().empty())
387 EnDecodeInit(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
389 char login[ADM_LOGIN_LEN + 1];
390 for (size_t i = 0; i < ADM_LOGIN_LEN / 8; i++)
391 DecodeString(login + i * 8, loginS + i * 8, &ctx);
393 if (currAdmin == admins->GetNoAdmin())
395 // If there are no admins registered in the system - give access with any password
400 if (strncmp(currAdmin->GetLogin().c_str(), login, ADM_LOGIN_LEN) != 0)
407 adminPassword = currAdmin->GetPassword();
410 //-----------------------------------------------------------------------------
411 int CONFIGPROTO::SendLoginSAnswer(int sock, int err)
415 if (send(sock, ERR_LOGINS, sizeof(ERR_LOGINS) - 1, 0) < 0)
417 logger("send error: %s", strerror(errno));
423 if (send(sock, OK_LOGINS, sizeof(OK_LOGINS) - 1, 0) < 0)
425 logger("send error: %s", strerror(errno));
431 //-----------------------------------------------------------------------------
432 int CONFIGPROTO::RecvData(int sock)
437 EnDecodeInit(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
444 while (pos < sizeof(bufferS))
446 if (!WaitPackets(sock))
452 ssize_t ret = recv(sock, &bufferS[pos], sizeof(bufferS) - static_cast<int>(pos), 0);
456 logger("recv error: %s", strerror(errno));
457 printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
473 DecodeString(buffer, bufferS, &ctx);
474 requestList.push_back(std::string(buffer, pos));
476 if (done || memchr(buffer, 0, pos) != NULL)
481 SendError("Bad command");
483 return SendDataAnswer(sock);
488 //-----------------------------------------------------------------------------
489 int CONFIGPROTO::SendDataAnswer(int sock)
491 std::list<std::string>::iterator li;
492 li = answerList.begin();
501 EnDecodeInit(adminPassword.c_str(), ADM_PASSWD_LEN, &ctx);
503 while (li != answerList.end())
505 while ((*li).c_str()[k])
507 buff[n % 8] = (*li).c_str()[k];
513 EncodeString(buffS, buff, &ctx);
514 if (send(sock, buffS, 8, 0) < 0)
522 if (answerList.empty()) {
527 EncodeString(buffS, buff, &ctx);
531 return static_cast<int>(send(sock, buffS, 8, 0));
533 //-----------------------------------------------------------------------------
534 void CONFIGPROTO::SendError(const char * text)
538 snprintf(s, 255, "<Error value=\"%s\"/>", text);
539 answerList.push_back(s);
541 //-----------------------------------------------------------------------------
542 void CONFIGPROTO::WriteLogAccessFailed(uint32_t ip)
544 logger("Admin's connection failed. IP %s", inet_ntostring(ip).c_str());
546 //-----------------------------------------------------------------------------