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),
165 logger("accept error: %s", strerror(errno));
166 printfd(__FILE__, "accept failed\n");
170 adminIP = *(unsigned int*)&(outerAddr.sin_addr);
172 if (state == confHdr)
174 if (RecvHdr(outerSocket) < 0)
179 if (state == confLogin)
181 if (SendHdrAnswer(outerSocket, ans_ok) < 0)
186 if (RecvLogin(outerSocket) < 0)
191 if (state == confLoginCipher)
193 if (SendLoginAnswer(outerSocket) < 0)
198 if (RecvLoginS(outerSocket) < 0)
203 if (state == confData)
205 if (SendLoginSAnswer(outerSocket, ans_ok) < 0)
210 if (RecvData(outerSocket) < 0)
219 if (SendLoginSAnswer(outerSocket, ans_err) < 0)
224 WriteLogAccessFailed(adminIP);
229 WriteLogAccessFailed(adminIP);
234 WriteLogAccessFailed(adminIP);
235 if (SendHdrAnswer(outerSocket, ans_err) < 0)
244 WriteLogAccessFailed(adminIP);
246 printfd(__FILE__, "Successfull connection from %s\n", inet_ntostring(outerAddr.sin_addr.s_addr).c_str());
250 //-----------------------------------------------------------------------------
251 int CONFIGPROTO::RecvHdr(int sock)
253 char buf[sizeof(STG_HEADER)];
254 memset(buf, 0, sizeof(STG_HEADER));
255 size_t stgHdrLen = sizeof(STG_HEADER) - 1; // Without 0-char
257 while (pos < stgHdrLen)
259 if (!WaitPackets(sock))
262 SendError(sock, "Bad request");
265 ssize_t ret = recv(sock, &buf[pos], static_cast<int>(stgHdrLen) - static_cast<int>(pos), 0);
269 logger("recv error: %s", strerror(errno));
276 if (0 == strncmp(buf, STG_HEADER, strlen(STG_HEADER)))
283 SendError(sock, "Bad request");
289 //-----------------------------------------------------------------------------
290 int CONFIGPROTO::SendHdrAnswer(int sock, int err)
294 if (send(sock, ERR_HEADER, sizeof(ERR_HEADER) - 1, 0) < 0)
296 logger("send error: %s", strerror(errno));
302 if (send(sock, OK_HEADER, sizeof(OK_HEADER) - 1, 0) < 0)
304 logger("send error: %s", strerror(errno));
311 //-----------------------------------------------------------------------------
312 int CONFIGPROTO::RecvLogin(int sock)
314 char login[ADM_LOGIN_LEN + 1];
316 memset(login, 0, ADM_LOGIN_LEN + 1);
319 while (pos < ADM_LOGIN_LEN) {
320 if (!WaitPackets(sock))
326 ssize_t ret = recv(sock, &login[pos], ADM_LOGIN_LEN - static_cast<int>(pos), 0);
331 logger("recv error: %s", strerror(errno));
339 if (admins->Find(login, &currAdmin))
346 currAdmin->SetIP(adminIP);
348 state = confLoginCipher;
351 //-----------------------------------------------------------------------------
352 int CONFIGPROTO::SendLoginAnswer(int sock)
354 if (send(sock, OK_LOGIN, sizeof(OK_LOGIN) - 1, 0) < 0)
356 logger("Send OK_LOGIN error in SendLoginAnswer.");
361 //-----------------------------------------------------------------------------
362 int CONFIGPROTO::RecvLoginS(int sock)
364 char loginS[ADM_LOGIN_LEN + 1];
365 memset(loginS, 0, ADM_LOGIN_LEN + 1);
368 while (pos < ADM_LOGIN_LEN)
370 if (!WaitPackets(sock))
376 ssize_t ret = recv(sock, &loginS[pos], ADM_LOGIN_LEN - static_cast<int>(pos), 0);
381 printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
382 logger("recv error: %s", strerror(errno));
390 if (currAdmin->GetLogin().empty())
397 EnDecodeInit(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
399 char login[ADM_LOGIN_LEN + 1];
400 for (size_t i = 0; i < ADM_LOGIN_LEN / 8; i++)
402 DecodeString(login + i * 8, loginS + i * 8, &ctx);
405 if (currAdmin == admins->GetNoAdmin())
407 // If there are no admins registered in the system - give access with any password
412 if (strncmp(currAdmin->GetLogin().c_str(), login, ADM_LOGIN_LEN) != 0)
419 adminPassword = currAdmin->GetPassword();
422 //-----------------------------------------------------------------------------
423 int CONFIGPROTO::SendLoginSAnswer(int sock, int err)
427 if (send(sock, ERR_LOGINS, sizeof(ERR_LOGINS) - 1, 0) < 0)
429 logger("send error: %s", strerror(errno));
435 if (send(sock, OK_LOGINS, sizeof(OK_LOGINS) - 1, 0) < 0)
437 logger("send error: %s", strerror(errno));
443 //-----------------------------------------------------------------------------
444 int CONFIGPROTO::RecvData(int sock)
449 EnDecodeInit(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
456 while (pos < sizeof(bufferS))
458 if (!WaitPackets(sock))
464 ssize_t ret = recv(sock, &bufferS[pos], sizeof(bufferS) - static_cast<int>(pos), 0);
468 logger("recv error: %s", strerror(errno));
469 printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
485 DecodeString(buffer, bufferS, &ctx);
486 requestList.push_back(std::string(buffer, pos));
488 if (done || memchr(buffer, 0, pos) != NULL)
492 return SendError(sock, "Bad command");
494 return SendDataAnswer(sock, currParser->GetAnswer());
499 //-----------------------------------------------------------------------------
500 int CONFIGPROTO::SendDataAnswer(int sock, const std::string & answer)
506 EnDecodeInit(adminPassword.c_str(), ADM_PASSWD_LEN, &ctx);
508 std::string::size_type pos = 0;
509 std::string::size_type length = answer.length();
513 std::string::size_type chunkLength = std::min(length - pos, sizeof(buffer));
514 EncodeFullString(buffer, answer.c_str() + pos, chunkLength, ctx);
515 if (send(sock, buffer, chunkLength, 0) < 0)
522 //-----------------------------------------------------------------------------
523 int CONFIGPROTO::SendError(int sock, const std::string & text)
525 return SendDataAnswer(sock, "<Error value=\"" + text + "\"/>");
527 //-----------------------------------------------------------------------------
528 void CONFIGPROTO::WriteLogAccessFailed(uint32_t ip)
530 logger("Admin's connection failed. IP %s", inet_ntostring(ip).c_str());
532 //-----------------------------------------------------------------------------