#include "baseserv.h"

#include "dongle.h"

#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif

///////////////////////////////////////////////////////////
// TUserInfo
///////////////////////////////////////////////////////////

class TUserInfo : public wxObject
{
public:
	wxString m_strName;
	wxString m_strHost;
	wxDateTime m_time;
	size_t m_nPrograms;
		
	TUserInfo(const wxChar* user, const wxChar* host);
};

TUserInfo::TUserInfo(const wxChar* user, const wxChar* host)
         : m_strName(user), m_strHost(host), 
				   m_time(wxDateTime::Now()), m_nPrograms(0) 
{ 
}

class TUserTable : public wxObject
{
	wxHashTable m_Hash;
	

public:
  TUserInfo* AddConnection(wxSocketBase& sock, const wxChar* user, int session);
	TUserInfo* Find(wxSocketBase& sock, const wxChar* user, int session);
	void RemoveConnection(wxSocketBase& sock, const wxChar* user, int session);
	void Kill(const wxChar* user);
  void KillSession(wxSocketBase& sock, int session);

	size_t GetCount() const { return m_Hash.GetCount(); } 
	size_t GetLicenses(); 
	void BeginFind() { m_Hash.BeginFind(); } 
  TUserInfo* Next() { wxHashTable::Node* n = m_Hash.Next(); return n ? (TUserInfo*)n->GetData() : NULL; } 

	TUserTable(size_t size = 13);
};

size_t TUserTable::GetLicenses()
{
	wxHashTable Hosts;
	
	BeginFind();
  for (TUserInfo* ui = Next(); ui; ui = Next())
  	if (Hosts.Get(ui->m_strHost) == NULL)
			Hosts.Put(ui->m_strHost, ui);

	return Hosts.GetCount();
}

TUserInfo* TUserTable::AddConnection(wxSocketBase& sock, const wxChar* user, int session)
{
	TUserInfo* ui = Find(sock, user, session);
	if (ui == NULL)
	{
	  wxIPV4address peer; sock.GetPeer(peer);
    wxString host;
    host.Printf("%s:%d", (const char *) peer.Hostname(), session);
		ui = new TUserInfo(user, host);
		m_Hash.Put(wxString::Format("%s@%s", (const char *) user, host.c_str()), ui);
	}
	ui->m_nPrograms++;

	return ui;
}

TUserInfo* TUserTable::Find(wxSocketBase& sock, const wxChar* user, int session)
{
	wxIPV4address peer; sock.GetPeer(peer);
  wxString host;

  host = wxString::Format("%s:%d", (const char *) peer.Hostname(), session);

	if (user && *user)
  	return (TUserInfo*)m_Hash.Get(wxString::Format("%s@%s", (const char *) user, host.c_str()));

  BeginFind();
  for (TUserInfo* ui = Next(); ui; ui = Next())
	{
		if (ui->m_strHost == host)
			return ui;
	}

	return NULL;
}

void TUserTable::RemoveConnection(wxSocketBase& sock, const wxChar* user, int session)
{
	TUserInfo* ui = Find(sock, user, session);
	if (ui)
	{
		ui->m_nPrograms--;
		if (ui->m_nPrograms <= 0)
		  m_Hash.Delete(wxString::Format("%s@%s", ui->m_strName.c_str(), ui->m_strHost.c_str()));
	}
}

void TUserTable::Kill(const wxChar* strUser)
{
  BeginFind();
  for (TUserInfo* ui = Next(); ui; ui = Next())
	{
		if (ui->m_strName == strUser)
		{
		  m_Hash.Delete(wxString::Format("%s@%s", ui->m_strName.c_str(), ui->m_strHost.c_str()));
			break;
		}
	}
}

void TUserTable::KillSession(wxSocketBase& sock, int session)
{
  while (true)
  {
	  TUserInfo* ui = Find(sock, NULL, session);
		if (ui != NULL)
		  m_Hash.Delete(wxString::Format("%s@%s", ui->m_strName.c_str(), ui->m_strHost.c_str()));
    else
			break;
	}
}

TUserTable::TUserTable(size_t size) : m_Hash(wxKEY_STRING, size) 
{ 
	m_Hash.DeleteContents(true); 
}

///////////////////////////////////////////////////////////
// TAuthorizationServer
///////////////////////////////////////////////////////////

class TAuthorizationServer : public TBaseServerApp
{
private:
	TDongle m_Dongle;
	unsigned long m_Modules[3];
	unsigned int m_nModules;
	wxString m_strLastPassword;
	wxString m_strLastApp;
  unsigned int m_nPwdCount;
	unsigned int m_MaxTries;

	TUserTable m_Users;

protected:  
	virtual const wxChar* GetAppName() const;
  virtual void ProcessCommand(wxString cmd, wxSocketBase& outs);

  void AddNumber(TXmlItem& tr, int n) const;
  wxString DescribeModule(int m) const;
  bool KeyIsGood(const wxString& key, const wxString& gar) const;

	void InitModules();
  wxString GetModulesFilename() const;
  wxString Garble(unsigned short n, const wxDateTime& date) const;

public:
	bool IsMagicName(wxString& strFilename) const;

  void GenerateIndex(wxString& strFilename);
  void GenerateUsers(wxString& strFilename);
  void GenerateModules(wxString& strFilename);
	void GenerateFile(wxString& strFile);
  void ProcessFormCommand(wxString cmd, wxSocketBase& outs);

  void ProcessActivation(int nModuble, bool act, wxSocketBase& outs);
	void ReturnInt(wxSocketBase& outs, unsigned int i);
	void ReturnBool(wxSocketBase&, bool b);

  unsigned int DecodePassword(const wxChar* strPassword, const wxChar* strApp);
	void ProcessUserLogin(wxString cmd, wxSocketBase& sock);
	bool ProcessUserLogout(wxString cmd, wxSocketBase& sock);

	virtual bool Initialization();
	virtual bool Deinitialization();
};

void TAuthorizationServer::InitModules()
{
	m_nModules = 0;
	memset(m_Modules, 0, sizeof(m_Modules));
	m_Modules[0] = 2*sizeof(long);
	if (m_Dongle.Ok())
	{
		for (int i = 0; i < 48; i++)
		{
			if (m_Dongle.Active(i+1))
			{
				const size_t index = i / 32;
				const unsigned long mask = 1 << (i & 31);
				m_Modules[index+1] |= mask;
				m_nModules++;
			}
		}
	}
}

wxString TAuthorizationServer::Garble(unsigned short n, const wxDateTime& date) const
{
  const long val = date2julian(date);
  
  unsigned short data[4];
  data[0] = m_Dongle.Number();
  data[1] = n;
  data[2] = (unsigned short)(val >> 16);
  data[3] = (unsigned short)(val & 0xFFFF);
  m_Dongle.garble(data);
  return wxString::Format("%04X%04X", data[0], data[1]);
}

// Implementare almeno queste due funzioni pure virtuali

const wxChar* TAuthorizationServer::GetAppName() const
{
	return "Authorization";
}

bool TAuthorizationServer::IsMagicName(wxString& strFilename) const
{
  wxString strName;
	wxSplitPath(strFilename, NULL, &strName, NULL);
  strName.MakeLower();
  const int q = strName.Find('?');
	if (q > 0)
	  strName.Truncate(q);

	if (strName == "index" || strName == "users" || strName == "modules")
	{
		strFilename = strName;
		return true;
	} else
	if (strName == "log")
	{
		strFilename = GetLogFileName();
	} else
	if (strName == "activate" || strName == "deactivate" || 
		  strName == "year" || strName == "maxusers" || strName == "kill")
	{
		return true;
	}

  return false;
}

wxString TAuthorizationServer::GetModulesFilename() const
{
  return "../campo.aut";
}

wxString TAuthorizationServer::DescribeModule(int m) const
{
  const wxString strAut = GetModulesFilename();
	wxFileInputStream aut(strAut);
	wxString line;
	for (int nModule = 0; !aut.Eof(); nModule++)
	{
    aut >> line;
    if (nModule == m)
			return line.Mid(3).Trim();
	}
	return line;  // Should never happen!
}


void TAuthorizationServer::AddNumber(TXmlItem& tr, int n) const
{
	TXmlItem& td = tr.AddChild("td");
	td.SetAttr("align", "right");
	td << wxString::Format("%d", n);
}

void TAuthorizationServer::GenerateIndex(wxString& strFilename)
{
	TXmlItem html; 
	TXmlItem& body = CreatePageBody(html);

	TXmlItem& title = body.AddChild("h1").AddChild("center");
	if (m_Dongle.Ok())
	{
  	TXmlItem& tr = title.AddChild("table").SetAttr("width", "40%").AddChild("tr");
		
		TXmlItem& td = tr.AddChild("td").SetAttr("width", "30%");
		const bool hard = m_Dongle.hardware() == _dongle_hardlock;
		TXmlItem& img = td.AddChild("img");
		img.SetAttr("src", hard ? "hardlock.gif" : "eutron.gif");

		tr.AddChild("td").SetAttr("align", "center").AddChild("h1") << (hard ? "Hardlock EYE" : "Eutron Smartkey");
	}
	else
	{
		title << "No Dongle Connected!";
	}
	body.AddChild("br");

	TXmlItem& table = body.AddChild("center").AddChild("table");
  table.SetAttr("border", "1");
	table.SetAttr("width", "70%");

	TXmlItem& tr0 = body.AddChild("tr");
  tr0.AddChild("td") << "Serial Number";
	AddNumber(tr0, m_Dongle.Number());
	
	TXmlItem& tr1 = body.AddChild("tr");
  TXmlItem& ay = tr1.AddChild("td").AddChild("a");
  ay.SetAttr("href", "year.htm") << "Assistance Year";
	AddNumber(tr1, m_Dongle.YearAssist());

	TXmlItem& tr2 = body.AddChild("tr");
	TXmlItem& mu = tr2.AddChild("td").AddChild("a");
  mu.SetAttr("href", "maxusers.htm") << "Maximum Users";
	AddNumber(tr2, m_Dongle.MaxUsers());

	TXmlItem& tr3 = body.AddChild("tr");
  TXmlItem& au = tr3.AddChild("td").AddChild("a");
  au.SetAttr("href", "Users.htm");	au << "Active Users";
	AddNumber(tr3,  m_Users.GetCount());

	TXmlItem& tr3a = body.AddChild("tr");
  TXmlItem& lu = tr3a.AddChild("td").AddChild("a");
  lu << "Active Licenses";
	AddNumber(tr3a,  m_Users.GetLicenses());

	TXmlItem& tr4 = body.AddChild("tr");
  TXmlItem& am = tr4.AddChild("td").AddChild("a");
	am.SetAttr("href", "Modules.htm"); am << "Active Modules";
	AddNumber(tr4, m_nModules);

	TXmlItem& tr5 = body.AddChild("tr");
  tr5.AddChild("td") << "Host name:port";
  wxIPV4address addr; addr.AnyAddress(); addr.Service(GetDefaultPort());
  wxString strHP; strHP << addr.Hostname() << ":" << addr.Service();
  tr5.AddChild("td").SetAttr("align", "right") << strHP;

	TXmlItem& tr6 = body.AddChild("tr");
  TXmlItem& al = tr6.AddChild("td").AddChild("a");
	al.SetAttr("href", "Log"); al << "Log File";

	TXmlItem& tr7 = body.AddChild("tr");
  TXmlItem& as = tr7.AddChild("td").AddChild("a");
	as.SetAttr("href", "stop.cgi"); as << "Stop the Server";

	strFilename = GetTempFilename();
	html.Save(strFilename);
}

void TAuthorizationServer::GenerateModules(wxString& strFilename)
{
	TXmlItem html; 
	TXmlItem& body = CreatePageBody(html, "Modules");
	TXmlItem& table = body.AddChild("table");
  table.SetAttr("border", "1");	table.SetAttr("width", "100%");

	TXmlItem& th = body.AddChild("thead");
	th.AddChild("th").SetAttr("width", "7%") << "N.";
	th.AddChild("th").SetAttr("width", "8%") << "Module";
	th.AddChild("th").SetAttr("width", "70%") << "Description";

  const bool bNormalUser = m_Dongle.Number() > 0;
  if (bNormalUser)
	  th.AddChild("th").SetAttr("width", "15%") << "De/Activate";

  const wxString strAut = GetModulesFilename();
	wxFileInputStream aut(strAut);
	for (int nModule = 0; !aut.Eof(); nModule++)
	{
  	wxString line;
    aut >> line;
    if (line.IsEmpty())
      break;

		const wxString strCode = line.Left(2);
		const wxString strDesc = line.Mid(3);
		if (nModule > 0 && strCode != "xx" && !strDesc.IsEmpty())
		{
      const bool bOn = nModule == 0 || m_Dongle.Active(nModule);
			TXmlItem& tr = body.AddChild("tr");
			AddNumber(tr, nModule);
			tr.AddChild("td").SetAttr("align", "center") << strCode;
			TXmlItem& td = tr.AddChild("td");
      td.AddChild(bOn ? "b" : "i") << strDesc;
			if (bNormalUser && nModule > 0)
			{
				const char* prompt = bOn ? "Deactivate" : "Activate";
        const wxString href = wxString::Format("%s?%d", prompt, nModule);
				TXmlItem& bu = AddLinkButton(tr.AddChild("td"), prompt, href);
				bu.SetAttr("width", "100%");
				bu.SetAttr("title", wxString::Format("Click to %s Module %d", prompt, nModule));
			}
		}
	}

	body.AddChild("br");
	AddLinkButton(body.AddChild("center"), "Return to main page", "/");
	
	strFilename = GetTempFilename();
	html.Save(strFilename);
}

void TAuthorizationServer::GenerateUsers(wxString& strFilename)
{
	TXmlItem html; 
	TXmlItem& body = CreatePageBody(html, "Users");

	TXmlItem& table = body.AddChild("table");
  table.SetAttr("border", "1");	table.SetAttr("width", "100%");

  TXmlItem& th = table.AddChild("thead");
	th.AddChild("th").SetAttr("width", "15%") << "N.";
	th.AddChild("th").SetAttr("width", "10%") << "User";
	th.AddChild("th").SetAttr("width", "15%") << "Host";
	th.AddChild("th").SetAttr("width", "15%") << "Programs";
	th.AddChild("th").SetAttr("width", "15%") << "Time";
	th.AddChild("th").SetAttr("width", "15%") << "Date";
	th.AddChild("th").SetAttr("width", "15%") << "Kill";

	m_Users.BeginFind();
	int nUser = 0;
	for (TUserInfo* ui = m_Users.Next(); ui; ui = m_Users.Next())
	{
    TXmlItem& tr = table.AddChild("tr");
		tr.AddChild("td").SetAttr("align", "right") << wxString::Format("%d", ++nUser);
    tr.AddChild("td").AddChild("b") << ui->m_strName;
    tr.AddChild("td") << ui->m_strHost;
    tr.AddChild("td").SetAttr("align", "right") << wxString::Format("%u", ui->m_nPrograms);
    tr.AddChild("td").SetAttr("align", "center") << ui->m_time.Format("%H:%M:%S");
    tr.AddChild("td").SetAttr("align", "center") << ui->m_time.Format("%d-%m-%Y");

		wxString href = wxString::Format("kill.cgi?%s", ui->m_strName.c_str());
    AddLinkButton(tr.AddChild("td"), "Kill", href).SetAttr("width", "100%");
	}

	body.AddChild("br");
	AddLinkButton(body.AddChild("center"), "Return to main page", "/");

	strFilename = GetTempFilename();
	html.Save(strFilename);
}

void TAuthorizationServer::GenerateFile(wxString& strFilename)
{
  const int q = strFilename.Find('?');
	wxString strArgs;
	if (q > 0)
	{
		strArgs = strFilename.Mid(q+1);
	  strFilename.Truncate(q);
	}

  wxString strName;
	wxSplitPath(strFilename, NULL, &strName, NULL);
  strName.MakeLower();

	if (strName == "index")
	{
		GenerateIndex(strFilename);
	} else
	if (strName == "modules")
	{
		GenerateModules(strFilename);
	} else
	if (strName == "users")
	{
		GenerateUsers(strFilename);
	} else
	if (strName == "log")
	{
		strFilename = GetLogFileName();
	} else
	if (strName == "activate")
	{
		const int nModule = atoi(strArgs);

		TXmlItem html; 
		TXmlItem& body = CreatePageBody(html);
    TXmlItem& form = body.AddChild("form");
		form.SetAttr("action", "activate"); form.SetAttr("method", "post");

    TXmlItem& table = form.AddChild("center").AddChild("table");
		table.SetAttr("width", "70%").SetAttr("border", "1");
		table.AddChild("caption").AddChild("h2") << "Module Activation";
		
		TXmlItem& tr0 = table.AddChild("tr");
    tr0.AddChild("td") << wxString::Format("Module %d", nModule);
		tr0.AddChild("td") << DescribeModule(nModule);
 		TXmlItem& module = tr0.AddChild("td").AddChild("input");
		module.SetAttr("type", "hidden"); module.SetAttr("name", "module");
		module.SetAttr("value", nModule);

		TXmlItem& tr1 = table.AddChild("tr");
  	tr1.AddChild("td") << "Activation date (dd-mm-yyyy)";
		TXmlItem& date = tr1.AddChild("td").AddChild("input");
		date.SetAttr("type", "string"); date.SetAttr("name", "date");
		date.SetAttr("size", "10"); date.SetAttr("maxlength", "10");
		date.SetAttr("value", Date2String(wxDateTime::Now()));

		TXmlItem& tr2 = table.AddChild("tr");
		tr2.AddChild("td") << "Activation code";

		TXmlItem& key = tr2.AddChild("td").AddChild("input");
		key.SetAttr("type", "string"); key.SetAttr("name", "key");
		key.SetAttr("size", "8"); key.SetAttr("maxlength", "8");
		
		TXmlItem& tr3 = table.AddChild("tr");
		tr3.AddChild("td").AddChild("a").SetAttr("href", "/") << "Return to main page";
		TXmlItem& submit = tr3.AddChild("td").AddChild("input");
		submit.SetAttr("type", "submit");	
		submit.SetAttr("value", "Confirm Activation");
		
		strFilename = GetTempFilename();
		html.Save(strFilename);
	} else
	if (strName == "deactivate")
	{
		const int nModule = atoi(strArgs);
		TXmlItem html; 
		TXmlItem& body = CreatePageBody(html).AddChild("center");
		body.AddChild("h1") << "WARNING!";
		body.AddChild("br");
		body.AddChild("h3") << "You are about to deactivate the following module:";
		body.AddChild("br");
		body.AddChild("h2") << DescribeModule(nModule);
		body.AddChild("br");
    TXmlItem& form = body.AddChild("form");
		form.SetAttr("action", "deactivate"); form.SetAttr("method", "post");
		TXmlItem& module = form.AddChild("input");
		module.SetAttr("type", "hidden");
		module.SetAttr("name", "module");
		module.SetAttr("value", wxString::Format("%d", nModule));

		TXmlItem& submit = form.AddChild("input");
		submit.SetAttr("type", "submit");
		submit.SetAttr("value", "Confirm Deactivation");

		body.AddChild("br"); body.AddChild("br");
		AddLinkButton(body, "Return to modules list", "Modules");
		
		strFilename = GetTempFilename();
		html.Save(strFilename);
	} else
	if (strName == "year")
	{
//		const int nModule = atoi(strArgs);
		TXmlItem html; 
		TXmlItem& body = CreatePageBody(html);
    TXmlItem& form = body.AddChild("form");
		form.SetAttr("action", "year"); form.SetAttr("method", "post");

    TXmlItem& table = form.AddChild("center").AddChild("table");
		table.SetAttr("width", "70%").SetAttr("border", "1");
		table.AddChild("caption").AddChild("h2") << "Year of Assistance";
		
		TXmlItem& tr0 = table.AddChild("tr");
		tr0.AddChild("td") << "Assistance year to be activated";

		TXmlItem& year = tr0.AddChild("td").AddChild("input");
		year.SetAttr("type", "string");	year.SetAttr("name", "year");
		year.SetAttr("size", "4"); year.SetAttr("maxlength", "4");
		year.SetAttr("value", wxString::Format("%d", m_Dongle.YearAssist()+1));

		TXmlItem& tr1 = table.AddChild("tr");
  	tr1.AddChild("td") << "Activation date (dd-mm-yyyy)";
		TXmlItem& date = tr1.AddChild("td").AddChild("input");
		date.SetAttr("type", "string"); date.SetAttr("name", "date");
		date.SetAttr("size", "10"); date.SetAttr("maxlength", "10");
		date.SetAttr("value", Date2String(wxDateTime::Now()));

		TXmlItem& tr2 = table.AddChild("tr");
		tr2.AddChild("td") << "Activation code";

		TXmlItem& key = tr2.AddChild("td").AddChild("input");
		key.SetAttr("type", "string"); key.SetAttr("name", "key");
		key.SetAttr("size", "8"); key.SetAttr("maxlength", "8");
		
		TXmlItem& tr3 = table.AddChild("tr");
		tr3.AddChild("td").AddChild("a").SetAttr("href", "/") << "Return to main page";
		TXmlItem& submit = tr3.AddChild("td").AddChild("input");
		submit.SetAttr("type", "submit");	
		submit.SetAttr("value", "Confirm Activation");
		
		strFilename = GetTempFilename();
		html.Save(strFilename);
	} else
	if (strName == "maxusers")
	{
//		const int nModule = atoi(strArgs);
		TXmlItem html; 
		TXmlItem& body = CreatePageBody(html);
	  TXmlItem& form = body.AddChild("form");
		form.SetAttr("action", "maxusers"); form.SetAttr("method", "post");

    TXmlItem& table = form.AddChild("center").AddChild("table");
		table.SetAttr("width", "70%").SetAttr("border", "1");
		table.AddChild("caption").AddChild("h2") << "Maximum Users";
   	
		TXmlItem& tr0 = table.AddChild("tr");
		tr0.AddChild("td") << "Number of users";

		TXmlItem& year = tr0.AddChild("td").AddChild("input");
		year.SetAttr("type", "string");	year.SetAttr("name", "users");
		year.SetAttr("size", "4"); year.SetAttr("maxlength", "4");
		year.SetAttr("value", wxString::Format("%d", m_Dongle.MaxUsers()));

		TXmlItem& tr1 = table.AddChild("tr");
  	tr1.AddChild("td") << "Activation date (dd-mm-yyyy)";
		TXmlItem& date = tr1.AddChild("td").AddChild("input");
		date.SetAttr("type", "string"); date.SetAttr("name", "date");
		date.SetAttr("size", "10"); date.SetAttr("maxlength", "10");
		date.SetAttr("value", Date2String(wxDateTime::Now()));

		TXmlItem& tr2 = table.AddChild("tr");
		tr2.AddChild("td") << "Activation code";

		TXmlItem& key = tr2.AddChild("td").AddChild("input");
		key.SetAttr("type", "string"); key.SetAttr("name", "key");
		key.SetAttr("size", "8"); key.SetAttr("maxlength", "8");
    form.AddChild("br"); form.AddChild("br");
		
		TXmlItem& tr3 = table.AddChild("tr");
		tr3.AddChild("td").AddChild("a").SetAttr("href", "index") << "Return to main page";
		TXmlItem& submit = tr3.AddChild("td").AddChild("input");
		submit.SetAttr("type", "submit");	
		submit.SetAttr("value", "Confirm Activation");
		
		strFilename = GetTempFilename();
		html.Save(strFilename);
	} else
	if (strName == "kill")
	{
		m_Users.Kill(strArgs);
		GenerateUsers(strFilename);
	}
}

void TAuthorizationServer::ProcessActivation(int nModule, bool act, wxSocketBase& outs)
{
	if (nModule > 0)
	{
		if (act)
		  m_Dongle.Activate(nModule);
		else
		  m_Dongle.Deactivate(nModule);
		m_Dongle.Burn();
		InitModules();
	}
	wxString strFileName = "Modules";
	GenerateFile(strFileName);
	SendFile(strFileName, outs);
}

bool TAuthorizationServer::KeyIsGood(const wxString& key, const wxString& gar) const
{
#ifdef NDEBUG
  return key.IsSameAs(gar, false);
#else
  return key.Length() == 8;
#endif
}

void TAuthorizationServer::ProcessFormCommand(wxString cmd, wxSocketBase& outs)
{
  const int stop = cmd.Find(" HTTP");
	wxString strFileName = cmd.Mid(5, stop-5).Trim();

	wxString strName, args;
	wxSplitPath(strFileName, NULL, &strName, NULL);
	strName.MakeLower();

	const int pos = cmd.Find("\r\n\r\n");
	if (pos > 0)
	  args = cmd.Mid(pos+4);

	THashTable hashArgs(13);
  ParseArguments(args, hashArgs);

	if (strName == "activate")
	{
		const int nModule = hashArgs.GetInt("module");
		const wxDateTime date = hashArgs.GetDate("date");
		const wxString key = hashArgs.Get("key");
		const wxString gar = Garble(nModule, date);
		if (KeyIsGood(key, gar))
	    ProcessActivation(nModule, true, outs);
    else
			MessageBox("ERROR!", "You supplied the wrong activation code", outs);
	} else
	if (strName == "deactivate")
	{
		const int nModule = atoi(hashArgs.Get("module"));
		ProcessActivation(nModule, false, outs);
	} else
	if (strName == "year")
	{
		const int year = hashArgs.GetInt("year");
		const wxDateTime date = hashArgs.GetDate("date");
		const wxString key = hashArgs.Get("key");
		const wxString gar = Garble(year, date);
		if (KeyIsGood(key, gar))
		{
			m_Dongle.set_year_assist(year);
  		m_Dongle.Burn();
			wxString strFileName = "index";
			GenerateFile(strFileName);
			SendFile(strFileName, outs);
		}
    else
			MessageBox("ERROR!", "You supplied the wrong activation code", outs);
	} else
	if (strName == "maxusers")
	{
		const int users = atoi(hashArgs.Get("users"));
		const wxDateTime date = hashArgs.GetDate("date");
		const wxString key = hashArgs.Get("key");
		const wxString gar = Garble(users, date);
		if (KeyIsGood(key, gar))
		{
			m_Dongle.set_max_users(users);
  		m_Dongle.Burn();
			wxString strFileName = "index";
			GenerateFile(strFileName);
			SendFile(strFileName, outs);
		}
    else
			MessageBox("ERROR!", "You supplied the wrong activation code", outs);
	}
}

unsigned int TAuthorizationServer::DecodePassword(const wxChar* strPassword, const wxChar* strApp)
{
	const unsigned int BASE = 19;
  unsigned int num = 0;
	size_t len = 0;
	for (const wxChar* c = strPassword; *c; c++)
	{
		num *= BASE;
		if (*c >= '0' && *c <= '9')
		{
			num += *c - '0';
		}	
		else
		{
  		if (*c >= 'A' && *c <= 'Z')
			{
	  		num += *c - 'A' + 10;
			}
			else
				break; // Carattere non valido
		}
    len++;
	}
	// Per essereva valido deve essere divisibile per 883
	if (len >= 5 && (num%883) == 0)
	{
    if (m_strLastPassword != strPassword || m_strLastApp != strApp)
    {
		  m_strLastPassword = strPassword;
		  m_strLastApp = strApp;
      m_nPwdCount = 0;
    }
    else
      m_nPwdCount++;

    if (m_nPwdCount < m_MaxTries)
    {
		  // Creo la risposta: met� di num reso divisibile per 883
		  num /= 2;
		  while (num % 883 != 0)
			  num++;
    }
    else
      num = 0;
	}
	else
		num = 0;
	return num;
}

void TAuthorizationServer::ProcessUserLogin(wxString cmd, wxSocketBase& sock)
{
	wxChar strUser[32];
	wxChar strPassword[32];
	wxChar strProgram[32];
	int session;
	
	cmd.Replace(",", " "); 	cmd.Replace(")", " ");
	const int nStr = sscanf(cmd, "UserLogin(%s %s %s %d)", strUser, strPassword, strProgram, &session);
	
	if (nStr < 4)
		session = 0;

	unsigned int num = 0;
	if (m_Dongle.Connected())
	{
// Preliminary GUEST login
//		if (wxStricmp(strUser,"******")==0 && wxStricmp(strProgram, "ba0100")==0)
//		{
//			num = 1;
//		}
//		else
		{
      if (m_Users.GetLicenses() >= m_Dongle.MaxUsers() && m_Users.Find(sock, NULL, session) == NULL)
      {
        WriteLog("*** Maximum users exceeded");
	  		num = 0;
      }
			else
			{
				if (strcmp(strPassword, "******") == 0) // Older 16 bit version
          num = 1;
				else
					num = DecodePassword(strPassword, strProgram);
  			if (num > 0)
					m_Users.AddConnection(sock, strUser, session);
        else
          WriteLog("*** Bad password");
			}
		}
	}
  else
    WriteLog("*** Dongle not responding");

	ReturnInt(sock, num);
}

bool TAuthorizationServer::ProcessUserLogout(wxString cmd, wxSocketBase& sock)
{
	wxChar strUser[32];
  wxChar strProgram[32];
	int session;
	
	cmd.Replace(",", " "); 	cmd.Replace(")", " ");
	const int nStr = sscanf(cmd, "UserLogout(%s %d %s)", strUser, &session, strProgram);

	if (nStr < 2)
		session = 0;

  m_Users.RemoveConnection(sock, strUser, session);
  if (strcmp(strProgram, "ba0100") == 0)
    m_Users.KillSession(sock, session);

	return true;
}

void TAuthorizationServer::ReturnInt(wxSocketBase& outs, unsigned int i)
{
	const unsigned int buf[2] = { sizeof(i), i };
	outs.Write(buf, sizeof(buf));
}

void TAuthorizationServer::ReturnBool(wxSocketBase& outs, bool b)
{
	ReturnInt(outs, b ? 1 : 0);
}

void TAuthorizationServer::ProcessCommand(wxString cmd, wxSocketBase& outs)
{
	if (cmd.StartsWith("GET "))
	{
		const int stop = cmd.Find(" HTTP");
		wxString str;
		if (stop > 4)
			str = cmd.Mid(4, stop-4).Trim();
		else
      str = cmd.Mid(4).Trim();
		if (str == "/")
			str += "index.htm";
		wxString strFilename = GetDocumentRoot() + str;

		if (IsMagicName(strFilename))
			GenerateFile(strFilename);

		SendFile(strFilename, outs);
	} else
	if (cmd.StartsWith("POST "))
	{
		ProcessFormCommand(cmd, outs);
	} else
	if (cmd.StartsWith("UserLogin"))
	{
		ProcessUserLogin(cmd, outs);
	} else
	if (cmd.StartsWith("UserLogout"))
	{
		bool ok = ProcessUserLogout(cmd, outs);
		ReturnBool(outs, ok);
	} else
	if (cmd.StartsWith("DongleNumber"))
	{
		ReturnInt(outs, m_Dongle.Number());
	} else
	if (cmd.StartsWith("DongleYear"))
	{
		ReturnInt(outs, m_Dongle.YearAssist());
	} else
	if (cmd.StartsWith("DongleModules"))
	{
   	outs.Write(m_Modules, sizeof(m_Modules));
	}
}

#define ATOMIC_SEMAPHORE "DONGLE_SERVER_ATOM"

bool TAuthorizationServer::Initialization()
{
#ifdef WIN32
	// Add global atom if not already present
	if (::GlobalFindAtom(ATOMIC_SEMAPHORE) == NULL)
	  ::GlobalAddAtom(ATOMIC_SEMAPHORE); // Same as old Frontend.exe
#endif
	const int delay =	GetConfigInt("Delay", 10);
	m_MaxTries =	GetConfigInt("MaxTries", 8);

	if (m_MaxTries < 8)
		m_MaxTries = 8;

	for (int i = 0; i < 3; i++)
	{
		if (m_Dongle.Login())
			break;
		wxSleep(delay);
	}
	InitModules();

	return true;
}

bool TAuthorizationServer::Deinitialization()
{
	m_Dongle.Logout();
#ifdef WIN32
	// Definitely kill global atom
	for (ATOM a = ::GlobalFindAtom(ATOMIC_SEMAPHORE);
	     a != NULL;
			 a = ::GlobalDeleteAtom(a));
#endif
	return true;
}

// Istanziare l'applicazione principale

IMPLEMENT_APP(TAuthorizationServer)