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 <unistd.h> // cloase, usleep
33 #include <cstdio> // snprintf
35 #include "stg/blowfish.h"
36 #include "configproto.h"
57 //-----------------------------------------------------------------------------
58 int CONFIGPROTO::Prepare()
60 list<string> ansList; //óÀÄÁ ÂÕÄÅÔ ÐÏÍÅÝÅÎ ÏÔ×ÅÔ ÄÌÑ ÍÅÎÅÄÖÅÒÁ ËÌÉÅÎÔÏ×
62 struct sockaddr_in listenAddr;
64 sigset_t sigmask, oldmask;
65 sigemptyset(&sigmask);
66 sigaddset(&sigmask, SIGINT);
67 sigaddset(&sigmask, SIGTERM);
68 sigaddset(&sigmask, SIGUSR1);
69 sigaddset(&sigmask, SIGHUP);
70 pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
72 listenSocket = socket(PF_INET, SOCK_STREAM, 0);
76 errorStr = "Create NET_CONFIGURATOR socket failed.";
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. " + string(strerror(errno));
92 res = bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr));
96 errorStr = "Bind admin socket failed";
100 res = listen(listenSocket, 0);
103 errorStr = "Listen admin socket failed";
111 //-----------------------------------------------------------------------------
112 int CONFIGPROTO::Stop()
118 struct sockaddr_in addr;
120 addr.sin_family = PF_INET;
121 addr.sin_port = htons(port);
122 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
124 addrLen = sizeof(addr);
125 sock = socket(PF_INET, SOCK_STREAM, 0);
126 connect(sock, (sockaddr*)&addr, addrLen);
131 //-----------------------------------------------------------------------------
132 // æÕÎËÃÉÑ ÏÂÝÅÎÉÑ Ó ËÏÎÆÉÇÕÒÁÔÏÒÏÍ
133 void * CONFIGPROTO::Run(void * a)
136 * Function Name:ReciveSendConf
137 * Parameters: void * a ÕËÁÚÁÔÅÌØ ÎÁ ÜËÚÅÍÐÌÑÒ ËÌÁÓÓÁ CONFIGPROTO
138 * Description: üÔÁ ÆÕÎËÃÉÑ ÏÂÅÓÐÅÞÉ×ÁÅÔ ÓÅÔÅ×ÏÅ ×ÚÁÉÍÏÄÅÊÓÔ×ÉÅ
139 * Ó íÅÎÅÄÖÅÒÏÍ ëÌÉÅÎÔÏ×. ÷ ÅÅ ÚÁÄÁÞÉ ×ÈÏÄÉÔ: ÐÒÉÅÍ ÚÁÐÒÏÓÏ× ÐÏ TCP/IP
140 * ÉÈ ÒÁÓÛÉÆÒÏ×ËÁ, ÐÅÒÅÄÁÞÁ ÐÒÉÎÑÔÙÈ ÄÁÎÎÙÈ ÁÎÁÌÉÚÁÔÏÒÕ É ÏÔÐÒÁ×ËÁ ÏÔ×ÅÔÁ ÎÁÚÁÄ.
141 * Returns: ×ÏÚ×ÒÁÝÁÅÔ NULL
144 CONFIGPROTO * cp = (CONFIGPROTO*)a;
150 struct sockaddr_in outerAddr;
151 socklen_t outerAddrLen(sizeof(outerAddr));
152 int outerSocket = accept(cp->listenSocket,
153 (struct sockaddr*)(&outerAddr),
161 if (outerSocket == -1)
163 printfd(__FILE__, "accept failed\n");
168 cp->adminIP = *(unsigned int*)&(outerAddr.sin_addr);
170 printfd(__FILE__, "Connection accepted from %s\n", inet_ntostring(outerAddr.sin_addr.s_addr).c_str());
172 if (cp->state == confHdr)
174 if (cp->RecvHdr(outerSocket) < 0)
179 if (cp->state == confLogin)
181 if (cp->SendHdrAnswer(outerSocket, ans_ok) < 0)
187 if (cp->RecvLogin(outerSocket) < 0)
193 if (cp->state == confLoginCipher)
195 if (cp->SendLoginAnswer(outerSocket, ans_ok) < 0)
200 if (cp->RecvLoginS(outerSocket) < 0)
206 if (cp->state == confData)
208 if (cp->SendLoginSAnswer(outerSocket, ans_ok) < 0)
213 if (cp->RecvData(outerSocket) < 0)
222 if (cp->SendLoginSAnswer(outerSocket, ans_err) < 0)
227 cp->WriteLogAccessFailed(cp->adminIP);
232 cp->WriteLogAccessFailed(cp->adminIP);
237 cp->WriteLogAccessFailed(cp->adminIP);
238 if (cp->SendHdrAnswer(outerSocket, ans_err) < 0)
247 cp->WriteLogAccessFailed(cp->adminIP);
254 //-----------------------------------------------------------------------------
255 int CONFIGPROTO::RecvHdr(int sock)
257 char buf[sizeof(STG_HEADER)];
258 memset(buf, 0, sizeof(STG_HEADER));
260 int stgHdrLen = strlen(STG_HEADER);
261 for (int i = 0; i < stgHdrLen; i++)
263 ret = recv(sock, &buf[i], 1, 0);
271 if (0 == strncmp(buf, STG_HEADER, strlen(STG_HEADER)))
278 SendError("Bad request");
284 //-----------------------------------------------------------------------------
285 int CONFIGPROTO::SendHdrAnswer(int sock, int err)
291 ret = send(sock, ERR_HEADER, sizeof(ERR_HEADER)-1, 0);
294 WriteServLog("send ERR_HEADER error in SendHdrAnswer.");
300 ret = send(sock, OK_HEADER, sizeof(OK_HEADER)-1, 0);
303 WriteServLog("send OK_HEADER error in SendHdrAnswer.");
310 //-----------------------------------------------------------------------------
311 int CONFIGPROTO::RecvLogin(int sock)
313 char login[ADM_LOGIN_LEN+1];
316 memset(login, 0, ADM_LOGIN_LEN + 1);
318 ret = recv(sock, login, ADM_LOGIN_LEN, 0);
328 if (ret < ADM_LOGIN_LEN)
336 if (admins->FindAdmin(login, &currAdmin))
343 currAdmin->SetIP(adminIP);
345 state = confLoginCipher;
348 //-----------------------------------------------------------------------------
349 int CONFIGPROTO::SendLoginAnswer(int sock, int)
353 ret = send(sock, OK_LOGIN, sizeof(OK_LOGIN)-1, 0);
356 WriteServLog("Send OK_LOGIN error in SendLoginAnswer.");
361 //-----------------------------------------------------------------------------
362 int CONFIGPROTO::RecvLoginS(int sock)
364 char loginS[ADM_LOGIN_LEN + 1];
365 char login[ADM_LOGIN_LEN + 1];
368 memset(loginS, 0, ADM_LOGIN_LEN + 1);
372 while (total < ADM_LOGIN_LEN)
374 ret = recv(sock, &loginS[total], ADM_LOGIN_LEN - total, 0);
379 printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
388 if (currAdmin->GetLogin() == "")
394 EnDecodeInit(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
396 for (int i = 0; i < ADM_LOGIN_LEN/8; i++)
398 DecodeString(login + i*8, loginS + i*8, &ctx);
401 if (currAdmin == admins->GetNoAdmin())
403 // If there are no admins registered in the system - give access with any password
408 if (strncmp(currAdmin->GetLogin().c_str(), login, ADM_LOGIN_LEN) != 0)
417 //-----------------------------------------------------------------------------
418 int CONFIGPROTO::SendLoginSAnswer(int sock, int err)
424 ret = send(sock, ERR_LOGINS, sizeof(ERR_LOGINS)-1, 0);
427 WriteServLog("send ERR_LOGIN error in SendLoginAnswer.");
433 ret = send(sock, OK_LOGINS, sizeof(OK_LOGINS)-1, 0);
436 WriteServLog("send OK_LOGINS error in SendLoginSAnswer.");
442 //-----------------------------------------------------------------------------
443 int CONFIGPROTO::RecvData(int sock)
454 EnDecodeInit(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
462 ret = recv(sock, &bufferS[total], 8 - total, 0);
472 if (memchr(buffer, 0, ret) != NULL)
482 DecodeString(buffer, bufferS, &ctx);
483 requestList.push_back(std::string(buffer, total));
485 if (done || memchr(buffer, 0, total) != NULL)
490 SendError("Bad command");
492 return SendDataAnswer(sock);
497 //-----------------------------------------------------------------------------
498 int CONFIGPROTO::SendDataAnswer(int sock)
500 list<string>::iterator li;
501 li = answerList.begin();
511 EnDecodeInit(currAdmin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
513 while (li != answerList.end())
515 while ((*li).c_str()[k])
517 buff[n%8] = (*li).c_str()[k];
523 EncodeString(buffS, buff, &ctx);
524 ret = send(sock, buffS, 8, 0);
535 if (answerList.empty()) {
540 EncodeString(buffS, buff, &ctx);
544 return send(sock, buffS, 8, 0);
546 //-----------------------------------------------------------------------------
547 void CONFIGPROTO::SendError(const char * text)
551 snprintf(s, 255, "<Error value=\"%s\"/>", text);
552 answerList.push_back(s);
554 //-----------------------------------------------------------------------------
555 void CONFIGPROTO::WriteLogAccessFailed(uint32_t ip)
557 WriteServLog("Admin's connect failed. IP %s", inet_ntostring(ip).c_str());
559 //-----------------------------------------------------------------------------