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 : Maxim Mamontov <faust@stargazer.dp.ua>
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"
33 #include <sys/socket.h>
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";
45 Conn::Conn(const BASE_PARSER::REGISTRY & registry,
46 ADMINS & admins, int sock, const sockaddr_in& addr)
47 : m_registry(registry),
54 m_xmlParser(XML_ParserCreate(NULL)),
57 m_bufferSize(sizeof(m_header)),
59 m_dataState(false, *this)
61 if (m_xmlParser == NULL)
62 throw Error("Failed to create XML parser.");
64 XML_ParserReset(m_xmlParser, NULL);
65 XML_SetElementHandler(m_xmlParser, ParseXMLStart, ParseXMLEnd);
66 XML_SetUserData(m_xmlParser, this);
68 /*m_parsers.push_back(new STG::PARSER::GET_SERVER_INFO(m_settings, m_users, m_tariffs));
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);
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);
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);*/
93 shutdown(m_sock, SHUT_RDWR);
96 XML_ParserFree(m_xmlParser);
101 ssize_t res = read(m_sock, m_buffer, m_bufferSize);
108 return HandleBuffer(res);
111 bool Conn::WriteAnswer(const void* buffer, size_t size)
113 ssize_t res = write(m_sock, buffer, size);
122 BASE_PARSER * Conn::GetParser(const std::string & tag) const
124 BASE_PARSER::REGISTRY::const_iterator it = m_registry.find(tag);
125 if (it == m_registry.end())
127 return it->second->create(*m_admin);
130 bool Conn::HandleBuffer(size_t size)
133 return HandleData(size);
135 if (m_bufferSize > 0)
140 case HEADER: return HandleHeader();
141 case LOGIN: return HandleLogin();
142 case CRYPTO_LOGIN: return HandleCryptoLogin();
143 default: return true;
149 bool Conn::HandleHeader()
151 if (strncmp(m_header, STG_HEADER, sizeof(m_header)) != 0)
153 WriteAnswer(ERR_HEADER, sizeof(ERR_HEADER));
160 m_bufferSize = sizeof(m_login);
161 return WriteAnswer(OK_HEADER, sizeof(OK_HEADER));
164 bool Conn::HandleLogin()
166 if (m_admins.Find(m_login, &m_admin)) // ADMINS::Find returns true on error.
168 WriteAnswer(ERR_LOGIN, sizeof(ERR_LOGIN));
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));
180 bool Conn::HandleCryptoLogin()
182 char login[ADM_LOGIN_LEN + 1];
184 InitContext(m_admin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
185 DecryptString(login, m_cryptoLogin, ADM_LOGIN_LEN, &ctx);
187 if (strncmp(m_login, login, sizeof(login)) != 0)
189 WriteAnswer(ERR_LOGINS, sizeof(ERR_LOGINS));
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));
202 bool Conn::HandleData(size_t size)
204 m_stream->Put(m_buffer, size, size == 0 || memchr(m_buffer, 0, size) != NULL);
205 return m_stream->IsOk();
208 bool Conn::DataCallback(const void * block, size_t size, void * data)
210 assert(data != NULL);
211 DataState& state = *static_cast<DataState *>(data);
213 if (XML_Parse(state.conn.m_xmlParser,
214 static_cast<const char *>(block),
215 size, state.final) == XML_STATUS_ERROR)
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);
227 if (!state.conn.WriteResponse())
230 state.conn.m_state = ERROR;
233 state.conn.m_state = DONE;
239 void Conn::ParseXMLStart(void * data, const char * el, const char ** attr)
241 assert(data != NULL);
242 Conn & conn = *static_cast<Conn *>(data);
244 if (conn.m_parser == NULL)
245 conn.m_parser = conn.GetParser(el);
247 if (conn.m_parser == NULL)
250 conn.m_state = ERROR;
254 conn.m_parser->Start(data, el, attr);
257 void Conn::ParseXMLEnd(void * data, const char * el)
259 assert(data != NULL);
260 Conn & conn = *static_cast<Conn *>(data);
262 if (conn.m_parser == NULL)
265 conn.m_state = ERROR;
269 conn.m_parser->End(data, el);
272 bool Conn::WriteResponse()
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();
280 bool Conn::WriteCallback(const void * block, size_t size, void * data)
282 assert(data != NULL);
283 Conn & conn = *static_cast<Conn *>(data);
284 return WriteAll(conn.m_sock, block, size);;