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>
25 #include "parser_server_info.h"
26 #include "parser_admins.h"
27 #include "parser_tariffs.h"
28 #include "parser_users.h"
29 #include "parser_message.h"
30 #include "parser_auth_by.h"
31 #include "parser_user_info.h"
33 #include "stg/settings.h"
34 #include "stg/admins.h"
35 #include "stg/users.h"
36 #include "stg/tariffs.h"
37 #include "stg/admin.h"
38 #include "stg/blowfish.h"
39 #include "stg/bfstream.h"
40 #include "stg/common.h"
45 #include <sys/socket.h>
49 const char Conn::STG_HEADER[] = "SG04";
50 const char Conn::OK_HEADER[] = "OKHD";
51 const char Conn::ERR_HEADER[] = "ERHD";
52 const char Conn::OK_LOGIN[] = "OKLG";
53 const char Conn::ERR_LOGIN[] = "ERLG";
54 const char Conn::OK_LOGINS[] = "OKLS";
55 const char Conn::ERR_LOGINS[] = "ERLS";
57 Conn::Conn(const SETTINGS & settings,
61 int sock, const sockaddr_in& addr)
62 : m_settings(settings),
71 m_xmlParser(XML_ParserCreate(NULL)),
74 m_bufferSize(sizeof(m_header)),
76 m_dataState(false, *this)
78 if (m_xmlParser == NULL)
79 throw Error("Failed to create XML parser.");
81 XML_ParserReset(m_xmlParser, NULL);
82 XML_SetElementHandler(m_xmlParser, ParseXMLStart, ParseXMLEnd);
83 XML_SetUserData(m_xmlParser, this);
85 /*m_parsers.push_back(new STG::PARSER::GET_SERVER_INFO(m_settings, m_users, m_tariffs));
87 m_parsers.push_back(new PARSER_GET_USERS);
88 m_parsers.push_back(new PARSER_GET_USER);
89 m_parsers.push_back(new PARSER_CHG_USER);
90 m_parsers.push_back(new PARSER_ADD_USER);
91 m_parsers.push_back(new PARSER_DEL_USER);
92 m_parsers.push_back(new PARSER_CHECK_USER);
93 m_parsers.push_back(new PARSER_SEND_MESSAGE);
94 m_parsers.push_back(new PARSER_AUTH_BY);
95 m_parsers.push_back(new PARSER_USER_INFO);
97 m_parsers.push_back(new PARSER_GET_TARIFFS);
98 m_parsers.push_back(new PARSER_ADD_TARIFF);
99 m_parsers.push_back(new PARSER_DEL_TARIFF);
100 m_parsers.push_back(new PARSER_CHG_TARIFF);
102 m_parsers.push_back(new PARSER_GET_ADMINS);
103 m_parsers.push_back(new PARSER_CHG_ADMIN);
104 m_parsers.push_back(new PARSER_DEL_ADMIN);
105 m_parsers.push_back(new PARSER_ADD_ADMIN);*/
110 shutdown(m_sock, SHUT_RDWR);
113 /*std::map<std::string, BASE_PARSER *>::iterator it(m_parsers.begin());
114 for (; it != m_parsers.end(); ++it)
116 XML_ParserFree(m_xmlParser);
121 ssize_t res = read(m_sock, m_buffer, m_bufferSize);
128 return HandleBuffer(res);
131 bool Conn::WriteAnswer(const void* buffer, size_t size)
133 ssize_t res = write(m_sock, buffer, size);
142 BASE_PARSER * Conn::GetParser(const std::string & tag)
144 if (strcasecmp(tag.c_str(), "getserverinfo") == 0)
145 return new STG::PARSER::GET_SERVER_INFO(*m_admin, m_settings, m_users, m_tariffs);
146 if (strcasecmp(tag.c_str(), "getadmins") == 0)
147 return new STG::PARSER::GET_ADMINS(*m_admin, m_admins);
148 if (strcasecmp(tag.c_str(), "addadmin") == 0)
149 return new STG::PARSER::ADD_ADMIN(*m_admin, m_admins);
150 if (strcasecmp(tag.c_str(), "deladmin") == 0)
151 return new STG::PARSER::DEL_ADMIN(*m_admin, m_admins);
152 if (strcasecmp(tag.c_str(), "chgadmin") == 0)
153 return new STG::PARSER::CHG_ADMIN(*m_admin, m_admins);
157 bool Conn::HandleBuffer(size_t size)
160 return HandleData(size);
162 if (m_bufferSize > 0)
167 case HEADER: return HandleHeader();
168 case LOGIN: return HandleLogin();
169 case CRYPTO_LOGIN: return HandleCryptoLogin();
170 default: return true;
176 bool Conn::HandleHeader()
178 if (strncmp(m_header, STG_HEADER, sizeof(m_header)) != 0)
180 WriteAnswer(ERR_HEADER, sizeof(ERR_HEADER));
187 m_bufferSize = sizeof(m_login);
188 return WriteAnswer(OK_HEADER, sizeof(OK_HEADER));
191 bool Conn::HandleLogin()
193 if (m_admins.Find(m_login, &m_admin)) // ADMINS::Find returns true on error.
195 WriteAnswer(ERR_LOGIN, sizeof(ERR_LOGIN));
200 m_admin->SetIP(IP());
201 m_state = CRYPTO_LOGIN;
202 m_buffer = m_cryptoLogin;
203 m_bufferSize = sizeof(m_cryptoLogin);
204 return WriteAnswer(OK_LOGIN, sizeof(OK_LOGIN));
207 bool Conn::HandleCryptoLogin()
209 char login[ADM_LOGIN_LEN + 1];
211 InitContext(m_admin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
212 DecryptString(login, m_cryptoLogin, ADM_LOGIN_LEN, &ctx);
214 if (strncmp(m_login, login, sizeof(login)) != 0)
216 WriteAnswer(ERR_LOGINS, sizeof(ERR_LOGINS));
224 m_bufferSize = sizeof(m_data);
225 m_stream = new STG::DECRYPT_STREAM(m_admin->GetPassword(), DataCallback, &m_dataState);
226 return WriteAnswer(OK_LOGINS, sizeof(OK_LOGINS));
229 bool Conn::HandleData(size_t size)
231 m_stream->Put(m_buffer, size, size == 0 || memchr(m_buffer, 0, size) != NULL);
232 return m_stream->IsOk();
235 bool Conn::DataCallback(const void * block, size_t size, void * data)
237 assert(data != NULL);
238 DataState& state = *static_cast<DataState *>(data);
240 if (XML_Parse(state.conn.m_xmlParser,
241 static_cast<const char *>(block),
242 size, state.final) == XML_STATUS_ERROR)
245 printfd(__FILE__, "XML parse error at line %d, %d: %s. Is final: %d",
246 static_cast<int>(XML_GetCurrentLineNumber(state.conn.m_xmlParser)),
247 static_cast<int>(XML_GetCurrentColumnNumber(state.conn.m_xmlParser)),
248 XML_ErrorString(XML_GetErrorCode(state.conn.m_xmlParser)), (int)state.final);
254 if (!state.conn.WriteResponse())
257 state.conn.m_state = ERROR;
260 state.conn.m_state = DONE;
266 void Conn::ParseXMLStart(void * data, const char * el, const char ** attr)
268 assert(data != NULL);
269 Conn & conn = *static_cast<Conn *>(data);
271 if (conn.m_parser == NULL)
272 conn.m_parser = conn.GetParser(el);
274 if (conn.m_parser == NULL)
277 conn.m_state = ERROR;
281 conn.m_parser->Start(data, el, attr);
284 void Conn::ParseXMLEnd(void * data, const char * el)
286 assert(data != NULL);
287 Conn & conn = *static_cast<Conn *>(data);
289 if (conn.m_parser == NULL)
292 conn.m_state = ERROR;
296 conn.m_parser->End(data, el);
299 bool Conn::WriteResponse()
301 STG::ENCRYPT_STREAM stream(m_admin->GetPassword(), WriteCallback, this);
302 const std::string & answer = m_parser->GetAnswer();
303 stream.Put(answer.c_str(), answer.length() + 1 /* including \0 */, true /* final */);
304 return stream.IsOk();
307 bool Conn::WriteCallback(const void * block, size_t size, void * data)
309 assert(data != NULL);
310 Conn & conn = *static_cast<Conn *>(data);
311 return WriteAll(conn.m_sock, block, size);;