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),
158 logger("accept error: %s", strerror(errno));
159 printfd(__FILE__, "accept failed\n");
163 adminIP = *(unsigned int*)&(outerAddr.sin_addr);
165 if (state == confHdr)
167 if (RecvHdr(outerSocket) < 0)
172 if (state == confLogin)
174 if (SendHdrAnswer(outerSocket, ans_ok) < 0)
179 if (RecvLogin(outerSocket) < 0)
184 if (state == confLoginCipher)
186 if (SendLoginAnswer(outerSocket) < 0)
191 if (RecvLoginS(outerSocket) < 0)
196 if (state == confData)
198 if (SendLoginSAnswer(outerSocket, ans_ok) < 0)
203 if (RecvData(outerSocket) < 0)
212 if (SendLoginSAnswer(outerSocket, ans_err) < 0)
217 WriteLogAccessFailed(adminIP);
222 WriteLogAccessFailed(adminIP);
227 WriteLogAccessFailed(adminIP);
228 if (SendHdrAnswer(outerSocket, ans_err) < 0)
237 WriteLogAccessFailed(adminIP);
239 printfd(__FILE__, "Successfull connection from %s\n", inet_ntostring(outerAddr.sin_addr.s_addr).c_str());
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 EnDecodeInit(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++)
395 DecodeString(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 EnDecodeInit(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 DecodeString(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, currParser->GetAnswer());
492 //-----------------------------------------------------------------------------
493 int CONFIGPROTO::SendDataAnswer(int sock, const std::string & answer)
499 EnDecodeInit(adminPassword.c_str(), ADM_PASSWD_LEN, &ctx);
501 std::string::size_type pos = 0;
502 std::string::size_type length = answer.length();
506 std::string::size_type chunkLength = std::min(length - pos, sizeof(buffer));
507 EncodeFullString(buffer, answer.c_str() + pos, chunkLength, ctx);
508 if (send(sock, buffer, chunkLength, 0) < 0)
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 //-----------------------------------------------------------------------------