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/admins.h"
32 #include "stg/logger.h"
33 #include "stg/common.h"
34 #include "stg/blowfish.h"
38 #include <cstdio> // snprintf
39 #include <cstring> // strerror
41 #include <unistd.h> // close
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
66 //-----------------------------------------------------------------------------
67 int CONFIGPROTO::Prepare()
70 struct sockaddr_in listenAddr;
72 sigset_t sigmask, oldmask;
73 sigemptyset(&sigmask);
74 sigaddset(&sigmask, SIGINT);
75 sigaddset(&sigmask, SIGTERM);
76 sigaddset(&sigmask, SIGUSR1);
77 sigaddset(&sigmask, SIGHUP);
78 pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
80 listenSocket = socket(PF_INET, SOCK_STREAM, 0);
84 errorStr = "Create NET_CONFIGURATOR socket failed.";
85 logger("Cannot create a socket: %s", strerror(errno));
89 listenAddr.sin_family = PF_INET;
90 listenAddr.sin_port = htons(port);
91 listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
95 if (0 != setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &lng, 4))
97 errorStr = "Setsockopt failed. " + std::string(strerror(errno));
98 logger("setsockopt error: %s", strerror(errno));
102 res = bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr));
106 errorStr = "Bind admin socket failed";
107 logger("Cannot bind the socket: %s", strerror(errno));
111 res = listen(listenSocket, 0);
114 errorStr = "Listen admin socket failed";
115 logger("Cannot listen the socket: %s", strerror(errno));
123 //-----------------------------------------------------------------------------
124 int CONFIGPROTO::Stop()
130 struct sockaddr_in addr;
132 addr.sin_family = PF_INET;
133 addr.sin_port = htons(port);
134 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
136 addrLen = sizeof(addr);
137 sock = socket(PF_INET, SOCK_STREAM, 0);
138 connect(sock, (sockaddr*)&addr, addrLen);
143 //-----------------------------------------------------------------------------
144 void CONFIGPROTO::Run()
151 struct sockaddr_in outerAddr;
152 socklen_t outerAddrLen(sizeof(outerAddr));
153 int outerSocket = accept(listenSocket,
154 (struct sockaddr*)(&outerAddr),
162 logger("accept error: %s", strerror(errno));
163 printfd(__FILE__, "accept failed\n");
167 adminIP = *(unsigned int*)&(outerAddr.sin_addr);
169 if (state == confHdr)
171 if (RecvHdr(outerSocket) < 0)
176 if (state == confLogin)
178 if (SendHdrAnswer(outerSocket, ans_ok) < 0)
183 if (RecvLogin(outerSocket) < 0)
188 if (state == confLoginCipher)
190 if (SendLoginAnswer(outerSocket) < 0)
195 if (RecvLoginS(outerSocket) < 0)
200 if (state == confData)
202 if (SendLoginSAnswer(outerSocket, ans_ok) < 0)
207 if (RecvData(outerSocket) < 0)
216 if (SendLoginSAnswer(outerSocket, ans_err) < 0)
221 WriteLogAccessFailed(adminIP);
226 WriteLogAccessFailed(adminIP);
231 WriteLogAccessFailed(adminIP);
232 if (SendHdrAnswer(outerSocket, ans_err) < 0)
241 WriteLogAccessFailed(adminIP);
243 printfd(__FILE__, "Successfull connection from %s\n", inet_ntostring(outerAddr.sin_addr.s_addr).c_str());
247 //-----------------------------------------------------------------------------
248 int CONFIGPROTO::RecvHdr(int sock)
250 char buf[sizeof(STG_HEADER)];
251 memset(buf, 0, sizeof(STG_HEADER));
252 size_t stgHdrLen = sizeof(STG_HEADER) - 1; // Without 0-char
254 while (pos < stgHdrLen)
256 if (!WaitPackets(sock))
259 SendError("Bad request");
262 ssize_t ret = recv(sock, &buf[pos], static_cast<int>(stgHdrLen) - static_cast<int>(pos), 0);
266 logger("recv error: %s", strerror(errno));
273 if (0 == strncmp(buf, STG_HEADER, strlen(STG_HEADER)))
280 SendError("Bad request");
286 //-----------------------------------------------------------------------------
287 int CONFIGPROTO::SendHdrAnswer(int sock, int err)
291 if (send(sock, ERR_HEADER, sizeof(ERR_HEADER) - 1, 0) < 0)
293 logger("send error: %s", strerror(errno));
299 if (send(sock, OK_HEADER, sizeof(OK_HEADER) - 1, 0) < 0)
301 logger("send error: %s", strerror(errno));
308 //-----------------------------------------------------------------------------
309 int CONFIGPROTO::RecvLogin(int sock)
311 char login[ADM_LOGIN_LEN + 1];
313 memset(login, 0, ADM_LOGIN_LEN + 1);
316 while (pos < ADM_LOGIN_LEN) {
317 if (!WaitPackets(sock))
323 ssize_t ret = recv(sock, &login[pos], ADM_LOGIN_LEN - static_cast<int>(pos), 0);
328 logger("recv error: %s", strerror(errno));
336 if (admins->Find(login, &currAdmin))
343 currAdmin->SetIP(adminIP);
345 state = confLoginCipher;
348 //-----------------------------------------------------------------------------
349 int CONFIGPROTO::SendLoginAnswer(int sock)
351 if (send(sock, OK_LOGIN, sizeof(OK_LOGIN) - 1, 0) < 0)
353 logger("Send OK_LOGIN error in SendLoginAnswer.");
358 //-----------------------------------------------------------------------------
359 int CONFIGPROTO::RecvLoginS(int sock)
361 char loginS[ADM_LOGIN_LEN + 1];
362 memset(loginS, 0, ADM_LOGIN_LEN + 1);
365 while (pos < ADM_LOGIN_LEN)
367 if (!WaitPackets(sock))
373 ssize_t ret = recv(sock, &loginS[pos], ADM_LOGIN_LEN - static_cast<int>(pos), 0);
378 printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
379 logger("recv error: %s", strerror(errno));
387 if (currAdmin->GetLogin().empty())
394 EnDecodeInit(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
396 char login[ADM_LOGIN_LEN + 1];
397 for (size_t i = 0; i < ADM_LOGIN_LEN / 8; i++)
398 DecodeString(login + i * 8, loginS + i * 8, &ctx);
400 if (currAdmin == admins->GetNoAdmin())
402 // If there are no admins registered in the system - give access with any password
407 if (strncmp(currAdmin->GetLogin().c_str(), login, ADM_LOGIN_LEN) != 0)
414 adminPassword = currAdmin->GetPassword();
417 //-----------------------------------------------------------------------------
418 int CONFIGPROTO::SendLoginSAnswer(int sock, int err)
422 if (send(sock, ERR_LOGINS, sizeof(ERR_LOGINS) - 1, 0) < 0)
424 logger("send error: %s", strerror(errno));
430 if (send(sock, OK_LOGINS, sizeof(OK_LOGINS) - 1, 0) < 0)
432 logger("send error: %s", strerror(errno));
438 //-----------------------------------------------------------------------------
439 int CONFIGPROTO::RecvData(int sock)
444 EnDecodeInit(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
451 while (pos < sizeof(bufferS))
453 if (!WaitPackets(sock))
459 ssize_t ret = recv(sock, &bufferS[pos], sizeof(bufferS) - static_cast<int>(pos), 0);
463 logger("recv error: %s", strerror(errno));
464 printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
480 DecodeString(buffer, bufferS, &ctx);
481 requestList.push_back(std::string(buffer, pos));
483 if (done || memchr(buffer, 0, pos) != NULL)
488 SendError("Bad command");
490 return SendDataAnswer(sock);
495 //-----------------------------------------------------------------------------
496 int CONFIGPROTO::SendDataAnswer(int sock)
498 std::list<std::string>::iterator li;
499 li = answerList.begin();
508 EnDecodeInit(adminPassword.c_str(), ADM_PASSWD_LEN, &ctx);
510 while (li != answerList.end())
512 while ((*li).c_str()[k])
514 buff[n % 8] = (*li).c_str()[k];
520 EncodeString(buffS, buff, &ctx);
521 if (send(sock, buffS, 8, 0) < 0)
529 if (answerList.empty()) {
534 EncodeString(buffS, buff, &ctx);
538 return static_cast<int>(send(sock, buffS, 8, 0));
540 //-----------------------------------------------------------------------------
541 void CONFIGPROTO::SendError(const char * text)
545 snprintf(s, 255, "<Error value=\"%s\"/>", text);
546 answerList.push_back(s);
548 //-----------------------------------------------------------------------------
549 void CONFIGPROTO::WriteLogAccessFailed(uint32_t ip)
551 logger("Admin's connection failed. IP %s", inet_ntostring(ip).c_str());
553 //-----------------------------------------------------------------------------