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()
61 struct sockaddr_in listenAddr;
63 sigset_t sigmask, oldmask;
64 sigemptyset(&sigmask);
65 sigaddset(&sigmask, SIGINT);
66 sigaddset(&sigmask, SIGTERM);
67 sigaddset(&sigmask, SIGUSR1);
68 sigaddset(&sigmask, SIGHUP);
69 pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
71 listenSocket = socket(PF_INET, SOCK_STREAM, 0);
75 errorStr = "Create NET_CONFIGURATOR socket failed.";
76 logger("Cannot create a socket: %s", strerror(errno));
80 listenAddr.sin_family = PF_INET;
81 listenAddr.sin_port = htons(port);
82 listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
86 if (0 != setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &lng, 4))
88 errorStr = "Setsockopt failed. " + std::string(strerror(errno));
89 logger("setsockopt error: %s", strerror(errno));
93 res = bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr));
97 errorStr = "Bind admin socket failed";
98 logger("Cannot bind the socket: %s", strerror(errno));
102 res = listen(listenSocket, 0);
105 errorStr = "Listen admin socket failed";
106 logger("Cannot listen the socket: %s", strerror(errno));
114 //-----------------------------------------------------------------------------
115 int CONFIGPROTO::Stop()
121 struct sockaddr_in addr;
123 addr.sin_family = PF_INET;
124 addr.sin_port = htons(port);
125 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
127 addrLen = sizeof(addr);
128 sock = socket(PF_INET, SOCK_STREAM, 0);
129 connect(sock, (sockaddr*)&addr, addrLen);
134 //-----------------------------------------------------------------------------
135 void CONFIGPROTO::Run()
142 struct sockaddr_in outerAddr;
143 socklen_t outerAddrLen(sizeof(outerAddr));
144 int outerSocket = accept(listenSocket,
145 (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++)
392 DecodeString(login + i * 8, loginS + i * 8, &ctx);
395 if (currAdmin == admins->GetNoAdmin())
397 // If there are no admins registered in the system - give access with any password
402 if (strncmp(currAdmin->GetLogin().c_str(), login, ADM_LOGIN_LEN) != 0)
409 adminPassword = currAdmin->GetPassword();
412 //-----------------------------------------------------------------------------
413 int CONFIGPROTO::SendLoginSAnswer(int sock, int err)
417 if (send(sock, ERR_LOGINS, sizeof(ERR_LOGINS) - 1, 0) < 0)
419 logger("send error: %s", strerror(errno));
425 if (send(sock, OK_LOGINS, sizeof(OK_LOGINS) - 1, 0) < 0)
427 logger("send error: %s", strerror(errno));
433 //-----------------------------------------------------------------------------
434 int CONFIGPROTO::RecvData(int sock)
439 EnDecodeInit(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
446 while (pos < sizeof(bufferS))
448 if (!WaitPackets(sock))
454 ssize_t ret = recv(sock, &bufferS[pos], sizeof(bufferS) - static_cast<int>(pos), 0);
458 logger("recv error: %s", strerror(errno));
459 printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
475 DecodeString(buffer, bufferS, &ctx);
476 requestList.push_back(std::string(buffer, pos));
478 if (done || memchr(buffer, 0, pos) != NULL)
483 SendError("Bad command");
485 return SendDataAnswer(sock);
490 //-----------------------------------------------------------------------------
491 int CONFIGPROTO::SendDataAnswer(int sock)
493 std::list<std::string>::iterator li;
494 li = answerList.begin();
503 EnDecodeInit(adminPassword.c_str(), ADM_PASSWD_LEN, &ctx);
505 while (li != answerList.end())
507 while ((*li).c_str()[k])
509 buff[n % 8] = (*li).c_str()[k];
515 EncodeString(buffS, buff, &ctx);
516 int ret = static_cast<int>(send(sock, buffS, 8, 0));
527 if (answerList.empty()) {
532 EncodeString(buffS, buff, &ctx);
536 return static_cast<int>(send(sock, buffS, 8, 0));
538 //-----------------------------------------------------------------------------
539 void CONFIGPROTO::SendError(const char * text)
543 snprintf(s, 255, "<Error value=\"%s\"/>", text);
544 answerList.push_back(s);
546 //-----------------------------------------------------------------------------
547 void CONFIGPROTO::WriteLogAccessFailed(uint32_t ip)
549 logger("Admin's connection failed. IP %s", inet_ntostring(ip).c_str());
551 //-----------------------------------------------------------------------------