Leaked source code of windows server 2003
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.

1131 lines
30 KiB

  1. #include "precomp.hxx"
  2. #include <stdio.h>
  3. #include <script.h>
  4. #include "LogScript.hxx"
  5. #include <ilogobj.hxx>
  6. #include "filectl.hxx"
  7. #include "asclogc.hxx"
  8. #include "ncslogc.hxx"
  9. #include "lkrhash.h"
  10. #include "extlogc.hxx"
  11. #include "odbcconn.hxx"
  12. #include "odblogc.hxx"
  13. #include <initguid.h>
  14. #include <iadmw.h>
  15. #include "resource.h"
  16. HINSTANCE hDLLInstance;
  17. BOOL
  18. AddClsIdRegKeys(
  19. IN LPCSTR ControlKey,
  20. IN LPCSTR ClsId,
  21. IN LPCSTR ControlName,
  22. IN LPCSTR PPageClsId,
  23. IN LPCSTR PPageName,
  24. IN LPCSTR BitmapIndex
  25. );
  26. BOOL
  27. AddControlRegKeys(
  28. IN LPCSTR ControlKey,
  29. IN LPCSTR ControlName,
  30. IN LPCSTR ClsId,
  31. IN LPCSTR PPageClsId,
  32. IN LPCSTR PPageName,
  33. IN LPCSTR BitmapIndex
  34. );
  35. BOOL
  36. CreateMetabaseKeys();
  37. HRESULT SetAdminACL(
  38. IMSAdminBase *pAdminBase,
  39. METADATA_HANDLE hMeta,
  40. LPWSTR wszKeyName
  41. );
  42. DWORD
  43. GetPrincipalSID (
  44. LPTSTR Principal,
  45. PSID *Sid,
  46. BOOL *pbWellKnownSID
  47. );
  48. BEGIN_OBJECT_MAP(ObjectMap)
  49. OBJECT_ENTRY(CLSID_NCSALOG, CNCSALOG)
  50. OBJECT_ENTRY(CLSID_ODBCLOG, CODBCLOG)
  51. OBJECT_ENTRY(CLSID_EXTLOG, CEXTLOG)
  52. OBJECT_ENTRY(CLSID_ASCLOG, CASCLOG)
  53. END_OBJECT_MAP()
  54. DECLARE_DEBUG_VARIABLE();
  55. DECLARE_DEBUG_PRINTS_OBJECT();
  56. DECLARE_PLATFORM_TYPE();
  57. extern "C" {
  58. BOOL
  59. WINAPI
  60. DLLEntry(
  61. HINSTANCE hDll,
  62. DWORD dwReason,
  63. LPVOID lpvReserved
  64. );
  65. }
  66. BOOL
  67. WINAPI
  68. DLLEntry(
  69. HINSTANCE hDll,
  70. DWORD dwReason,
  71. LPVOID
  72. )
  73. /*++
  74. Routine Description:
  75. DLL entrypoint.
  76. Arguments:
  77. hDLL - Instance handle.
  78. Reason - The reason the entrypoint was called.
  79. DLL_PROCESS_ATTACH
  80. DLL_PROCESS_DETACH
  81. DLL_THREAD_ATTACH
  82. DLL_THREAD_DETACH
  83. Return Value:
  84. BOOL - TRUE if the action succeeds.
  85. --*/
  86. {
  87. hDLLInstance = hDll;
  88. BOOL bReturn = TRUE;
  89. switch ( dwReason ) {
  90. case DLL_PROCESS_ATTACH:
  91. INITIALIZE_PLATFORM_TYPE();
  92. CREATE_DEBUG_PRINT_OBJECT("iislog.dll");
  93. LOAD_DEBUG_FLAGS_FROM_REG_STR("System\\CurrentControlSet\\Services\\InetInfo\\Parameters", 0);
  94. _Module.Init(ObjectMap,hDll);
  95. g_eventLog = new EVENT_LOG(IISLOG_EVENTLOG_SOURCE);
  96. if ( g_eventLog == NULL ) {
  97. DBGPRINTF((DBG_CONTEXT,
  98. "Unable to create eventlog[%s] object[err %d]\n",
  99. IISLOG_EVENTLOG_SOURCE, GetLastError()));
  100. }
  101. (VOID)IISGetPlatformType();
  102. DisableThreadLibraryCalls(hDll);
  103. break;
  104. case DLL_PROCESS_DETACH:
  105. if ( g_eventLog != NULL ) {
  106. delete g_eventLog;
  107. g_eventLog = NULL;
  108. }
  109. _Module.Term();
  110. DELETE_DEBUG_PRINT_OBJECT( );
  111. break;
  112. default:
  113. break;
  114. }
  115. return bReturn;
  116. } // DllEntry
  117. //
  118. // constants used in self registration
  119. //
  120. #define TYPELIB_CLSID "{FF160650-DE82-11CF-BC0A-00AA006111E0}"
  121. #define NCSA_CLSID NCSALOG_CLSID // %F
  122. #define NCSA_KEY "MSIISLOG.MSNCSALogCtrl.1"
  123. #define NCSA_NAME "MSNCSALog Control"
  124. #define NCSA_PP_CLSID "{FF160660-DE82-11CF-BC0A-00AA006111E0}"
  125. #define NCSA_PP_NAME "MSNCSALog Property Page"
  126. #define ODBC_CLSID ODBCLOG_CLSID //5B
  127. #define ODBC_KEY "MSIISLOG.MSODBCLogCtrl.1"
  128. #define ODBC_NAME "MSODBCLog Control"
  129. #define ODBC_PP_CLSID "{FF16065C-DE82-11CF-BC0A-00AA006111E0}"
  130. #define ODBC_PP_NAME "MSODBCLog Property Page"
  131. #define ASCII_CLSID ASCLOG_CLSID // 57
  132. #define ASCII_KEY "MSIISLOG.MSASCIILogCtrl.1"
  133. #define ASCII_NAME "MSASCIILog Control"
  134. #define ASCII_PP_CLSID "{FF160658-DE82-11CF-BC0A-00AA006111E0}"
  135. #define ASCII_PP_NAME "MSASCIILog Property Page"
  136. #define CUSTOM_CLSID EXTLOG_CLSID // 63
  137. #define CUSTOM_KEY "MSIISLOG.MSCustomLogCtrl.1"
  138. #define CUSTOM_NAME "MSCustomLog Control"
  139. #define CUSTOM_PP_CLSID "{FF160664-DE82-11CF-BC0A-00AA006111E0}"
  140. #define CUSTOM_PP_NAME "MSCustomLog Property Page"
  141. /////////////////////////////////////////////////////////////////////////////
  142. // DllRegisterServer - Adds entries to the system registry
  143. STDAPI
  144. DllRegisterServer(void)
  145. {
  146. //
  147. // MS NCSA Log Support
  148. //
  149. if ( !AddControlRegKeys(
  150. NCSA_KEY,
  151. NCSA_NAME,
  152. NCSA_CLSID,
  153. NCSA_PP_CLSID,
  154. NCSA_PP_NAME,
  155. "4") ) {
  156. goto error;
  157. }
  158. //
  159. // MS ODBC Log Support
  160. //
  161. if ( !AddControlRegKeys(
  162. ODBC_KEY,
  163. ODBC_NAME,
  164. ODBC_CLSID,
  165. ODBC_PP_CLSID,
  166. ODBC_PP_NAME,
  167. "3") ) {
  168. goto error;
  169. }
  170. //
  171. // MS Ascii Log Support
  172. //
  173. if ( !AddControlRegKeys(
  174. ASCII_KEY,
  175. ASCII_NAME,
  176. ASCII_CLSID,
  177. ASCII_PP_CLSID,
  178. ASCII_PP_NAME,
  179. "2") ) {
  180. goto error;
  181. }
  182. //
  183. // MS Custom Log Support
  184. //
  185. if ( !AddClsIdRegKeys(
  186. CUSTOM_KEY,
  187. CUSTOM_CLSID,
  188. CUSTOM_NAME,
  189. CUSTOM_PP_CLSID,
  190. CUSTOM_PP_NAME,
  191. "2"
  192. ) ) {
  193. goto error;
  194. }
  195. //
  196. // Metabase entries for W3C custom logging
  197. //
  198. if ( !CreateMetabaseKeys() ) {
  199. goto error;
  200. }
  201. return S_OK;
  202. error:
  203. return E_UNEXPECTED;
  204. } // DllRegisterServer
  205. /////////////////////////////////////////////////////////////////////////////
  206. // DllUnregisterServer - Removes entries from the system registry
  207. STDAPI
  208. DllUnregisterServer(void)
  209. {
  210. HKEY hCLSID;
  211. //
  212. // Delete controls
  213. //
  214. ZapRegistryKey(HKEY_CLASSES_ROOT,NCSA_KEY);
  215. ZapRegistryKey(HKEY_CLASSES_ROOT,ODBC_KEY);
  216. ZapRegistryKey(HKEY_CLASSES_ROOT,ASCII_KEY);
  217. //
  218. // Get CLSID handle
  219. //
  220. if ( RegOpenKeyExA(HKEY_CLASSES_ROOT,
  221. "CLSID",
  222. 0,
  223. KEY_ALL_ACCESS,
  224. &hCLSID) != ERROR_SUCCESS ) {
  225. IIS_PRINTF((buff,"IISLOG: Cannot open CLSID key\n"));
  226. return E_UNEXPECTED;
  227. }
  228. ZapRegistryKey(hCLSID, NCSA_CLSID);
  229. ZapRegistryKey(hCLSID, NCSA_PP_CLSID);
  230. ZapRegistryKey(hCLSID, ODBC_CLSID);
  231. ZapRegistryKey(hCLSID, ODBC_PP_CLSID);
  232. ZapRegistryKey(hCLSID, ASCII_CLSID);
  233. ZapRegistryKey(hCLSID, ASCII_PP_CLSID);
  234. ZapRegistryKey(hCLSID, CUSTOM_CLSID);
  235. ZapRegistryKey(hCLSID, CUSTOM_PP_CLSID);
  236. RegCloseKey(hCLSID);
  237. //
  238. // Open the metabase path and remove Custom Logging keys
  239. //
  240. /*
  241. IMSAdminBase* pMBCom = NULL;
  242. METADATA_HANDLE hMeta = NULL;
  243. if ( SUCCEEDED( CoCreateInstance(GETAdminBaseCLSID(TRUE), NULL, CLSCTX_LOCAL_SERVER,
  244. IID_IMSAdminBase, (void **)(&pMBCom) )))
  245. {
  246. if ( SUCCEEDED( pMBCom->OpenKey( METADATA_MASTER_ROOT_HANDLE, L"LM",
  247. METADATA_PERMISSION_READ | METADATA_PERMISSION_WRITE, MB_TIMEOUT,
  248. &hMeta)) )
  249. {
  250. pMBCom->DeleteKey(hMeta, L"Logging/Custom Logging");
  251. pMBCom->CloseKey(hMeta);
  252. }
  253. pMBCom->Release();
  254. }
  255. */
  256. return S_OK;
  257. } // DllUnregisterServer
  258. BOOL
  259. AddControlRegKeys(
  260. IN LPCSTR ControlKey,
  261. IN LPCSTR ControlName,
  262. IN LPCSTR ControlClsId,
  263. IN LPCSTR PPageClsId,
  264. IN LPCSTR PPageName,
  265. IN LPCSTR BitmapIndex
  266. )
  267. {
  268. HKEY hProgID = NULL;
  269. HKEY hCLSID = NULL;
  270. BOOL fRet = FALSE;
  271. //
  272. // Add control name
  273. //
  274. hProgID = CreateKey(HKEY_CLASSES_ROOT,ControlKey,ControlName);
  275. if ( hProgID == NULL ) {
  276. goto exit;
  277. }
  278. hCLSID = CreateKey(hProgID,"CLSID",ControlClsId);
  279. if ( hCLSID == NULL ) {
  280. goto exit;
  281. }
  282. //
  283. // Add CLSID keys
  284. //
  285. if ( !AddClsIdRegKeys(
  286. ControlKey,
  287. ControlClsId,
  288. ControlName,
  289. PPageClsId,
  290. PPageName,
  291. BitmapIndex) ) {
  292. goto exit;
  293. }
  294. fRet = TRUE;
  295. exit:
  296. if ( hProgID != NULL ) {
  297. RegCloseKey(hProgID);
  298. }
  299. if ( hCLSID != NULL ) {
  300. RegCloseKey(hCLSID);
  301. }
  302. return(fRet);
  303. } // AddControlRegKeys
  304. BOOL
  305. AddClsIdRegKeys(
  306. IN LPCSTR ControlKey,
  307. IN LPCSTR ClsId,
  308. IN LPCSTR ControlName,
  309. IN LPCSTR PPageClsId,
  310. IN LPCSTR PPageName,
  311. IN LPCSTR BitmapIndex
  312. )
  313. {
  314. BOOL fRet = FALSE;
  315. HKEY hCLSID = NULL;
  316. HKEY hRoot = NULL;
  317. HKEY hKey, hKey2;
  318. CHAR szName[MAX_PATH+1];
  319. HMODULE hModule;
  320. //
  321. // open CLASSES/CLSID
  322. //
  323. if ( RegOpenKeyEx(HKEY_CLASSES_ROOT,
  324. "CLSID",
  325. 0,
  326. KEY_ALL_ACCESS,
  327. &hCLSID) != ERROR_SUCCESS ) {
  328. IIS_PRINTF((buff,"IISLOG: Cannot open CLSID key\n"));
  329. goto exit;
  330. }
  331. //
  332. // Create the Guid and set the control name
  333. //
  334. hRoot = CreateKey(hCLSID,ClsId,ControlName);
  335. if ( hRoot == NULL ) {
  336. goto exit;
  337. }
  338. //
  339. // Control
  340. //
  341. hKey = CreateKey(hRoot, "Control", "");
  342. if ( hKey == NULL ) {
  343. goto exit;
  344. }
  345. RegCloseKey(hKey);
  346. //
  347. // InProcServer32
  348. //
  349. hModule=GetModuleHandleA("iislog.dll");
  350. if (hModule == NULL) {
  351. goto exit;
  352. }
  353. if (GetModuleFileName(hModule, szName, sizeof(szName)) == 0) {
  354. goto exit;
  355. }
  356. hKey = CreateKey(hRoot, "InProcServer32", szName);
  357. if ( hKey == NULL ) {
  358. goto exit;
  359. }
  360. if (RegSetValueExA(hKey,
  361. "ThreadingModel",
  362. NULL,
  363. REG_SZ,
  364. (LPBYTE)"Both",
  365. sizeof("Both")) != ERROR_SUCCESS) {
  366. RegCloseKey(hKey);
  367. goto exit;
  368. }
  369. RegCloseKey(hKey);
  370. //
  371. // Misc Status
  372. //
  373. hKey = CreateKey(hRoot,"MiscStatus","0");
  374. if ( hKey == NULL ) {
  375. goto exit;
  376. }
  377. hKey2 = CreateKey(hKey,"1","131473");
  378. if ( hKey2 == NULL ) {
  379. RegCloseKey(hKey);
  380. goto exit;
  381. }
  382. RegCloseKey(hKey2);
  383. RegCloseKey(hKey);
  384. //
  385. // ProgID
  386. //
  387. hKey = CreateKey(hRoot,"ProgID",ControlKey);
  388. if ( hKey == NULL ) {
  389. goto exit;
  390. }
  391. RegCloseKey(hKey);
  392. //
  393. // ToolboxBitmap32
  394. //
  395. {
  396. CHAR tmpBuf[MAX_PATH+1];
  397. strcpy(tmpBuf,szName);
  398. strcat(tmpBuf,", ");
  399. strcat(tmpBuf,BitmapIndex);
  400. hKey = CreateKey(hRoot,"ToolboxBitmap32",tmpBuf);
  401. if ( hKey == NULL ) {
  402. goto exit;
  403. }
  404. RegCloseKey(hKey);
  405. }
  406. //
  407. // TypeLib
  408. //
  409. hKey = CreateKey(hRoot,"TypeLib",TYPELIB_CLSID);
  410. if ( hKey == NULL ) {
  411. goto exit;
  412. }
  413. RegCloseKey(hKey);
  414. //
  415. // Version
  416. //
  417. hKey = CreateKey(hRoot,"Version","1.0");
  418. if ( hKey == NULL ) {
  419. goto exit;
  420. }
  421. RegCloseKey(hKey);
  422. //
  423. // Property page
  424. //
  425. RegCloseKey(hRoot);
  426. hRoot = NULL;
  427. hRoot = CreateKey(hCLSID, PPageClsId, PPageName);
  428. if ( hRoot == NULL ) {
  429. goto exit;
  430. }
  431. hKey = CreateKey(hRoot, "InProcServer32", szName );
  432. if ( hKey == NULL ) {
  433. goto exit;
  434. }
  435. RegCloseKey(hKey);
  436. fRet = TRUE;
  437. exit:
  438. if ( hRoot != NULL ) {
  439. RegCloseKey(hRoot);
  440. }
  441. if ( hCLSID != NULL ) {
  442. RegCloseKey(hCLSID);
  443. }
  444. return fRet;
  445. } // AddClsIdRegKeys
  446. BOOL
  447. CreateMetabaseKeys()
  448. {
  449. USES_CONVERSION;
  450. typedef struct _MB_PROP_
  451. {
  452. LPWSTR wcsPath;
  453. DWORD dwNameString;
  454. LPSTR szHeaderName;
  455. DWORD dwPropID;
  456. DWORD dwPropMask;
  457. DWORD dwDataType;
  458. DWORD dwNodeID;
  459. } MB_PROP;
  460. IMSAdminBase* pMBCom;
  461. CHAR szString[256];
  462. int i;
  463. METADATA_HANDLE hMeta = NULL;
  464. METADATA_RECORD mdRecord;
  465. HRESULT hr;
  466. MB_PROP mbProperties[] =
  467. {
  468. { L"Logging/Custom Logging/Date", IDS_DATE,
  469. EXTLOG_DATE_ID, MD_LOGEXT_FIELD_MASK, MD_EXTLOG_DATE, MD_LOGCUSTOM_DATATYPE_LPSTR, 1},
  470. { L"Logging/Custom Logging/Time", IDS_TIME,
  471. EXTLOG_TIME_ID, MD_LOGEXT_FIELD_MASK, MD_EXTLOG_TIME, MD_LOGCUSTOM_DATATYPE_LPSTR, 2},
  472. { L"Logging/Custom Logging/Extended Properties", IDS_EXTENDED_PROP,
  473. NULL, MD_LOGEXT_FIELD_MASK, 0, MD_LOGCUSTOM_DATATYPE_LPSTR, 3},
  474. { L"Logging/Custom Logging/Extended Properties/Client IP Address", IDS_CLIENT_IP_ADDRESS,
  475. EXTLOG_CLIENT_IP_ID, 0, MD_EXTLOG_CLIENT_IP, MD_LOGCUSTOM_DATATYPE_LPSTR, 5},
  476. { L"Logging/Custom Logging/Extended Properties/User Name", IDS_USER_NAME,
  477. EXTLOG_USERNAME_ID, 0, MD_EXTLOG_USERNAME, MD_LOGCUSTOM_DATATYPE_LPSTR, 6},
  478. { L"Logging/Custom Logging/Extended Properties/Service Name", IDS_SERVICE_NAME,
  479. EXTLOG_SITE_NAME_ID, 0, MD_EXTLOG_SITE_NAME, MD_LOGCUSTOM_DATATYPE_LPSTR, 7},
  480. { L"Logging/Custom Logging/Extended Properties/Server Name", IDS_SERVER_NAME,
  481. EXTLOG_COMPUTER_NAME_ID, 0, MD_EXTLOG_COMPUTER_NAME, MD_LOGCUSTOM_DATATYPE_LPSTR, 8},
  482. { L"Logging/Custom Logging/Extended Properties/Server IP", IDS_SERVER_IP,
  483. EXTLOG_SERVER_IP_ID, 0, MD_EXTLOG_SERVER_IP, MD_LOGCUSTOM_DATATYPE_LPSTR, 9},
  484. { L"Logging/Custom Logging/Extended Properties/Server Port", IDS_SERVER_PORT,
  485. EXTLOG_SERVER_PORT_ID, 0, MD_EXTLOG_SERVER_PORT, MD_LOGCUSTOM_DATATYPE_ULONG, 10},
  486. { L"Logging/Custom Logging/Extended Properties/Method", IDS_METHOD,
  487. EXTLOG_METHOD_ID, 0, MD_EXTLOG_METHOD, MD_LOGCUSTOM_DATATYPE_LPSTR, 11},
  488. { L"Logging/Custom Logging/Extended Properties/URI Stem", IDS_URI_STEM,
  489. EXTLOG_URI_STEM_ID, 0, MD_EXTLOG_URI_STEM, MD_LOGCUSTOM_DATATYPE_LPSTR, 12},
  490. { L"Logging/Custom Logging/Extended Properties/URI Query", IDS_URI_QUERY,
  491. EXTLOG_URI_QUERY_ID, 0, MD_EXTLOG_URI_QUERY, MD_LOGCUSTOM_DATATYPE_LPSTR, 13},
  492. { L"Logging/Custom Logging/Extended Properties/Protocol Status", IDS_HTTP_STATUS,
  493. EXTLOG_HTTP_STATUS_ID, 0, MD_EXTLOG_HTTP_STATUS, MD_LOGCUSTOM_DATATYPE_ULONG, 14},
  494. { L"Logging/Custom Logging/Extended Properties/Win32 Status", IDS_WIN32_STATUS,
  495. EXTLOG_WIN32_STATUS_ID, 0, MD_EXTLOG_WIN32_STATUS, MD_LOGCUSTOM_DATATYPE_ULONG, 15},
  496. { L"Logging/Custom Logging/Extended Properties/Bytes Sent", IDS_BYTES_SENT,
  497. EXTLOG_BYTES_SENT_ID, 0, MD_EXTLOG_BYTES_SENT, MD_LOGCUSTOM_DATATYPE_ULONG, 16},
  498. { L"Logging/Custom Logging/Extended Properties/Bytes Received", IDS_BYTES_RECEIVED,
  499. EXTLOG_BYTES_RECV_ID, 0, MD_EXTLOG_BYTES_RECV, MD_LOGCUSTOM_DATATYPE_ULONG, 17},
  500. { L"Logging/Custom Logging/Extended Properties/Time Taken", IDS_TIME_TAKEN,
  501. EXTLOG_TIME_TAKEN_ID, 0, MD_EXTLOG_TIME_TAKEN, MD_LOGCUSTOM_DATATYPE_ULONG, 18},
  502. { L"Logging/Custom Logging/Extended Properties/Protocol Version", IDS_PROTOCOL_VERSION,
  503. EXTLOG_PROTOCOL_VERSION_ID, 0, MD_EXTLOG_PROTOCOL_VERSION, MD_LOGCUSTOM_DATATYPE_LPSTR, 19},
  504. { L"Logging/Custom Logging/Extended Properties/Host", IDS_HOST,
  505. EXTLOG_HOST_ID, 0, EXTLOG_HOST, MD_LOGCUSTOM_DATATYPE_LPSTR, 20},
  506. { L"Logging/Custom Logging/Extended Properties/User Agent", IDS_USER_AGENT,
  507. EXTLOG_USER_AGENT_ID, 0, MD_EXTLOG_USER_AGENT, MD_LOGCUSTOM_DATATYPE_LPSTR, 21},
  508. { L"Logging/Custom Logging/Extended Properties/Cookie", IDS_COOKIE,
  509. EXTLOG_COOKIE_ID, 0, MD_EXTLOG_COOKIE, MD_LOGCUSTOM_DATATYPE_LPSTR, 22},
  510. { L"Logging/Custom Logging/Extended Properties/Referer", IDS_REFERER,
  511. EXTLOG_REFERER_ID, 0, MD_EXTLOG_REFERER, MD_LOGCUSTOM_DATATYPE_LPSTR, 23},
  512. { L"Logging/Custom Logging/Extended Properties/Protocol Substatus", IDS_HTTP_SUB_STATUS,
  513. EXTLOG_HTTP_SUB_STATUS_ID, 0, MD_EXTLOG_HTTP_SUB_STATUS, MD_LOGCUSTOM_DATATYPE_ULONG, 32},
  514. { L"\0", 0, NULL, 0, 0, 0 },
  515. };
  516. //
  517. // Open the metabase path
  518. //
  519. if ( FAILED( CoCreateInstance(GETAdminBaseCLSID(TRUE), NULL, CLSCTX_LOCAL_SERVER,
  520. IID_IMSAdminBase, (void **)(&pMBCom) )))
  521. {
  522. return FALSE;
  523. }
  524. // Create the LM key
  525. if ( FAILED( pMBCom->OpenKey( METADATA_MASTER_ROOT_HANDLE, L"/",
  526. METADATA_PERMISSION_READ | METADATA_PERMISSION_WRITE, MB_TIMEOUT,
  527. &hMeta) ))
  528. {
  529. // Create the LM key
  530. pMBCom->Release();
  531. return FALSE;
  532. }
  533. hr = pMBCom->AddKey( hMeta, L"LM");
  534. if ( FAILED(hr) && (HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS) != hr))
  535. {
  536. goto cleanup;
  537. }
  538. pMBCom->CloseKey(hMeta);
  539. hMeta = NULL;
  540. if ( FAILED( pMBCom->OpenKey( METADATA_MASTER_ROOT_HANDLE, L"LM",
  541. METADATA_PERMISSION_READ | METADATA_PERMISSION_WRITE, MB_TIMEOUT,
  542. &hMeta) ))
  543. {
  544. pMBCom->Release();
  545. return FALSE;
  546. }
  547. //
  548. // Create the initial set of Keys.
  549. //
  550. hr = pMBCom->AddKey( hMeta, L"Logging");
  551. if ( FAILED(hr) && (HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS) != hr))
  552. {
  553. goto cleanup;
  554. }
  555. hr = pMBCom->AddKey( hMeta, L"Logging/Custom Logging");
  556. if ( FAILED(hr) && (HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS) != hr))
  557. {
  558. goto cleanup;
  559. }
  560. //
  561. // Set all the properties
  562. //
  563. mdRecord.dwMDUserType = IIS_MD_UT_SERVER;
  564. for (i=0; 0 != mbProperties[i].wcsPath[0]; i++)
  565. {
  566. hr = pMBCom->AddKey( hMeta, mbProperties[i].wcsPath);
  567. if ( FAILED(hr) && (HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS) != hr))
  568. {
  569. goto cleanup;
  570. }
  571. // don't overwrite it entry already exist.
  572. if ( SUCCEEDED(hr) || (HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS) != hr))
  573. {
  574. // We won't get here if the key already exists.
  575. mdRecord.dwMDAttributes = METADATA_INHERIT; // name and header is not inheritable.
  576. mdRecord.dwMDDataType = STRING_METADATA;
  577. if ( (0 != mbProperties[i].dwNameString) &&
  578. (0 < LoadString( hDLLInstance, mbProperties[i].dwNameString, szString, sizeof(szString)) )
  579. )
  580. {
  581. mdRecord.dwMDIdentifier = MD_LOGCUSTOM_PROPERTY_NAME;
  582. mdRecord.pbMDData = (PBYTE)A2W(szString);
  583. mdRecord.dwMDDataLen = (DWORD)((sizeof(WCHAR)/sizeof(BYTE)) * (wcslen((LPWSTR) mdRecord.pbMDData)+1));
  584. if ( FAILED(pMBCom->SetData( hMeta, mbProperties[i].wcsPath, &mdRecord)) )
  585. {
  586. goto cleanup;
  587. }
  588. }
  589. if ( NULL != mbProperties[i].szHeaderName)
  590. {
  591. mdRecord.dwMDIdentifier = MD_LOGCUSTOM_PROPERTY_HEADER;
  592. mdRecord.pbMDData = (PBYTE) (A2W(mbProperties[i].szHeaderName));
  593. mdRecord.dwMDDataLen = (DWORD)((sizeof(WCHAR)/sizeof(BYTE)) * (wcslen((LPWSTR) mdRecord.pbMDData)+1));
  594. if ( FAILED(pMBCom->SetData( hMeta, mbProperties[i].wcsPath, &mdRecord)) )
  595. {
  596. goto cleanup;
  597. }
  598. }
  599. mdRecord.dwMDDataType = DWORD_METADATA;
  600. mdRecord.dwMDDataLen = sizeof(DWORD);
  601. if ( 0 != mbProperties[i].dwPropID)
  602. {
  603. mdRecord.dwMDIdentifier = MD_LOGCUSTOM_PROPERTY_ID;
  604. mdRecord.pbMDData = (PBYTE) &(mbProperties[i].dwPropID);
  605. if ( FAILED(pMBCom->SetData( hMeta, mbProperties[i].wcsPath, &mdRecord)) )
  606. {
  607. goto cleanup;
  608. }
  609. }
  610. if ( 0 != mbProperties[i].dwPropMask)
  611. {
  612. mdRecord.dwMDIdentifier = MD_LOGCUSTOM_PROPERTY_MASK;
  613. mdRecord.pbMDData = (PBYTE) &(mbProperties[i].dwPropMask);
  614. if ( FAILED(pMBCom->SetData( hMeta, mbProperties[i].wcsPath, &mdRecord)) )
  615. {
  616. goto cleanup;
  617. }
  618. }
  619. if ( 0 != mbProperties[i].dwNodeID)
  620. {
  621. mdRecord.dwMDIdentifier = MD_LOGCUSTOM_PROPERTY_NODE_ID;
  622. mdRecord.pbMDData = (PBYTE) &(mbProperties[i].dwNodeID);
  623. if ( FAILED(pMBCom->SetData( hMeta, mbProperties[i].wcsPath, &mdRecord)) )
  624. {
  625. goto cleanup;
  626. }
  627. }
  628. mdRecord.dwMDIdentifier = MD_LOGCUSTOM_PROPERTY_DATATYPE;
  629. mdRecord.pbMDData = (PBYTE) &(mbProperties[i].dwDataType);
  630. if ( FAILED(pMBCom->SetData( hMeta, mbProperties[i].wcsPath, &mdRecord)) )
  631. {
  632. goto cleanup;
  633. }
  634. WCHAR wcsKeyType[] = L"IIsCustomLogModule";
  635. MD_SET_DATA_RECORD ( &mdRecord,
  636. MD_KEY_TYPE,
  637. METADATA_NO_ATTRIBUTES,
  638. IIS_MD_UT_SERVER,
  639. STRING_METADATA,
  640. sizeof(wcsKeyType),
  641. wcsKeyType
  642. );
  643. if ( FAILED(pMBCom->SetData( hMeta, mbProperties[i].wcsPath, &mdRecord)) )
  644. {
  645. goto cleanup;
  646. }
  647. }
  648. }
  649. //
  650. // Set the Key Type and Services List Property
  651. //
  652. {
  653. WCHAR wcsKeyType[] = L"IIsCustomLogModule";
  654. MD_SET_DATA_RECORD ( &mdRecord,
  655. MD_KEY_TYPE,
  656. METADATA_NO_ATTRIBUTES,
  657. IIS_MD_UT_SERVER,
  658. STRING_METADATA,
  659. sizeof(wcsKeyType),
  660. wcsKeyType
  661. );
  662. if ( FAILED(pMBCom->SetData( hMeta, L"Logging/Custom Logging", &mdRecord)) )
  663. {
  664. goto cleanup;
  665. }
  666. WCHAR wcsServices[] = L"W3SVC\0MSFTPSVC\0SMTPSVC\0NNTPSVC\0";
  667. MD_SET_DATA_RECORD ( &mdRecord,
  668. MD_LOGCUSTOM_SERVICES_STRING,
  669. METADATA_INHERIT,
  670. IIS_MD_UT_SERVER,
  671. MULTISZ_METADATA,
  672. sizeof(wcsServices),
  673. wcsServices
  674. );
  675. if ( FAILED(pMBCom->SetData( hMeta, L"Logging/Custom Logging", &mdRecord)) )
  676. {
  677. goto cleanup;
  678. }
  679. }
  680. //
  681. // Set the Admin ACL to allow everyone to read the /LM/Logging tree. This is to allow
  682. // operators to effectively use the ILogScripting components.
  683. //
  684. if (FAILED(SetAdminACL(pMBCom, hMeta, L"Logging")))
  685. {
  686. goto cleanup;
  687. }
  688. if (NULL != hMeta)
  689. {
  690. pMBCom->CloseKey(hMeta);
  691. }
  692. pMBCom->Release();
  693. return TRUE;
  694. cleanup:
  695. if (NULL != hMeta)
  696. {
  697. pMBCom->CloseKey(hMeta);
  698. }
  699. pMBCom->Release();
  700. return FALSE;
  701. }
  702. HRESULT SetAdminACL(
  703. IMSAdminBase * pAdminBase,
  704. METADATA_HANDLE hMeta,
  705. LPWSTR wszKeyName
  706. )
  707. {
  708. BOOL b = FALSE;
  709. DWORD dwLength = 0;
  710. PSECURITY_DESCRIPTOR pSD = NULL;
  711. PSECURITY_DESCRIPTOR outpSD = NULL;
  712. DWORD cboutpSD = 0;
  713. PACL pACLNew = NULL;
  714. DWORD cbACL = 0;
  715. PSID pAdminsSID = NULL;
  716. BOOL bWellKnownSID = FALSE;
  717. METADATA_RECORD mdr;
  718. HRESULT hr = NO_ERROR;
  719. // Initialize a new security descriptor
  720. pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
  721. // BugFix: 57647 Whistler
  722. // Prefix bug pSD being used when NULL.
  723. // EBK 5/5/2000
  724. if (pSD == NULL)
  725. {
  726. hr = E_OUTOFMEMORY;
  727. goto cleanup;
  728. }
  729. InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION);
  730. // Get Local Admins Sid
  731. GetPrincipalSID (_T("Administrators"), &pAdminsSID, &bWellKnownSID);
  732. // Initialize a new ACL, which only contains 2 aaace
  733. cbACL = sizeof(ACL) +
  734. (sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(pAdminsSID) - sizeof(DWORD));
  735. pACLNew = (PACL) LocalAlloc(LPTR, cbACL);
  736. // BugFix: 57646 Whistler
  737. // Prefix bug pACLNew being used when NULL.
  738. // EBK 5/5/2000
  739. if (pACLNew == NULL)
  740. {
  741. hr = E_OUTOFMEMORY;
  742. goto cleanup;
  743. }
  744. InitializeAcl(pACLNew, cbACL, ACL_REVISION);
  745. AddAccessAllowedAce(
  746. pACLNew,
  747. ACL_REVISION,
  748. MD_ACR_READ | MD_ACR_WRITE | MD_ACR_RESTRICTED_WRITE | MD_ACR_UNSECURE_PROPS_READ | MD_ACR_ENUM_KEYS | MD_ACR_WRITE_DAC,
  749. pAdminsSID);
  750. // Add the ACL to the security descriptor
  751. b = SetSecurityDescriptorDacl(pSD, TRUE, pACLNew, FALSE);
  752. b = SetSecurityDescriptorOwner(pSD, pAdminsSID, TRUE);
  753. b = SetSecurityDescriptorGroup(pSD, pAdminsSID, TRUE);
  754. // Security descriptor blob must be self relative
  755. b = MakeSelfRelativeSD(pSD, outpSD, &cboutpSD);
  756. outpSD = (PSECURITY_DESCRIPTOR)GlobalAlloc(GPTR, cboutpSD);
  757. // BugFix: 57648, 57649 Whistler
  758. // Prefix bug outpSD being used when NULL.
  759. // EBK 5/5/2000
  760. if (outpSD == NULL)
  761. {
  762. hr = E_OUTOFMEMORY;
  763. goto cleanup;
  764. }
  765. // BugFix: 57649 Whistler
  766. // Prefix bug outpSD being used when not inintalized.
  767. // EmilyK 2/19/2001
  768. if ( !MakeSelfRelativeSD( pSD, outpSD, &cboutpSD ) )
  769. {
  770. hr = HRESULT_FROM_WIN32(GetLastError());
  771. goto cleanup;
  772. }
  773. // below this modify pSD to outpSD
  774. // Apply the new security descriptor to the file
  775. dwLength = GetSecurityDescriptorLength(outpSD);
  776. mdr.dwMDIdentifier = MD_ADMIN_ACL;
  777. mdr.dwMDAttributes = METADATA_INHERIT | METADATA_REFERENCE | METADATA_SECURE;
  778. mdr.dwMDUserType = IIS_MD_UT_SERVER;
  779. mdr.dwMDDataType = BINARY_METADATA;
  780. mdr.dwMDDataLen = dwLength;
  781. mdr.pbMDData = (LPBYTE)outpSD;
  782. hr = pAdminBase->SetData(hMeta, wszKeyName, &mdr);
  783. cleanup:
  784. // both of Administrators and Everyone are well-known SIDs, use FreeSid() to free them.
  785. if (outpSD)
  786. GlobalFree(outpSD);
  787. if (pAdminsSID)
  788. FreeSid(pAdminsSID);
  789. if (pSD)
  790. LocalFree((HLOCAL) pSD);
  791. if (pACLNew)
  792. LocalFree((HLOCAL) pACLNew);
  793. return (hr);
  794. }
  795. DWORD
  796. GetPrincipalSID (
  797. LPTSTR Principal,
  798. PSID *Sid,
  799. BOOL *pbWellKnownSID
  800. )
  801. {
  802. SID_IDENTIFIER_AUTHORITY SidIdentifierNTAuthority = SECURITY_NT_AUTHORITY;
  803. SID_IDENTIFIER_AUTHORITY SidIdentifierWORLDAuthority = SECURITY_WORLD_SID_AUTHORITY;
  804. PSID_IDENTIFIER_AUTHORITY pSidIdentifierAuthority = NULL;
  805. BYTE Count = 0;
  806. DWORD dwRID[8];
  807. *pbWellKnownSID = TRUE;
  808. memset(&(dwRID[0]), 0, 8 * sizeof(DWORD));
  809. if ( lstrcmp(Principal,_T("Administrators")) == 0 ) {
  810. // Administrators group
  811. pSidIdentifierAuthority = &SidIdentifierNTAuthority;
  812. Count = 2;
  813. dwRID[0] = SECURITY_BUILTIN_DOMAIN_RID;
  814. dwRID[1] = DOMAIN_ALIAS_RID_ADMINS;
  815. } else if ( lstrcmp(Principal,_T("System")) == 0) {
  816. // SYSTEM
  817. pSidIdentifierAuthority = &SidIdentifierNTAuthority;
  818. Count = 1;
  819. dwRID[0] = SECURITY_LOCAL_SYSTEM_RID;
  820. } else if ( lstrcmp(Principal,_T("Interactive")) == 0) {
  821. // INTERACTIVE
  822. pSidIdentifierAuthority = &SidIdentifierNTAuthority;
  823. Count = 1;
  824. dwRID[0] = SECURITY_INTERACTIVE_RID;
  825. } else if ( lstrcmp(Principal,_T("Everyone")) == 0) {
  826. // Everyone
  827. pSidIdentifierAuthority = &SidIdentifierWORLDAuthority;
  828. Count = 1;
  829. dwRID[0] = SECURITY_WORLD_RID;
  830. } else {
  831. *pbWellKnownSID = FALSE;
  832. }
  833. if (*pbWellKnownSID) {
  834. if ( !AllocateAndInitializeSid(pSidIdentifierAuthority,
  835. (BYTE)Count,
  836. dwRID[0],
  837. dwRID[1],
  838. dwRID[2],
  839. dwRID[3],
  840. dwRID[4],
  841. dwRID[5],
  842. dwRID[6],
  843. dwRID[7],
  844. Sid) )
  845. return GetLastError();
  846. } else {
  847. // get regular account sid
  848. DWORD sidSize;
  849. TCHAR refDomain [256];
  850. DWORD refDomainSize;
  851. DWORD returnValue;
  852. SID_NAME_USE snu;
  853. sidSize = 0;
  854. refDomainSize = 255;
  855. LookupAccountName (NULL,
  856. Principal,
  857. *Sid,
  858. &sidSize,
  859. refDomain,
  860. &refDomainSize,
  861. &snu);
  862. returnValue = GetLastError();
  863. if (returnValue != ERROR_INSUFFICIENT_BUFFER)
  864. return returnValue;
  865. *Sid = (PSID) malloc (sidSize);
  866. refDomainSize = 255;
  867. if (!LookupAccountName (NULL,
  868. Principal,
  869. *Sid,
  870. &sidSize,
  871. refDomain,
  872. &refDomainSize,
  873. &snu))
  874. {
  875. return GetLastError();
  876. }
  877. }
  878. return ERROR_SUCCESS;
  879. }