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"
33 #include "stg/logger.h"
34 #include "stg/common.h"
35 #include "stg/blowfish.h"
39 #include <cstdio> // snprintf
40 #include <cstring> // strerror
42 #include <unistd.h> // close
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
67 //-----------------------------------------------------------------------------
68 int CONFIGPROTO::Prepare()
71 struct sockaddr_in listenAddr;
73 sigset_t sigmask, oldmask;
74 sigemptyset(&sigmask);
75 sigaddset(&sigmask, SIGINT);
76 sigaddset(&sigmask, SIGTERM);
77 sigaddset(&sigmask, SIGUSR1);
78 sigaddset(&sigmask, SIGHUP);
79 pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
81 listenSocket = socket(PF_INET, SOCK_STREAM, 0);
85 errorStr = "Create NET_CONFIGURATOR socket failed.";
86 logger("Cannot create a socket: %s", strerror(errno));
90 listenAddr.sin_family = PF_INET;
91 listenAddr.sin_port = htons(port);
92 listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
96 if (0 != setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &lng, 4))
98 errorStr = "Setsockopt failed. " + std::string(strerror(errno));
99 logger("setsockopt error: %s", strerror(errno));
103 res = bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr));
107 errorStr = "Bind admin socket failed";
108 logger("Cannot bind the socket: %s", strerror(errno));
112 res = listen(listenSocket, 0);
115 errorStr = "Listen admin socket failed";
116 logger("Cannot listen the socket: %s", strerror(errno));
124 //-----------------------------------------------------------------------------
125 int CONFIGPROTO::Stop()
131 struct sockaddr_in addr;
133 addr.sin_family = PF_INET;
134 addr.sin_port = htons(port);
135 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
137 addrLen = sizeof(addr);
138 sock = socket(PF_INET, SOCK_STREAM, 0);
139 connect(sock, (sockaddr*)&addr, addrLen);
144 //-----------------------------------------------------------------------------
145 void CONFIGPROTO::Run()
152 struct sockaddr_in outerAddr;
153 socklen_t outerAddrLen(sizeof(outerAddr));
154 int outerSocket = accept(listenSocket,
155 (struct sockaddr*)(&outerAddr),
163 logger("accept error: %s", strerror(errno));
164 printfd(__FILE__, "accept failed\n");
168 adminIP = *(unsigned int*)&(outerAddr.sin_addr);
170 if (state == confHdr)
172 if (RecvHdr(outerSocket) < 0)
177 if (state == confLogin)
179 if (SendHdrAnswer(outerSocket, ans_ok) < 0)
184 if (RecvLogin(outerSocket) < 0)
189 if (state == confLoginCipher)
191 if (SendLoginAnswer(outerSocket) < 0)
196 if (RecvLoginS(outerSocket) < 0)
201 if (state == confData)
203 if (SendLoginSAnswer(outerSocket, ans_ok) < 0)
208 if (RecvData(outerSocket) < 0)
217 if (SendLoginSAnswer(outerSocket, ans_err) < 0)
222 WriteLogAccessFailed(adminIP);
227 WriteLogAccessFailed(adminIP);
232 WriteLogAccessFailed(adminIP);
233 if (SendHdrAnswer(outerSocket, ans_err) < 0)
242 WriteLogAccessFailed(adminIP);
244 printfd(__FILE__, "Successfull connection from %s\n", inet_ntostring(outerAddr.sin_addr.s_addr).c_str());
248 //-----------------------------------------------------------------------------
249 int CONFIGPROTO::RecvHdr(int sock)
251 char buf[sizeof(STG_HEADER)];
252 memset(buf, 0, sizeof(STG_HEADER));
253 size_t stgHdrLen = sizeof(STG_HEADER) - 1; // Without 0-char
255 while (pos < stgHdrLen)
257 if (!WaitPackets(sock))
260 SendError(sock, "Bad request");
263 ssize_t ret = recv(sock, &buf[pos], static_cast<int>(stgHdrLen) - static_cast<int>(pos), 0);
267 logger("recv error: %s", strerror(errno));
274 if (0 == strncmp(buf, STG_HEADER, strlen(STG_HEADER)))
281 SendError(sock, "Bad request");
287 //-----------------------------------------------------------------------------
288 int CONFIGPROTO::SendHdrAnswer(int sock, int err)
292 if (send(sock, ERR_HEADER, sizeof(ERR_HEADER) - 1, 0) < 0)
294 logger("send error: %s", strerror(errno));
300 if (send(sock, OK_HEADER, sizeof(OK_HEADER) - 1, 0) < 0)
302 logger("send error: %s", strerror(errno));
309 //-----------------------------------------------------------------------------
310 int CONFIGPROTO::RecvLogin(int sock)
312 char login[ADM_LOGIN_LEN + 1];
314 memset(login, 0, ADM_LOGIN_LEN + 1);
317 while (pos < ADM_LOGIN_LEN) {
318 if (!WaitPackets(sock))
324 ssize_t ret = recv(sock, &login[pos], ADM_LOGIN_LEN - static_cast<int>(pos), 0);
329 logger("recv error: %s", strerror(errno));
337 if (admins->Find(login, &currAdmin))
344 currAdmin->SetIP(adminIP);
346 state = confLoginCipher;
349 //-----------------------------------------------------------------------------
350 int CONFIGPROTO::SendLoginAnswer(int sock)
352 if (send(sock, OK_LOGIN, sizeof(OK_LOGIN) - 1, 0) < 0)
354 logger("Send OK_LOGIN error in SendLoginAnswer.");
359 //-----------------------------------------------------------------------------
360 int CONFIGPROTO::RecvLoginS(int sock)
362 char loginS[ADM_LOGIN_LEN + 1];
363 memset(loginS, 0, ADM_LOGIN_LEN + 1);
366 while (pos < ADM_LOGIN_LEN)
368 if (!WaitPackets(sock))
374 ssize_t ret = recv(sock, &loginS[pos], ADM_LOGIN_LEN - static_cast<int>(pos), 0);
379 printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
380 logger("recv error: %s", strerror(errno));
388 if (currAdmin->GetLogin().empty())
395 EnDecodeInit(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
397 char login[ADM_LOGIN_LEN + 1];
398 for (size_t i = 0; i < ADM_LOGIN_LEN / 8; i++)
399 DecodeString(login + i * 8, loginS + i * 8, &ctx);
401 if (currAdmin == admins->GetNoAdmin())
403 // If there are no admins registered in the system - give access with any password
408 if (strncmp(currAdmin->GetLogin().c_str(), login, ADM_LOGIN_LEN) != 0)
415 adminPassword = currAdmin->GetPassword();
418 //-----------------------------------------------------------------------------
419 int CONFIGPROTO::SendLoginSAnswer(int sock, int err)
423 if (send(sock, ERR_LOGINS, sizeof(ERR_LOGINS) - 1, 0) < 0)
425 logger("send error: %s", strerror(errno));
431 if (send(sock, OK_LOGINS, sizeof(OK_LOGINS) - 1, 0) < 0)
433 logger("send error: %s", strerror(errno));
439 //-----------------------------------------------------------------------------
440 int CONFIGPROTO::RecvData(int sock)
445 EnDecodeInit(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
452 while (pos < sizeof(bufferS))
454 if (!WaitPackets(sock))
460 ssize_t ret = recv(sock, &bufferS[pos], sizeof(bufferS) - static_cast<int>(pos), 0);
464 logger("recv error: %s", strerror(errno));
465 printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
481 DecodeString(buffer, bufferS, &ctx);
482 requestList.push_back(std::string(buffer, pos));
484 if (done || memchr(buffer, 0, pos) != NULL)
488 return SendError(sock, "Bad command");
490 return SendDataAnswer(sock, currParser->GetAnswer());
495 //-----------------------------------------------------------------------------
496 int CONFIGPROTO::SendDataAnswer(int sock, const std::string & answer)
502 EnDecodeInit(adminPassword.c_str(), ADM_PASSWD_LEN, &ctx);
504 std::string::size_type pos = 0;
505 std::string::size_type length = answer.length();
509 std::string::size_type chunkLength = std::min(length - pos, sizeof(buffer));
510 EncodeFullString(buffer, answer.c_str() + pos, chunkLength, ctx);
511 if (send(sock, buffer, chunkLength, 0) < 0)
518 //-----------------------------------------------------------------------------
519 int CONFIGPROTO::SendError(int sock, const std::string & text)
521 return SendDataAnswer(sock, "<Error value=\"" + text + "\"/>");
523 //-----------------------------------------------------------------------------
524 void CONFIGPROTO::WriteLogAccessFailed(uint32_t ip)
526 logger("Admin's connection failed. IP %s", inet_ntostring(ip).c_str());
528 //-----------------------------------------------------------------------------