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 *******************************************************************/
32 #include "configproto.h"
49 //-----------------------------------------------------------------------------
50 int CONFIGPROTO::Prepare()
52 list<string> ansList; //óÀÄÁ ÂÕÄÅÔ ÐÏÍÅÝÅÎ ÏÔ×ÅÔ ÄÌÑ ÍÅÎÅÄÖÅÒÁ ËÌÉÅÎÔÏ×
54 struct sockaddr_in listenAddr;
56 sigset_t sigmask, oldmask;
57 sigemptyset(&sigmask);
58 sigaddset(&sigmask, SIGINT);
59 sigaddset(&sigmask, SIGTERM);
60 sigaddset(&sigmask, SIGUSR1);
61 sigaddset(&sigmask, SIGHUP);
62 pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
64 listenSocket = socket(PF_INET, SOCK_STREAM, 0);
68 errorStr = "Create NET_CONFIGURATOR socket failed.";
72 listenAddr.sin_family = PF_INET;
73 listenAddr.sin_port = htons(port);
74 listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
78 if (0 != setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &lng, 4))
80 errorStr = "Setsockopt failed. " + string(strerror(errno));
84 res = bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr));
88 errorStr = "Bind admin socket failed";
92 res = listen(listenSocket, 0);
95 errorStr = "Listen admin socket failed";
103 //-----------------------------------------------------------------------------
104 int CONFIGPROTO::Stop()
110 struct sockaddr_in addr;
112 addr.sin_family = PF_INET;
113 addr.sin_port = htons(port);
114 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
116 addrLen = sizeof(addr);
117 sock = socket(PF_INET, SOCK_STREAM, 0);
118 connect(sock, (sockaddr*)&addr, addrLen);
123 //-----------------------------------------------------------------------------
124 // æÕÎËÃÉÑ ÏÂÝÅÎÉÑ Ó ËÏÎÆÉÇÕÒÁÔÏÒÏÍ
125 void * CONFIGPROTO::Run(void * a)
128 * Function Name:ReciveSendConf
129 * Parameters: void * a ÕËÁÚÁÔÅÌØ ÎÁ ÜËÚÅÍÐÌÑÒ ËÌÁÓÓÁ CONFIGPROTO
130 * Description: üÔÁ ÆÕÎËÃÉÑ ÏÂÅÓÐÅÞÉ×ÁÅÔ ÓÅÔÅ×ÏÅ ×ÚÁÉÍÏÄÅÊÓÔ×ÉÅ
131 * Ó íÅÎÅÄÖÅÒÏÍ ëÌÉÅÎÔÏ×. ÷ ÅÅ ÚÁÄÁÞÉ ×ÈÏÄÉÔ: ÐÒÉÅÍ ÚÁÐÒÏÓÏ× ÐÏ TCP/IP
132 * ÉÈ ÒÁÓÛÉÆÒÏ×ËÁ, ÐÅÒÅÄÁÞÁ ÐÒÉÎÑÔÙÈ ÄÁÎÎÙÈ ÁÎÁÌÉÚÁÔÏÒÕ É ÏÔÐÒÁ×ËÁ ÏÔ×ÅÔÁ ÎÁÚÁÄ.
133 * Returns: ×ÏÚ×ÒÁÝÁÅÔ NULL
136 CONFIGPROTO * cp = (CONFIGPROTO*)a;
142 struct sockaddr_in outerAddr;
143 socklen_t outerAddrLen(sizeof(outerAddr));
144 int outerSocket = accept(cp->listenSocket,
145 (struct sockaddr*)(&outerAddr),
153 if (outerSocket == -1)
155 printfd(__FILE__, "accept failed\n");
160 cp->adminIP = *(unsigned int*)&(outerAddr.sin_addr);
162 printfd(__FILE__, "Connection accepted from %s\n", inet_ntostring(outerAddr.sin_addr.s_addr).c_str());
164 if (cp->state == confHdr)
166 if (cp->RecvHdr(outerSocket) < 0)
171 if (cp->state == confLogin)
173 if (cp->SendHdrAnswer(outerSocket, ans_ok) < 0)
179 if (cp->RecvLogin(outerSocket) < 0)
185 if (cp->state == confLoginCipher)
187 if (cp->SendLoginAnswer(outerSocket, ans_ok) < 0)
192 if (cp->RecvLoginS(outerSocket) < 0)
198 if (cp->state == confData)
200 if (cp->SendLoginSAnswer(outerSocket, ans_ok) < 0)
205 if (cp->RecvData(outerSocket) < 0)
214 if (cp->SendLoginSAnswer(outerSocket, ans_err) < 0)
219 cp->WriteLogAccessFailed(cp->adminIP);
224 cp->WriteLogAccessFailed(cp->adminIP);
229 cp->WriteLogAccessFailed(cp->adminIP);
230 if (cp->SendHdrAnswer(outerSocket, ans_err) < 0)
239 cp->WriteLogAccessFailed(cp->adminIP);
246 //-----------------------------------------------------------------------------
247 int CONFIGPROTO::RecvHdr(int sock)
249 char buf[sizeof(STG_HEADER)];
250 memset(buf, 0, sizeof(STG_HEADER));
252 int stgHdrLen = strlen(STG_HEADER);
253 for (int i = 0; i < stgHdrLen; i++)
255 ret = recv(sock, &buf[i], 1, 0);
263 if (0 == strncmp(buf, STG_HEADER, strlen(STG_HEADER)))
270 SendError("Bad request");
276 //-----------------------------------------------------------------------------
277 int CONFIGPROTO::SendHdrAnswer(int sock, int err)
283 ret = send(sock, ERR_HEADER, sizeof(ERR_HEADER)-1, 0);
286 WriteServLog("send ERR_HEADER error in SendHdrAnswer.");
292 ret = send(sock, OK_HEADER, sizeof(OK_HEADER)-1, 0);
295 WriteServLog("send OK_HEADER error in SendHdrAnswer.");
302 //-----------------------------------------------------------------------------
303 int CONFIGPROTO::RecvLogin(int sock)
305 char login[ADM_LOGIN_LEN+1];
308 memset(login, 0, ADM_LOGIN_LEN + 1);
310 ret = recv(sock, login, ADM_LOGIN_LEN, 0);
320 if (ret < ADM_LOGIN_LEN)
328 if (admins->FindAdmin(login, &currAdmin))
335 currAdmin.SetAdminIP(adminIP);
337 state = confLoginCipher;
340 //-----------------------------------------------------------------------------
341 int CONFIGPROTO::SendLoginAnswer(int sock, int)
345 ret = send(sock, OK_LOGIN, sizeof(OK_LOGIN)-1, 0);
348 WriteServLog("Send OK_LOGIN error in SendLoginAnswer.");
353 //-----------------------------------------------------------------------------
354 int CONFIGPROTO::RecvLoginS(int sock)
356 char loginS[ADM_LOGIN_LEN + 1];
357 char login[ADM_LOGIN_LEN + 1];
360 memset(loginS, 0, ADM_LOGIN_LEN + 1);
364 while (total < ADM_LOGIN_LEN)
366 ret = recv(sock, &loginS[total], ADM_LOGIN_LEN - total, 0);
371 printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
380 if (currAdmin.GetLogin() == "")
386 EnDecodeInit(currAdmin.GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
388 for (int i = 0; i < ADM_LOGIN_LEN/8; i++)
390 DecodeString(login + i*8, loginS + i*8, &ctx);
393 if (currAdmin == admins->GetNoAdmin())
395 // If there are no admins registered in the system - give access with any password
400 if (strncmp(currAdmin.GetLogin().c_str(), login, ADM_LOGIN_LEN) != 0)
409 //-----------------------------------------------------------------------------
410 int CONFIGPROTO::SendLoginSAnswer(int sock, int err)
416 ret = send(sock, ERR_LOGINS, sizeof(ERR_LOGINS)-1, 0);
419 WriteServLog("send ERR_LOGIN error in SendLoginAnswer.");
425 ret = send(sock, OK_LOGINS, sizeof(OK_LOGINS)-1, 0);
428 WriteServLog("send OK_LOGINS error in SendLoginSAnswer.");
434 //-----------------------------------------------------------------------------
435 int CONFIGPROTO::RecvData(int sock)
446 EnDecodeInit(currAdmin.GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
454 ret = recv(sock, &bufferS[total], 8 - total, 0);
464 if (memchr(buffer, 0, ret) != NULL)
474 DecodeString(buffer, bufferS, &ctx);
475 requestList.push_back(std::string(buffer, total));
477 if (done || memchr(buffer, 0, total) != NULL)
482 SendError("Bad command");
484 return SendDataAnswer(sock);
489 //-----------------------------------------------------------------------------
490 int CONFIGPROTO::SendDataAnswer(int sock)
492 list<string>::iterator li;
493 li = answerList.begin();
503 EnDecodeInit(currAdmin.GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
505 while (li != answerList.end())
507 while ((*li).c_str()[k])
509 buff[n%8] = (*li).c_str()[k];
515 EncodeString(buffS, buff, &ctx);
516 ret = send(sock, buffS, 8, 0);
527 if (answerList.empty()) {
532 EncodeString(buffS, buff, &ctx);
536 return send(sock, buffS, 8, 0);
538 //-----------------------------------------------------------------------------
539 void CONFIGPROTO::SendError(const char * text)
543 sprintf(s, "<Error value=\"%s\"/>", text);
544 answerList.push_back(s);
546 //-----------------------------------------------------------------------------
547 void CONFIGPROTO::WriteLogAccessFailed(uint32_t ip)
549 WriteServLog("Admin's connect failed. IP %s", inet_ntostring(ip).c_str());
551 //-----------------------------------------------------------------------------