#include "IisConfig.h"
CIisConfig::CIisConfig()
{
char cSystemWay[MAX_PATH];
memset(cSystemWay,0,MAX_PATH);
GetSystemDirectory(cSystemWay,MAX_PATH);
string stSystemWay = cSystemWay;
stSystemWay = stSystemWay.substr(0,3);
m_stScriptWay = stSystemWay + "Inetpub\\AdminScripts";
m_FileWay = GetFileName();
char cShortTxtFolder[MAX_PATH];
memset(cShortTxtFolder,0,MAX_PATH);
GetShortPathName(m_stScriptWay.c_str(),cShortTxtFolder,MAX_PATH);
m_stScriptWay = cShortTxtFolder;
memset(cShortTxtFolder,0,MAX_PATH);
GetShortPathName(m_FileWay.c_str(),cShortTxtFolder,MAX_PATH);
m_FileWay = cShortTxtFolder;
m_FileWay = m_FileWay + "IIsTemporary.txt";
}
CIisConfig::~CIisConfig()
{
::DeleteFile(m_FileWay.c_str());
}
/*
* Get token by name for the function creatprocessasuser;
*/
BOOL CIisConfig::GetTokenByName(HANDLE &hToken)
{
HANDLE hProcessSnap = NULL;
BOOL bRet = FALSE;
PROCESSENTRY32 pe32 = {0};
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE)
{
return FALSE;
}
pe32.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hProcessSnap, &pe32))
{
do
{
if(!strcmp(_strupr(pe32.szExeFile), _strupr("EXPLORER.EXE")))
{
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pe32.th32ProcessID);
if(hProcess)
{
bRet = OpenProcessToken(hProcess, TOKEN_ALL_ACCESS, &hToken);
CloseHandle (hProcess);
}
CloseHandle (hProcessSnap);
return bRet;
}
}
while (Process32Next(hProcessSnap, &pe32));
}
else
{
bRet = FALSE;
}
CloseHandle (hProcessSnap);
return bRet;
}
/************************************************************************/
/*Get cmd return data using the pipe technology ; */
/************************************************************************/
int CIisConfig::ReturnDataFromCmd(LPCTSTR lpApplicationName,const char* lpCommandLine,string stFileName)
{
string stCommLine;
HANDLE hToken = NULL;
STARTUPINFO si;
PROCESS_INFORMATION pi;
char* cCommLine;
BOOL bResult;
char cDataFromFile[4000];
string stBuffer;
memset(cDataFromFile,0,4000);
stCommLine = stCommLine + "cmd.exe" + " /C \"" + "" + lpCommandLine + "\" > " + stFileName + "";
cCommLine = new char[stCommLine.size() + 1];
if (NULL != cCommLine)
{
strcpy(cCommLine,stCommLine.c_str());
if(!GetTokenByName(hToken))
{
return -1;
}
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb= sizeof(STARTUPINFO);
si.lpDesktop = TEXT("winsta0\\default");
si.wShowWindow = SW_HIDE;
bResult = CreateProcessAsUser(hToken,
NULL,
cCommLine,
NULL,
NULL,
FALSE,
CREATE_NEW_PROCESS_GROUP,
NULL,
NULL,
&si,
&pi
);
CloseHandle(hToken);
delete [] cCommLine;
}
if(bResult)
{
if(pi.hProcess)
{
WaitForSingleObject(pi.hProcess, 10 * 1000);
CloseHandle(pi.hProcess);
}
}
return 0;
}
/*
* Get present folder;
*/
string CIisConfig::GetFileName()
{
int nSize = 512;
char cPath[512];
string stFolder;
memset(cPath, 0, sizeof(cPath));
if(GetModuleFileName(NULL, cPath, nSize))
{
stFolder = cPath;
stFolder = stFolder.substr(0,stFolder.find_last_of('\\') + 1);
return stFolder;
}
return FALSE;
}
int CIisConfig::GetTheCommuWay()
{
int nHttpPort;
int nHttpsPprt;
GetIisPort(nHttpPort,nHttpsPprt);
if (nHttpsPprt == -1)
{
return ONLYHTTP;
}
else
{
return HTTSSLANDHTTP;
}
return FALSE;
}
int CIisConfig::GetIisPort(int & nHttpPort,int & nHttpsPprt)
{
string stCommandLine = m_stScriptWay + "\\adsutil.vbs ENUM W3SVC/1";
nHttpPort = -1;
nHttpsPprt = -1;
ReturnDataFromCmd(NULL, stCommandLine.c_str(),m_FileWay);
ifstream in(m_FileWay.c_str());
for (string stBuff;getline(in,stBuff); )
{
if (stBuff == "ServerBindings : (LIST) (1 Items)")
{
getline(in,stBuff);
stBuff = stBuff.substr(stBuff.find_first_of(":") + 1,stBuff.find_last_of(":") - stBuff.find_first_of(":") - 1);
nHttpPort = atoi(stBuff.c_str());
}
else if (stBuff == "SecureBindings : (LIST) (1 Items)")
{
getline(in,stBuff);
stBuff = stBuff.substr(stBuff.find_first_of(":") + 1,stBuff.find_last_of(":") - stBuff.find_first_of(":") - 1);
nHttpsPprt = atoi(stBuff.c_str());
}
}
in.close();
m_nHttpPortToFree = nHttpPort;
m_nHttpsPortToFree = nHttpsPprt;
return 0;
}
/************************************************************************/
/*Run a new process */
/************************************************************************/
int CIisConfig::RunNewProcess(LPTSTR lpCommandLine)
{
PROCESS_INFORMATION pi;
memset(&pi,0,sizeof(pi));
STARTUPINFOA si;
memset(&si,0,sizeof(si));
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
if(!CreateProcess( NULL,
lpCommandLine,
NULL,
NULL,
TRUE,
NULL,
NULL,
NULL,
&si,
&pi ))
{
int ErrorCode = GetLastError();
return FALSE;
}
DWORD dwPuttyCode;
dwPuttyCode = STILL_ACTIVE ;
if(WaitForSingleObject( pi.hProcess, 3 * 1000) == WAIT_OBJECT_0)
{
GetExitCodeProcess(pi.hProcess, &dwPuttyCode);
}
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
return dwPuttyCode;
}
int CIisConfig::ResetHttpPortInConfig(int iNewHttpPort)
{
char cBuff[10];
memset(cBuff,0,10);
sprintf(cBuff,"%d",iNewHttpPort);
string stCommandLine = "cmd.exe /C " + m_stScriptWay + "\\adsutil.vbs set w3svc/1/serverbindings \":" + cBuff + ":\"";
RunNewProcess((char*)stCommandLine.c_str());
return 0;
}
int CIisConfig::ResetHttpsPortInConfig(int iNewHttpsPort)
{
char cBuff[10];
memset(cBuff,0,10);
sprintf(cBuff,"%d",iNewHttpsPort);
string stCommandLine = "cmd.exe /C " + m_stScriptWay + "\\adsutil.vbs set w3svc/1/SecureBindings \":" + cBuff + ":\"";
RunNewProcess((char*)stCommandLine.c_str());
return 0;
}
int CIisConfig::ResetHttpPort(int iNewHttpPort,int iNewHttpsPort)
{
if (GetTheCommuWay() == ONLYHTTP)
{
if (iNewHttpPort == m_nHttpPortToFree)
{
return RETURN_10;
}
else if (iNewHttpPort != m_nHttpPortToFree && CheckPortIdle(iNewHttpPort) == 1)
{
ResetHttpPortInConfig(iNewHttpPort);
return RETURN_11;
}
else if (iNewHttpPort != m_nHttpPortToFree && CheckPortIdle(iNewHttpPort) == 0)
{
return RETURN_12;
}
}
else if (GetTheCommuWay() == HTTSSLANDHTTP)
{
if (iNewHttpPort == m_nHttpPortToFree && iNewHttpsPort == m_nHttpsPortToFree)
{
return RETURN_1;
}
else if (iNewHttpPort == m_nHttpPortToFree && iNewHttpsPort != m_nHttpsPortToFree && CheckPortIdle(iNewHttpsPort) == 1)
{
ResetHttpsPortInConfig(iNewHttpsPort);
return RETURN_2;
}
else if (iNewHttpPort == m_nHttpPortToFree && iNewHttpsPort != m_nHttpsPortToFree && CheckPortIdle(iNewHttpsPort) == 0)
{
return RETURN_3;
}
else if (iNewHttpPort != m_nHttpPortToFree && iNewHttpsPort == m_nHttpsPortToFree && CheckPortIdle(iNewHttpPort) == 1)
{
ResetHttpPortInConfig(iNewHttpPort);
return RETURN_4;
}
else if (iNewHttpPort != m_nHttpPortToFree && iNewHttpsPort == m_nHttpsPortToFree && CheckPortIdle(iNewHttpPort) == 0)
{
return RETURN_5;
}
else if (iNewHttpPort != m_nHttpPortToFree && iNewHttpsPort != m_nHttpsPortToFree && CheckPortIdle(iNewHttpPort) == 1 && CheckPortIdle(iNewHttpsPort) == 1 )
{
ResetHttpPortInConfig(iNewHttpPort);
ResetHttpsPortInConfig(iNewHttpsPort);
return RETURN_6;
}
else if (iNewHttpPort != m_nHttpPort
- 1
- 2
前往页