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),
150 logger("accept error: %s", strerror(errno));
151 printfd(__FILE__, "accept failed\n");
155 adminIP = *(unsigned int*)&(outerAddr.sin_addr);
157 if (state == confHdr)
159 if (RecvHdr(outerSocket) < 0)
161 shutdown(outerSocket, SHUT_RDWR);
165 if (state == confLogin)
167 if (SendHdrAnswer(outerSocket, ans_ok) < 0)
169 shutdown(outerSocket, SHUT_RDWR);
173 if (RecvLogin(outerSocket) < 0)
175 shutdown(outerSocket, SHUT_RDWR);
179 if (state == confLoginCipher)
181 if (SendLoginAnswer(outerSocket) < 0)
183 shutdown(outerSocket, SHUT_RDWR);
187 if (RecvLoginS(outerSocket) < 0)
189 shutdown(outerSocket, SHUT_RDWR);
193 if (state == confData)
195 if (SendLoginSAnswer(outerSocket, ans_ok) < 0)
197 shutdown(outerSocket, SHUT_RDWR);
201 if (RecvData(outerSocket) < 0)
203 shutdown(outerSocket, SHUT_RDWR);
211 if (SendLoginSAnswer(outerSocket, ans_err) < 0)
213 shutdown(outerSocket, SHUT_RDWR);
217 WriteLogAccessFailed(adminIP);
222 WriteLogAccessFailed(adminIP);
227 WriteLogAccessFailed(adminIP);
228 if (SendHdrAnswer(outerSocket, ans_err) < 0)
230 shutdown(outerSocket, SHUT_RDWR);
238 WriteLogAccessFailed(adminIP);
240 printfd(__FILE__, "Successfull connection from %s\n", inet_ntostring(outerAddr.sin_addr.s_addr).c_str());
241 shutdown(outerSocket, SHUT_RDWR);
245 //-----------------------------------------------------------------------------
246 int CONFIGPROTO::RecvHdr(int sock)
248 char buf[sizeof(STG_HEADER)];
249 memset(buf, 0, sizeof(STG_HEADER));
250 size_t stgHdrLen = sizeof(STG_HEADER) - 1; // Without 0-char
252 while (pos < stgHdrLen)
254 if (!WaitPackets(sock))
257 SendError(sock, "Bad request");
260 ssize_t ret = recv(sock, &buf[pos], static_cast<int>(stgHdrLen) - static_cast<int>(pos), 0);
264 logger("recv error: %s", strerror(errno));
271 if (0 == strncmp(buf, STG_HEADER, strlen(STG_HEADER)))
278 SendError(sock, "Bad request");
284 //-----------------------------------------------------------------------------
285 int CONFIGPROTO::SendHdrAnswer(int sock, int err)
289 if (send(sock, ERR_HEADER, sizeof(ERR_HEADER) - 1, 0) < 0)
291 logger("send error: %s", strerror(errno));
297 if (send(sock, OK_HEADER, sizeof(OK_HEADER) - 1, 0) < 0)
299 logger("send error: %s", strerror(errno));
306 //-----------------------------------------------------------------------------
307 int CONFIGPROTO::RecvLogin(int sock)
309 char login[ADM_LOGIN_LEN + 1];
311 memset(login, 0, ADM_LOGIN_LEN + 1);
314 while (pos < ADM_LOGIN_LEN) {
315 if (!WaitPackets(sock))
321 ssize_t ret = recv(sock, &login[pos], ADM_LOGIN_LEN - static_cast<int>(pos), 0);
326 logger("recv error: %s", strerror(errno));
334 if (admins->Find(login, &currAdmin))
341 currAdmin->SetIP(adminIP);
343 state = confLoginCipher;
346 //-----------------------------------------------------------------------------
347 int CONFIGPROTO::SendLoginAnswer(int sock)
349 if (send(sock, OK_LOGIN, sizeof(OK_LOGIN) - 1, 0) < 0)
351 logger("Send OK_LOGIN error in SendLoginAnswer.");
356 //-----------------------------------------------------------------------------
357 int CONFIGPROTO::RecvLoginS(int sock)
359 char loginS[ADM_LOGIN_LEN + 1];
360 memset(loginS, 0, ADM_LOGIN_LEN + 1);
363 while (pos < ADM_LOGIN_LEN)
365 if (!WaitPackets(sock))
371 ssize_t ret = recv(sock, &loginS[pos], ADM_LOGIN_LEN - static_cast<int>(pos), 0);
376 printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
377 logger("recv error: %s", strerror(errno));
385 if (currAdmin->GetLogin().empty())
392 InitContext(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
394 char login[ADM_LOGIN_LEN + 1];
395 for (size_t i = 0; i < ADM_LOGIN_LEN / 8; i++)
396 DecryptBlock(login + i * 8, loginS + i * 8, &ctx);
398 if (currAdmin == admins->GetNoAdmin())
400 // If there are no admins registered in the system - give access with any password
405 if (strncmp(currAdmin->GetLogin().c_str(), login, ADM_LOGIN_LEN) != 0)
412 adminPassword = currAdmin->GetPassword();
415 //-----------------------------------------------------------------------------
416 int CONFIGPROTO::SendLoginSAnswer(int sock, int err)
420 if (send(sock, ERR_LOGINS, sizeof(ERR_LOGINS) - 1, 0) < 0)
422 logger("send error: %s", strerror(errno));
428 if (send(sock, OK_LOGINS, sizeof(OK_LOGINS) - 1, 0) < 0)
430 logger("send error: %s", strerror(errno));
436 //-----------------------------------------------------------------------------
437 int CONFIGPROTO::RecvData(int sock)
442 InitContext(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
449 while (pos < sizeof(bufferS))
451 if (!WaitPackets(sock))
457 ssize_t ret = recv(sock, &bufferS[pos], sizeof(bufferS) - static_cast<int>(pos), 0);
461 logger("recv error: %s", strerror(errno));
462 printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
478 DecryptBlock(buffer, bufferS, &ctx);
479 requestList.push_back(std::string(buffer, pos));
481 if (done || memchr(buffer, 0, pos) != NULL)
485 return SendError(sock, "Bad command");
487 return SendDataAnswer(sock, GetDataAnswer());
492 //-----------------------------------------------------------------------------
493 int CONFIGPROTO::SendDataAnswer(int sock, const std::string & answer)
499 InitContext(adminPassword.c_str(), ADM_PASSWD_LEN, &ctx);
501 std::string::size_type pos = 0;
502 std::string::size_type length = answer.length() + 1;
506 std::string::size_type chunkLength = std::min(length - pos, sizeof(buffer));
507 EncryptString(buffer, answer.c_str() + pos, chunkLength, &ctx);
508 if (send(sock, buffer, (chunkLength & ~7) < chunkLength ? chunkLength + 8 : chunkLength, 0) < 0) // Need to send data adjusted to the 8-byte boundary.
515 //-----------------------------------------------------------------------------
516 int CONFIGPROTO::SendError(int sock, const std::string & text)
518 return SendDataAnswer(sock, "<Error value=\"" + text + "\"/>");
520 //-----------------------------------------------------------------------------
521 void CONFIGPROTO::WriteLogAccessFailed(uint32_t ip)
523 logger("Admin's connection failed. IP %s", inet_ntostring(ip).c_str());
525 //-----------------------------------------------------------------------------