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()
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. " + std::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),
154 logger("accept error: %s", strerror(errno));
155 printfd(__FILE__, "accept failed\n");
159 adminIP = *(unsigned int*)&(outerAddr.sin_addr);
161 if (state == confHdr)
163 if (RecvHdr(outerSocket) < 0)
168 if (state == confLogin)
170 if (SendHdrAnswer(outerSocket, ans_ok) < 0)
175 if (RecvLogin(outerSocket) < 0)
180 if (state == confLoginCipher)
182 if (SendLoginAnswer(outerSocket) < 0)
187 if (RecvLoginS(outerSocket) < 0)
192 if (state == confData)
194 if (SendLoginSAnswer(outerSocket, ans_ok) < 0)
199 if (RecvData(outerSocket) < 0)
208 if (SendLoginSAnswer(outerSocket, ans_err) < 0)
213 WriteLogAccessFailed(adminIP);
218 WriteLogAccessFailed(adminIP);
223 WriteLogAccessFailed(adminIP);
224 if (SendHdrAnswer(outerSocket, ans_err) < 0)
233 WriteLogAccessFailed(adminIP);
235 printfd(__FILE__, "Successfull connection from %s\n", inet_ntostring(outerAddr.sin_addr.s_addr).c_str());
239 //-----------------------------------------------------------------------------
240 int CONFIGPROTO::RecvHdr(int sock)
242 char buf[sizeof(STG_HEADER)];
243 memset(buf, 0, sizeof(STG_HEADER));
244 size_t stgHdrLen = sizeof(STG_HEADER) - 1; // Without 0-char
246 while (pos < stgHdrLen)
248 if (!WaitPackets(sock))
251 SendError("Bad request");
254 ssize_t ret = recv(sock, &buf[pos], static_cast<int>(stgHdrLen) - static_cast<int>(pos), 0);
258 logger("recv error: %s", strerror(errno));
265 if (0 == strncmp(buf, STG_HEADER, strlen(STG_HEADER)))
272 SendError("Bad request");
278 //-----------------------------------------------------------------------------
279 int CONFIGPROTO::SendHdrAnswer(int sock, int err)
283 if (send(sock, ERR_HEADER, sizeof(ERR_HEADER) - 1, 0) < 0)
285 logger("send error: %s", strerror(errno));
291 if (send(sock, OK_HEADER, sizeof(OK_HEADER) - 1, 0) < 0)
293 logger("send error: %s", strerror(errno));
300 //-----------------------------------------------------------------------------
301 int CONFIGPROTO::RecvLogin(int sock)
303 char login[ADM_LOGIN_LEN + 1];
305 memset(login, 0, ADM_LOGIN_LEN + 1);
308 while (pos < ADM_LOGIN_LEN) {
309 if (!WaitPackets(sock))
315 ssize_t ret = recv(sock, &login[pos], ADM_LOGIN_LEN - static_cast<int>(pos), 0);
320 logger("recv error: %s", strerror(errno));
328 if (admins->Find(login, &currAdmin))
335 currAdmin->SetIP(adminIP);
337 state = confLoginCipher;
340 //-----------------------------------------------------------------------------
341 int CONFIGPROTO::SendLoginAnswer(int sock)
343 if (send(sock, OK_LOGIN, sizeof(OK_LOGIN) - 1, 0) < 0)
345 logger("Send OK_LOGIN error in SendLoginAnswer.");
350 //-----------------------------------------------------------------------------
351 int CONFIGPROTO::RecvLoginS(int sock)
353 char loginS[ADM_LOGIN_LEN + 1];
354 memset(loginS, 0, ADM_LOGIN_LEN + 1);
357 while (pos < ADM_LOGIN_LEN)
359 if (!WaitPackets(sock))
365 ssize_t ret = recv(sock, &loginS[pos], ADM_LOGIN_LEN - static_cast<int>(pos), 0);
370 printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
371 logger("recv error: %s", strerror(errno));
379 if (currAdmin->GetLogin().empty())
386 EnDecodeInit(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
388 char login[ADM_LOGIN_LEN + 1];
389 for (size_t i = 0; i < ADM_LOGIN_LEN / 8; i++)
390 DecodeString(login + i * 8, loginS + i * 8, &ctx);
392 if (currAdmin == admins->GetNoAdmin())
394 // If there are no admins registered in the system - give access with any password
399 if (strncmp(currAdmin->GetLogin().c_str(), login, ADM_LOGIN_LEN) != 0)
406 adminPassword = currAdmin->GetPassword();
409 //-----------------------------------------------------------------------------
410 int CONFIGPROTO::SendLoginSAnswer(int sock, int err)
414 if (send(sock, ERR_LOGINS, sizeof(ERR_LOGINS) - 1, 0) < 0)
416 logger("send error: %s", strerror(errno));
422 if (send(sock, OK_LOGINS, sizeof(OK_LOGINS) - 1, 0) < 0)
424 logger("send error: %s", strerror(errno));
430 //-----------------------------------------------------------------------------
431 int CONFIGPROTO::RecvData(int sock)
436 EnDecodeInit(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
443 while (pos < sizeof(bufferS))
445 if (!WaitPackets(sock))
451 ssize_t ret = recv(sock, &bufferS[pos], sizeof(bufferS) - static_cast<int>(pos), 0);
455 logger("recv error: %s", strerror(errno));
456 printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
472 DecodeString(buffer, bufferS, &ctx);
473 requestList.push_back(std::string(buffer, pos));
475 if (done || memchr(buffer, 0, pos) != NULL)
480 SendError("Bad command");
482 return SendDataAnswer(sock);
487 //-----------------------------------------------------------------------------
488 int CONFIGPROTO::SendDataAnswer(int sock)
490 std::list<std::string>::iterator li;
491 li = answerList.begin();
500 EnDecodeInit(adminPassword.c_str(), ADM_PASSWD_LEN, &ctx);
502 while (li != answerList.end())
504 while ((*li).c_str()[k])
506 buff[n % 8] = (*li).c_str()[k];
512 EncodeString(buffS, buff, &ctx);
513 if (send(sock, buffS, 8, 0) < 0)
521 if (answerList.empty()) {
526 EncodeString(buffS, buff, &ctx);
530 return static_cast<int>(send(sock, buffS, 8, 0));
532 //-----------------------------------------------------------------------------
533 void CONFIGPROTO::SendError(const char * text)
537 snprintf(s, 255, "<Error value=\"%s\"/>", text);
538 answerList.push_back(s);
540 //-----------------------------------------------------------------------------
541 void CONFIGPROTO::WriteLogAccessFailed(uint32_t ip)
543 logger("Admin's connection failed. IP %s", inet_ntostring(ip).c_str());
545 //-----------------------------------------------------------------------------