Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

437 lines
13 KiB

  1. // logui.cpp : Implementation of CLoguiApp and DLL registration.
  2. #include "stdafx.h"
  3. #include "logui.h"
  4. #include "wrapmb.h"
  5. #include <iiscnfg.h>
  6. #include <iiscnfgp.h>
  7. #include <inetinfo.h>
  8. #include "initguid.h"
  9. #include <logtype.h>
  10. #include <ilogobj.hxx>
  11. #include "uincsa.h"
  12. #include "uiextnd.h"
  13. #include "uimsft.h"
  14. #include "uiodbc.h"
  15. #include "dcomperm.h"
  16. //_tlid
  17. // the global factory objects
  18. CFacNcsaLogUI facNcsa;
  19. CFacMsftLogUI facMsft;
  20. CFacOdbcLogUI facOdbc;
  21. CFacExtndLogUI facExtnd;
  22. const GUID CDECL BASED_CODE _tlid =
  23. { 0x31dcab8a, 0xbb3e, 0x11d0, { 0x92, 0x99, 0x0, 0xc0, 0x4f, 0xb6, 0x67, 0x8b } };
  24. const WORD _wVerMajor = 1;
  25. const WORD _wVerMinor = 0;
  26. // the key type strings for the metabaes keys
  27. #define SZ_LOGGING_MAIN_TYPE _T("IIsLogModules")
  28. #define SZ_LOGGING_TYPE _T("IIsLogModule")
  29. BOOL _cdecl RegisterInMetabase( PWCHAR pszMachine );
  30. int SetInfoAdminACL( CWrapMetaBase* pMB, LPCTSTR szSubKeyPath );
  31. #ifdef _DEBUG
  32. #define new DEBUG_NEW
  33. #undef THIS_FILE
  34. static char THIS_FILE[] = __FILE__;
  35. #endif
  36. CLoguiApp NEAR theApp;
  37. HINSTANCE g_hInstance = NULL;
  38. //---------------------------------------------------------------
  39. void CLoguiApp::PrepHelp( OLECHAR* pocMetabasePath )
  40. {
  41. // figure out the correct help file to use
  42. CString szMetaPath = pocMetabasePath;
  43. szMetaPath.MakeLower();
  44. // default to the w3 help
  45. UINT iHelp = IDS_HELPLOC_W3SVCHELP;
  46. // test for ftp
  47. if ( szMetaPath.Find(_T("msftpsvc")) >= 0 )
  48. iHelp = IDS_HELPLOC_FTPHELP;
  49. // finally, we need to redirect the winhelp file location to something more desirable
  50. CString sz;
  51. CString szHelpLocation;
  52. sz.LoadString( iHelp );
  53. // expand the path
  54. ExpandEnvironmentStrings(
  55. sz, // pointer to string with environment variables
  56. szHelpLocation.GetBuffer(MAX_PATH + 1), // pointer to string with expanded environment variables
  57. MAX_PATH // maximum characters in expanded string
  58. );
  59. szHelpLocation.ReleaseBuffer();
  60. // free the existing path, and copy in the new one
  61. if ( m_pszHelpFilePath )
  62. free((void*)m_pszHelpFilePath);
  63. m_pszHelpFilePath = _tcsdup(szHelpLocation);
  64. }
  65. ////////////////////////////////////////////////////////////////////////////
  66. // CLoguiApp::InitInstance - DLL initialization
  67. BOOL CLoguiApp::InitInstance()
  68. {
  69. g_hInstance = m_hInstance;
  70. BOOL bInit = COleControlModule::InitInstance();
  71. InitCommonDll();
  72. if (bInit)
  73. {
  74. CString sz;
  75. // set the name of the application correctly
  76. sz.LoadString( IDS_LOGUI_ERR_TITLE );
  77. // Never free this string because now MF...kingC
  78. // uses it internally BEFORE call to this function
  79. //free((void*)m_pszAppName);
  80. m_pszAppName = _tcsdup(sz);
  81. }
  82. return bInit;
  83. }
  84. ////////////////////////////////////////////////////////////////////////////
  85. // CLoguiApp::ExitInstance - DLL termination
  86. int CLoguiApp::ExitInstance()
  87. {
  88. return COleControlModule::ExitInstance();
  89. }
  90. /////////////////////////////////////////////////////////////////////////////
  91. // DllRegisterServer - Adds entries to the system registry
  92. STDAPI DllRegisterServer(void)
  93. {
  94. AFX_MANAGE_STATE(_afxModuleAddrThis);
  95. if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))
  96. return ResultFromScode(SELFREG_E_CLASS);
  97. // intialize the metabase /logging tree
  98. if ( !RegisterInMetabase( NULL ) )
  99. return GetLastError();
  100. return NOERROR;
  101. }
  102. /////////////////////////////////////////////////////////////////////////////
  103. // DllUnregisterServer - Removes entries from the system registry
  104. STDAPI DllUnregisterServer(void)
  105. {
  106. AFX_MANAGE_STATE(_afxModuleAddrThis);
  107. // if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor))
  108. // return ResultFromScode(SELFREG_E_TYPELIB);
  109. if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))
  110. return ResultFromScode(SELFREG_E_CLASS);
  111. return NOERROR;
  112. }
  113. //-------------------------------------------------------------------------
  114. // add all the base logging info to the /LM portion of the tree Also, add in
  115. // the ftp and w3 service logging load strings
  116. BOOL _cdecl RegisterInMetabase( PWCHAR pszMachine )
  117. {
  118. CString sz;
  119. BOOL f;
  120. DWORD dw;
  121. BOOL fODBCW3 = FALSE;
  122. BOOL fODBCFTP = FALSE;
  123. DWORD fAnswer = FALSE;
  124. CString szAvail;
  125. CWrapMetaBase mbWrap;
  126. // specify the resources to use
  127. HINSTANCE hOldRes = AfxGetResourceHandle();
  128. AfxSetResourceHandle( g_hInstance );
  129. // prep the metabase - during install we always target the local machine
  130. IMSAdminBase * pMB = NULL;
  131. if (!FInitMetabaseWrapperEx( pszMachine, &pMB ))
  132. {
  133. goto CLEANUP_RES;
  134. }
  135. if ( !mbWrap.FInit(pMB) )
  136. {
  137. goto CLEANUP_RES;
  138. }
  139. // first, we will add the basic tree to the metabase
  140. // start with the bottom item
  141. // open the target
  142. if ( !mbWrap.Open( _T("/lm"), METADATA_PERMISSION_READ | METADATA_PERMISSION_WRITE ) )
  143. {
  144. goto CLEANUP_RES;
  145. }
  146. // test to see if we can do odbc logging
  147. if ( mbWrap.GetDword( _T("/w3svc/Info"), MD_SERVER_CAPABILITIES, IIS_MD_UT_SERVER, &dw ) )
  148. fODBCW3 = (dw & IIS_CAP1_ODBC_LOGGING) > 0;
  149. if ( mbWrap.GetDword( _T("/MSFTPSVC/Info"), MD_SERVER_CAPABILITIES, IIS_MD_UT_SERVER, &dw ) )
  150. fODBCFTP = (dw & IIS_CAP1_ODBC_LOGGING) > 0;
  151. // we shouldn't tie up the /lm object, so close it and open logging
  152. mbWrap.Close();
  153. // open the logging object
  154. if ( !mbWrap.Open( _T("/lm/logging"), METADATA_PERMISSION_WRITE ) )
  155. {
  156. // the logging node doesn't exist. Create it
  157. if ( !mbWrap.Open( _T("/lm"), METADATA_PERMISSION_READ | METADATA_PERMISSION_WRITE ) )
  158. goto CLEANUP_RES;
  159. // add the logging object
  160. if ( mbWrap.AddObject(_T("logging")) )
  161. {
  162. // add the ACL to the node
  163. SetInfoAdminACL( &mbWrap, _T("logging") );
  164. }
  165. // we shouldn't tie up the /lm object, so close it and open logging
  166. mbWrap.Close();
  167. // open the logging object
  168. if ( !mbWrap.Open( _T("/lm/logging"), METADATA_PERMISSION_WRITE ) )
  169. goto CLEANUP_RES;
  170. }
  171. // set the logging key type
  172. mbWrap.SetString( _T(""), MD_KEY_TYPE, IIS_MD_UT_SERVER, SZ_LOGGING_MAIN_TYPE, 0 );
  173. // add ncsa first
  174. sz.LoadString( IDS_MTITLE_NCSA );
  175. if ( mbWrap.AddObject( sz ) )
  176. {
  177. // set the key type
  178. mbWrap.SetString( sz, MD_KEY_TYPE, IIS_MD_UT_SERVER, SZ_LOGGING_TYPE, 0 );
  179. // add the logging module's guid string
  180. f = mbWrap.SetString( sz, MD_LOG_PLUGIN_MOD_ID, IIS_MD_UT_SERVER, NCSALOG_CLSID );
  181. // add the logging ui's guid string
  182. f = mbWrap.SetString( sz, MD_LOG_PLUGIN_UI_ID, IIS_MD_UT_SERVER, NCSALOGUI_CLSID );
  183. }
  184. // add odbc logging
  185. sz.LoadString( IDS_MTITLE_ODBC );
  186. if ( (fODBCW3 || fODBCFTP) && mbWrap.AddObject( sz ) )
  187. {
  188. // set the key type
  189. mbWrap.SetString( sz, MD_KEY_TYPE, IIS_MD_UT_SERVER, SZ_LOGGING_TYPE, 0 );
  190. // add the logging module's guid string
  191. f = mbWrap.SetString( sz, MD_LOG_PLUGIN_MOD_ID, IIS_MD_UT_SERVER, ODBCLOG_CLSID );
  192. // add the logging ui's guid string
  193. f = mbWrap.SetString( sz, MD_LOG_PLUGIN_UI_ID, IIS_MD_UT_SERVER, ODBCLOGUI_CLSID );
  194. }
  195. // add microsoft logging
  196. sz.LoadString( IDS_MTITLE_MSFT );
  197. if ( mbWrap.AddObject( sz ) )
  198. {
  199. // set the key type
  200. mbWrap.SetString( sz, MD_KEY_TYPE, IIS_MD_UT_SERVER, SZ_LOGGING_TYPE, 0 );
  201. // add the logging module's guid string
  202. f = mbWrap.SetString( sz, MD_LOG_PLUGIN_MOD_ID, IIS_MD_UT_SERVER, ASCLOG_CLSID );
  203. // add the logging ui's guid string
  204. f = mbWrap.SetString( sz, MD_LOG_PLUGIN_UI_ID, IIS_MD_UT_SERVER, ASCLOGUI_CLSID );
  205. }
  206. // add extended logging
  207. sz.LoadString( IDS_MTITLE_XTND );
  208. if ( mbWrap.AddObject( sz ) )
  209. {
  210. // set the key type
  211. mbWrap.SetString( sz, MD_KEY_TYPE, IIS_MD_UT_SERVER, SZ_LOGGING_TYPE, 0 );
  212. // add the logging module's guid string
  213. f = mbWrap.SetString( sz, MD_LOG_PLUGIN_MOD_ID, IIS_MD_UT_SERVER, EXTLOG_CLSID );
  214. // add the logging ui's guid string
  215. f = mbWrap.SetString( sz, MD_LOG_PLUGIN_UI_ID, IIS_MD_UT_SERVER, EXTLOGUI_CLSID );
  216. }
  217. // close the wrapper
  218. mbWrap.Close();
  219. // prepare the available logging extensions string
  220. // start with w3svc
  221. sz.LoadString( IDS_MTITLE_NCSA );
  222. szAvail = sz;
  223. sz.LoadString( IDS_MTITLE_MSFT );
  224. szAvail += _T(',') + sz;
  225. sz.LoadString( IDS_MTITLE_XTND );
  226. szAvail += _T(',') + sz;
  227. if ( fODBCW3 )
  228. {
  229. sz.LoadString( IDS_MTITLE_ODBC );
  230. szAvail += _T(',') + sz;
  231. }
  232. // save the string
  233. if ( mbWrap.Open( _T("/lm/w3svc/Info"), METADATA_PERMISSION_WRITE ) )
  234. {
  235. f = mbWrap.SetString( _T(""), MD_LOG_PLUGINS_AVAILABLE, IIS_MD_UT_SERVER, szAvail );
  236. // close the wrapper
  237. mbWrap.Close();
  238. }
  239. // now ftp - no ncsa
  240. sz.LoadString( IDS_MTITLE_MSFT );
  241. szAvail = sz;
  242. sz.LoadString( IDS_MTITLE_XTND );
  243. szAvail += _T(',') + sz;
  244. if ( fODBCFTP )
  245. {
  246. sz.LoadString( IDS_MTITLE_ODBC );
  247. szAvail += _T(',') + sz;
  248. }
  249. // save the string
  250. if ( mbWrap.Open( _T("/lm/msftpsvc/Info"), METADATA_PERMISSION_WRITE ) )
  251. {
  252. f = mbWrap.SetString( _T(""), MD_LOG_PLUGINS_AVAILABLE, IIS_MD_UT_SERVER, szAvail );
  253. // close the wrapper
  254. mbWrap.Close();
  255. }
  256. // close the metabase wrappings
  257. FCloseMetabaseWrapperEx(&pMB);
  258. fAnswer = TRUE;
  259. CLEANUP_RES:
  260. // we want to be able to recover a meaningful error, so get it and set it again after
  261. // restoring the resource handle
  262. DWORD err = GetLastError();
  263. // restore the resources
  264. if ( hOldRes )
  265. AfxSetResourceHandle( hOldRes );
  266. // reset the error code
  267. SetLastError( err );
  268. // return the error - hopefully success
  269. return fAnswer;
  270. }
  271. //-------------------------------------------------------------------------
  272. int SetInfoAdminACL( CWrapMetaBase* pMB, LPCTSTR szSubKeyPath )
  273. {
  274. int retCode=-1;
  275. BOOL b = FALSE;
  276. DWORD dwLength = 0;
  277. PSECURITY_DESCRIPTOR pSD = NULL;
  278. PSECURITY_DESCRIPTOR outpSD = NULL;
  279. DWORD cboutpSD = 0;
  280. PACL pACLNew = NULL;
  281. DWORD cbACL = 0;
  282. PSID pAdminsSID = NULL, pEveryoneSID = NULL;
  283. BOOL bWellKnownSID = FALSE;
  284. // Initialize a new security descriptor
  285. pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
  286. if (NULL == pSD)
  287. goto Cleanup;
  288. InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION);
  289. // Get Local Admins Sid
  290. GetPrincipalSID (_T("Administrators"), &pAdminsSID, &bWellKnownSID);
  291. // Get everyone Sid
  292. GetPrincipalSID (_T("Everyone"), &pEveryoneSID, &bWellKnownSID);
  293. // Initialize a new ACL, which only contains 2 aaace
  294. cbACL = sizeof(ACL) +
  295. (sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(pAdminsSID) - sizeof(DWORD)) +
  296. (sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(pEveryoneSID) - sizeof(DWORD)) ;
  297. pACLNew = (PACL) LocalAlloc(LPTR, cbACL);
  298. if (NULL == pACLNew)
  299. goto Cleanup;
  300. InitializeAcl(pACLNew, cbACL, ACL_REVISION);
  301. AddAccessAllowedAce(
  302. pACLNew,
  303. ACL_REVISION,
  304. (MD_ACR_READ |
  305. MD_ACR_WRITE |
  306. MD_ACR_RESTRICTED_WRITE |
  307. MD_ACR_UNSECURE_PROPS_READ |
  308. MD_ACR_ENUM_KEYS |
  309. MD_ACR_WRITE_DAC),
  310. pAdminsSID);
  311. AddAccessAllowedAce(
  312. pACLNew,
  313. ACL_REVISION,
  314. (MD_ACR_READ | MD_ACR_ENUM_KEYS),
  315. pEveryoneSID);
  316. // Add the ACL to the security descriptor
  317. b = SetSecurityDescriptorDacl(pSD, TRUE, pACLNew, FALSE);
  318. b = SetSecurityDescriptorOwner(pSD, pAdminsSID, TRUE);
  319. b = SetSecurityDescriptorGroup(pSD, pAdminsSID, TRUE);
  320. // Security descriptor blob must be self relative
  321. if (!MakeSelfRelativeSD(pSD, outpSD, &cboutpSD))
  322. goto Cleanup;
  323. outpSD = (PSECURITY_DESCRIPTOR)GlobalAlloc(GPTR, cboutpSD);
  324. if (NULL == outpSD)
  325. goto Cleanup;
  326. if (!MakeSelfRelativeSD( pSD, outpSD, &cboutpSD ))
  327. goto Cleanup;
  328. // below this modify pSD to outpSD
  329. // Apply the new security descriptor to the file
  330. dwLength = GetSecurityDescriptorLength(outpSD);
  331. // set the acl into the metabase at the given location
  332. b = pMB->SetData( szSubKeyPath, MD_ADMIN_ACL, IIS_MD_UT_SERVER, BINARY_METADATA,
  333. (LPBYTE)outpSD, dwLength,
  334. METADATA_INHERIT | METADATA_REFERENCE | METADATA_SECURE );
  335. retCode = 0;
  336. Cleanup:
  337. // both of Administrators and Everyone are well-known SIDs, use FreeSid() to free them.
  338. if (outpSD)
  339. GlobalFree(outpSD);
  340. if (pAdminsSID)
  341. FreeSid(pAdminsSID);
  342. if (pEveryoneSID)
  343. FreeSid(pEveryoneSID);
  344. if (pSD)
  345. LocalFree((HLOCAL) pSD);
  346. if (pACLNew)
  347. LocalFree((HLOCAL) pACLNew);
  348. return (retCode);
  349. }