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.

673 lines
21 KiB

  1. /*===================================================================
  2. Microsoft IIS Active Server Pages
  3. Microsoft Confidential.
  4. Copyright 1997 Microsoft Corporation. All Rights Reserved.
  5. Component: Registry stuff
  6. File: reg.cpp
  7. Owner: AndrewS/LeiJin
  8. ===================================================================*/
  9. #include "denpre.h"
  10. #pragma hdrstop
  11. #if _IIS_5_1
  12. #include <iadm.h>
  13. #elif _IIS_6_0
  14. #include <iadmw.h>
  15. #else
  16. #error "Neither _IIS_6_0 nor _IIS_5_1 is defined"
  17. #endif
  18. #include "comadmin.h"
  19. #include "dlldatax.h"
  20. #include "memchk.h"
  21. #include "Accctrl.h"
  22. #include "aclapi.h"
  23. #include "iiscnfg.h"
  24. //External functions, defined in glob.cpp
  25. extern HRESULT MDRegisterProperties(void);
  26. extern HRESULT MDUnRegisterProperties(void);
  27. // Globals
  28. const REGSAM samDesired = KEY_READ | KEY_WRITE;
  29. HRESULT RegisterASPProperties(BOOL fReg = TRUE);
  30. /*
  31. * Info about our intrinsics used by Register & UnRegister
  32. */
  33. const char *szClassDesc[] = { "ASP Response Object",
  34. "ASP Request Object",
  35. "ASP Request Dictionary",
  36. "ASP Server Object",
  37. "ASP Application Object",
  38. "ASP Session Object",
  39. "ASP String List Object",
  40. "ASP Read Cookie",
  41. "ASP Write Cookie",
  42. "ASP Scripting Context Object",
  43. "ASP Certificate Object",
  44. };
  45. const char *szCLSIDEntry[] = { "CLSID\\{D97A6DA0-A864-11cf-83BE-00A0C90C2BD8}", // IResponse
  46. "CLSID\\{D97A6DA0-A861-11cf-93AE-00A0C90C2BD8}", // IRequest
  47. "CLSID\\{D97A6DA0-A85F-11df-83AE-00A0C90C2BD8}", // IRequestDictionary
  48. "CLSID\\{D97A6DA0-A867-11cf-83AE-01A0C90C2BD8}", // IServer
  49. "CLSID\\{D97A6DA0-A866-11cf-83AE-10A0C90C2BD8}", // IApplicationObject
  50. "CLSID\\{D97A6DA0-A865-11cf-83AF-00A0C90C2BD8}", // ISessionObject
  51. "CLSID\\{D97A6DA0-A85D-11cf-83AE-00A0C90C2BD8}", // IStringList
  52. "CLSID\\{71EAF260-0CE0-11d0-A53E-00A0C90C2091}", // IReadCookie
  53. "CLSID\\{D97A6DA0-A862-11cf-84AE-00A0C90C2BD8}", // IWriteCookie
  54. "CLSID\\{D97A6DA0-A868-11cf-83AE-00B0C90C2BD8}", // IScriptingContext
  55. "CLSID\\{b3192190-1176-11d0-8ce8-00aa006c400c}", // ICertificate
  56. };
  57. const cClassesMax = sizeof(szCLSIDEntry) / sizeof(char *);
  58. /*===================================================================
  59. RegisterIntrinsics
  60. Register info about our intrinsics in the registry.
  61. Returns:
  62. HRESULT - S_OK on success
  63. Side effects:
  64. Registers denali objects in the registry
  65. ===================================================================*/
  66. HRESULT RegisterIntrinsics(void)
  67. {
  68. static const char szDenaliDLL[] = "asp.DLL";
  69. static const char szThreadingModel[] = "ThreadingModel";
  70. static const char szInprocServer32[] = "InprocServer32";
  71. static const char szFreeThreaded[] = "Both";
  72. static const char szProgIdKey[] = "ProgId";
  73. static const char szCLSIDKey[] = "CLSID";
  74. HRESULT hr = S_OK;
  75. char szPath[MAX_PATH];
  76. char *pch;
  77. HKEY hkeyCLSID = NULL;
  78. HKEY hkeyT = NULL;
  79. HKEY hkey2;
  80. DWORD iClass;
  81. // Get the path and name of Denali
  82. if (!GetModuleFileNameA(g_hinstDLL, szPath, sizeof(szPath)/sizeof(char)))
  83. return E_FAIL;
  84. // bug fix 102010 DBCS fixes
  85. //
  86. //for (pch = szPath + lstrlen(szPath); pch > szPath && *pch != TEXT('\\'); pch--)
  87. // ;
  88. //if (pch == szPath)
  89. pch = (char*) _mbsrchr((const unsigned char*)szPath, '\\');
  90. if (pch == NULL)
  91. {
  92. Assert(FALSE);
  93. goto LErrExit;
  94. }
  95. strcpy(pch + 1, szDenaliDLL);
  96. for (iClass = 0; iClass < cClassesMax; iClass++)
  97. {
  98. // install the CLSID key
  99. // Setting the value of the description creates the key for the clsid
  100. if ((RegSetValueA(HKEY_CLASSES_ROOT, szCLSIDEntry[iClass], REG_SZ, szClassDesc[iClass],
  101. strlen(szClassDesc[iClass])) != ERROR_SUCCESS))
  102. goto LErrExit;
  103. // Open the CLSID key so we can set values on it
  104. if (RegOpenKeyExA(HKEY_CLASSES_ROOT, szCLSIDEntry[iClass], 0, samDesired, &hkeyCLSID) != ERROR_SUCCESS)
  105. goto LErrExit;
  106. // install the InprocServer32 key and open the sub-key to set the named value
  107. if ((RegSetValueA(hkeyCLSID, szInprocServer32, REG_SZ, szPath, strlen(szPath)) != ERROR_SUCCESS))
  108. goto LErrExit;
  109. if ((RegOpenKeyExA(hkeyCLSID, szInprocServer32, 0, samDesired, &hkeyT) != ERROR_SUCCESS))
  110. goto LErrExit;
  111. // install the ThreadingModel named value
  112. if (RegSetValueExA(hkeyT, szThreadingModel, 0, REG_SZ, (const BYTE *)szFreeThreaded,
  113. (strlen(szFreeThreaded)+1) * sizeof(char)) != ERROR_SUCCESS)
  114. goto LErrExit;
  115. if (RegCloseKey(hkeyT) != ERROR_SUCCESS)
  116. goto LErrExit;
  117. hkeyT = NULL;
  118. RegCloseKey(hkeyCLSID);
  119. hkeyCLSID = NULL;
  120. }
  121. return hr;
  122. LErrExit:
  123. RegCloseKey(hkeyT);
  124. RegCloseKey(hkeyCLSID);
  125. return E_FAIL;
  126. }
  127. /*===================================================================
  128. UnRegisterKey
  129. Given a string which is the name of a key under HKEY_CLASSES_ROOT,
  130. delete everything under that key and the key itself from the registry
  131. (why the heck isnt there an API that does this!?!?)
  132. Returns:
  133. HRESULT - S_OK on success
  134. Side effects:
  135. Removes a key & all subkeys from the registry
  136. ===================================================================*/
  137. HRESULT UnRegisterKey(CHAR *szKey)
  138. {
  139. HKEY hkey = NULL;
  140. CHAR szKeyName[255];
  141. DWORD cbKeyName;
  142. LONG errT;
  143. // Open the HKEY_CLASSES_ROOT\CLSID\{...} key so we can delete its subkeys
  144. if (RegOpenKeyExA(HKEY_CLASSES_ROOT, szKey, 0, samDesired, &hkey) != ERROR_SUCCESS)
  145. goto LErrExit;
  146. // Enumerate all its subkeys, and delete them
  147. while (TRUE)
  148. {
  149. cbKeyName = sizeof(szKeyName);
  150. if ((errT = RegEnumKeyExA(hkey, 0, szKeyName, &cbKeyName, 0, NULL, 0, NULL)) != ERROR_SUCCESS)
  151. break;
  152. if ((errT = RegDeleteKeyA(hkey, szKeyName)) != ERROR_SUCCESS)
  153. goto LErrExit;
  154. }
  155. // Close the key, and then delete it
  156. if ((errT = RegCloseKey(hkey)) != ERROR_SUCCESS)
  157. return(E_FAIL);
  158. if ((errT = RegDeleteKeyA(HKEY_CLASSES_ROOT, szKey)) != ERROR_SUCCESS)
  159. {
  160. DBGPRINTF((DBG_CONTEXT, "Deleting key %s returned %d\n",
  161. szKey, GetLastError()));
  162. return(E_FAIL);
  163. }
  164. return S_OK;
  165. LErrExit:
  166. RegCloseKey(hkey);
  167. return E_FAIL;
  168. }
  169. /*===================================================================
  170. UnRegisterIntrinsics
  171. UnRegister the info about our intrinsics from the registry.
  172. Returns:
  173. HRESULT - S_OK on success
  174. Side effects:
  175. Removes denali objects from the registry
  176. ===================================================================*/
  177. HRESULT UnRegisterIntrinsics(void)
  178. {
  179. HRESULT hr = S_OK, hrT;
  180. DWORD iClass;
  181. // Now delete the keys for the objects
  182. for (iClass = 0; iClass < cClassesMax; iClass++)
  183. {
  184. // Open the HKEY_CLASSES_ROOT\CLSID\{...} key so we can delete its subkeys
  185. if (FAILED(hrT = UnRegisterKey((CHAR *)szCLSIDEntry[iClass])))
  186. hr = hrT; // Hold onto the error, but keep going
  187. }
  188. return hr;
  189. }
  190. /*===================================================================
  191. RegisterTypeLib
  192. Register denali typelib in the registry.
  193. Returns:
  194. HRESULT - S_OK on success
  195. Side effects:
  196. register denali typelib in the registry
  197. ===================================================================*/
  198. HRESULT RegisterTypeLib(void)
  199. {
  200. HRESULT hr;
  201. ITypeLib *pITypeLib = NULL;
  202. char szFile[MAX_PATH + 4];
  203. BSTR bstrFile;
  204. // Get the path and name of Denali
  205. if (!GetModuleFileNameA(g_hinstDLL, szFile, sizeof(szFile)/sizeof(char)))
  206. return E_FAIL;
  207. // There are two type libraries: First the standard ASP typelib
  208. // then the typelib for the Transacted Script Context object.
  209. // Load them both.
  210. // First type lib, from default (first ITypeLib entry) location
  211. hr = SysAllocStringFromSz(szFile, 0, &bstrFile);
  212. if (FAILED(hr))
  213. return hr;
  214. hr = LoadTypeLibEx(bstrFile, REGKIND_REGISTER, &pITypeLib);
  215. if (pITypeLib)
  216. {
  217. pITypeLib->Release();
  218. pITypeLib = NULL;
  219. }
  220. SysFreeString(bstrFile);
  221. return hr;
  222. }
  223. /*===================================================================
  224. UnRegisterTypeLib
  225. UnRegister denali typelib in the registry. Note: Only the current version used by asp.dll is removed.
  226. Returns:
  227. HRESULT - S_OK on success
  228. Side effects:
  229. unregister denali typelib in the registry
  230. ===================================================================*/
  231. HRESULT UnRegisterTypeLib(void)
  232. {
  233. HRESULT hr;
  234. ITypeLib *pITypeLib = NULL;
  235. TLIBATTR *pTLibAttr = NULL;
  236. char szFile[MAX_PATH + 4];
  237. BSTR bstrFile;
  238. // Get the path and name of Denali
  239. if (!GetModuleFileNameA(g_hinstDLL, szFile, sizeof(szFile)/sizeof(char)))
  240. return E_FAIL;
  241. hr = SysAllocStringFromSz(szFile, 0, &bstrFile);
  242. if (FAILED(hr))
  243. return hr;
  244. hr = LoadTypeLibEx(bstrFile, REGKIND_REGISTER, &pITypeLib);
  245. if(SUCCEEDED(hr) && pITypeLib)
  246. {
  247. hr = pITypeLib->GetLibAttr(&pTLibAttr);
  248. if(SUCCEEDED(hr) && pTLibAttr)
  249. {
  250. hr = UnRegisterTypeLib( pTLibAttr->guid,
  251. pTLibAttr->wMajorVerNum,
  252. pTLibAttr->wMinorVerNum,
  253. pTLibAttr->lcid,
  254. pTLibAttr->syskind);
  255. pITypeLib->ReleaseTLibAttr(pTLibAttr);
  256. pTLibAttr = NULL;
  257. }
  258. pITypeLib->Release();
  259. pITypeLib = NULL;
  260. }
  261. SysFreeString(bstrFile);
  262. return hr;
  263. }
  264. HRESULT GetIWAMAccountName(LPSTR pszIdentity,
  265. DWORD cbIdentity)
  266. {
  267. HRESULT hr = S_OK;
  268. IMSAdminBase *pMetabase = NULL;
  269. METADATA_HANDLE hMetabase = NULL;
  270. METADATA_RECORD recMetaData;
  271. DWORD dwRequiredLen;
  272. CWCharToMBCS convStr;
  273. MD_SET_DATA_RECORD( &recMetaData,
  274. MD_WAM_USER_NAME,
  275. METADATA_NO_ATTRIBUTES,
  276. IIS_MD_UT_WAM,
  277. STRING_METADATA,
  278. cbIdentity,
  279. pszIdentity);
  280. if (FAILED(hr = CoCreateInstance(CLSID_MSAdminBase,
  281. NULL,
  282. CLSCTX_SERVER,
  283. IID_IMSAdminBase,
  284. (void **)&pMetabase)));
  285. // Open key to the Web service, and get a handle of \LM\w3svc
  286. else if (FAILED(hr = pMetabase->OpenKey(METADATA_MASTER_ROOT_HANDLE,
  287. (LPWSTR)(L"\\LM\\W3SVC"),
  288. METADATA_PERMISSION_READ | METADATA_PERMISSION_WRITE,
  289. 2000,
  290. &hMetabase)));
  291. // get the data. This will return the IWAM username in the caller
  292. // supplied buffer. The name will be UNICODE.
  293. else if (FAILED(hr = pMetabase->GetData(hMetabase,
  294. NULL,
  295. &recMetaData,
  296. &dwRequiredLen)));
  297. // convert the string to MBCS using the CP_ACP
  298. else if (FAILED(hr = convStr.Init((LPWSTR)pszIdentity)));
  299. // overwrite the caller's memory with the converted string
  300. else strcpy(pszIdentity, convStr.GetString());
  301. // cleanup
  302. if (hMetabase && pMetabase)
  303. pMetabase->CloseKey(hMetabase);
  304. if (pMetabase)
  305. pMetabase->Release();
  306. return(hr);
  307. }
  308. HRESULT CreateCompiledTemplatesTempDir()
  309. {
  310. HRESULT hr = S_OK;
  311. BYTE szRegString[MAX_PATH];
  312. BYTE pszExpanded[MAX_PATH];
  313. int result = 0;
  314. EXPLICIT_ACCESSA ea[3];
  315. PACL pNewDACL = NULL;
  316. char szWamIdentity[1024];
  317. SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
  318. // read the temp dir name for the persistant templ
  319. // cache.
  320. CchLoadStringOfId(IDS_DEFAULTPERSISTDIR, (LPSTR)szRegString, MAX_PATH);
  321. result = ExpandEnvironmentStringsA((LPCSTR)szRegString,
  322. (LPSTR)pszExpanded,
  323. MAX_PATH);
  324. if ((result <= MAX_PATH) && (result > 0)) {
  325. CreateDirectoryA((LPCSTR)pszExpanded,NULL);
  326. }
  327. // this next section of code will place the SYSTEM and IWAM_<ComputerName>
  328. // ACEs on the directorie's ACL
  329. ZeroMemory(ea, sizeof(EXPLICIT_ACCESSA) * 3);
  330. ea[0].grfAccessPermissions = SYNCHRONIZE | GENERIC_ALL;
  331. ea[0].grfAccessMode = GRANT_ACCESS;
  332. ea[0].grfInheritance= SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  333. ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  334. ea[1].grfAccessPermissions = SYNCHRONIZE | GENERIC_ALL;
  335. ea[1].grfAccessMode = GRANT_ACCESS;
  336. ea[1].grfInheritance= SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  337. ea[1].Trustee.TrusteeForm = TRUSTEE_IS_NAME;
  338. ea[1].Trustee.ptstrName = szWamIdentity;
  339. ea[2].grfAccessPermissions = SYNCHRONIZE | GENERIC_ALL;
  340. ea[2].grfAccessMode = GRANT_ACCESS;
  341. ea[2].grfInheritance= SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  342. ea[2].Trustee.TrusteeForm = TRUSTEE_IS_SID;
  343. // go get the IWAM account name from the metabase
  344. if (FAILED(hr = GetIWAMAccountName(szWamIdentity, sizeof(szWamIdentity))));
  345. // build the new DACL with just these ACEs
  346. else if (!AllocateAndInitializeSid(&NtAuthority,
  347. 1,
  348. SECURITY_LOCAL_SYSTEM_RID,
  349. 0,0,0,0,0,0,0,
  350. (PSID *)(&ea[0].Trustee.ptstrName)))
  351. hr = HRESULT_FROM_WIN32(GetLastError());
  352. else if (!AllocateAndInitializeSid(&NtAuthority,
  353. 2, // 2 sub-authorities
  354. SECURITY_BUILTIN_DOMAIN_RID,
  355. DOMAIN_ALIAS_RID_ADMINS,
  356. 0,0,0,0,0,0,
  357. (PSID *)(&ea[2].Trustee.ptstrName)))
  358. hr = HRESULT_FROM_WIN32(GetLastError());
  359. else if ((hr = SetEntriesInAclA(3,
  360. ea,
  361. NULL,
  362. &pNewDACL)) != ERROR_SUCCESS);
  363. // set the ACL on the directory
  364. else hr = SetNamedSecurityInfoA((LPSTR)pszExpanded,
  365. SE_FILE_OBJECT,
  366. DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION,
  367. NULL,
  368. NULL,
  369. pNewDACL,
  370. NULL);
  371. if (pNewDACL)
  372. LocalFree(pNewDACL);
  373. if (ea[0].Trustee.ptstrName)
  374. FreeSid(ea[0].Trustee.ptstrName);
  375. if (ea[2].Trustee.ptstrName)
  376. FreeSid(ea[2].Trustee.ptstrName);
  377. return(hr);
  378. }
  379. /*===================================================================
  380. DllRegisterServer
  381. Entry point used by RegSvr32.exe to register the DLL.
  382. Returns:
  383. HRESULT - S_OK on success
  384. Side effects:
  385. Registers denali objects in the registry
  386. ===================================================================*/
  387. STDAPI DllRegisterServer(void)
  388. {
  389. HRESULT hr;
  390. HRESULT hrCoInit;
  391. hrCoInit = CoInitialize(NULL);
  392. // First try to unregister some stuff
  393. // This is important when we are registering on top of
  394. // an old IIS 3.0 Denali registration
  395. // Don't care if fails
  396. UnRegisterEventLog();
  397. UnRegisterIntrinsics();
  398. UnRegisterTypeLib();
  399. // Now do the registration
  400. if (FAILED(hr = MDRegisterProperties()))
  401. goto LErr;
  402. // Register NT event log
  403. if(FAILED(hr = RegisterEventLog()))
  404. goto LErr;
  405. if (FAILED(hr = RegisterTypeLib()))
  406. goto LErr;
  407. // Register our intrinsics
  408. if (FAILED(hr = RegisterIntrinsics()))
  409. goto LErr;
  410. if(FAILED(hr = RegisterASPProperties(TRUE)))
  411. goto LErr;
  412. if (FAILED(hr = CreateCompiledTemplatesTempDir()))
  413. goto LErr;
  414. LErr:
  415. if (SUCCEEDED(hrCoInit))
  416. CoUninitialize();
  417. return(hr);
  418. }
  419. /*===================================================================
  420. DllUnregisterServer
  421. Entry point used by RegSvr32.exe to unregister the DLL.
  422. Returns:
  423. HRESULT - S_OK on success
  424. Side effects:
  425. Removes denali registrations from the registry
  426. ===================================================================*/
  427. STDAPI DllUnregisterServer(void)
  428. {
  429. HRESULT hr = S_OK, hrT;
  430. HRESULT hrCoInit;
  431. hrCoInit = CoInitialize(NULL);
  432. hrT = UnRegisterEventLog();
  433. if (FAILED(hrT))
  434. hr = hrT;
  435. hrT = MDUnRegisterProperties();
  436. if (FAILED(hrT))
  437. hr = hrT;
  438. hrT = UnRegisterIntrinsics();
  439. if (FAILED(hrT))
  440. hr = hrT;
  441. hrT = UnRegisterTypeLib();
  442. if (FAILED(hrT))
  443. hr = hrT;
  444. hrT = RegisterASPProperties(FALSE);
  445. if (FAILED(hrT))
  446. hr = hrT;
  447. // UNDONE BUG 80063: Ignore errors from this call
  448. #ifdef UNDONE
  449. if (FAILED(hrT))
  450. hr = hrT;
  451. #endif
  452. if (SUCCEEDED(hrCoInit))
  453. CoUninitialize();
  454. return(hr);
  455. }
  456. /*===================================================================
  457. RegisterASPProperties
  458. Entry point used by RegSvr32.exe to register and unregister
  459. ASP setting stored in the SYSTEM registry.
  460. Parameters
  461. fReg = TRUE - to register
  462. FALSE - to unregister
  463. Returns:
  464. HRESULT - S_OK on success
  465. Side effects:
  466. Removes denali registrations from the registry
  467. ===================================================================*/
  468. HRESULT RegisterASPProperties(BOOL fReg)
  469. {
  470. HKEY hkey1 = NULL, hkey2 = NULL;
  471. DWORD iValue;
  472. LONG lT;
  473. static const char szW3SVC[] = "System\\CurrentControlSet\\Services\\W3SVC";
  474. static const char szW3SVCASP[] = "System\\CurrentControlSet\\Services\\W3SVC\\ASP\\Parameters";
  475. static const char szASPParm[] = "ASP\\Parameters";
  476. BYTE szDefailtString[1024];
  477. // Open the key for W3SVCASP so we can add denali under it if it does not exist
  478. //
  479. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, szW3SVCASP, 0, samDesired, &hkey2) != ERROR_SUCCESS)
  480. {
  481. // if the key does not exits and you call with FALSE the exit with error
  482. //
  483. if (!fReg)
  484. goto LErrExit;
  485. // Open the key for W3SVC so we can add denali under it
  486. //
  487. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, szW3SVC, 0, samDesired, &hkey1) != ERROR_SUCCESS)
  488. {
  489. MSG_Error(IDS_IIS_NOTINSTALLED);
  490. return(E_FAIL);
  491. }
  492. // Add the key for Denali\Parameters
  493. //
  494. if (RegCreateKeyA(hkey1, szASPParm, &hkey2) != ERROR_SUCCESS)
  495. return(E_FAIL);
  496. }
  497. // release the extra key
  498. //
  499. if (hkey1)
  500. RegCloseKey(hkey1);
  501. hkey1 = hkey2;
  502. // set whatever is needed under hkey1
  503. // (currently nothing)
  504. // done clean up and exit
  505. if (RegCloseKey(hkey1) != ERROR_SUCCESS)
  506. return(E_FAIL);
  507. return S_OK;
  508. LErrExit:
  509. RegCloseKey(hkey1);
  510. return E_FAIL;
  511. }