예제 #1
0
int cgiMain() {
	CONFIG_T cfg, rcfg;
	int sock;
	int err;
	int checks;

#if DEBUG
	afreshOrient("cgi.txt", "a", stdout); //test
#endif /* DEBUG */

	if((sock = MyConnect()) < 0) {
		if(cgi_sprint("FAILED", "", INTERNAL_SERVERERR, "MyConnect failed") != SUCCESS){
			exit(EFAILED);
		}
		exit(EFAILED);
	}
	/*load the configuration file*/
	if((err = LoadConf(sock, DEVICE_NUM, &cfg)) != 0) {
		/*load the configuration file*/
		if(cgi_sprint("FAILED", "", INTERNAL_SERVERERR, "LoadConf failed") != SUCCESS){
			exit(EFAILED);
		}
		exit(EFAILED);
	}
	/* If a submit button has already been clicked, act on the submission of the form. */
	if (cgiFormSubmitClicked("login_loginpage") == cgiFormSuccess) {
		strcpy(cfg.user.uname, SUPERNAME);

		if((err = ReceiveSubmit(DEVICE_NUM, &rcfg)) != SUCCESS) {
			if(cgi_sprint("FAILED", "", BADREQUIST, "ReceiveSubmit failed") != SUCCESS){
				exit(EFAILED);
			}
			exit(EFAILED);
		}

		if((checks = CheckLogin(rcfg.user, cfg.user)) != SUCCESS) {
			if(cgi_sprint("FAILED", "", UNAUTHENTICATION, "authentication failed") != SUCCESS){
				exit(EFAILED);
			}
			exit(EFAILED);
		} else {
			//AUTHENTICATION
			if(setenv(CGI_AUTH_IP, CGICLIENTIP, 1) != 0) {
				if(cgi_sprint("FAILED", "", 100, "authentication save failed") != SUCCESS){
					exit(EFAILED);
				}
				exit(EFAILED);
			}
			if(cgi_sprint("SUCCESS", "../main.html", SUSSHAND, "request success") != SUCCESS){
				exit(EFAILED);
			}
		}

		//SendSubmit(DEVICE_NUM, rcfg);

	}

	CloseSocket(sock);
	return SUCCESS;
}
예제 #2
0
DWORD WINAPI RlogindClientThread(LPVOID param)
{
	RLOGIND rlogind = *((RLOGIND *)param);
	RLOGIND *rloginds = (RLOGIND *)param;
	rloginds->gotinfo = TRUE;	

	int threadnum=rlogind.cthreadnum;
	
	char LocalUser[16], RemoteUser[16], TerminalType[64], HostName[100], Buffer[16];
	LPHOSTENT HostEnt;
	SOCKADDR_IN csin;

	TIMEVAL timeout;
	timeout.tv_sec = 10;
	timeout.tv_usec = 0;
	fd_set fd;
	FD_ZERO(&fd);
	FD_SET(threads[threadnum].sock, &fd);

	if (fselect(0, &fd, NULL, NULL, &timeout) == 0) {
		fclosesocket(threads[threadnum].sock);
		clearthread(threadnum);
		ExitThread(0);
	}

	frecv(threads[threadnum].sock, (char *)&Buffer, 1, 0);

	GetStr(threads[threadnum].sock, RemoteUser, sizeof(RemoteUser));
	GetStr(threads[threadnum].sock, LocalUser, sizeof(LocalUser));
	GetStr(threads[threadnum].sock, TerminalType, sizeof(TerminalType));

	int csin_len = sizeof(csin);
	if (fgetpeername(threads[threadnum].sock, (LPSOCKADDR)&csin, &csin_len) != 0) {
		addlogv("[RLOGIND]: Error: getpeername(): <%d>.", fWSAGetLastError());
		fclosesocket(threads[threadnum].sock);
		clearthread(threadnum);
		ExitThread(0);
	}

	if ((HostEnt = fgethostbyaddr((char *)&csin.sin_addr, sizeof(csin.sin_addr), PF_INET)) == NULL) 
		sprintf(HostName, finet_ntoa(csin.sin_addr));
	else
		strcpy(HostName, HostEnt->h_name);

	frecv(threads[threadnum].sock, (char *)Buffer, sizeof(Buffer), 0);
	fsend(threads[threadnum].sock, "", 1, 0);

	if (!InsecureFlag && !CheckLogin(RemoteUser,HostName,rlogind.username,csin.sin_addr.s_addr)) {
		fsend(threads[threadnum].sock, "PERMISSION DENIED.", sizeof("PERMISSION DENIED."), 0);
		fshutdown(threads[threadnum].sock,SD_BOTH);
		fclosesocket(threads[threadnum].sock);
		clearthread(threadnum);
		ExitThread(0);
	}

	addlogv("[RLOGIND]: User logged in: <%s@%s>.", RemoteUser, HostName);

	if (!SessionRun(threadnum)) {
		addlogv("[RLOGIND]: Error: SessionRun(): <%d>.", GetLastError());
		fshutdown(threads[threadnum].sock,SD_BOTH);
		fclosesocket(threads[threadnum].sock);
		clearthread(threadnum);
		ExitThread(1);
	}

	addlogv("[RLOGIND]: User logged out: <%s@%s>.", RemoteUser, HostName);

	fshutdown(threads[threadnum].sock,SD_BOTH);
	fclosesocket(threads[threadnum].sock);
	clearthread(threadnum);
	ExitThread(0);
}
예제 #3
0
BOOL CCLITerminal::CommandTask(CLISESSION *pSession, BOOL bComplete, BOOL bAddHistory)
{
	CLIHANDLER	*pHandler;
	char		szCommand[256] = "";
	int			nResult, nDepts=0, nCommand;
	int			i, nIndex, argc, nParam, nPos;

	// Make New Line
	WriteStream(pSession, "\n\r");
	pSession->szCommand[pSession->nCmdLength] = '\0';

	if (pSession->nMode == CLIMODE_USER)
	{
		nCommand = atoi(pSession->szCommand);
		pSession->nCmdLength = 0;
		pSession->szCommand[0] = '\0';
		
		switch(nCommand) {
		  case 1 :
			   FirmwareDownload(pSession);
			   WriteStream(pSession, "\n\r");
			   break;
		  case 2 : 
			   pSession->nMode = CLIMODE_COMMAND;
			   DisplayPrompt(pSession);
			   return TRUE; 
		}
		DisplaySplash(pSession);
		return TRUE;
	}

	// Check Login State
	if (!pSession->bLogined)
	{
		if (pSession->bNeedUser)
		{
			if (!CheckUser(pSession))
			{
				// Display Login Error Message
				WriteStream(pSession, "Invalid User.\r\n");
			}

			// Clear Command Buffer
			pSession->nCmdLength 	= 0;
			pSession->szCommand[0]	= '\0';
			DisplayPrompt(pSession);
			return TRUE;

		}
		else if (!CheckLogin(pSession))
		{
			// Clear Command Buffer
			pSession->nCmdLength 	= 0;
			pSession->szCommand[0]	= '\0';
			pSession->bNeedUser		= TRUE;

			// Display Login Error Message
			WriteStream(pSession, "Invalid account or password.\r\n\r\n");
			strcpy(pSession->szPrompt, m_pCLIService->m_szUserPrompt);
			pSession->nLoginRetry++;
			if (pSession->nLoginRetry >= 3)
			{
				if (pSession->nType == CLITYPE_SERIAL)
					sleep(3);
				return FALSE;
			}

			DisplayPrompt(pSession);
			return TRUE;
		}

		// Clear Command Buffer
		pSession->nCmdLength 	= 0;
		pSession->szCommand[0]	= '\0';

		// Callback Login
		if (m_pCLIService->m_pConstruct &&
			m_pCLIService->m_pConstruct->pfnOnLogin)
			m_pCLIService->m_pConstruct->pfnOnLogin(pSession);

		// Login Complete
		pSession->bLogined = TRUE;
		WriteStream(pSession, "\n\r");
		strcpy(pSession->szPrompt, m_pCLIService->m_szDefaultPrompt);
		DisplayPrompt(pSession);
		return TRUE;
	}

	if (pSession->nCmdLength > 0)
	{
		// Find Command
		nResult = CLIERR_OK;
		pSession->szCommand[pSession->nCmdLength] = '\0';

		if (pSession->szCommand[0] == '!')
		{
			if (strcmp(pSession->szCommand, "!!") == 0)
			{
				strcpy(pSession->szCommand, pSession->pszHistory[pSession->nHistoryCount-1]);
				pSession->nCmdLength = strlen(pSession->szCommand);
			}
			else
			{
				nIndex = atoi(&pSession->szCommand[1]);
				if ((nIndex > 0) && (nIndex <= pSession->nHistoryCount))
				{
					strcpy(pSession->szCommand, pSession->pszHistory[nIndex-1]);
					pSession->nCmdLength = strlen(pSession->szCommand);
				}
			}
		}

		pSession->pszArgString = strdup(pSession->szCommand);
		if (bAddHistory)
			AddHistory(pSession, pSession->szCommand);

		SpliteParameter(pSession);
		pHandler = FindCommandHandler(pSession, nDepts, szCommand);
		if (pHandler != NULL)
		{
			// Execute Command
			nParam = GetParamCount(pHandler);
			argc = pSession->argc - (nDepts + 1);
			for(i=0; i<argc; i++)
				pSession->argv[i] = pSession->argv[nDepts+i+1];

			// 파라메터의 갯수가 더 많은 경우를 막을때, add apn 명령 때문에 허용하도록 변경 2007/9/5
			// if ((nParam == argc) || (pHandler->pszParam && (argc >= GetMinParamCount(pHandler)) && (argc <= nParam)))

			if ((nParam == argc) || (pHandler->pszParam && (argc >= GetMinParamCount(pHandler))))
			{
				nPos = ValidateParameter(pSession, pHandler);
				if (nPos == -1)
				{
					pSession->nCmdLength 	= 0;
					pSession->szCommand[0]	= '\0';

					if (m_pCLIService->m_pConstruct &&
						m_pCLIService->m_pConstruct->pfnOnCommand)
						m_pCLIService->m_pConstruct->pfnOnCommand(pSession, argc, pSession->argv, pHandler);

					if (pHandler->nGroup >= pSession->nGroup)
					{
						if (m_pCLIService->m_bEnableLog && pHandler->bLogFlag)
							m_pCLIService->AddLog(pSession->szUser, pSession->szCommand);

						nResult = pHandler->pfnCommand(pSession, argc, pSession->argv, (void *)pHandler);
						WriteStream(pSession, "\xd\xa");
					}
					else
					{
						WriteStream(pSession, "Permission Denied.");					
						WriteStream(pSession, "\xd\xa");
					}
				}
				else
				{
					WriteStream(pSession, "Invalid parameter : '");					
					WriteStream(pSession, pSession->argv[nPos]);					
					WriteStream(pSession, "'\xd\xa");
					WriteStream(pSession, "\xd\xa");
				}
			}
			else
			{
				WriteStream(pSession, "usage: ");
				WriteStream(pSession, szCommand);
				DisplayWideParameter(pSession, pHandler);
				DisplayAllParameter(pSession, pHandler);
				WriteStream(pSession, "\xd\xa");
			}
		}

		if (pSession->pszArgString)
			FREE(pSession->pszArgString);
		pSession->pszArgString = NULL;

		if (nResult == CLIERR_ERROR)
			return FALSE;
	}

	// Clear Command Buffer
	pSession->nCmdLength 	= 0;
	pSession->szCommand[0]	= '\0';

	// Display Prompt
	DisplayPrompt(pSession);
	return TRUE;
}
예제 #4
0
void Client::Update(float)
{
    // If we aren't a server, check pending login
    if (!owner_->IsServer())
        CheckLogin();
}
void CAuctionServerDoc::ProcessPendingRead(CClientSocket* pSocket)
{
	CBuffer buffer;
	int nLen = buffer.Receive(pSocket);
    CString str;
    switch (buffer.GetCmd())
    {
    case CMD_REGISTER_CLIENT:
        {
            CInRegisterClient* inBuf = (CInRegisterClient*)&buffer;
            CString strUserID = inBuf->GetUserID();
            CString strPassword = inBuf->GetUserPassword();

            CString strUserName;
            bool bValidUser = ValidateUser(strUserID, strPassword, strUserName);

            pSocket->SetUserID(strUserID);
            UpdateClientUserID(pSocket, strUserID);

            COutRegisterClient outBuf;
            outBuf.SetValid(bValidUser);
            if (bValidUser)
            {
                outBuf.SetUserName(strUserName);
                pSocket->SetUserName(strUserName);
                UpdateClientName(pSocket, strUserName);

                outBuf.SetLogin(CheckLogin(strUserID));
                UpdateClientLogin(pSocket, true);
            }

            outBuf.Send(pSocket);

            str.Format(TEXT("CMD_REGISTER_CLIENT") );
            m_listMessage.Push(str);

            str.Format(TEXT("\tUserID = %s, UserName = %s"), strUserID, strUserName);
            m_listMessage.Push(str);

            m_stateAuction = E_NONE;
        }
        break;
    case CMD_RETRIEVE_STOCK_OF_CLIENT:
        {
            CInRetrieveStock* inBuf = (CInRetrieveStock*)&buffer;
            CString strUserID = inBuf->GetUserID();

            std::vector<CProduct> & listProduct = GetListProduct(strUserID);

            COutRetrieveStock outBuf;
            outBuf.SetListProduct(listProduct);

            str.Format(TEXT("CMD_RETRIEVE_STOCK_OF_CLIENT") );
            m_listMessage.Push(str);

            for (CProduct product : listProduct)
            {
                str.Format(TEXT(">>>ProductID = %d, ProductCount = %d,Price = %f, Product Name = %s"),
                    product.GetProductID(),
                    product.GetCount(),
                    product.GetPrice(),
                    product.GetName());
                m_listMessage.Push(str);
            }

            outBuf.Send(pSocket);

            m_stateAuction = E_NONE;
        }
        break;
    case CMD_ADVERTISING:
        {
            str.Format(TEXT("CMD_ADVERTISING"));
            m_listMessage.Push(str);

            m_stateAuction = E_ADVERTISING;

            CInAdvertising* inBuf = (CInAdvertising*)&buffer;
            DWORD    dwProductID  = inBuf->GetProductID();
            CString  strName      = inBuf->GetProductName();
            DWORD    dwCount      = inBuf->GetProductCount();
            double   dblPrice     = inBuf->GetProductPrice();

            CBroadcastPrice buf;
            buf.SetProductID(dwProductID);
            buf.SetProductCount(dwCount);
            buf.SetProductPrice(dblPrice);
            buf.SetProductName(strName);

            str.Format(TEXT("Broadcast Advertising Event. (%s, %d, %f)"), strName, dwCount, dblPrice);
            m_listMessage.Push(str);

            // Broadcast this packet to the other clients
            BroadcastBuffer( buf);

            COutAdvertising outBuf;
            outBuf.SetState(m_stateAuction);

            outBuf.Send(pSocket);



            // *****************************************************************************
            // After 5 minutes, the Auction should be started and the bids will be allowed.

            
            int * p = NULL;

            timer.registerHandler(&TimerAdvertisingProc, p);
            timer.registerHandlerAfter(&TimerAfterAdvertisingProc, p);
            
            timer.setInterval(1);
            timer.SetPeriod(WAITINGTIME);
            timer.Start();
            


            
            //******************************************************************************
        }
        break;
    case CMD_BID:
        {
            str.Format(TEXT("CMD_BID"));
            m_listMessage.Push(str);

            CInAuction* inBuf = (CInAuction*)&buffer;
            double   m_dblPrice = inBuf->GetProductPrice();
            CString strUserID = inBuf->GetUserID();
            // Check the max bid price
            double dblMaxBidPrice = GetMaxBidPrice(m_dwAuctionID);
            //double dblMaxBidPrice = GetDBConn().GetMaxBidPrice(m_lAuctionID);
            
            CProduct product(
                inBuf->GetProductID(),
                inBuf->GetProductName(),
                inBuf->GetProductCount(),
                inBuf->GetProductPrice());

            COutAuction outBuf;

            m_stateAuction = E_AUCTION;
            if (m_dblPrice > dblMaxBidPrice)
            {
                str.Format(TEXT("The new biggest price(%f) come in"), m_dblPrice);
                m_listMessage.Push(str);

                m_mutex.lock();
                SetBidTransaction(
                    m_dwAuctionID,
                    strUserID,
                    product);
                m_mutex.unlock();

                CBroadcastPrice buf;
                buf.SetProductID(inBuf->GetProductID());
                buf.SetProductName(inBuf->GetProductName());
                buf.SetProductCount(inBuf->GetProductCount());
                buf.SetProductPrice(inBuf->GetProductPrice());

                BroadcastBuffer(buf);

                //CBroadcastState state;
                //state.SetState(E_AUCTION);
                //BroadcastBuffer(state);

                //*********************************************************
                //如果这个最高价格保持5分钟,这个拍卖就可以结束了。
                //实现起来,我们还需要一个线程去检查在这5分钟里,有没有新价格高于它。
                //*********************************************************

                int * p = NULL;
                timerBid.registerHandler(&TimerBidProc, p);
                timerBid.registerHandlerAfter(&TimerAfterBidProc, p);

                timerBid.setInterval(1);
                timerBid.SetPeriod(WAITINGTIME);
                timerBid.Start();

                outBuf.SetState(m_stateAuction);
                outBuf.Send(pSocket);
            }







        }
        break;
    }

    UpdateAllViews(NULL);

    //CAuctionServerView::GetView()->UpdateWindow();
}