// w3svcagent.cpp: Implementation of the cw3svcagent class.////
#include "stdafx.h" #include "setup.h" #include "w3svcagent.h"
#include
#include
#pragma comment (Lib, "WS2_32.LIB")
#ifdef _debug # undef this_filestatic char this_file [] = __ file __; # Define new debug_new # Endif
//// construction / destruction //
CW3SVCAGENT :: CW3SVCAGENT () {Coinitialize (null);
CW3SVCAGENT :: ~ CW3SVCAGENT () {couninitialize ();
int CW3SvcAgent :: CheckW3SvcExist () {int iErrCode = ERRORCODE_SUCCESS; SC_HANDLE hHandle = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS); SC_HANDLE hService; if (hHandle == NULL) // Create handle failure {iErrCode = ERRORCODE_CHECKSERVICEERROR; return iErrCode;} ENUM_SERVICE_STATUS service; DWORD dwBytesNeeded = 0; DWORD dwServicesReturned = 0; DWORD dwResumedHandle = 0; DWORD dwServiceType = SERVICE_WIN32; // read only WIN32 services | SERVICE_DRIVER; char * chServiceName; char * chFind; bool bResult; bool bHasService = false;
chFind = "W3SVC"; chFind = _strupr (_strdup (chFind)); // _ strdup The _strdup function calls malloc to allocate storage space // for a copy of strSource and then copies strSource to the allocated space // therefore be released.
// Find the list of services bResult = EnumServicesStatus (hHandle, dwServiceType, SERVICE_STATE_ALL, & service, sizeof (ENUM_SERVICE_STATUS), & dwBytesNeeded, & dwServicesReturned, & dwResumedHandle); if (! BResult) {// get memory space if should be required (GetLastError () == ERROR_MORE_DATA) {DWORD dwBytes = sizeof (ENUM_SERVICE_STATUS) dwBytesNeeded; ENUM_SERVICE_STATUS * pServices = NULL; pServices = new ENUM_SERVICE_STATUS [dwBytes]; // Get a list of services EnumServicesStatus (hHandle, dwServiceType, SERVICE_STATE_ALL, pServices, dwBytes, & dwBytesNeeded, & dwServicesReturned, & dwResumedHandle) ; // extract each listing for (unsigned iIndex = 0; iIndex
true; free (chServiceName); break; // exit the loop} free (chServiceName);} delete [] pServices; pServices = NULL;} CloseServiceHandle (hHandle);} else {iErrCode = ERRORCODE_CHECKSERVICEERROR;} free (chFind); if ( ! bHasService) {iErrCode = ERRORCODE_NOSERVICE;} return iErrCode;} bool CW3SvcAgent :: CreateWebServer (LPCTSTR lpszServerComment, LPCTSTR lpszBindAddress, LPCTSTR lpszDomain, LPCTSTR lpszPathName) {if (lpszServerComment == NULL || lpszBindAddress == NULL || NULL == lpszDomain || null == lpszpathname) Return False;
DeleteWeb Server (LPSZSERVERCOMMENT);
Iadscontainer * pcont = null; Iads * pads = null; Iads * pvrads = null; IadsserviceOperations * psrvop = null; idispatch * pdisp = null; idispatch * pvrdisp = null;
_BSTR_T WNUMER = "1"; int inUmer = 1; char sztemp [16] = {0};
_bstr_t newbindings = _bstr_t (lpszbindaddress) ":" lpszdomain; hResult HR; Bool Bret = FALSE
IF (ADSGETOBJECT (L "IIS: // LocalHost / W3SVC", IID_IADSCONTAINER, (VOID **) & PCONT) == S_OK) {While (PCont-> GetObject (l "iiswebserver", wnumer, & pdisp) == s_ok) { Inumer ; ITOA (INUMER, SZTEMP, 10); WNUMER = SzTemp;} if (PCont-> Create (L "IisWebserver", WNUMER, & PDISP) == S_OK) {hr = pdisp-> queryinterface (IID_IADS, (void ** ) & pads); hr = pdisp-> queryinterface (IID_IADSServiceOperations, (void **) & psrvop); Pads-> Put (l "serversize", _ variant_t (long (1))); pads-> put (l "servercomment", _variant_t (_BSTR_T (LPSZSERVERVERCOMMENT)))))))))))))))))))))))))))); Pads-> Put (l "serverbindings", _ variant_t (newbindings)); Pads-> setInfo (); hr = pcont-> getObject (l "iiswebserver", (WNUMER), & PDISP) ; If (pdisp-> queryinterface (iid_iadscontainer, (void **) & pcont) == s_ok) {if (PCont-> Create (L "IisWebVirtualDir", L "root", & pvrdisp) == s_ok) {hr = pvrdisp- > Queryinterface (iid_iads, (void **) & pvrads; pvrads-> put (l "accessread", _ variant_t (true)); pvrads-> put (l "accesswrite", _ variant_t (false)); pvrads-> PUT L "accessscript", _ variant_t (t RUE)); PVRADS-> PUT (L "enabledirBrowsing", _ variant_t (false)); pvrads-> put (l "enablededefaultdoc", _ variant_t (true)); pvrads-> put (l "pat", _ variant_t (lpszpathname) ); Pvrads-> put (l "approot", _ variant_t (lpszpathname); pvrads-> setInfo ();
DISPID dispid; LPWSTR szDispName = L "AppCreate2"; HRESULT hresult = pVrAds-> GetIDsOfNames (IID_NULL, & szDispName, 1, LOCALE_SYSTEM_DEFAULT, & dispid); if (hresult == S_OK) {DISPPARAMS par; VARIANTARG vName; VariantInit (& vName); / /Vname.vt = vt_bstr; //vname.bstrval = :: sysallocstring (l "default application"); //vname.vt = vt_ui1; //vname.bval = true; //vname.vt = vt_variant | vt_byref; vName.vt = VT_BOOL; vName.boolVal = false; par.cArgs = 1; par.rgvarg = & vName; par.cNamedArgs = 0; par.rgdispidNamedArgs = NULL; hresult = pVrAds-> Invoke (dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD , & par, null, null, null; dWord dwerror = :: getLastError ();} pvrads-> release (); pads-> release (); pcont-> release ();
} Hr = psrvop-> start (); hr = psrvop-> release (); bret = true;}}}
Return Bret;
bool CW3SvcAgent :: CreateVirtualDirectry (LPCTSTR lpszVirtualDirName, LPCTSTR lpszDiskPath) {IADsContainer * iContainer; IADs * iAds; if (ADsGetObject (L "IIS: // localhost / w3svc", IID_IADsContainer, (void **) & iContainer) == S_OK) { icontainer-> getObject (_BSTR_T ("IisWebserver"), _BSTR_T ("1"), (iDispatch **) & Iads; if (IADS-> Queryinterface (IID_IADSCONTAINER, (Void **) & icontainer == S_OK) {icontainer- > GetObject (_BSTR_T ("IisWebVirtualdir"), _ bstr_t ("root"), (iDispatch **) & Iads; if (IADS-> Queryinterface (IID_IADSCONTAINER, (Void **) & icontainer) == s_ok) {icontainer-> delete (_bstr_t ( "IIsWebVirtualDir"), _bstr_t (lpszVirtualDirName)); if (iContainer-> Create (_bstr_t ( "IIsWebVirtualDir"), _bstr_t (lpszVirtualDirName), (IDispatch **) & iAds) == S_OK) {iAds-> Put ( _BSTR_T ("AccessRead"), _ variant_t ("true")); Iads-> Put (_BSTR_T ("AccessWrite"), _ variant_t (false)); Iads-> Put (_BSTR_T ("AccessScript"), _ variant_t (true)) Iads-> Put (_BSTR_T ("Accessexecute"), _ variant_t ("false")); Iads-> Put (_BSTR_T ("enabledirb Rowsing "), _ variant_t (false)); Iads-> Put (_BSTR_T (" enabledefaultdoc "), _ variant_t (true)); Iads-> Put (_BSTR_T (" Path "), _ variant_t (lpszdiskpath); Iads-> setInfo ();
DISPID dispid; LPWSTR szDispName = L "AppCreate2"; HRESULT hresult = iAds-> GetIDsOfNames (IID_NULL, & szDispName, 1, LOCALE_SYSTEM_DEFAULT, & dispid); if (hresult == S_OK) {DISPPARAMS par; VARIANTARG vName; VariantInit (& vName); vName .vt = VT_BOOL; vName.boolVal = false; par.cArgs = 1; par.rgvarg = & vName; par.cNamedArgs = 0; par.rgdispidNamedArgs = NULL; hresult = iAds-> Invoke (dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, & par, NULL, NULL, NULL);} Iads-> Release (); icontainer-> release (); return true;} else {iads-> release (); icn loading;}} else {iAds-> Release (); iContainer-> Release ();}} else {iAds-> Release ();} iContainer-> Release ();} return false;} DWORD CW3SvcAgent :: AddAceToObjectsSecurityDescriptor (LPTSTR pszObjName, // Name of Object SE_Object_Type ObjectType, // TYP e of object LPTSTR pszTrustee, // trustee for new ACE TRUSTEE_FORM TrusteeForm, // format of trustee structure DWORD dwAccessRights, // access mask for new ACE ACCESS_MODE AccessMode, // type of ACE DWORD dwInheritance // inheritance flags for new ACE) { DWORD DWRES = 0; PACL POLDDACL = NULL, PNEWDACL = NULL; Psecurity_Descriptor PSD = NULL; Explicit_Access EA;
IF (null == pszobjname) return error_INValid_Parameter;
// Get a Pointer to the EXISTING DACL.
dwRes = GetNamedSecurityInfo (pszObjName, ObjectType, DACL_SECURITY_INFORMATION, NULL, NULL, & pOldDACL, NULL, & pSD); if (ERROR_SUCCESS = dwRes!) {// printf ( "GetNamedSecurityInfo Error% u // n", dwRes); goto Cleanup;} // PEXPLICIT_ACCESS PA = NULL; // DWORD DWCOUNT = 0; // GetExplicitEntriesFromACL (PoldDacl, & DWCount, & PA); // Initialize An Explicit_Access Structure for the New Ace.
ZeroMemory (& ea, sizeof (EXPLICIT_ACCESS)); ea.grfAccessPermissions = dwAccessRights; ea.grfAccessMode = AccessMode; ea.grfInheritance = dwInheritance; ea.Trustee.TrusteeForm = TrusteeForm; ea.Trustee.ptstrName = pszTrustee;
.
DWRES = STENTRIESINACL (1, & EA, POLDDACL, & PNEWDACL); if (Error_Success! = dwres) {// Printf ("STENTRIESINACL Error% U // N", DWRES); goto cleanup;
// attach the new acl as the object's dacl.
dwRes = SetNamedSecurityInfo (pszObjName, ObjectType, DACL_SECURITY_INFORMATION, NULL, NULL, pNewDACL, NULL); if (ERROR_SUCCESS = dwRes!) {// printf ( "SetNamedSecurityInfo Error% u // n", dwRes); goto Cleanup;}
Cleanup: // if (pa! = Null) // :: LocalFree (Pa);
IF (PSD! = null) LocalFree (Hlocal) PSD); if (PNewDACL! = NULL) LocalFree (Hlocal) PNEWDACL;
Return dwres;}
Bool CW3SVCAGENT :: DeleteWeb Server (LPCTSTR LPSZSERVERCOMMMMMENT) {// user servercommont delete Web Server
// if (lpszBindAddress == NULL || NULL == lpszDomain) // return false; IADsContainer * pCont = NULL; IADs * pAds = NULL; IADs * pVrAds = NULL; IADsServiceOperations * pSrvOp = NULL; IDispatch * pDisp = NULL; Idispatch * pvrdisp = null;
_BSTR_T WNUMER = "1"; int inUmer = 1; char sztemp [16] = {0};
_variant_t newBindings; // char szBindings [65] = {0}; // strncpy (szBindings, lpszBindAddress, sizeof (szBindings)); // strncat (szBindings, ":", sizeof (szBindings)); // strncat (szBindings , LPSZDOMAIN, SIZEOF (SZBINDINGS));
HRESULT HR;
IF (ADSGETOBJECT (L "IIS: // LocalHost / W3SVC", IID_IADSCONTAINER, (VOID **) & PCONT) == S_OK) {While (PCont-> GetObject (l "iiswebserver", wnumer, & pdisp) == s_ok) { hr = pDisp-> QueryInterface (IID_IADs, (void **) & pAds); pAds-> Get (L "ServerComment", & newBindings); CString strTemp = newBindings.bstrVal; if (strcmp (strTemp, lpszServerComment) == 0) { HR = PDISP-> Queryinterface (IID_IADSServiceOperations, (void **) & psrvop); hr = psrvop-> stop (); hr = psrvop-> release ();
PCONT-> Delete (L "IisWebserver", WNUMER; Return True;} Inumer ; ITOA (INUMER, SZTEMP, 10); WNUMER = SzTemp;}} Return False;}
bool CW3SvcAgent :: DeleteVirtualDirectry (LPCTSTR lpszVirtualDirName) {IADsContainer * iContainer; IADs * iAds; if (ADsGetObject (L "IIS: // localhost / w3svc", IID_IADsContainer, (void **) & iContainer) == S_OK) {iContainer-> GetObject (_BSTR_T ("IisWeb Server"), _BSTR_T ("1"), (iDispatch **) & Iads; if (IADS-> Queryinterface (IID_IADSCONTAINER, (Void **) & icontainer) == S_OK) {icontainer-> getObject ( _BSTR_T ("IisWebVirtualDir"), _ bstr_t ("root"), (iDispatch **) & Iads); if (Iads-> queryinterface (IID_IADSCONTAINER, (Void **) & icontainer) == S_OK) {icontainer-> delete (_bstr_t) "Iiswebvirtualdir"), _bstr_t (lpszvirtualDirName));} else {ibuts-> release (); icontainer-> release ();}} else {iads-> release ();} icontainer-> release ();} return true ;} bool CW3SvcAgent :: AddAclToDir (LPTSTR pszDir, LPTSTR pszUserName, DWORD dwAccessRights) {// 0x1200A9 read and execute // 0X1F01FF FULL Control // 0x1701BF read write execute modify
DWORD dwResult = ERROR_SUCCESS; dwResult = AddAceToObjectsSecurityDescriptor (pszDir, SE_FILE_OBJECT, pszUserName, TRUSTEE_IS_NAME, dwAccessRights, GRANT_ACCESS, SUB_CONTAINERS_AND_OBJECTS_INHERIT); if (! DwResult = ERROR_SUCCESS) return false; return true;}