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),
164 logger("accept error: %s", strerror(errno));
165 printfd(__FILE__, "accept failed\n");
169 adminIP = *(unsigned int*)&(outerAddr.sin_addr);
171 if (state == confHdr)
173 if (RecvHdr(outerSocket) < 0)
178 if (state == confLogin)
180 if (SendHdrAnswer(outerSocket, ans_ok) < 0)
185 if (RecvLogin(outerSocket) < 0)
190 if (state == confLoginCipher)
192 if (SendLoginAnswer(outerSocket) < 0)
197 if (RecvLoginS(outerSocket) < 0)
202 if (state == confData)
204 if (SendLoginSAnswer(outerSocket, ans_ok) < 0)
209 if (RecvData(outerSocket) < 0)
218 if (SendLoginSAnswer(outerSocket, ans_err) < 0)
223 WriteLogAccessFailed(adminIP);
228 WriteLogAccessFailed(adminIP);
233 WriteLogAccessFailed(adminIP);
234 if (SendHdrAnswer(outerSocket, ans_err) < 0)
243 WriteLogAccessFailed(adminIP);
245 printfd(__FILE__, "Successfull connection from %s\n", inet_ntostring(outerAddr.sin_addr.s_addr).c_str());
249 //-----------------------------------------------------------------------------
250 int CONFIGPROTO::RecvHdr(int sock)
252 char buf[sizeof(STG_HEADER)];
253 memset(buf, 0, sizeof(STG_HEADER));
254 size_t stgHdrLen = sizeof(STG_HEADER) - 1; // Without 0-char
256 while (pos < stgHdrLen)
258 if (!WaitPackets(sock))
261 SendError("Bad request");
264 ssize_t ret = recv(sock, &buf[pos], static_cast<int>(stgHdrLen) - static_cast<int>(pos), 0);
268 logger("recv error: %s", strerror(errno));
275 if (0 == strncmp(buf, STG_HEADER, strlen(STG_HEADER)))
282 SendError("Bad request");
288 //-----------------------------------------------------------------------------
289 int CONFIGPROTO::SendHdrAnswer(int sock, int err)
293 if (send(sock, ERR_HEADER, sizeof(ERR_HEADER) - 1, 0) < 0)
295 logger("send error: %s", strerror(errno));
301 if (send(sock, OK_HEADER, sizeof(OK_HEADER) - 1, 0) < 0)
303 logger("send error: %s", strerror(errno));
310 //-----------------------------------------------------------------------------
311 int CONFIGPROTO::RecvLogin(int sock)
313 char login[ADM_LOGIN_LEN + 1];
315 memset(login, 0, ADM_LOGIN_LEN + 1);
318 while (pos < ADM_LOGIN_LEN) {
319 if (!WaitPackets(sock))
325 ssize_t ret = recv(sock, &login[pos], ADM_LOGIN_LEN - static_cast<int>(pos), 0);
330 logger("recv error: %s", strerror(errno));
338 if (admins->Find(login, &currAdmin))
345 currAdmin->SetIP(adminIP);
347 state = confLoginCipher;
350 //-----------------------------------------------------------------------------
351 int CONFIGPROTO::SendLoginAnswer(int sock)
353 if (send(sock, OK_LOGIN, sizeof(OK_LOGIN) - 1, 0) < 0)
355 logger("Send OK_LOGIN error in SendLoginAnswer.");
360 //-----------------------------------------------------------------------------
361 int CONFIGPROTO::RecvLoginS(int sock)
363 char loginS[ADM_LOGIN_LEN + 1];
364 memset(loginS, 0, ADM_LOGIN_LEN + 1);
367 while (pos < ADM_LOGIN_LEN)
369 if (!WaitPackets(sock))
375 ssize_t ret = recv(sock, &loginS[pos], ADM_LOGIN_LEN - static_cast<int>(pos), 0);
380 printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
381 logger("recv error: %s", strerror(errno));
389 if (currAdmin->GetLogin().empty())
396 EnDecodeInit(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
398 char login[ADM_LOGIN_LEN + 1];
399 for (size_t i = 0; i < ADM_LOGIN_LEN / 8; i++)
401 DecodeString(login + i * 8, loginS + i * 8, &ctx);
404 if (currAdmin == admins->GetNoAdmin())
406 // If there are no admins registered in the system - give access with any password
411 if (strncmp(currAdmin->GetLogin().c_str(), login, ADM_LOGIN_LEN) != 0)
418 adminPassword = currAdmin->GetPassword();
421 //-----------------------------------------------------------------------------
422 int CONFIGPROTO::SendLoginSAnswer(int sock, int err)
426 if (send(sock, ERR_LOGINS, sizeof(ERR_LOGINS) - 1, 0) < 0)
428 logger("send error: %s", strerror(errno));
434 if (send(sock, OK_LOGINS, sizeof(OK_LOGINS) - 1, 0) < 0)
436 logger("send error: %s", strerror(errno));
442 //-----------------------------------------------------------------------------
443 int CONFIGPROTO::RecvData(int sock)
448 EnDecodeInit(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
455 while (pos < sizeof(bufferS))
457 if (!WaitPackets(sock))
463 ssize_t ret = recv(sock, &bufferS[pos], sizeof(bufferS) - static_cast<int>(pos), 0);
467 logger("recv error: %s", strerror(errno));
468 printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
484 DecodeString(buffer, bufferS, &ctx);
485 requestList.push_back(std::string(buffer, pos));
487 if (done || memchr(buffer, 0, pos) != NULL)
492 SendError("Bad command");
494 return SendDataAnswer(sock);
499 //-----------------------------------------------------------------------------
500 int CONFIGPROTO::SendDataAnswer(int sock)
502 std::list<std::string>::iterator li;
503 li = answerList.begin();
512 EnDecodeInit(adminPassword.c_str(), ADM_PASSWD_LEN, &ctx);
514 while (li != answerList.end())
516 while ((*li).c_str()[k])
518 buff[n % 8] = (*li).c_str()[k];
524 EncodeString(buffS, buff, &ctx);
525 int ret = static_cast<int>(send(sock, buffS, 8, 0));
536 if (answerList.empty()) {
541 EncodeString(buffS, buff, &ctx);
545 return static_cast<int>(send(sock, buffS, 8, 0));
547 //-----------------------------------------------------------------------------
548 void CONFIGPROTO::SendError(const char * text)
552 snprintf(s, 255, "<Error value=\"%s\"/>", text);
553 answerList.push_back(s);
555 //-----------------------------------------------------------------------------
556 void CONFIGPROTO::WriteLogAccessFailed(uint32_t ip)
558 logger("Admin's connection failed. IP %s", inet_ntostring(ip).c_str());
560 //-----------------------------------------------------------------------------