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"
54 //-----------------------------------------------------------------------------
55 int CONFIGPROTO::Prepare()
57 list<string> ansList; //óÀÄÁ ÂÕÄÅÔ ÐÏÍÅÝÅÎ ÏÔ×ÅÔ ÄÌÑ ÍÅÎÅÄÖÅÒÁ ËÌÉÅÎÔÏ×
59 struct sockaddr_in listenAddr;
61 sigset_t sigmask, oldmask;
62 sigemptyset(&sigmask);
63 sigaddset(&sigmask, SIGINT);
64 sigaddset(&sigmask, SIGTERM);
65 sigaddset(&sigmask, SIGUSR1);
66 sigaddset(&sigmask, SIGHUP);
67 pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
69 listenSocket = socket(PF_INET, SOCK_STREAM, 0);
73 errorStr = "Create NET_CONFIGURATOR socket failed.";
77 listenAddr.sin_family = PF_INET;
78 listenAddr.sin_port = htons(port);
79 listenAddr.sin_addr.s_addr = inet_addr("0.0.0.0");
83 if (0 != setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &lng, 4))
85 errorStr = "Setsockopt failed. " + string(strerror(errno));
89 res = bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr));
93 errorStr = "Bind admin socket failed";
97 res = listen(listenSocket, 0);
100 errorStr = "Listen admin socket failed";
108 //-----------------------------------------------------------------------------
109 int CONFIGPROTO::Stop()
115 struct sockaddr_in addr;
117 addr.sin_family = PF_INET;
118 addr.sin_port = htons(port);
119 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
121 addrLen = sizeof(addr);
122 sock = socket(PF_INET, SOCK_STREAM, 0);
123 connect(sock, (sockaddr*)&addr, addrLen);
128 //-----------------------------------------------------------------------------
129 // æÕÎËÃÉÑ ÏÂÝÅÎÉÑ Ó ËÏÎÆÉÇÕÒÁÔÏÒÏÍ
130 void * CONFIGPROTO::Run(void * a)
133 * Function Name:ReciveSendConf
134 * Parameters: void * a ÕËÁÚÁÔÅÌØ ÎÁ ÜËÚÅÍÐÌÑÒ ËÌÁÓÓÁ CONFIGPROTO
135 * Description: üÔÁ ÆÕÎËÃÉÑ ÏÂÅÓÐÅÞÉ×ÁÅÔ ÓÅÔÅ×ÏÅ ×ÚÁÉÍÏÄÅÊÓÔ×ÉÅ
136 * Ó íÅÎÅÄÖÅÒÏÍ ëÌÉÅÎÔÏ×. ÷ ÅÅ ÚÁÄÁÞÉ ×ÈÏÄÉÔ: ÐÒÉÅÍ ÚÁÐÒÏÓÏ× ÐÏ TCP/IP
137 * ÉÈ ÒÁÓÛÉÆÒÏ×ËÁ, ÐÅÒÅÄÁÞÁ ÐÒÉÎÑÔÙÈ ÄÁÎÎÙÈ ÁÎÁÌÉÚÁÔÏÒÕ É ÏÔÐÒÁ×ËÁ ÏÔ×ÅÔÁ ÎÁÚÁÄ.
138 * Returns: ×ÏÚ×ÒÁÝÁÅÔ NULL
141 CONFIGPROTO * cp = (CONFIGPROTO*)a;
147 struct sockaddr_in outerAddr;
148 socklen_t outerAddrLen(sizeof(outerAddr));
149 int outerSocket = accept(cp->listenSocket,
150 (struct sockaddr*)(&outerAddr),
158 if (outerSocket == -1)
160 printfd(__FILE__, "accept failed\n");
165 cp->adminIP = *(unsigned int*)&(outerAddr.sin_addr);
167 printfd(__FILE__, "Connection accepted from %s\n", inet_ntostring(outerAddr.sin_addr.s_addr).c_str());
169 if (cp->state == confHdr)
171 if (cp->RecvHdr(outerSocket) < 0)
176 if (cp->state == confLogin)
178 if (cp->SendHdrAnswer(outerSocket, ans_ok) < 0)
184 if (cp->RecvLogin(outerSocket) < 0)
190 if (cp->state == confLoginCipher)
192 if (cp->SendLoginAnswer(outerSocket, ans_ok) < 0)
197 if (cp->RecvLoginS(outerSocket) < 0)
203 if (cp->state == confData)
205 if (cp->SendLoginSAnswer(outerSocket, ans_ok) < 0)
210 if (cp->RecvData(outerSocket) < 0)
219 if (cp->SendLoginSAnswer(outerSocket, ans_err) < 0)
224 cp->WriteLogAccessFailed(cp->adminIP);
229 cp->WriteLogAccessFailed(cp->adminIP);
234 cp->WriteLogAccessFailed(cp->adminIP);
235 if (cp->SendHdrAnswer(outerSocket, ans_err) < 0)
244 cp->WriteLogAccessFailed(cp->adminIP);
251 //-----------------------------------------------------------------------------
252 int CONFIGPROTO::RecvHdr(int sock)
254 char buf[sizeof(STG_HEADER)];
255 memset(buf, 0, sizeof(STG_HEADER));
257 int stgHdrLen = strlen(STG_HEADER);
258 for (int i = 0; i < stgHdrLen; i++)
260 ret = recv(sock, &buf[i], 1, 0);
268 if (0 == strncmp(buf, STG_HEADER, strlen(STG_HEADER)))
275 SendError("Bad request");
281 //-----------------------------------------------------------------------------
282 int CONFIGPROTO::SendHdrAnswer(int sock, int err)
288 ret = send(sock, ERR_HEADER, sizeof(ERR_HEADER)-1, 0);
291 WriteServLog("send ERR_HEADER error in SendHdrAnswer.");
297 ret = send(sock, OK_HEADER, sizeof(OK_HEADER)-1, 0);
300 WriteServLog("send OK_HEADER error in SendHdrAnswer.");
307 //-----------------------------------------------------------------------------
308 int CONFIGPROTO::RecvLogin(int sock)
310 char login[ADM_LOGIN_LEN+1];
313 memset(login, 0, ADM_LOGIN_LEN + 1);
315 ret = recv(sock, login, ADM_LOGIN_LEN, 0);
325 if (ret < ADM_LOGIN_LEN)
333 if (admins->FindAdmin(login, &currAdmin))
340 currAdmin.SetAdminIP(adminIP);
342 state = confLoginCipher;
345 //-----------------------------------------------------------------------------
346 int CONFIGPROTO::SendLoginAnswer(int sock, int)
350 ret = send(sock, OK_LOGIN, sizeof(OK_LOGIN)-1, 0);
353 WriteServLog("Send OK_LOGIN error in SendLoginAnswer.");
358 //-----------------------------------------------------------------------------
359 int CONFIGPROTO::RecvLoginS(int sock)
361 char loginS[ADM_LOGIN_LEN + 1];
362 char login[ADM_LOGIN_LEN + 1];
365 memset(loginS, 0, ADM_LOGIN_LEN + 1);
369 while (total < ADM_LOGIN_LEN)
371 ret = recv(sock, &loginS[total], ADM_LOGIN_LEN - total, 0);
376 printfd(__FILE__, "recv error: '%s'\n", strerror(errno));
385 if (currAdmin.GetLogin() == "")
391 EnDecodeInit(currAdmin.GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
393 for (int 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)
414 //-----------------------------------------------------------------------------
415 int CONFIGPROTO::SendLoginSAnswer(int sock, int err)
421 ret = send(sock, ERR_LOGINS, sizeof(ERR_LOGINS)-1, 0);
424 WriteServLog("send ERR_LOGIN error in SendLoginAnswer.");
430 ret = send(sock, OK_LOGINS, sizeof(OK_LOGINS)-1, 0);
433 WriteServLog("send OK_LOGINS error in SendLoginSAnswer.");
439 //-----------------------------------------------------------------------------
440 int CONFIGPROTO::RecvData(int sock)
451 EnDecodeInit(currAdmin.GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
459 ret = recv(sock, &bufferS[total], 8 - total, 0);
469 if (memchr(buffer, 0, ret) != NULL)
479 DecodeString(buffer, bufferS, &ctx);
480 requestList.push_back(std::string(buffer, total));
482 if (done || memchr(buffer, 0, total) != NULL)
487 SendError("Bad command");
489 return SendDataAnswer(sock);
494 //-----------------------------------------------------------------------------
495 int CONFIGPROTO::SendDataAnswer(int sock)
497 list<string>::iterator li;
498 li = answerList.begin();
508 EnDecodeInit(currAdmin.GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
510 while (li != answerList.end())
512 while ((*li).c_str()[k])
514 buff[n%8] = (*li).c_str()[k];
520 EncodeString(buffS, buff, &ctx);
521 ret = send(sock, buffS, 8, 0);
532 if (answerList.empty()) {
537 EncodeString(buffS, buff, &ctx);
541 return send(sock, buffS, 8, 0);
543 //-----------------------------------------------------------------------------
544 void CONFIGPROTO::SendError(const char * text)
548 sprintf(s, "<Error value=\"%s\"/>", text);
549 answerList.push_back(s);
551 //-----------------------------------------------------------------------------
552 void CONFIGPROTO::WriteLogAccessFailed(uint32_t ip)
554 WriteServLog("Admin's connect failed. IP %s", inet_ntostring(ip).c_str());
556 //-----------------------------------------------------------------------------