Esempio n. 1
0
bool IsValidAddressFilter(CStdString& filter)
{
	CStdString left;
	int pos = filter.Find(_T("/"));
	int prefixLength = 0;
	if (!pos)
		return false;
	else if (pos != -1)
	{
		left = filter.Left(pos);
		prefixLength = _ttoi(filter.Mid(pos + 1));
		if (prefixLength <= 0 || prefixLength > 128)
			return false;
	}
	else
		left = filter;

	if (!IsIpAddress(left))
		return false;

	if (left.Find(':') != -1)
		left = GetIPV6ShortForm(left);
	if (prefixLength)
		filter.Format(_T("%s/%d"), (LPCTSTR)left, prefixLength);
	else
		filter = left;


	return true;
}
Esempio n. 2
0
int CRealControlSocket::ContinueConnect()
{
	wxString host;
	unsigned int port = 0;

	const int proxy_type = m_pEngine->GetOptions()->GetOptionVal(OPTION_PROXY_TYPE);
	if (proxy_type > CProxySocket::unknown && proxy_type < CProxySocket::proxytype_count && !m_pCurrentServer->GetBypassProxy())
	{
		LogMessage(::Status, _("Connecting to %s through proxy"), m_pCurrentServer->FormatHost().c_str());

		host = m_pEngine->GetOptions()->GetOption(OPTION_PROXY_HOST);
		port = m_pEngine->GetOptions()->GetOptionVal(OPTION_PROXY_PORT);

		delete m_pBackend;
		m_pProxyBackend = new CProxySocket(this, m_pSocket, this);
		m_pBackend = m_pProxyBackend;
		int res = m_pProxyBackend->Handshake((enum CProxySocket::ProxyType)proxy_type,
											 m_pCurrentServer->GetHost(), m_pCurrentServer->GetPort(),
											  m_pEngine->GetOptions()->GetOption(OPTION_PROXY_USER),
											  m_pEngine->GetOptions()->GetOption(OPTION_PROXY_PASS));

		if (res != EINPROGRESS)
		{
			LogMessage(::Error, _("Could not start proxy handshake: %s"), CSocket::GetErrorDescription(res).c_str());
			DoClose();
			return FZ_REPLY_ERROR;
		}
	}
	else
	{
		if (m_pCurOpData && m_pCurOpData->opId == cmd_connect)
		{
			CConnectOpData* pData = (CConnectOpData*)m_pCurOpData;
			host = ConvertDomainName(pData->host);
			port = pData->port;
		}
		if (host == _T(""))
		{
			host = m_pCurrentServer->GetHost();
			port = m_pCurrentServer->GetPort();
		}
	}
	if (!IsIpAddress(host))
		LogMessage(Status, _("Resolving address of %s"), host.c_str());

	int res = m_pSocket->Connect(host, port);

	// Treat success same as EINPROGRESS, we wait for connect notification in any case
	if (res && res != EINPROGRESS)
	{
		LogMessage(::Error, _("Could not connect to server: %s"), CSocket::GetErrorDescription(res).c_str());
		DoClose();
		return FZ_REPLY_ERROR;
	}

	return FZ_REPLY_WOULDBLOCK;
}
Esempio n. 3
0
int CHttpControlSocket::InternalConnect(wxString host, unsigned short port, bool tls)
{
	LogMessage(Debug_Verbose, _T("CHttpControlSocket::InternalConnect()"));

	CHttpConnectOpData* pData = new CHttpConnectOpData;
	pData->pNextOpData = m_pCurOpData;
	m_pCurOpData = pData;
	pData->port = port;
	pData->tls = tls;

	if (!IsIpAddress(host))
		LogMessage(Status, _("Resolving address of %s"), host.c_str());

	pData->host = host;
	return DoInternalConnect();
}
Esempio n. 4
0
int CTlsSocket::Handshake(const CTlsSocket* pPrimarySocket, bool try_resume)
{
	m_pOwner->LogMessage(MessageType::Debug_Verbose, _T("CTlsSocket::Handshake()"));
	wxASSERT(m_session);

	m_tlsState = TlsState::handshake;

	wxString hostname;

	if (pPrimarySocket) {
		// Implicitly trust certificate of primary socket
		unsigned int cert_list_size;
		const gnutls_datum_t* const cert_list = gnutls_certificate_get_peers(pPrimarySocket->m_session, &cert_list_size);
		if (cert_list && cert_list_size)
		{
			delete [] m_implicitTrustedCert.data;
			m_implicitTrustedCert.data = new unsigned char[cert_list[0].size];
			memcpy(m_implicitTrustedCert.data, cert_list[0].data, cert_list[0].size);
			m_implicitTrustedCert.size = cert_list[0].size;
		}

		if (try_resume)
		{
			if (!CopySessionData(pPrimarySocket))
				return FZ_REPLY_ERROR;
		}

		hostname = pPrimarySocket->m_pSocket->GetPeerHost();
	}
	else {
		hostname = m_pSocket->GetPeerHost();
	}

	if( !hostname.empty() && !IsIpAddress(hostname) ) {
		const wxWX2MBbuf utf8 = hostname.mb_str(wxConvUTF8);
		if( utf8 ) {
			int res = gnutls_server_name_set( m_session, GNUTLS_NAME_DNS, utf8, strlen(utf8) );
			if( res ) {
				LogError(res, _T("gnutls_server_name_set"), MessageType::Debug_Warning );
			}
		}
	}

	return ContinueHandshake();
}
Esempio n. 5
0
int CRealControlSocket::Connect(const CServer &server)
{
	SetWait(true);

	if (server.GetEncodingType() == ENCODING_CUSTOM)
		m_pCSConv = new wxCSConv(server.GetCustomEncoding());

	if (m_pCurrentServer)
		delete m_pCurrentServer;
	m_pCurrentServer = new CServer(server);

	CConnectOpData* pData;
	if (!m_pCurOpData || m_pCurOpData->opId != cmd_connect)
		pData = 0;
	else
		pData = static_cast<CConnectOpData *>(m_pCurOpData);
	
	const wxString& host = pData ? pData->host : server.GetHost();
	if (!IsIpAddress(host))
	{
		LogMessage(Status, _("Resolving IP-Address for %s"), host.c_str());
		CAsyncHostResolver *resolver = new CAsyncHostResolver(m_pEngine, ConvertDomainName(host));
		m_pEngine->AddNewAsyncHostResolver(resolver);

		resolver->Create();
		resolver->Run();
	}
	else
	{
		wxIPV4address addr;
		addr.Hostname(host);
		return ContinueConnect(&addr);
	}

	return FZ_REPLY_WOULDBLOCK;
}
bool COptionsPageConnectionActive::Validate()
{
	// Validate port limiting settings
	if (GetCheck(XRCID("ID_LIMITPORTS")))
	{
		wxTextCtrl* pLow = XRCCTRL(*this, "ID_LOWESTPORT", wxTextCtrl);

		long low;
		if (!pLow->GetValue().ToLong(&low) || low < 1024 || low > 65535)
			return DisplayError(pLow, _("Lowest available port has to be a number between 1024 and 65535."));

		wxTextCtrl* pHigh = XRCCTRL(*this, "ID_LOWESTPORT", wxTextCtrl);

		long high;
		if (!pHigh->GetValue().ToLong(&high) || high < 1024 || high > 65535)
			return DisplayError(pHigh, _("Highest available port has to be a number between 1024 and 65535."));

		if (low > high)
			return DisplayError(pLow, _("The lowest available port has to be less or equal than the highest available port."));
	}

	int mode;
	if (GetRCheck(XRCID("ID_ACTIVEMODE1")))
		mode = 0;
	else
		mode = GetRCheck(XRCID("ID_ACTIVEMODE2")) ? 1 : 2;

	if (mode == 1)
	{
		wxTextCtrl* pActiveIP = XRCCTRL(*this, "ID_ACTIVEIP", wxTextCtrl);
		if (!IsIpAddress(pActiveIP->GetValue()))
			return DisplayError(pActiveIP, _("You have to enter a valid IP address."));
	}

	return true;
}
Esempio n. 7
0
bool AppSecInc::TcpIp::DNS::IsIpAddress(const std::wstring& name, int family)
{
    return IsIpAddress(AppSecInc::StringUtils::wc2mb(name), family);
}
Esempio n. 8
0
void COptions::SetOption(int nOptionID, LPCTSTR value, bool save /*=true*/)
{
	CStdString str = value;
	Init();

	switch (nOptionID)
	{
	case OPTION_SERVERPORT:
	case OPTION_TLSPORTS:
		{
			std::set<int> portSet;

			str.TrimLeft(_T(" ,"));

			int pos = str.FindOneOf(_T(" ,"));
			while (pos != -1)
			{
				int port = _ttoi(str.Left(pos));
				if (port >= 1 && port <= 65535)
					portSet.insert(port);
				str = str.Mid(pos + 1);
				str.TrimLeft(_T(" ,"));
				pos = str.FindOneOf(_T(" ,"));
			}
			if (str != _T(""))
			{
				int port = _ttoi(str);
				if (port >= 1 && port <= 65535)
					portSet.insert(port);
			}

			str = _T("");
			for (std::set<int>::const_iterator iter = portSet.begin(); iter != portSet.end(); iter++)
			{
				CStdString tmp;
				tmp.Format(_T("%d "), *iter);
				str += tmp;
			}
			str.TrimRight(' ');
		}
		break;
	case OPTION_WELCOMEMESSAGE:
		{
			std::vector<CStdString> msgLines;
			int oldpos = 0;
			str.Replace(_T("\r\n"), _T("\n"));
			int pos = str.Find(_T("\n"));
			CStdString line;
			while (pos != -1)
			{
				if (pos)
				{
					line = str.Mid(oldpos, pos - oldpos);
					line = line.Left(CONST_WELCOMEMESSAGE_LINESIZE);
					line.TrimRight(_T(" "));
					if (msgLines.size() || line != _T(""))
						msgLines.push_back(line);
				}
				oldpos = pos + 1;
				pos = str.Find(_T("\n"), oldpos);
			}
			line = str.Mid(oldpos);
			if (line != _T(""))
			{
				line = line.Left(CONST_WELCOMEMESSAGE_LINESIZE);
				msgLines.push_back(line);
			}
			str = _T("");
			for (unsigned int i = 0; i < msgLines.size(); i++)
				str += msgLines[i] + _T("\r\n");
			str.TrimRight(_T("\r\n"));
			if (str == _T(""))
			{
				str = _T("%v");
				str += _T("\r\nwritten by Tim Kosse ([email protected])");
				str += _T("\r\nPlease visit https://filezilla-project.org/");
			}
		}
		break;
	case OPTION_ADMINIPBINDINGS:
		{
			CStdString sub;
			std::list<CStdString> ipBindList;
			for (unsigned int i = 0; i<_tcslen(value); i++)
			{
				TCHAR cur = value[i];
				if ((cur < '0' || cur > '9') && cur != '.' && cur != ':')
				{
					if (sub == _T("") && cur == '*')
					{
						ipBindList.clear();
						ipBindList.push_back(_T("*"));
						break;
					}

					if (sub != _T(""))
					{
						if (IsIpAddress(sub))
							ipBindList.push_back(sub);
						sub = _T("");
					}
				}
				else
					sub += cur;
			}
			if (sub != _T(""))
			{
				if (IsIpAddress(sub))
					ipBindList.push_back(sub);
			}
			str = _T("");
			for (std::list<CStdString>::iterator iter = ipBindList.begin(); iter!=ipBindList.end(); iter++)
				if (!IsLocalhost(*iter))
					str += *iter + _T(" ");

			str.TrimRight(_T(" "));
		}
		break;
	case OPTION_ADMINPASS:
		if (str != _T("") && str.GetLength() < 6)
			return;
		break;
	case OPTION_MODEZ_DISALLOWED_IPS:
	case OPTION_IPFILTER_ALLOWED:
	case OPTION_IPFILTER_DISALLOWED:
	case OPTION_ADMINIPADDRESSES:
		{
			str.Replace('\r', ' ');
			str.Replace('\n', ' ');
			str.Replace('\r', ' ');
			while (str.Replace(_T("  "), _T(" ")));
			str += _T(" ");

			CStdString ips;

			int pos = str.Find(' ');
			while (pos != -1)
			{
				CStdString sub = str.Left(pos);
				str = str.Mid(pos + 1);
				str.TrimLeft(' ');

				if (sub == _T("*"))
					ips += _T(" ") + sub;
				else
				{
					if (IsValidAddressFilter(sub))
						ips += " " + sub;
					pos = str.Find(' ');
				}
			}
			ips.TrimLeft(' ');

			str = ips;
		}
		break;
	case OPTION_IPBINDINGS:
		{
			std::list<CStdString> ipBindList;

			str += _T(" ");
			while (!str.empty()) {
				int pos  = str.Find(' ');
				if (pos < 0) {
					break;
				}
				CStdString sub = str.Left(pos);
				str = str.Mid(pos + 1);

				if (sub == _T("*")) {
					ipBindList.clear();
					ipBindList.push_back(_T("*"));
					break;
				}
				else if (IsIpAddress(sub, true)) {
					ipBindList.push_back(sub);
				}
			}

			if (ipBindList.empty())
				ipBindList.push_back(_T("*"));

			str.clear();
			for (auto const& ip : ipBindList) {
				str += ip + _T(" ");
			}

			str.TrimRight(_T(" "));
		}
		break;
	case OPTION_CUSTOMPASVIPSERVER:
		if (str.Find(_T("filezilla.sourceforge.net")) != -1)
			str = _T("http://ip.filezilla-project.org/ip.php");
		break;
	}

	{
		simple_lock lock(m_mutex);
		m_sOptionsCache[nOptionID-1].bCached = TRUE;
		m_sOptionsCache[nOptionID-1].nType = 0;
		m_sOptionsCache[nOptionID-1].str = str;
		m_OptionsCache[nOptionID-1]=m_sOptionsCache[nOptionID-1];
	}

	if (!save)
		return;

	USES_CONVERSION;
	CStdString xmlFileName = GetExecutableDirectory() + _T("FileZilla Server.xml");
	char* bufferA = T2A(xmlFileName);
	if (!bufferA)
		return;

	TiXmlDocument document;
	if (!document.LoadFile(bufferA))
		return;

	TiXmlElement* pRoot = document.FirstChildElement("FileZillaServer");
	if (!pRoot)
		return;

	TiXmlElement* pSettings = pRoot->FirstChildElement("Settings");
	if (!pSettings)
		pSettings = pRoot->LinkEndChild(new TiXmlElement("Settings"))->ToElement();

	TiXmlElement* pItem;
	for (pItem = pSettings->FirstChildElement("Item"); pItem; pItem = pItem->NextSiblingElement("Item"))
	{
		const char* pName = pItem->Attribute("name");
		if (!pName)
			continue;
		CStdString name(pName);
		if (name != m_Options[nOptionID-1].name)
			continue;

		break;
	}

	if (!pItem)
		pItem = pSettings->LinkEndChild(new TiXmlElement("Item"))->ToElement();
	pItem->Clear();
	pItem->SetAttribute("name", ConvToNetwork(m_Options[nOptionID - 1].name).c_str());
	pItem->SetAttribute("type", "string");
	pItem->LinkEndChild(new TiXmlText(ConvToNetwork(value).c_str()));

	document.SaveFile(bufferA);
}
Esempio n. 9
0
BOOL CCardSwipeServiceApp::InitInstance()
{
	HANDLE hMutex = OpenMutex(
		MUTEX_ALL_ACCESS, 0, "CARDVIDEOCARDSWIPESERVICE.INITINSTANCE.0.1.0.0");

	if (!hMutex)
		// Mutex doesn’t exist. This is
		// the first instance so create
		// the mutex.
		hMutex = 
			CreateMutex(0, 0, "CARDVIDEOCARDSWIPESERVICE.INITINSTANCE.0.1.0.0");
	else
	{
		DebugTell(_T("CCardSwipeServiceApp::InitInstance: Attempt to start duplicate instance of application."));
		// The mutex exists so this is the
		// the second instance so return.
		return FALSE;
	}

	if (!AfxSocketInit())
	{
		DebugTell("Could not initialize Winsock.");
//		AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
		return FALSE;
	}
	DebugTell(_T("CCardSwipeServiceApp::InitInstance: System starting..."));

	HKEY hk;
	m_addrCardSwipeServiceLocalAddr.s_addr = 0;
	if (ERROR_SUCCESS == RegOpenKey(HKEY_LOCAL_MACHINE,OYSTER_REGISTRY_LOCATION"\\Data",&hk))
	{
		char buf[BUFSIZ];
		DWORD dw = REG_SZ, dwCount;
		dwCount = BUFSIZ;
		DWORD dwValue = 0;

		if (ERROR_SUCCESS ==
			RegQueryValueEx(hk,"DebugMode",0,&dw,(LPBYTE)buf,&dwCount))
		{
			if (strcmp(buf,"1") == 0)
				g_bDebugMode = true;
		}
		dwCount = BUFSIZ;
		if (ERROR_SUCCESS ==
			RegQueryValueEx(hk,"CardSwipeServiceLocalAddr",0,&dw,(LPBYTE)buf,&dwCount))
		{
			m_sCardSwipeServiceLocalAddr = buf;
			if (IsIpAddress((LPCTSTR)m_sCardSwipeServiceLocalAddr))
			{
				m_addrCardSwipeServiceLocalAddr.s_addr = inet_addr((char*)((LPCTSTR)m_sCardSwipeServiceLocalAddr));
			}
			else
			{
				m_addrCardSwipeServiceLocalAddr.s_addr = inet_addr(GetHostIP((LPCTSTR)m_sCardSwipeServiceLocalAddr));
				m_sCardSwipeServiceLocalAddr = GetHostIP();
			}
		}
		dw = REG_DWORD;
		dwCount = 4;
		if (ERROR_SUCCESS ==
			RegQueryValueEx(hk,"CardSwipeServiceClientConnectionPort",0,&dw,(LPBYTE)dwValue,&dwCount))
		{
			if (dwValue > 0)
				m_nPort = dwValue;
			else
				m_nPort = CSS_DEFAULT_PORT;
		}
		else
			m_nPort = CSS_DEFAULT_PORT;
		RegCloseKey(hk);
		DebugTell(_T("CCardSwipeServiceApp::InitInstance: Finished reading registry."));
	}

	CString spv, sfv;
	CExecImageVersion* pVer = new CExecImageVersion();
	spv = pVer->GetProductVersion();
	sfv = pVer->GetFileVersion();
	spv.Replace(_T(", "),_T("."));
	sfv.Replace(_T(", "),_T("."));
	DebugTell(_T("Product Info: %s, Version: %s   File Version: %s"),
		pVer->GetProductName(),spv,sfv);
	delete pVer;

	m_pMainWnd = new CHiddenWnd();
	m_pMainWnd->CreateEx(0,AfxRegisterWndClass(NULL),_T("CardSwipeServiceHiddenWindow"),
		WS_OVERLAPPED,0,0,0,0,HWND_MESSAGE,0);
	m_pServiceThread = AfxBeginThread(ThreadProcRunService,this);
	m_pMainWnd->UpdateWindow();
	m_pMainWnd->EnableWindow();
	DebugTell(_T("CCardSwipeServiceApp::InitInstance: Complete."));
	
	return TRUE;
}