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 BASE_PARSER * Conn::GetParser(const std::string & tag)
133 if (strcasecmp(tag.c_str(), "getserverinfo") == 0)
134 return new STG::PARSER::GET_SERVER_INFO(*m_admin, m_settings, m_users, m_tariffs);
135 if (strcasecmp(tag.c_str(), "getadmins") == 0)
136 return new STG::PARSER::GET_ADMINS(*m_admin, m_admins);
137 if (strcasecmp(tag.c_str(), "addadmin") == 0)
138 return new STG::PARSER::ADD_ADMIN(*m_admin, m_admins);
139 if (strcasecmp(tag.c_str(), "deladmin") == 0)
140 return new STG::PARSER::DEL_ADMIN(*m_admin, m_admins);
141 if (strcasecmp(tag.c_str(), "chgadmin") == 0)
142 return new STG::PARSER::CHG_ADMIN(*m_admin, m_admins);
146 bool Conn::HandleBuffer(size_t size)
149 return HandleData(size);
151 if (m_bufferSize > 0)
156 case HEADER: return HandleHeader();
157 case LOGIN: return HandleLogin();
158 case CRYPTO_LOGIN: return HandleCryptoLogin();
159 default: return true;
165 bool Conn::HandleHeader()
167 if (strncmp(m_header, STG_HEADER, sizeof(m_header)) != 0)
175 m_bufferSize = sizeof(m_login);
179 bool Conn::HandleLogin()
181 if (m_admins.Find(m_login, &m_admin)) // ADMINS::Find returns true on error.
187 m_admin->SetIP(IP());
188 m_state = CRYPTO_LOGIN;
189 m_buffer = m_cryptoLogin;
190 m_bufferSize = sizeof(m_cryptoLogin);
194 bool Conn::HandleCryptoLogin()
196 char login[ADM_LOGIN_LEN + 1];
198 InitContext(m_admin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
199 DecryptString(login, m_cryptoLogin, ADM_LOGIN_LEN, &ctx);
201 if (strncmp(m_login, login, sizeof(login)) != 0)
210 m_bufferSize = sizeof(m_data);
211 m_stream = new STG::DECRYPT_STREAM(m_admin->GetPassword(), DataCallback, &m_dataState);
215 bool Conn::HandleData(size_t size)
217 m_stream->Put(m_buffer, size, size == 0 || memchr(m_buffer, 0, size) != NULL);
218 return m_stream->IsOk();
221 bool Conn::DataCallback(const void * block, size_t size, void * data)
223 assert(data != NULL);
224 DataState& state = *static_cast<DataState *>(data);
226 if (XML_Parse(state.conn.m_xmlParser,
227 static_cast<const char *>(block),
228 size, state.final) == XML_STATUS_ERROR)
231 printfd(__FILE__, "XML parse error at line %d, %d: %s. Is final: %d",
232 static_cast<int>(XML_GetCurrentLineNumber(state.conn.m_xmlParser)),
233 static_cast<int>(XML_GetCurrentColumnNumber(state.conn.m_xmlParser)),
234 XML_ErrorString(XML_GetErrorCode(state.conn.m_xmlParser)), (int)state.final);
241 void Conn::ParseXMLStart(void * data, const char * el, const char ** attr)
243 assert(data != NULL);
244 Conn & conn = *static_cast<Conn *>(data);
246 if (conn.m_parser == NULL)
247 conn.m_parser = conn.GetParser(el);
249 if (conn.m_parser == NULL)
252 conn.m_state = ERROR;
256 conn.m_parser->Start(data, el, attr);
259 void Conn::ParseXMLEnd(void * data, const char * el)
261 assert(data != NULL);
262 Conn & conn = *static_cast<Conn *>(data);
264 if (conn.m_parser == NULL)
267 conn.m_state = ERROR;
271 conn.m_parser->End(data, el);