Ejemplo n.º 1
0
void PTelnetDServerMode()
{
int listensock, fd, i;
struct sigaction sigact;
char *Tempstr=NULL, *IPStr=NULL;

listensock=InitServerSock(Settings.Interface,Settings.Port);
if (listensock==-1)
{
	printf("ERROR: Cannot bind to port %d on interface %s\n",Settings.Port,Settings.Interface);
	exit(3);
}

if (! (Settings.Flags & FLAG_NODEMON)) demonize();

SetupPidFile();

if (Settings.Flags & FLAG_HONEYPOT) JailAndSwitchUser(FLAG_CHROOT, Settings.RealUser, Settings.ChDir);

while (1)
{
/*Set up a signal handler for SIGCHLD so that our 'select' gets interrupted when something exits*/
sigact.sa_handler = default_signal_handler;   
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction(SIGCHLD, &sigact, NULL);

if (FDSelect(listensock, SELECT_READ, NULL)) 
{
	fd=TCPServerSockAccept(listensock, &IPStr);
	if (fork()==0) 
	{
		//Sub processes shouldn't keep the pid file open, only the parent server
		//should
		close(PidFile);

		//if we've been passed a socket, then make it into stdin/stdout/stderr
		//but don't do this is fd==0, because then this has already been done by inetd
		close(0);
		close(1);
		close(2);
		dup(fd);
		dup(fd);
		dup(fd);

		//Having dupped it we no longer need to keep this copy open
		close(fd);
		Tempstr=MCopyStr(Tempstr, g_argv[0]," ",IPStr,NULL);
		for (i=0; i <g_argc; i++) memset(g_argv[i],0,StrLen(g_argv[i]));
		strcpy(g_argv[0],Tempstr);

		//In case logging demon was restarted, ensure we have connection before we chroot
		//Eric Wedaa modified the following line to log to the LongTail honeypot consolidation server
		openlog("ptelnetd",LOG_PID|LOG_NDELAY,LOG_AUTH);
		HandleClient();

		//Should be redundant, but if something goes wrong in HandleClient, we might want this
		//exit call
		_exit(0);
	}
	close(fd);
}
waitpid(-1,NULL,WNOHANG);
}

}
Ejemplo n.º 2
0
DWORD CDBPSServer::StartServer(DWORD dwPort, DWORD dwNumberOfConnection)
{
	/*
		Server have to be give a port for itself from Server Administrator
	*/
	DWORD dwRet = E_RET_SUCCESS;
	dwRet = InitServerSock(dwPort);
	if (dwRet != E_RET_SUCCESS) {
		ErrorLog("Fail to initailize Server Sock");
		return E_RET_FAIL;
	}

	/*
		Create Completion Port for opertation IOCP.
		the variable 0 mean that IOCP set value itself.
	*/
	dwRet = InitIOCompletionPort(0);
	if (dwRet != E_RET_SUCCESS) {
		ErrorLog("Fail to initailize Server Sock");
		return E_RET_FAIL;
	}

	/*
		Make worker threads for operating IOCP
	*/
	dwRet = InitWorkerThread();
	if (dwRet != E_RET_SUCCESS) {
		ErrorLog("Fail to initailize Server Sock");
		return E_RET_FAIL;
	}

	dwRet = InitDBCQueue(dwNumberOfConnection);
	if (dwRet != E_RET_SUCCESS) {
		ErrorLog("Fail to initailize DB Connection Queue");
		return E_RET_FAIL;
	}


	dwRet = InitServerValue(dwPort);
	if (dwRet != E_RET_SUCCESS) {
		ErrorLog("Fail to configure server value");
		return E_RET_FAIL;
	}

	m_bStartServer = TRUE;
	while (m_bStartServer)
	{
		try
		{
			ST_CLIENT_SOCKET stClientSocket;
			dwRet = AcceptServer(stClientSocket);
			if (dwRet != E_RET_SUCCESS) {
				continue;
			}

			dwRet = CompleteReadFromClient(stClientSocket);
			if (dwRet != E_RET_SUCCESS) {
				continue;
			}
		}
		catch (std::exception &e)
		{
			/*
				Abnormally Exception
			*/
			ErrorLog("%s", e.what());
			DestoryDBCQueue(dwNumberOfConnection);
			return dwRet;
		}
	}

	/*
		All Thread is waiting for stopping their operation
	*/
	WaitForMultipleObjects(m_stServerWorkerThreads.dwNumberOfThread, m_stServerWorkerThreads.phWorkerThread, TRUE, INFINITE);
	DestoryDBCQueue(dwNumberOfConnection);
	
	return dwRet;
}