Elevator.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // Elevator.cpp : Implementation of WinMain
  2. #include "stdafx.h"
  3. #include "resource1.h"
  4. #include "FileTypeRegistrar.h"
  5. #include "ElevatorFactory.h"
  6. #include <sddl.h>
  7. #include <strsafe.h>
  8. HRESULT RegisterServer(HINSTANCE hInstance);
  9. HRESULT UnregisterServer(HINSTANCE hInstance);
  10. DWORD g_allLocks = 0;
  11. int APIENTRY WinMain(HINSTANCE hInstance,
  12. HINSTANCE hPrevInstance,
  13. LPSTR lpCmdLine,
  14. int nCmdShow)
  15. {
  16. if (lpCmdLine == NULL || !*lpCmdLine)
  17. {
  18. MSGBOXPARAMSW msgbx = {sizeof(MSGBOXPARAMS),0};
  19. msgbx.lpszText = L"Winamp Elevator\nCopyright © 2008-2014 Winamp SA";
  20. msgbx.lpszCaption = L"About...";
  21. msgbx.lpszIcon = MAKEINTRESOURCEW(IDI_ICON1);
  22. msgbx.hInstance = GetModuleHandle(0);
  23. msgbx.dwStyle = MB_USERICON;
  24. MessageBoxIndirectW(&msgbx);
  25. return 0;
  26. }
  27. if ( lpCmdLine != NULL && ( _strcmpi( lpCmdLine, "/RegServer" ) == 0
  28. || _strcmpi( lpCmdLine, "-RegServer" ) == 0 )) {
  29. RegisterServer(hInstance);
  30. return 0;
  31. }
  32. if ( lpCmdLine != NULL && ( _strcmpi( lpCmdLine, "/UnregServer" ) == 0
  33. || _strcmpi( lpCmdLine, "-UnregServer" ) == 0 )) {
  34. UnregisterServer(hInstance);
  35. return 0;
  36. }
  37. CoInitialize(NULL);
  38. if(lpCmdLine && (strstr(lpCmdLine, "/Embedding") || strstr(lpCmdLine, "-Embedding")))
  39. {
  40. ElevatorFactory cf;
  41. DWORD regID = 0;
  42. CoRegisterClassObject(CLSID_WFileTypeRegistrar,(IClassFactory*)&cf, CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &regID);
  43. MSG ms;
  44. while(GetMessage(&ms, 0, 0, 0))
  45. {
  46. TranslateMessage(&ms);
  47. DispatchMessage(&ms);
  48. }
  49. CoRevokeClassObject(regID);
  50. }
  51. CoUninitialize();
  52. return 0;
  53. }
  54. extern FileTypeRegistrar registrar;
  55. void Lock()
  56. {
  57. ++g_allLocks;
  58. }
  59. void UnLock()
  60. {
  61. --g_allLocks;
  62. if(g_allLocks == 0 )//&& registrar.refCount == 0/* benski> a hack, for now */)
  63. {
  64. PostQuitMessage(0);
  65. }
  66. }
  67. static BOOL GetAccessPermissionsForLUAServer(SECURITY_DESCRIPTOR **ppSD)
  68. {
  69. // Local call permissions to IU, SY
  70. LPWSTR lpszSDDL = L"O:BAG:BAD:(A;;0x3;;;IU)(A;;0x3;;;SY)";
  71. SECURITY_DESCRIPTOR *pSD;
  72. *ppSD = NULL;
  73. if (ConvertStringSecurityDescriptorToSecurityDescriptorW(lpszSDDL, SDDL_REVISION_1, (PSECURITY_DESCRIPTOR *)&pSD, NULL))
  74. {
  75. *ppSD = pSD;
  76. return TRUE;
  77. }
  78. return FALSE;
  79. }
  80. // hKey is the HKCR\AppID\{GUID} key
  81. static BOOL SetAccessPermissions(HKEY hkey, PSECURITY_DESCRIPTOR pSD)
  82. {
  83. BOOL bResult = FALSE;
  84. DWORD dwLen = GetSecurityDescriptorLength(pSD);
  85. LONG lResult;
  86. lResult = RegSetValueExA(hkey,
  87. "AccessPermission",
  88. 0,
  89. REG_BINARY,
  90. (BYTE*)pSD,
  91. dwLen);
  92. if (lResult != ERROR_SUCCESS) goto done;
  93. bResult = TRUE;
  94. done:
  95. return bResult;
  96. }
  97. static BOOL GetLaunchActPermissionsWithIL (SECURITY_DESCRIPTOR **ppSD)
  98. {
  99. // Allow World Local Launch/Activation permissions. Label the SD for LOW IL Execute UP
  100. LPWSTR lpszSDDL = L"O:BAG:BAD:(A;;0xb;;;WD)S:(ML;;NX;;;LW)";
  101. SECURITY_DESCRIPTOR *pSD;
  102. if (ConvertStringSecurityDescriptorToSecurityDescriptorW(lpszSDDL, SDDL_REVISION_1, (PSECURITY_DESCRIPTOR *)&pSD, NULL))
  103. {
  104. *ppSD = pSD;
  105. return TRUE;
  106. }
  107. return FALSE;
  108. }
  109. static BOOL SetLaunchActPermissions(HKEY hkey, PSECURITY_DESCRIPTOR pSD)
  110. {
  111. BOOL bResult = FALSE;
  112. DWORD dwLen = GetSecurityDescriptorLength(pSD);
  113. LONG lResult;
  114. lResult = RegSetValueExA(hkey,
  115. "LaunchPermission",
  116. 0,
  117. REG_BINARY,
  118. (BYTE*)pSD,
  119. dwLen);
  120. if (lResult != ERROR_SUCCESS) goto done;
  121. bResult = TRUE;
  122. done:
  123. return bResult;
  124. };
  125. static HRESULT UnregisterComponent(const CLSID &clsid, LPCWSTR pszVersionIndProgId, LPCWSTR pszProgId);
  126. static HRESULT RegisterComponent(HMODULE hModule, const CLSID &clsid, LPCWSTR pszFriendlyName, LPCWSTR pszVersionIndProgId, LPCWSTR pszProgId);
  127. static const WCHAR szComponentFriendlyName[] = L"Winamp Elevator";
  128. static const WCHAR szVersionIndependentProgId[] = L"Elevator.WFileTypeRegistrar2";
  129. static const WCHAR szProgId[] = L"Elevator.WFileTypeRegistrar2.1";
  130. HRESULT RegisterServer(HINSTANCE hInstance)
  131. {
  132. HRESULT hr(S_OK);
  133. hr = RegisterComponent(hInstance, CLSID_WFileTypeRegistrar, szComponentFriendlyName, szVersionIndependentProgId, szProgId);
  134. return hr;
  135. }
  136. HRESULT UnregisterServer(HINSTANCE hInstance)
  137. {
  138. HRESULT hr(S_OK);
  139. hr = UnregisterComponent(CLSID_WFileTypeRegistrar, szVersionIndependentProgId, szProgId);
  140. return hr;
  141. }
  142. static BOOL WriteRegKey(HKEY hKeyParent, LPCWSTR pszKey, LPCWSTR pszValue, HKEY *phKey = NULL)
  143. {
  144. HKEY hKey;
  145. LONG result;
  146. result = RegCreateKeyExW(hKeyParent, pszKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
  147. if (ERROR_SUCCESS != result || !hKey) return FALSE;
  148. if (pszValue)
  149. {
  150. result = RegSetValueExW(hKey, NULL, 0, REG_SZ, (BYTE*)pszValue, (DWORD)(sizeof(wchar_t)*(1 + lstrlenW(pszValue))));
  151. }
  152. if (!phKey) RegCloseKey(hKey);
  153. else *phKey = hKey;
  154. return (ERROR_SUCCESS == result);
  155. }
  156. static BOOL WriteRegValue(HKEY hKeyParent, LPCWSTR pszKey, LPCWSTR pszEntry, LPCWSTR pszValue)
  157. {
  158. HKEY hKey;
  159. LONG result;
  160. result = RegOpenKeyExW(hKeyParent, pszKey, 0, KEY_SET_VALUE, &hKey);
  161. if (ERROR_SUCCESS != result || !hKey) return FALSE;
  162. if (pszValue)
  163. {
  164. result = RegSetValueEx(hKey, pszEntry, 0, REG_SZ, (BYTE*)pszValue, (DWORD)(sizeof(wchar_t)*(1 + lstrlenW(pszValue))));
  165. }
  166. RegCloseKey(hKey);
  167. return (ERROR_SUCCESS == result);
  168. }
  169. static BOOL WriteRegValue(HKEY hKeyParent, LPCWSTR pszKey, LPCWSTR pszEntry, DWORD pszValue)
  170. {
  171. HKEY hKey;
  172. LONG result;
  173. result = RegOpenKeyExW(hKeyParent, pszKey, 0, KEY_SET_VALUE, &hKey);
  174. if (ERROR_SUCCESS != result || !hKey) return FALSE;
  175. if (pszValue)
  176. {
  177. result = RegSetValueEx(hKey, pszEntry, 0, REG_DWORD, (BYTE*)&pszValue, sizeof(DWORD));
  178. }
  179. RegCloseKey(hKey);
  180. return (ERROR_SUCCESS == result);
  181. }
  182. static LONG DeleteRegKey(HKEY hKeyParent, LPCWSTR pszKey)
  183. {
  184. HKEY hKey;
  185. LONG result;
  186. FILETIME time = {0};
  187. WCHAR szBuffer[512] = {0};
  188. DWORD dwSize = sizeof(szBuffer)/sizeof(WCHAR);
  189. result = RegOpenKeyExW(hKeyParent, pszKey, 0, KEY_SET_VALUE | KEY_ENUMERATE_SUB_KEYS , &hKey);
  190. if (ERROR_SUCCESS != result) return result;
  191. while (ERROR_SUCCESS == RegEnumKeyExW(hKey, 0, szBuffer, &dwSize, NULL, NULL, NULL, &time))
  192. {
  193. if (ERROR_SUCCESS != (result = DeleteRegKey(hKey, szBuffer)))
  194. {
  195. RegCloseKey(hKey);
  196. return result;
  197. }
  198. dwSize = sizeof(szBuffer)/sizeof(WCHAR);
  199. }
  200. RegCloseKey(hKey);
  201. return RegDeleteKeyW(hKeyParent, pszKey);
  202. }
  203. HRESULT RegisterComponent(HMODULE hModule, const CLSID &clsid, LPCWSTR pszFriendlyName, LPCWSTR pszVersionIndProgId, LPCWSTR pszProgId)
  204. {
  205. SECURITY_DESCRIPTOR *sd;
  206. HKEY hKey, hKey2, hKey3;
  207. BOOL br;
  208. WCHAR szBuffer[MAX_PATH] = {0}, szCLSID[64] = {0};
  209. if (!StringFromGUID2(clsid, szCLSID, sizeof(szCLSID)/sizeof(WCHAR))) return E_OUTOFMEMORY;
  210. StringCchPrintfW(szBuffer, sizeof(szBuffer)/sizeof(WCHAR), L"SOFTWARE\\Classes\\CLSID\\%s", szCLSID);
  211. if (!WriteRegKey(HKEY_LOCAL_MACHINE, szBuffer, pszFriendlyName, &hKey)) return S_FALSE;
  212. if (!GetModuleFileNameW(hModule, szBuffer, sizeof(szBuffer)/sizeof(WCHAR))) return S_FALSE;
  213. RegCreateKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Classes\\AppID\\{3B29AB5C-52CB-4a36-9314-E3FEE0BA7468}", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey3, NULL);
  214. if (GetAccessPermissionsForLUAServer(&sd))
  215. SetAccessPermissions(hKey3, sd);
  216. if (GetLaunchActPermissionsWithIL(&sd))
  217. SetLaunchActPermissions(hKey3, sd);
  218. WriteRegValue(hKey3, NULL, NULL, szBuffer);
  219. RegCloseKey(hKey3);
  220. RegCreateKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Classes\\AppID\\elevator.exe", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey3, NULL);
  221. WriteRegValue(hKey3, NULL, L"AppID", L"{3B29AB5C-52CB-4a36-9314-E3FEE0BA7468}");
  222. RegCloseKey(hKey3);
  223. wchar_t localizedString[MAX_PATH+15] = {0};
  224. StringCbPrintf(localizedString, sizeof(localizedString), L"@%s,-%u", szBuffer, IDS_WINAMP);
  225. br = (WriteRegKey(hKey, L"LocalServer32" , szBuffer, &hKey2) &&
  226. WriteRegValue(hKey2, NULL, L"ThreadingModel", L"Both") &&
  227. WriteRegKey(hKey, L"ProgID", pszProgId) &&
  228. WriteRegKey(hKey, L"VersionIndependentProgID", pszVersionIndProgId)
  229. && WriteRegValue(hKey, NULL, L"LocalizedString", localizedString)
  230. && WriteRegValue(hKey, NULL, L"AppId", L"{3B29AB5C-52CB-4a36-9314-E3FEE0BA7468}")
  231. && WriteRegKey(hKey, L"Elevation", 0)
  232. //&& WriteRegValue(hKey, L"Elevation", L"IconReference",
  233. && WriteRegValue(hKey, L"Elevation", L"Enabled", 1)
  234. );
  235. RegCloseKey(hKey);
  236. if (hKey2) RegCloseKey(hKey2);
  237. if (!br) return S_FALSE;
  238. if (!WriteRegKey(HKEY_CLASSES_ROOT, pszVersionIndProgId, pszFriendlyName, &hKey)) return S_FALSE;
  239. br = (WriteRegKey(hKey, L"CLSID", szCLSID) &&
  240. WriteRegKey(hKey, L"CurVer", pszProgId));
  241. RegCloseKey(hKey);
  242. if (!br) return S_FALSE;
  243. if (!WriteRegKey(HKEY_CLASSES_ROOT, pszProgId, pszFriendlyName, &hKey)) return S_FALSE;
  244. br = WriteRegKey(hKey, L"CLSID", szCLSID);
  245. RegCloseKey(hKey);
  246. if (!br) return S_FALSE;
  247. return S_OK;
  248. }
  249. static HRESULT UnregisterComponent(const CLSID &clsid, LPCWSTR pszVersionIndProgId, LPCWSTR pszProgId)
  250. {
  251. LONG result;
  252. WCHAR szBuffer[MAX_PATH] = {0}, szCLSID[64] = {0};
  253. if (!StringFromGUID2(clsid, szCLSID, sizeof(szCLSID)/sizeof(WCHAR))) return E_OUTOFMEMORY;
  254. StringCchPrintfW(szBuffer, sizeof(szBuffer)/sizeof(WCHAR), L"CLSID\\%s", szCLSID);
  255. result = DeleteRegKey(HKEY_CLASSES_ROOT, szBuffer);
  256. if (ERROR_SUCCESS != result && ERROR_FILE_NOT_FOUND != result) return S_FALSE;
  257. result = DeleteRegKey(HKEY_CLASSES_ROOT, pszVersionIndProgId);
  258. if (ERROR_SUCCESS != result && ERROR_FILE_NOT_FOUND != result) return S_FALSE;
  259. result = DeleteRegKey(HKEY_CLASSES_ROOT, pszProgId);
  260. if (ERROR_SUCCESS != result && ERROR_FILE_NOT_FOUND != result) return S_FALSE;
  261. return S_OK;
  262. }