]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/conn.cpp
Implemented parser registry.
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / conn.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 #include "conn.h"
22
23 #include "stg/admins.h"
24 #include "stg/admin.h"
25 #include "stg/blowfish.h"
26 #include "stg/bfstream.h"
27 #include "stg/common.h"
28
29 #include <cassert>
30 #include <cstring>
31
32 #include <unistd.h>
33 #include <sys/socket.h>
34
35 using STG::Conn;
36
37 const char Conn::STG_HEADER[] = "SG04";
38 const char Conn::OK_HEADER[] = "OKHD";
39 const char Conn::ERR_HEADER[] = "ERHD";
40 const char Conn::OK_LOGIN[] = "OKLG";
41 const char Conn::ERR_LOGIN[] = "ERLG";
42 const char Conn::OK_LOGINS[] = "OKLS";
43 const char Conn::ERR_LOGINS[] = "ERLS";
44
45 Conn::Conn(const BASE_PARSER::REGISTRY & registry,
46            ADMINS & admins, int sock, const sockaddr_in& addr)
47     : m_registry(registry),
48       m_admins(admins),
49       m_admin(NULL),
50       m_sock(sock),
51       m_addr(addr),
52       m_keepAlive(false),
53       m_parser(NULL),
54       m_xmlParser(XML_ParserCreate(NULL)),
55       m_state(HEADER),
56       m_buffer(m_header),
57       m_bufferSize(sizeof(m_header)),
58       m_stream(NULL),
59       m_dataState(false, *this)
60 {
61     if (m_xmlParser == NULL)
62         throw Error("Failed to create XML parser.");
63
64     XML_ParserReset(m_xmlParser, NULL);
65     XML_SetElementHandler(m_xmlParser, ParseXMLStart, ParseXMLEnd);
66     XML_SetUserData(m_xmlParser, this);
67
68     /*m_parsers.push_back(new STG::PARSER::GET_SERVER_INFO(m_settings, m_users, m_tariffs));
69
70     m_parsers.push_back(new PARSER_GET_USERS);
71     m_parsers.push_back(new PARSER_GET_USER);
72     m_parsers.push_back(new PARSER_CHG_USER);
73     m_parsers.push_back(new PARSER_ADD_USER);
74     m_parsers.push_back(new PARSER_DEL_USER);
75     m_parsers.push_back(new PARSER_CHECK_USER);
76     m_parsers.push_back(new PARSER_SEND_MESSAGE);
77     m_parsers.push_back(new PARSER_AUTH_BY);
78     m_parsers.push_back(new PARSER_USER_INFO);
79
80     m_parsers.push_back(new PARSER_GET_TARIFFS);
81     m_parsers.push_back(new PARSER_ADD_TARIFF);
82     m_parsers.push_back(new PARSER_DEL_TARIFF);
83     m_parsers.push_back(new PARSER_CHG_TARIFF);
84
85     m_parsers.push_back(new PARSER_GET_ADMINS);
86     m_parsers.push_back(new PARSER_CHG_ADMIN);
87     m_parsers.push_back(new PARSER_DEL_ADMIN);
88     m_parsers.push_back(new PARSER_ADD_ADMIN);*/
89 }
90
91 Conn::~Conn()
92 {
93     shutdown(m_sock, SHUT_RDWR);
94     close(m_sock);
95
96     XML_ParserFree(m_xmlParser);
97 }
98
99 bool Conn::Read()
100 {
101     ssize_t res = read(m_sock, m_buffer, m_bufferSize);
102     if (res < 0)
103     {
104         // TODO: log it
105         return false;
106     }
107     m_bufferSize -= res;
108     return HandleBuffer(res);
109 }
110
111 bool Conn::WriteAnswer(const void* buffer, size_t size)
112 {
113     ssize_t res = write(m_sock, buffer, size);
114     if (res < 0)
115     {
116         // TODO: log it
117         return false;
118     }
119     return true;
120 }
121
122 BASE_PARSER * Conn::GetParser(const std::string & tag) const
123 {
124     BASE_PARSER::REGISTRY::const_iterator it = m_registry.find(tag);
125     if (it == m_registry.end())
126         return NULL;
127     return it->second->create(*m_admin);
128 }
129
130 bool Conn::HandleBuffer(size_t size)
131 {
132     if (m_state == DATA)
133         return HandleData(size);
134
135     if (m_bufferSize > 0)
136         return true;
137
138     switch (m_state)
139     {
140         case HEADER: return HandleHeader();
141         case LOGIN: return HandleLogin();
142         case CRYPTO_LOGIN: return HandleCryptoLogin();
143         default: return true;
144     }
145
146     return true;
147 }
148
149 bool Conn::HandleHeader()
150 {
151     if (strncmp(m_header, STG_HEADER, sizeof(m_header)) != 0)
152     {
153         WriteAnswer(ERR_HEADER, sizeof(ERR_HEADER));
154         // TODO: log it
155         m_state = ERROR;
156         return false;
157     }
158     m_state = LOGIN;
159     m_buffer = m_login;
160     m_bufferSize = sizeof(m_login);
161     return WriteAnswer(OK_HEADER, sizeof(OK_HEADER));
162 }
163
164 bool Conn::HandleLogin()
165 {
166     if (m_admins.Find(m_login, &m_admin)) // ADMINS::Find returns true on error.
167     {
168         WriteAnswer(ERR_LOGIN, sizeof(ERR_LOGIN));
169         // TODO: log it
170         m_state = ERROR;
171         return false;
172     }
173     m_admin->SetIP(IP());
174     m_state = CRYPTO_LOGIN;
175     m_buffer = m_cryptoLogin;
176     m_bufferSize = sizeof(m_cryptoLogin);
177     return WriteAnswer(OK_LOGIN, sizeof(OK_LOGIN));
178 }
179
180 bool Conn::HandleCryptoLogin()
181 {
182     char login[ADM_LOGIN_LEN + 1];
183     BLOWFISH_CTX ctx;
184     InitContext(m_admin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
185     DecryptString(login, m_cryptoLogin, ADM_LOGIN_LEN, &ctx);
186
187     if (strncmp(m_login, login, sizeof(login)) != 0)
188     {
189         WriteAnswer(ERR_LOGINS, sizeof(ERR_LOGINS));
190         // TODO: log it
191         m_state = ERROR;
192         return false;
193     }
194
195     m_state = DATA;
196     m_buffer = m_data;
197     m_bufferSize = sizeof(m_data);
198     m_stream = new STG::DECRYPT_STREAM(m_admin->GetPassword(), DataCallback, &m_dataState);
199     return WriteAnswer(OK_LOGINS, sizeof(OK_LOGINS));
200 }
201
202 bool Conn::HandleData(size_t size)
203 {
204     m_stream->Put(m_buffer, size, size == 0 || memchr(m_buffer, 0, size) != NULL);
205     return m_stream->IsOk();
206 }
207
208 bool Conn::DataCallback(const void * block, size_t size, void * data)
209 {
210     assert(data != NULL);
211     DataState& state = *static_cast<DataState *>(data);
212
213     if (XML_Parse(state.conn.m_xmlParser,
214                   static_cast<const char *>(block),
215                   size, state.final) == XML_STATUS_ERROR)
216     {
217         // TODO: log it
218         printfd(__FILE__, "XML parse error at line %d, %d: %s. Is final: %d",
219                   static_cast<int>(XML_GetCurrentLineNumber(state.conn.m_xmlParser)),
220                   static_cast<int>(XML_GetCurrentColumnNumber(state.conn.m_xmlParser)),
221                   XML_ErrorString(XML_GetErrorCode(state.conn.m_xmlParser)), (int)state.final);
222         return false;
223     }
224
225     if (state.final)
226     {
227         if (!state.conn.WriteResponse())
228         {
229             // TODO: log it
230             state.conn.m_state = ERROR;
231             return false;
232         }
233         state.conn.m_state = DONE;
234     }
235
236     return true;
237 }
238
239 void Conn::ParseXMLStart(void * data, const char * el, const char ** attr)
240 {
241     assert(data != NULL);
242     Conn & conn = *static_cast<Conn *>(data);
243
244     if (conn.m_parser == NULL)
245         conn.m_parser = conn.GetParser(el);
246
247     if (conn.m_parser == NULL)
248     {
249         // TODO: log it
250         conn.m_state = ERROR;
251         return;
252     }
253
254     conn.m_parser->Start(data, el, attr);
255 }
256
257 void Conn::ParseXMLEnd(void * data, const char * el)
258 {
259     assert(data != NULL);
260     Conn & conn = *static_cast<Conn *>(data);
261
262     if (conn.m_parser == NULL)
263     {
264         // TODO: log it
265         conn.m_state = ERROR;
266         return;
267     }
268
269     conn.m_parser->End(data, el);
270 }
271
272 bool Conn::WriteResponse()
273 {
274     STG::ENCRYPT_STREAM stream(m_admin->GetPassword(), WriteCallback, this);
275     const std::string & answer = m_parser->GetAnswer();
276     stream.Put(answer.c_str(), answer.length() + 1 /* including \0 */, true /* final */);
277     return stream.IsOk();
278 }
279
280 bool Conn::WriteCallback(const void * block, size_t size, void * data)
281 {
282     assert(data != NULL);
283     Conn & conn = *static_cast<Conn *>(data);
284     return WriteAll(conn.m_sock, block, size);;
285 }