aboutsummaryrefslogtreecommitdiff
path: root/Src/Elevator/Elevator.cpp
blob: 24e3ee9273eff325d2e44fb7c6b82bb90a1d54d0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Elevator.cpp : Implementation of WinMain

#include "stdafx.h"
#include "resource1.h"
#include "FileTypeRegistrar.h"
#include "ElevatorFactory.h"
#include <sddl.h>
#include <strsafe.h>

HRESULT RegisterServer(HINSTANCE hInstance);
HRESULT UnregisterServer(HINSTANCE hInstance);

DWORD g_allLocks = 0;
int APIENTRY WinMain(HINSTANCE hInstance,
										 HINSTANCE hPrevInstance,
										 LPSTR     lpCmdLine,
										 int       nCmdShow)
{
	if (lpCmdLine == NULL || !*lpCmdLine)
	{
		MSGBOXPARAMSW msgbx = {sizeof(MSGBOXPARAMS),0};
		msgbx.lpszText = L"Winamp Elevator\nCopyright © 2008-2014 Winamp SA";
		msgbx.lpszCaption = L"About...";
		msgbx.lpszIcon = MAKEINTRESOURCEW(IDI_ICON1);
		msgbx.hInstance = GetModuleHandle(0);
		msgbx.dwStyle = MB_USERICON;
		MessageBoxIndirectW(&msgbx);
		return 0;
	}

	if ( lpCmdLine != NULL && ( _strcmpi( lpCmdLine, "/RegServer" ) == 0 
		|| _strcmpi( lpCmdLine, "-RegServer" ) == 0 )) {
		RegisterServer(hInstance);
		return 0;
	}

	if ( lpCmdLine != NULL && ( _strcmpi( lpCmdLine, "/UnregServer" ) == 0 
		|| _strcmpi( lpCmdLine, "-UnregServer" ) == 0 )) {
		UnregisterServer(hInstance);
		return 0;
	}
	CoInitialize(NULL);    

	if(lpCmdLine && (strstr(lpCmdLine, "/Embedding") || strstr(lpCmdLine, "-Embedding")))
	{
		ElevatorFactory cf; 
		DWORD regID = 0;
		CoRegisterClassObject(CLSID_WFileTypeRegistrar,(IClassFactory*)&cf, CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &regID);
		MSG ms;
		while(GetMessage(&ms, 0, 0, 0))
		{
			TranslateMessage(&ms);
			DispatchMessage(&ms);
		}

		CoRevokeClassObject(regID);   
	}

	CoUninitialize();   
	return 0;
}

extern FileTypeRegistrar registrar;

void Lock()
{
	++g_allLocks;
}

void UnLock()
{
	--g_allLocks;
	if(g_allLocks == 0 )//&& registrar.refCount == 0/* benski> a hack, for now */) 
	{
		PostQuitMessage(0);
	}
}

static BOOL GetAccessPermissionsForLUAServer(SECURITY_DESCRIPTOR **ppSD)
{
// Local call permissions to IU, SY
    LPWSTR lpszSDDL = L"O:BAG:BAD:(A;;0x3;;;IU)(A;;0x3;;;SY)";
    SECURITY_DESCRIPTOR *pSD;
    *ppSD = NULL;

    if (ConvertStringSecurityDescriptorToSecurityDescriptorW(lpszSDDL, SDDL_REVISION_1, (PSECURITY_DESCRIPTOR *)&pSD, NULL))
    {
        *ppSD = pSD;
        return TRUE;
    }

    return FALSE;
}

// hKey is the HKCR\AppID\{GUID} key
static BOOL SetAccessPermissions(HKEY hkey, PSECURITY_DESCRIPTOR pSD)
{
    BOOL bResult = FALSE;
    DWORD dwLen = GetSecurityDescriptorLength(pSD);
    LONG lResult;
    lResult = RegSetValueExA(hkey, 
				"AccessPermission",
				0,
				REG_BINARY,
				(BYTE*)pSD,
				dwLen);
    if (lResult != ERROR_SUCCESS) goto done;
    bResult = TRUE;
done:
    return bResult;
}

static BOOL GetLaunchActPermissionsWithIL (SECURITY_DESCRIPTOR **ppSD)
{
	// Allow World Local Launch/Activation permissions. Label the SD for LOW IL Execute UP
	LPWSTR lpszSDDL = L"O:BAG:BAD:(A;;0xb;;;WD)S:(ML;;NX;;;LW)";
	SECURITY_DESCRIPTOR *pSD;
    if (ConvertStringSecurityDescriptorToSecurityDescriptorW(lpszSDDL, SDDL_REVISION_1, (PSECURITY_DESCRIPTOR *)&pSD, NULL))
    {
        *ppSD = pSD;
        return TRUE;
    }
	return FALSE;
}

static BOOL SetLaunchActPermissions(HKEY hkey, PSECURITY_DESCRIPTOR pSD)
{
    BOOL bResult = FALSE;
    DWORD dwLen = GetSecurityDescriptorLength(pSD);
    LONG lResult;
    lResult = RegSetValueExA(hkey, 
					"LaunchPermission",
					0,
					REG_BINARY,
					(BYTE*)pSD,
					dwLen);
    if (lResult != ERROR_SUCCESS) goto done;
    bResult = TRUE;
done:
    return bResult;
};

static HRESULT UnregisterComponent(const CLSID &clsid, LPCWSTR pszVersionIndProgId, LPCWSTR pszProgId);
static HRESULT RegisterComponent(HMODULE hModule, const CLSID &clsid, LPCWSTR pszFriendlyName, 	 LPCWSTR pszVersionIndProgId, LPCWSTR pszProgId);

static const WCHAR szComponentFriendlyName[]		= L"Winamp Elevator";
static const WCHAR szVersionIndependentProgId[]		= L"Elevator.WFileTypeRegistrar2";
static const WCHAR szProgId[]						= L"Elevator.WFileTypeRegistrar2.1";

HRESULT RegisterServer(HINSTANCE hInstance)
{
	HRESULT hr(S_OK);
	hr = RegisterComponent(hInstance, CLSID_WFileTypeRegistrar, szComponentFriendlyName,  szVersionIndependentProgId, szProgId);
	return hr;
}

HRESULT UnregisterServer(HINSTANCE hInstance)
{
	HRESULT hr(S_OK);
	hr = UnregisterComponent(CLSID_WFileTypeRegistrar, szVersionIndependentProgId, szProgId);
	return hr;
}

static BOOL WriteRegKey(HKEY hKeyParent, LPCWSTR pszKey, LPCWSTR pszValue, HKEY *phKey = NULL)
{
	HKEY hKey;
	LONG result;
	result = RegCreateKeyExW(hKeyParent, pszKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
	if (ERROR_SUCCESS != result || !hKey) return FALSE;
	if (pszValue) 
	{		
		result = RegSetValueExW(hKey, NULL, 0, REG_SZ, (BYTE*)pszValue, (DWORD)(sizeof(wchar_t)*(1 + lstrlenW(pszValue))));
	}
	if (!phKey) RegCloseKey(hKey);
	else *phKey = hKey;

	return (ERROR_SUCCESS == result);
}

static BOOL WriteRegValue(HKEY hKeyParent, LPCWSTR pszKey, LPCWSTR pszEntry, LPCWSTR pszValue)
{
	HKEY hKey;
	LONG result;
	result = RegOpenKeyExW(hKeyParent, pszKey, 0, KEY_SET_VALUE, &hKey);
	if (ERROR_SUCCESS != result || !hKey) return FALSE;
	if (pszValue) 
	{		
		result = RegSetValueEx(hKey, pszEntry, 0, REG_SZ, (BYTE*)pszValue, (DWORD)(sizeof(wchar_t)*(1 + lstrlenW(pszValue))));
	}
	RegCloseKey(hKey);
	return (ERROR_SUCCESS == result);
}

static BOOL WriteRegValue(HKEY hKeyParent, LPCWSTR pszKey, LPCWSTR pszEntry, DWORD pszValue)
{
	HKEY hKey;
	LONG result;
	result = RegOpenKeyExW(hKeyParent, pszKey, 0, KEY_SET_VALUE, &hKey);
	if (ERROR_SUCCESS != result || !hKey) return FALSE;
	if (pszValue) 
	{		
		result = RegSetValueEx(hKey, pszEntry, 0, REG_DWORD, (BYTE*)&pszValue, sizeof(DWORD));
	}
	RegCloseKey(hKey);
	return (ERROR_SUCCESS == result);
}

static LONG DeleteRegKey(HKEY hKeyParent, LPCWSTR pszKey)
{	
	HKEY hKey;
	LONG result;
	FILETIME time = {0};
	WCHAR szBuffer[512] = {0};
	DWORD dwSize = sizeof(szBuffer)/sizeof(WCHAR);

	result = RegOpenKeyExW(hKeyParent, pszKey, 0, KEY_SET_VALUE | KEY_ENUMERATE_SUB_KEYS , &hKey);
	if (ERROR_SUCCESS != result) return result;
	while (ERROR_SUCCESS == RegEnumKeyExW(hKey, 0, szBuffer, &dwSize, NULL, NULL, NULL, &time))
	{
		if (ERROR_SUCCESS != (result = DeleteRegKey(hKey, szBuffer)))
		{
			RegCloseKey(hKey);
			return result;
		}
		dwSize = sizeof(szBuffer)/sizeof(WCHAR);
	}

	RegCloseKey(hKey);
	return RegDeleteKeyW(hKeyParent, pszKey);
}

HRESULT RegisterComponent(HMODULE hModule, const CLSID &clsid, LPCWSTR pszFriendlyName, LPCWSTR pszVersionIndProgId, LPCWSTR pszProgId)
{
	SECURITY_DESCRIPTOR *sd;
	HKEY hKey, hKey2, hKey3;
	BOOL br;
	WCHAR szBuffer[MAX_PATH] = {0}, szCLSID[64] = {0};

	if (!StringFromGUID2(clsid, szCLSID, sizeof(szCLSID)/sizeof(WCHAR))) return E_OUTOFMEMORY;
	StringCchPrintfW(szBuffer, sizeof(szBuffer)/sizeof(WCHAR), L"SOFTWARE\\Classes\\CLSID\\%s", szCLSID);

	if (!WriteRegKey(HKEY_LOCAL_MACHINE, szBuffer, pszFriendlyName, &hKey)) return S_FALSE;

	if (!GetModuleFileNameW(hModule, szBuffer, sizeof(szBuffer)/sizeof(WCHAR))) return S_FALSE;

	RegCreateKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Classes\\AppID\\{3B29AB5C-52CB-4a36-9314-E3FEE0BA7468}", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey3, NULL);
	if (GetAccessPermissionsForLUAServer(&sd))
		SetAccessPermissions(hKey3, sd);
	if (GetLaunchActPermissionsWithIL(&sd))
		SetLaunchActPermissions(hKey3, sd);
	WriteRegValue(hKey3, NULL, NULL, szBuffer);
	RegCloseKey(hKey3);

	RegCreateKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Classes\\AppID\\elevator.exe", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey3, NULL);
	WriteRegValue(hKey3, NULL, L"AppID", L"{3B29AB5C-52CB-4a36-9314-E3FEE0BA7468}");
	RegCloseKey(hKey3);		

	wchar_t localizedString[MAX_PATH+15] = {0};
	StringCbPrintf(localizedString, sizeof(localizedString), L"@%s,-%u", szBuffer, IDS_WINAMP);

	br = (WriteRegKey(hKey, L"LocalServer32" , szBuffer, &hKey2) &&
			WriteRegValue(hKey2, NULL, L"ThreadingModel", L"Both") &&
			WriteRegKey(hKey, L"ProgID", pszProgId) &&
			WriteRegKey(hKey, L"VersionIndependentProgID", pszVersionIndProgId)
			&& WriteRegValue(hKey, NULL, L"LocalizedString", localizedString)						
			&& WriteRegValue(hKey, NULL, L"AppId", L"{3B29AB5C-52CB-4a36-9314-E3FEE0BA7468}")						
			&& WriteRegKey(hKey, L"Elevation", 0)
			//&& WriteRegValue(hKey, L"Elevation", L"IconReference", 
			&& WriteRegValue(hKey, L"Elevation", L"Enabled", 1)
			);
	RegCloseKey(hKey);
	if (hKey2) RegCloseKey(hKey2);
	if (!br) return S_FALSE;

	if (!WriteRegKey(HKEY_CLASSES_ROOT, pszVersionIndProgId, pszFriendlyName, &hKey)) return S_FALSE;
	br = (WriteRegKey(hKey, L"CLSID", szCLSID) &&
			WriteRegKey(hKey, L"CurVer", pszProgId));
	RegCloseKey(hKey);
	if (!br) return S_FALSE;

	if (!WriteRegKey(HKEY_CLASSES_ROOT, pszProgId, pszFriendlyName, &hKey)) return S_FALSE;
	br = WriteRegKey(hKey, L"CLSID", szCLSID);

	RegCloseKey(hKey);

	if (!br) return S_FALSE;

	return S_OK;
}

static HRESULT UnregisterComponent(const CLSID &clsid, LPCWSTR pszVersionIndProgId, LPCWSTR pszProgId)
{
	LONG result;
	WCHAR szBuffer[MAX_PATH] = {0}, szCLSID[64] = {0}; 
	if (!StringFromGUID2(clsid, szCLSID, sizeof(szCLSID)/sizeof(WCHAR))) return E_OUTOFMEMORY;
	StringCchPrintfW(szBuffer, sizeof(szBuffer)/sizeof(WCHAR), L"CLSID\\%s", szCLSID);

	result = DeleteRegKey(HKEY_CLASSES_ROOT, szBuffer);
	if (ERROR_SUCCESS != result && ERROR_FILE_NOT_FOUND != result) return S_FALSE;

	result = DeleteRegKey(HKEY_CLASSES_ROOT, pszVersionIndProgId);
	if (ERROR_SUCCESS != result && ERROR_FILE_NOT_FOUND != result) return S_FALSE;

	result = DeleteRegKey(HKEY_CLASSES_ROOT, pszProgId);
	if (ERROR_SUCCESS != result && ERROR_FILE_NOT_FOUND != result) return S_FALSE;

	return S_OK;
}