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/logger.h"
26 #include "stg/blowfish.h"
27 #include "stg/bfstream.h"
28 #include "stg/common.h"
35 #include <sys/socket.h>
39 const char Conn::STG_HEADER[] = "SG04";
40 const char Conn::OK_HEADER[] = "OKHD";
41 const char Conn::ERR_HEADER[] = "ERHD";
42 const char Conn::OK_LOGIN[] = "OKLG";
43 const char Conn::ERR_LOGIN[] = "ERLG";
44 const char Conn::OK_LOGINS[] = "OKLS";
45 const char Conn::ERR_LOGINS[] = "ERLS";
47 Conn::Conn(const BASE_PARSER::REGISTRY & registry,
48 ADMINS & admins, int sock, const sockaddr_in& addr,
49 PLUGIN_LOGGER & logger)
50 : m_registry(registry),
57 m_xmlParser(XML_ParserCreate(NULL)),
60 m_bufferSize(sizeof(m_header)),
63 m_dataState(false, *this)
65 if (m_xmlParser == NULL)
66 throw Error("Failed to create XML parser.");
68 XML_ParserReset(m_xmlParser, NULL);
69 XML_SetElementHandler(m_xmlParser, ParseXMLStart, ParseXMLEnd);
70 XML_SetUserData(m_xmlParser, this);
75 shutdown(m_sock, SHUT_RDWR);
78 XML_ParserFree(m_xmlParser);
83 ssize_t res = read(m_sock, m_buffer, m_bufferSize);
87 Log(__FILE__, "Failed to read data from " + inet_ntostring(IP()) + ":" + x2str(Port()) + ". Reason: '" + strerror(errno) + "'");
90 if (res == 0 && m_state != DATA) // EOF is ok for data.
93 Log(__FILE__, "Failed to read data from " + inet_ntostring(IP()) + ":" + x2str(Port()) + ". Unexpected EOF.");
97 return HandleBuffer(res);
100 bool Conn::WriteAnswer(const void* buffer, size_t size)
102 ssize_t res = write(m_sock, buffer, size);
106 Log(__FILE__, "Failed to write data to " + inet_ntostring(IP()) + ":" + x2str(Port()) + ". Reason: '" + strerror(errno) + "'.");
112 BASE_PARSER * Conn::GetParser(const std::string & tag) const
114 BASE_PARSER::REGISTRY::const_iterator it = m_registry.find(ToLower(tag));
115 if (it == m_registry.end())
117 return it->second->create(*m_admin);
120 bool Conn::HandleBuffer(size_t size)
123 return HandleData(size);
125 if (m_bufferSize > 0)
130 case HEADER: return HandleHeader();
131 case LOGIN: return HandleLogin();
132 case CRYPTO_LOGIN: return HandleCryptoLogin();
133 default: return true;
139 bool Conn::HandleHeader()
141 if (strncmp(m_header, STG_HEADER, sizeof(m_header)) != 0)
143 Log(__FILE__, "Received invalid header from " + inet_ntostring(IP()) + ":" + x2str(Port()) + ".");
144 WriteAnswer(ERR_HEADER, sizeof(ERR_HEADER) - 1); // Without \0
150 m_bufferSize = sizeof(m_login);
151 return WriteAnswer(OK_HEADER, sizeof(OK_HEADER) - 1); // Without \0
154 bool Conn::HandleLogin()
156 if (m_admins.Find(m_login, &m_admin)) // ADMINS::Find returns true on error.
158 std::string login(m_login, strnlen(m_login, sizeof(m_login)));
159 Log(__FILE__, "Received invalid login '" + ToPrintable(login) + "' from " + inet_ntostring(IP()) + ":" + x2str(Port()) + ".");
160 WriteAnswer(ERR_LOGIN, sizeof(ERR_LOGIN) - 1); // Without \0
164 m_admin->SetIP(IP());
165 m_state = CRYPTO_LOGIN;
166 m_buffer = m_cryptoLogin;
167 m_bufferSize = sizeof(m_cryptoLogin);
168 return WriteAnswer(OK_LOGIN, sizeof(OK_LOGIN) - 1); // Without \0
171 bool Conn::HandleCryptoLogin()
173 char login[ADM_LOGIN_LEN + 1];
175 InitContext(m_admin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx);
176 DecryptString(login, m_cryptoLogin, ADM_LOGIN_LEN, &ctx);
178 if (strncmp(m_login, login, sizeof(login)) != 0)
180 Log(__FILE__, "Attempt to connect with wrong password from " + m_admin->GetLogin() + "@" + inet_ntostring(IP()) + ":" + x2str(Port()) + ".");
181 WriteAnswer(ERR_LOGINS, sizeof(ERR_LOGINS) - 1); // Without \0
188 m_bufferSize = sizeof(m_data);
189 m_stream = new STG::DECRYPT_STREAM(m_admin->GetPassword(), DataCallback, &m_dataState);
190 return WriteAnswer(OK_LOGINS, sizeof(OK_LOGINS) - 1); // Without \0
193 bool Conn::HandleData(size_t size)
195 m_stream->Put(m_buffer, size, size == 0 || memchr(m_buffer, 0, size) != NULL);
196 return m_stream->IsOk();
199 bool Conn::DataCallback(const void * block, size_t size, void * data)
201 assert(data != NULL);
202 DataState& state = *static_cast<DataState *>(data);
204 const char * xml = static_cast<const char *>(block);
205 size_t length = strnlen(xml, size);
207 state.final = state.final || length < size || size == 0;
209 if (XML_Parse(state.conn.m_xmlParser, xml, length, state.final) == XML_STATUS_ERROR)
211 state.conn.Log(__FILE__, "Received invalid XML from " + state.conn.m_admin->GetLogin() + "@" + inet_ntostring(state.conn.IP()) + ":" + x2str(state.conn.Port()) + ".");
212 printfd(__FILE__, "XML parse error at line %d, %d: %s. Is final: %d\n",
213 static_cast<int>(XML_GetCurrentLineNumber(state.conn.m_xmlParser)),
214 static_cast<int>(XML_GetCurrentColumnNumber(state.conn.m_xmlParser)),
215 XML_ErrorString(XML_GetErrorCode(state.conn.m_xmlParser)), (int)state.final);
216 printfd(__FILE__, "Data block: '%s' of size %d\n", xml, length);
217 state.conn.m_state = ERROR;
223 if (!state.conn.WriteResponse())
225 state.conn.Log(__FILE__, "Failed to write response to " + state.conn.m_admin->GetLogin() + "@" + inet_ntostring(state.conn.IP()) + ":" + x2str(state.conn.Port()) + ".");
226 state.conn.m_state = ERROR;
229 state.conn.m_state = DONE;
235 void Conn::ParseXMLStart(void * data, const char * el, const char ** attr)
237 assert(data != NULL);
238 Conn & conn = *static_cast<Conn *>(data);
240 if (conn.m_parser == NULL)
241 conn.m_parser = conn.GetParser(el);
243 if (conn.m_parser == NULL)
245 conn.Log(__FILE__, "Received unknown command '" + std::string(el) + "' from " + conn.m_admin->GetLogin() + "@" + inet_ntostring(conn.IP()) + ":" + x2str(conn.Port()) + ".");
246 conn.m_state = ERROR;
250 conn.m_parser->Start(data, el, attr);
253 void Conn::ParseXMLEnd(void * data, const char * el)
255 assert(data != NULL);
256 Conn & conn = *static_cast<Conn *>(data);
258 if (conn.m_parser == NULL)
260 // No need to log it.
261 conn.m_state = ERROR;
265 conn.m_parser->End(data, el);
268 bool Conn::WriteResponse()
270 STG::ENCRYPT_STREAM stream(m_admin->GetPassword(), WriteCallback, this);
272 if (m_parser != NULL)
273 answer = m_parser->GetAnswer();
275 answer = "<Error result=\"Unknown command.\"/>";
276 printfd(__FILE__, "Writing %d bytes of answer.\n", answer.length());
277 stream.Put(answer.c_str(), answer.length() + 1 /* including \0 */, true /* final */);
278 return stream.IsOk();
281 bool Conn::WriteCallback(const void * block, size_t size, void * data)
283 assert(data != NULL);
284 Conn & conn = *static_cast<Conn *>(data);
285 return WriteAll(conn.m_sock, block, size);;
288 void Conn::Log(const char * file, const std::string & message)
290 printfd(file, "%s\n", message.c_str());