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 <cstring> // strerror
34 #include <unistd.h> // close
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
59 //-----------------------------------------------------------------------------
60 int CONFIGPROTO::Prepare()
62 sigset_t sigmask, oldmask;
63 sigemptyset(&sigmask);
64 sigaddset(&sigmask, SIGINT);
65 sigaddset(&sigmask, SIGTERM);
66 sigaddset(&sigmask, SIGUSR1);
67 sigaddset(&sigmask, SIGHUP);
68 pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
70 listenSocket = socket(PF_INET, SOCK_STREAM, 0);
74 errorStr = "Create socket failed.";
75 logger("Cannot create a socket: %s", strerror(errno));
79 struct sockaddr_in listenAddr;
80 listenAddr.sin_family = PF_INET;
81 listenAddr.sin_port = htons(port);
82 listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
86 if (0 != setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &lng, 4))
88 errorStr = "Setsockopt failed. " + std::string(strerror(errno));
89 logger("setsockopt error: %s", strerror(errno));
93 if (bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr)) == -1)
95 errorStr = "Bind admin socket failed";
96 logger("Cannot bind the socket: %s", strerror(errno));
100 if (listen(listenSocket, 0) == -1)
102 errorStr = "Listen admin socket failed";
103 logger("Cannot listen the socket: %s", strerror(errno));
111 //-----------------------------------------------------------------------------
112 int CONFIGPROTO::Stop()
115 shutdown(listenSocket, SHUT_RDWR);
118 struct sockaddr_in addr;
119 addr.sin_family = PF_INET;
120 addr.sin_port = htons(port);
121 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
123 socklen_t addrLen = sizeof(addr);
124 int sock = socket(PF_INET, SOCK_STREAM, 0);
125 connect(sock, (sockaddr*)&addr, addrLen);
126 shutdown(sock, SHUT_RDWR);
131 //-----------------------------------------------------------------------------
132 void CONFIGPROTO::Run()
137 struct sockaddr_in outerAddr;
138 socklen_t outerAddrLen(sizeof(outerAddr));
139 int outerSocket = accept(listenSocket,
140 (struct sockaddr*)(&outerAddr),
148 logger("accept error: %s", strerror(errno));
149 printfd(__FILE__, "accept failed\n");
153 adminIP = *(unsigned int*)&(outerAddr.sin_addr);
155 if (state == confHdr)
157 if (RecvHdr(outerSocket) < 0)
159 shutdown(outerSocket, SHUT_RDWR);
163 if (state == confLogin)
165 if (SendHdrAnswer(outerSocket, ans_ok) < 0)
167 shutdown(outerSocket, SHUT_RDWR);
171 if (RecvLogin(outerSocket) < 0)
173 shutdown(outerSocket, SHUT_RDWR);
177 if (state == confLoginCipher)
179 if (SendLoginAnswer(outerSocket) < 0)
181 shutdown(outerSocket, SHUT_RDWR);
185 if (RecvLoginS(outerSocket) < 0)
187 shutdown(outerSocket, SHUT_RDWR);
191 if (state == confData)
193 if (SendLoginSAnswer(outerSocket, ans_ok) < 0)
195 shutdown(outerSocket, SHUT_RDWR);
199 if (RecvData(outerSocket) < 0)
201 shutdown(outerSocket, SHUT_RDWR);
209 if (SendLoginSAnswer(outerSocket, ans_err) < 0)
211 shutdown(outerSocket, SHUT_RDWR);
215 WriteLogAccessFailed(adminIP);
220 WriteLogAccessFailed(adminIP);
225 WriteLogAccessFailed(adminIP);
226 if (SendHdrAnswer(outerSocket, ans_err) < 0)
228 shutdown(outerSocket, SHUT_RDWR);
236 WriteLogAccessFailed(adminIP);
238 printfd(__FILE__, "Successfull connection from %s\n", inet_ntostring(outerAddr.sin_addr.s_addr).c_str());
239 shutdown(outerSocket, SHUT_RDWR);
243 //-----------------------------------------------------------------------------
244 int CONFIGPROTO::RecvHdr(int sock)
246 char buf[sizeof(STG_HEADER)];
247 memset(buf, 0, sizeof(STG_HEADER));
248 size_t stgHdrLen = sizeof(STG_HEADER) - 1; // Without 0-char
250 while (pos < stgHdrLen)
252 if (!WaitPackets(sock))
255 SendError(sock, "Bad request");
258 ssize_t ret = recv(sock, &buf[pos], static_cast<int>(stgHdrLen) - static_cast<int>(pos), 0);
262 logger("recv error: %s", strerror(errno));
269 if (0 == strncmp(buf, STG_HEADER, strlen(STG_HEADER)))
276 SendError(sock, "Bad request");
282 //-----------------------------------------------------------------------------
283 int CONFIGPROTO::SendHdrAnswer(int sock, int err)
287 if (send(sock, ERR_HEADER, sizeof(ERR_HEADER) - 1, 0) < 0)
289 logger("send error: %s", strerror(errno));
295 if (send(sock, OK_HEADER, sizeof(OK_HEADER) - 1, 0) < 0)
297 logger("send error: %s", strerror(errno));
304 //-----------------------------------------------------------------------------
305 int CONFIGPROTO::RecvLogin(int sock)
307 char login[ADM_LOGIN_LEN + 1];
309 memset(login, 0, ADM_LOGIN_LEN + 1);
312 while (pos < ADM_LOGIN_LEN) {
313 if (!WaitPackets(sock))
319 ssize_t ret = recv(sock, &login[pos], ADM_LOGIN_LEN - static_cast<int>(pos), 0);
324 logger("recv error: %s", strerror(errno));
332 if (admins->Find(login, &currAdmin))
339 currAdmin->SetIP(adminIP);
341 state = confLoginCipher;
344 //-----------------------------------------------------------------------------
345 int CONFIGPROTO::SendLoginAnswer(int sock)
347 if (send(sock, OK_LOGIN, sizeof(OK_LOGIN) - 1, 0) < 0)
349 logger("Send OK_LOGIN error in SendLoginAnswer.");
354 //-----------------------------------------------------------------------------
355 int CONFIGPROTO::RecvLoginS(int sock)
357 char loginS[ADM_LOGIN_LEN + 1];
358 memset(loginS, 0, ADM_LOGIN_LEN + 1);
361 while (pos < ADM_LOGIN_LEN)
363 if (!WaitPackets(sock))
369 ssize_t ret = recv(sock, &loginS[pos], ADM_LOGIN_LEN - static_cast<int>(pos), 0);
374 printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
375 logger("recv error: %s", strerror(errno));
383 if (currAdmin->GetLogin().empty())
390 InitContext(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
392 char login[ADM_LOGIN_LEN + 1];
393 for (size_t i = 0; i < ADM_LOGIN_LEN / 8; i++)
394 DecryptBlock(login + i * 8, loginS + i * 8, &ctx);
396 if (currAdmin == admins->GetNoAdmin())
398 // If there are no admins registered in the system - give access with any password
403 if (strncmp(currAdmin->GetLogin().c_str(), login, ADM_LOGIN_LEN) != 0)
410 adminPassword = currAdmin->GetPassword();
413 //-----------------------------------------------------------------------------
414 int CONFIGPROTO::SendLoginSAnswer(int sock, int err)
418 if (send(sock, ERR_LOGINS, sizeof(ERR_LOGINS) - 1, 0) < 0)
420 logger("send error: %s", strerror(errno));
426 if (send(sock, OK_LOGINS, sizeof(OK_LOGINS) - 1, 0) < 0)
428 logger("send error: %s", strerror(errno));
434 //-----------------------------------------------------------------------------
435 int CONFIGPROTO::RecvData(int sock)
440 InitContext(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
447 while (pos < sizeof(bufferS))
449 if (!WaitPackets(sock))
455 ssize_t ret = recv(sock, &bufferS[pos], sizeof(bufferS) - static_cast<int>(pos), 0);
459 logger("recv error: %s", strerror(errno));
460 printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
476 DecryptBlock(buffer, bufferS, &ctx);
477 requestList.push_back(std::string(buffer, pos));
479 if (done || memchr(buffer, 0, pos) != NULL)
483 return SendError(sock, "Bad command");
485 return SendDataAnswer(sock, GetDataAnswer());
490 //-----------------------------------------------------------------------------
491 int CONFIGPROTO::SendDataAnswer(int sock, const std::string & answer)
497 InitContext(adminPassword.c_str(), ADM_PASSWD_LEN, &ctx);
499 std::string::size_type pos = 0;
500 std::string::size_type length = answer.length() + 1;
504 std::string::size_type chunkLength = std::min(length - pos, sizeof(buffer));
505 EncryptString(buffer, answer.c_str() + pos, chunkLength, &ctx);
506 if (send(sock, buffer, (chunkLength & ~7) < chunkLength ? chunkLength + 8 : chunkLength, 0) < 0) // Need to send data adjusted to the 8-byte boundary.
513 //-----------------------------------------------------------------------------
514 int CONFIGPROTO::SendError(int sock, const std::string & text)
516 return SendDataAnswer(sock, "<Error value=\"" + text + "\"/>");
518 //-----------------------------------------------------------------------------
519 void CONFIGPROTO::WriteLogAccessFailed(uint32_t ip)
521 logger("Admin's connection failed. IP %s", inet_ntostring(ip).c_str());
523 //-----------------------------------------------------------------------------