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
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
21 #include "configproto.h"
25 #include "stg/admins.h"
26 #include "stg/logger.h"
27 #include "stg/common.h"
28 #include "stg/blowfish.h"
32 #include <cstdio> // snprintf
33 #include <cstring> // strerror
35 #include <unistd.h> // close
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
60 //-----------------------------------------------------------------------------
61 int CONFIGPROTO::Prepare()
64 struct sockaddr_in listenAddr;
66 sigset_t sigmask, oldmask;
67 sigemptyset(&sigmask);
68 sigaddset(&sigmask, SIGINT);
69 sigaddset(&sigmask, SIGTERM);
70 sigaddset(&sigmask, SIGUSR1);
71 sigaddset(&sigmask, SIGHUP);
72 pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
74 listenSocket = socket(PF_INET, SOCK_STREAM, 0);
78 errorStr = "Create NET_CONFIGURATOR socket failed.";
79 logger("Cannot create a socket: %s", strerror(errno));
83 listenAddr.sin_family = PF_INET;
84 listenAddr.sin_port = htons(port);
85 listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
89 if (0 != setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &lng, 4))
91 errorStr = "Setsockopt failed. " + std::string(strerror(errno));
92 logger("setsockopt error: %s", strerror(errno));
96 res = bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr));
100 errorStr = "Bind admin socket failed";
101 logger("Cannot bind the socket: %s", strerror(errno));
105 res = listen(listenSocket, 0);
108 errorStr = "Listen admin socket failed";
109 logger("Cannot listen the socket: %s", strerror(errno));
117 //-----------------------------------------------------------------------------
118 int CONFIGPROTO::Stop()
124 struct sockaddr_in addr;
126 addr.sin_family = PF_INET;
127 addr.sin_port = htons(port);
128 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
130 addrLen = sizeof(addr);
131 sock = socket(PF_INET, SOCK_STREAM, 0);
132 connect(sock, (sockaddr*)&addr, addrLen);
137 //-----------------------------------------------------------------------------
138 void CONFIGPROTO::Run()
145 struct sockaddr_in outerAddr;
146 socklen_t outerAddrLen(sizeof(outerAddr));
147 int outerSocket = accept(listenSocket,
148 (struct sockaddr*)(&outerAddr),
156 logger("accept error: %s", strerror(errno));
157 printfd(__FILE__, "accept failed\n");
161 adminIP = *(unsigned int*)&(outerAddr.sin_addr);
163 if (state == confHdr)
165 if (RecvHdr(outerSocket) < 0)
170 if (state == confLogin)
172 if (SendHdrAnswer(outerSocket, ans_ok) < 0)
177 if (RecvLogin(outerSocket) < 0)
182 if (state == confLoginCipher)
184 if (SendLoginAnswer(outerSocket) < 0)
189 if (RecvLoginS(outerSocket) < 0)
194 if (state == confData)
196 if (SendLoginSAnswer(outerSocket, ans_ok) < 0)
201 if (RecvData(outerSocket) < 0)
210 if (SendLoginSAnswer(outerSocket, ans_err) < 0)
215 WriteLogAccessFailed(adminIP);
220 WriteLogAccessFailed(adminIP);
225 WriteLogAccessFailed(adminIP);
226 if (SendHdrAnswer(outerSocket, ans_err) < 0)
235 WriteLogAccessFailed(adminIP);
237 printfd(__FILE__, "Successfull connection from %s\n", inet_ntostring(outerAddr.sin_addr.s_addr).c_str());
241 //-----------------------------------------------------------------------------
242 int CONFIGPROTO::RecvHdr(int sock)
244 char buf[sizeof(STG_HEADER)];
245 memset(buf, 0, sizeof(STG_HEADER));
246 size_t stgHdrLen = sizeof(STG_HEADER) - 1; // Without 0-char
248 while (pos < stgHdrLen)
250 if (!WaitPackets(sock))
253 SendError(sock, "Bad request");
256 ssize_t ret = recv(sock, &buf[pos], static_cast<int>(stgHdrLen) - static_cast<int>(pos), 0);
260 logger("recv error: %s", strerror(errno));
267 if (0 == strncmp(buf, STG_HEADER, strlen(STG_HEADER)))
274 SendError(sock, "Bad request");
280 //-----------------------------------------------------------------------------
281 int CONFIGPROTO::SendHdrAnswer(int sock, int err)
285 if (send(sock, ERR_HEADER, sizeof(ERR_HEADER) - 1, 0) < 0)
287 logger("send error: %s", strerror(errno));
293 if (send(sock, OK_HEADER, sizeof(OK_HEADER) - 1, 0) < 0)
295 logger("send error: %s", strerror(errno));
302 //-----------------------------------------------------------------------------
303 int CONFIGPROTO::RecvLogin(int sock)
305 char login[ADM_LOGIN_LEN + 1];
307 memset(login, 0, ADM_LOGIN_LEN + 1);
310 while (pos < ADM_LOGIN_LEN) {
311 if (!WaitPackets(sock))
317 ssize_t ret = recv(sock, &login[pos], ADM_LOGIN_LEN - static_cast<int>(pos), 0);
322 logger("recv error: %s", strerror(errno));
330 if (admins->Find(login, &currAdmin))
337 currAdmin->SetIP(adminIP);
339 state = confLoginCipher;
342 //-----------------------------------------------------------------------------
343 int CONFIGPROTO::SendLoginAnswer(int sock)
345 if (send(sock, OK_LOGIN, sizeof(OK_LOGIN) - 1, 0) < 0)
347 logger("Send OK_LOGIN error in SendLoginAnswer.");
352 //-----------------------------------------------------------------------------
353 int CONFIGPROTO::RecvLoginS(int sock)
355 char loginS[ADM_LOGIN_LEN + 1];
356 memset(loginS, 0, ADM_LOGIN_LEN + 1);
359 while (pos < ADM_LOGIN_LEN)
361 if (!WaitPackets(sock))
367 ssize_t ret = recv(sock, &loginS[pos], ADM_LOGIN_LEN - static_cast<int>(pos), 0);
372 printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
373 logger("recv error: %s", strerror(errno));
381 if (currAdmin->GetLogin().empty())
388 InitContext(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
390 char login[ADM_LOGIN_LEN + 1];
391 for (size_t i = 0; i < ADM_LOGIN_LEN / 8; i++)
392 DecryptBlock(login + i * 8, loginS + i * 8, &ctx);
394 if (currAdmin == admins->GetNoAdmin())
396 // If there are no admins registered in the system - give access with any password
401 if (strncmp(currAdmin->GetLogin().c_str(), login, ADM_LOGIN_LEN) != 0)
408 adminPassword = currAdmin->GetPassword();
411 //-----------------------------------------------------------------------------
412 int CONFIGPROTO::SendLoginSAnswer(int sock, int err)
416 if (send(sock, ERR_LOGINS, sizeof(ERR_LOGINS) - 1, 0) < 0)
418 logger("send error: %s", strerror(errno));
424 if (send(sock, OK_LOGINS, sizeof(OK_LOGINS) - 1, 0) < 0)
426 logger("send error: %s", strerror(errno));
432 //-----------------------------------------------------------------------------
433 int CONFIGPROTO::RecvData(int sock)
438 InitContext(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
445 while (pos < sizeof(bufferS))
447 if (!WaitPackets(sock))
453 ssize_t ret = recv(sock, &bufferS[pos], sizeof(bufferS) - static_cast<int>(pos), 0);
457 logger("recv error: %s", strerror(errno));
458 printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
474 DecryptBlock(buffer, bufferS, &ctx);
475 requestList.push_back(std::string(buffer, pos));
477 if (done || memchr(buffer, 0, pos) != NULL)
481 return SendError(sock, "Bad command");
483 return SendDataAnswer(sock, GetDataAnswer());
488 //-----------------------------------------------------------------------------
489 int CONFIGPROTO::SendDataAnswer(int sock, const std::string & answer)
495 InitContext(adminPassword.c_str(), ADM_PASSWD_LEN, &ctx);
497 std::string::size_type pos = 0;
498 std::string::size_type length = answer.length() + 1;
502 std::string::size_type chunkLength = std::min(length - pos, sizeof(buffer));
503 EncryptString(buffer, answer.c_str() + pos, chunkLength, &ctx);
504 if (send(sock, buffer, (chunkLength & ~7) < chunkLength ? chunkLength + 8 : chunkLength, 0) < 0) // Need to send data adjusted to the 8-byte boundary.
511 //-----------------------------------------------------------------------------
512 int CONFIGPROTO::SendError(int sock, const std::string & text)
514 return SendDataAnswer(sock, "<Error value=\"" + text + "\"/>");
516 //-----------------------------------------------------------------------------
517 void CONFIGPROTO::WriteLogAccessFailed(uint32_t ip)
519 logger("Admin's connection failed. IP %s", inet_ntostring(ip).c_str());
521 //-----------------------------------------------------------------------------