Debian Codegraph @

Contrib/Library/RegTool/RegTool.cpp

raw
1 // RegTool v3
2 // Unicode support by Jim Park & Olivier Marcoux
3
4 #include "../../../Source/Platform.h"
5 #include <windows.h>
6
7 #define STR_SIZE 1024
8
9 #if defined(_MSC_VER) && _MSC_VER-0 >= 1600 && _MSC_VER-0 <= 1800 // MSVC complains about math stuff we are not even using (bug #1159)
10 EXTERN_C int _fltused = 0;
11 EXTERN_C double _hypot(double x, double y) { return 0.0; }
12 #endif
13
14 /*
15 All A/W functions need this ugly hack so we can call them in template functions.
16 Everything is implemented in template functions because the x86 version needs to
17 call the W functions on NT and the A functions on 9x.
18 The macros assume that "T" is a [W]CHAR type.
19 */
20 #define MKSTR(str) ( sizeof(T) > 1 ? (const T*) L##str : (const T*) str )
21 #ifdef UNICODE
22 #define CALL(func) hack::func##W<T>
23 #else
24 #define CALL(func) hack::func##A<T>
25 #endif
26 #define CALL_wsprintf (sizeof(T) > 1 ? (int(*)(T*,const T*,...)) wsprintfW : (int(*)(T*,const T*,...)) wsprintfA)
27 namespace hack {
28 // Allow cast from [const] T* to [W]CHAR* but nothing else (losing constness is acceptable).
29 inline WCHAR* WP(const WCHAR*p) { return (WCHAR*) p; }
30 inline WCHAR* WP(const CHAR*p) { return (WCHAR*) p; }
31 inline CHAR* NP(const WCHAR*p) { return (CHAR*) p; }
32 inline CHAR* NP(const CHAR*p) { return (CHAR*) p; }
33 // These function names rely on the C preprocessor & PSDK macros to append A/W to their name so they can be found by the CALL macro.
34 template<class T> T* GetCommandLine() { return sizeof(T) > 1 ? (T*) ::GetCommandLineW() : (T*) ::GetCommandLineA(); }
35 template<class T> T* CharNext(const T*p1) { return sizeof(T) > 1 ? (T*) ::CharNextW(WP(p1)) : (T*) ::CharNextA(NP(p1)); }
36 template<class T> T* lstrcpy(T*p1,const T*p2) { return sizeof(T) > 1 ? (T*) ::lstrcpyW(WP(p1),WP(p2)) : (T*) ::lstrcpyA(NP(p1),NP(p2)); }
37 template<class T> T* lstrcat(T*p1,const T*p2) { return sizeof(T) > 1 ? (T*) ::lstrcatW(WP(p1),WP(p2)) : (T*) ::lstrcatA(NP(p1),NP(p2)); }
38 template<class T> LONG RegEnumKey(HKEY p1, DWORD p2, T*p3,DWORD p4) { return sizeof(T) > 1 ? ::RegEnumKeyW(p1,p2,WP(p3),p4) : ::RegEnumKeyA(p1,p2,NP(p3),p4); }
39 template<class T> LONG RegOpenKeyEx(HKEY p1,const T*p2,DWORD p3,REGSAM p4,PHKEY p5) { return sizeof(T) > 1 ? ::RegOpenKeyExW(p1,WP(p2),p3,p4,p5) : ::RegOpenKeyExA(p1,NP(p2),p3,p4,p5); }
40 template<class T> LONG RegQueryValueEx(HKEY p1,const T*p2,LPDWORD p3,LPDWORD p4,LPBYTE p5,LPDWORD p6) { return sizeof(T) > 1 ? ::RegQueryValueExW(p1,WP(p2),p3,p4,p5,p6) : ::RegQueryValueExA(p1,NP(p2),p3,p4,p5,p6); }
41 template<class T> LONG RegDeleteKey(HKEY p1,const T*p2) { return sizeof(T) > 1 ? ::RegDeleteKeyW(p1,WP(p2)) : ::RegDeleteKeyA(p1,NP(p2)); }
42 template<class T> DWORD GetModuleFileName(HMODULE p1,T*p2,DWORD p3) { return sizeof(T) > 1 ? ::GetModuleFileNameW(p1,WP(p2),p3) : ::GetModuleFileNameA(p1,NP(p2),p3); }
43 template<class T> HMODULE LoadLibraryEx(const T*p1,void*p2,DWORD p3) { return sizeof(T) > 1 ? ::LoadLibraryExW(WP(p1),p2,p3) : ::LoadLibraryExA(NP(p1),p2,p3); }
44 template<class T> UINT GetWindowsDirectory(const T*p1,UINT p2) { return sizeof(T) > 1 ? ::GetWindowsDirectoryW(WP(p1),p2) : ::GetWindowsDirectoryA(NP(p1),p2); }
45 template<class T> UINT GetSystemDirectory(const T*p1,UINT p2) { return sizeof(T) > 1 ? ::GetSystemDirectoryW(WP(p1),p2) : ::GetSystemDirectoryA(NP(p1),p2); }
46 template<class T> DWORD GetShortPathName(const T*p1,const T*p2,DWORD p3) { return sizeof(T) > 1 ? ::GetShortPathNameW(WP(p1),WP(p2),p3) : ::GetShortPathNameA(NP(p1),NP(p2),p3); }
47 template<class T> DWORD GetFileAttributes(const T*p1) { return sizeof(T) > 1 ? ::GetFileAttributesW(WP(p1)) : ::GetFileAttributesA(NP(p1)); }
48 template<class T> BOOL MoveFileEx(const T*p1,const T*p2,DWORD p3) { return sizeof(T) > 1 ? ::MoveFileExW(WP(p1),WP(p2),p3) : ::MoveFileExA(NP(p1),NP(p2),p3); }
49 template<class T> HANDLE CreateFile(const T*p1,DWORD p2,DWORD p3,LPSECURITY_ATTRIBUTES p4,DWORD p5,DWORD p6,HANDLE p7) { return sizeof(T) > 1 ? ::CreateFileW(WP(p1),p2,p3,p4,p5,p6,p7) : ::CreateFileA(NP(p1),p2,p3,p4,p5,p6,p7); }
50 template<class T> BOOL CreateProcess(const T*p1,const T*p2,LPSECURITY_ATTRIBUTES p3,LPSECURITY_ATTRIBUTES p4,BOOL p5,DWORD p6,LPVOID p7,const T*p8,STARTUPINFO*p9,LPPROCESS_INFORMATION p10) { return sizeof(T) > 1 ? ::CreateProcessW(WP(p1),WP(p2),p3,p4,p5,p6,p7,WP(p8),(STARTUPINFOW*)p9,p10) : ::CreateProcessA(NP(p1),NP(p2),p3,p4,p5,p6,p7,NP(p8),(STARTUPINFOA*)p9,p10); }
51 }
52
53 #if defined(_MSC_VER) && _MSC_VER >= 1200
54 EXTERN_C IMAGE_DOS_HEADER __ImageBase;
55 #define HINST_THISCOMPONENT ( (HINSTANCE) &__ImageBase )
56 #define HINST_APPLICATION HINST_THISCOMPONENT
57 #else
58 #define HINST_APPLICATION ( (HINSTANCE) GetModuleHandle(NULL) )
59 #endif
60
61 static bool IsWinNT()
62 {
63 #if defined(_WIN64) || (defined(_M_ARM) || defined(__arm__))
64 return true;
65 #else
66 LPCWSTR str = L"count"; // Using this string because it's already used in other parts of the code
67 return CharNextW(str) != NULL;
68 #endif
69 }
70
71 void SafeWow64EnableWow64FsRedirection(BYTE EnableFsRedirection)
72 {
73 #ifndef _WIN64
74 HMODULE hK32 = LoadLibraryA("KERNEL32");
75 FARPROC proc = GetProcAddress(hK32, "Wow64EnableWow64FsRedirection");
76 if (proc)
77 {
78 typedef BYTE WINNTBOOLEAN;
79 typedef WINNTBOOLEAN (WINAPI*W64EW64FSR)(WINNTBOOLEAN);
80 W64EW64FSR Wow64EnableWow64FsRedirectionFunc = (W64EW64FSR) proc;
81 Wow64EnableWow64FsRedirectionFunc(EnableFsRedirection);
82 }
83 #endif
84 }
85
86 char *mystrstriA(char *a, const char *b)
87 {
88 int l = lstrlenA(b);
89 while (lstrlenA(a) >= l)
90 {
91 char c = a[l];
92 a[l] = 0;
93 if (!lstrcmpiA(a, b))
94 {
95 a[l] = c;
96 return a;
97 }
98 a[l] = c;
99 a = CharNextA(a);
100 }
101 return NULL;
102 }
103
104 void mini_memcpy(void *out, const void *in, int len)
105 {
106 char *c_out=(char*)out;
107 char *c_in=(char *)in;
108 while (len-- > 0)
109 {
110 *c_out++=*c_in++;
111 }
112 }
113
114 template<class T> static HANDLE myOpenFile(const T *fn, DWORD da, DWORD cd)
115 {
116 DWORD attr = CALL(GetFileAttributes)(fn), share = FILE_SHARE_READ;
117 return CALL(CreateFile)(fn, da, share, NULL, cd, attr == INVALID_FILE_ATTRIBUTES ? 0 : attr, NULL);
118 }
119
120 #ifndef _WIN64
121 /** Modifies the wininit.ini file to rename / delete a file.
122 *
123 * @param prevName The previous / current name of the file.
124 * @param newName The new name to move the file to. If NULL, the current file
125 * will be deleted.
126 */
127 template<class T> void RenameViaWininit(const T* prevName, const T* newName) // Note: Not thread safe!
128 {
129 static char szRenameLine[1024];
130 static T wininit[1024];
131 static T tmpbuf[1024];
132
133 int cchRenameLine;
134 LPCSTR szRenameSec = "[Rename]\r\n"; // rename section marker
135 HANDLE hfile;
136 DWORD dwFileSize;
137 DWORD dwBytes, dwRenameLinePos;
138 char *pszWinInit; // Contains the file contents of wininit.ini
139
140 int spn; // length of the short path name in TCHARs.
141
142 CALL(lstrcpy)(tmpbuf, MKSTR("NUL"));
143
144 if (newName) {
145 // create the file if it's not already there to prevent GetShortPathName from failing
146 CloseHandle(myOpenFile(newName,0,CREATE_NEW));
147 spn = CALL(GetShortPathName)(newName,tmpbuf,COUNTOF(tmpbuf));
148 if (!spn || spn > 1024)
149 return;
150 }
151 // wininit is used as a temporary here
152 spn = CALL(GetShortPathName)(prevName,wininit,COUNTOF(wininit));
153 if (!spn || spn > 1024)
154 return;
155 #ifdef _UNICODE
156 cchRenameLine = wsprintfA(szRenameLine, "%S=%S\r\n", tmpbuf, wininit);
157 #else
158 cchRenameLine = wsprintfA(szRenameLine, "%s=%s\r\n", tmpbuf, wininit);
159 #endif
160 // Get the path to the wininit.ini file.
161 CALL(GetWindowsDirectory)(wininit, COUNTOF(wininit)-16);
162 CALL(lstrcat)(wininit, MKSTR("\\wininit.ini"));
163
164 hfile = myOpenFile(wininit, GENERIC_READ | GENERIC_WRITE, OPEN_ALWAYS);
165
166 if (hfile != INVALID_HANDLE_VALUE)
167 {
168 // We are now working on the Windows wininit file
169 dwFileSize = GetFileSize(hfile, NULL);
170 pszWinInit = (char*) GlobalAlloc(GPTR, dwFileSize + cchRenameLine + 10);
171
172 if (pszWinInit != NULL)
173 {
174 if (ReadFile(hfile, pszWinInit, dwFileSize, &dwBytes, NULL) && dwFileSize == dwBytes)
175 {
176 // Look for the rename section in the current file.
177 LPSTR pszRenameSecInFile = mystrstriA(pszWinInit, szRenameSec);
178 if (pszRenameSecInFile == NULL)
179 {
180 // No rename section. So we add it to the end of file.
181 lstrcpyA(pszWinInit+dwFileSize, szRenameSec);
182 dwFileSize += 10;
183 dwRenameLinePos = dwFileSize;
184 }
185 else
186 {
187 // There is a rename section, but is there another section after it?
188 char *pszFirstRenameLine = pszRenameSecInFile+10;
189 char *pszNextSec = mystrstriA(pszFirstRenameLine,"\n[");
190 if (pszNextSec)
191 {
192 char *p = pszWinInit + dwFileSize;
193 char *pEnd = pszWinInit + dwFileSize + cchRenameLine;
194
195 while (p > pszNextSec)
196 {
197 *pEnd-- = *p--;
198 }
199
200 dwRenameLinePos = BUGBUG64TRUNCATE(DWORD, pszNextSec - pszWinInit) + 1; // +1 for the \n
201 }
202 // rename section is last, stick item at end of file
203 else dwRenameLinePos = dwFileSize;
204 }
205
206 mini_memcpy(&pszWinInit[dwRenameLinePos], szRenameLine, cchRenameLine);
207 dwFileSize += cchRenameLine;
208
209 SetFilePointer(hfile, 0, NULL, FILE_BEGIN);
210 WriteFile(hfile, pszWinInit, dwFileSize, &dwBytes, NULL);
211
212 GlobalFree(pszWinInit);
213 }
214 }
215
216 CloseHandle(hfile);
217 }
218 }
219 #endif
220
221 template<class T> BOOL DeleteFileOnReboot(const T *pszFile)
222 {
223 BOOL fOk = CALL(MoveFileEx)(pszFile, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
224 #ifndef _WIN64
225 if (!fOk && sizeof(T) == 1)
226 {
227 RenameViaWininit(pszFile, (const T*)NULL);
228 fOk = TRUE; // BUGBUG: We just pretend everything is OK, nobody checks our return value anyway
229 }
230 #endif
231 return fOk;
232 }
233
234 template<class T> void RegFile(T cmd, const T *file, BOOL x64)
235 {
236 T *self; // These are allocated on the heap to avoid _chkstk
237 T *cmdline;
238 int ready = 0;
239
240 if (!*file || (cmd != _T('D') && cmd != _T('T') && cmd != _T('E')))
241 return;
242
243 self = (T*) GlobalAlloc(GPTR, sizeof(T)*STR_SIZE);
244 cmdline = (T*) GlobalAlloc(GPTR, sizeof(T)*STR_SIZE);
245
246 if (cmd == ('E'))
247 {
248 CALL_wsprintf(cmdline, MKSTR("\"%s\" /regserver"), file);
249 ready++;
250 }
251 else if (!x64)
252 {
253 if (CALL(GetModuleFileName)(HINST_APPLICATION, self, STR_SIZE))
254 {
255 CALL_wsprintf(cmdline, MKSTR("\"%s\" /%c%s"), self, cmd, file);
256 ready++;
257 }
258 }
259 else
260 {
261 if (CALL(GetSystemDirectory)(self, STR_SIZE))
262 {
263 CALL_wsprintf(cmdline, MKSTR("\"%s\\regsvr32.exe\" /s \"%s\""), self, file);
264 ready++;
265
266 SafeWow64EnableWow64FsRedirection(FALSE);
267 }
268 }
269
270 if (ready)
271 {
272 PROCESS_INFORMATION pi;
273 BYTE sibuf[sizeof(T) > 1 ? sizeof(STARTUPINFOW) : sizeof(STARTUPINFOA)] = {0,};
274 STARTUPINFO &si = (STARTUPINFO&) sibuf;
275 si.cb = sizeof(sibuf);
276
277 if (CALL(CreateProcess)(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
278 {
279 WaitForSingleObject(pi.hProcess, INFINITE);
280 CloseHandle(pi.hProcess);
281 CloseHandle(pi.hThread);
282 }
283
284 if (x64)
285 {
286 SafeWow64EnableWow64FsRedirection(TRUE);
287 }
288 }
289
290 GlobalFree(self);
291 GlobalFree(cmdline);
292 }
293
294 template<class T> void RegDll(const T *file)
295 {
296 HMODULE mod = CALL(LoadLibraryEx)(file, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
297 if (mod)
298 {
299 FARPROC regfunc = GetProcAddress(mod, "DllRegisterServer");
300 if (regfunc)
301 regfunc();
302 FreeLibrary(mod);
303 }
304 }
305
306 template<class T> void RegTypeLib(T *file)
307 {
308 WCHAR wbuf[sizeof(T) > 1 ? 1 : STR_SIZE]; // Buffer only used by ANSI implementation!
309 WCHAR *wfile = wbuf; // Not const because of RegisterTypeLib in old PSDK
310 if (sizeof(T) > 1)
311 wfile = (WCHAR*) file;
312 else
313 {
314 if (MultiByteToWideChar(CP_ACP, 0, (const CHAR*) file, -1, wbuf, STR_SIZE) == 0)
315 return;
316 }
317
318 ITypeLib* tlib;
319 if (SUCCEEDED(LoadTypeLib(wfile, &tlib)))
320 {
321 RegisterTypeLib(tlib, wfile, NULL);
322 tlib->Release();
323 }
324 }
325
326 template<class T> int RegTool()
327 {
328 T *cmdline;
329 T seekchar = (' ');
330
331 cmdline = CALL(GetCommandLine)();
332 if (*cmdline == ('\"'))
333 seekchar = *cmdline++;
334
335 while (*cmdline && *cmdline != seekchar)
336 cmdline = CALL(CharNext)(cmdline);
337 cmdline = CALL(CharNext)(cmdline);
338 while (*cmdline == (' '))
339 cmdline++;
340
341 if (*cmdline++ != ('/'))
342 {
343 return 1;
344 }
345
346 if (*cmdline == ('S'))
347 {
348 HKEY rootkey;
349 T *keyname, *file; // These are allocated on the heap to avoid _chkstk
350 keyname = (T*) GlobalAlloc(GPTR, STR_SIZE*sizeof(T));
351 file = (T*) GlobalAlloc(GPTR, STR_SIZE*sizeof(T));
352
353 if (SUCCEEDED(RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\NSIS.Library.RegTool.v3", 0, KEY_READ, &rootkey)))
354 {
355 DWORD keyidx = 0;
356 while (CALL(RegEnumKey)(rootkey, keyidx, keyname, STR_SIZE) == ERROR_SUCCESS)
357 {
358 HKEY key;
359
360 if (SUCCEEDED(CALL(RegOpenKeyEx)(rootkey, keyname, 0, KEY_READ, &key)))
361 {
362 DWORD t, count, l = sizeof(DWORD);
363
364 if (SUCCEEDED(CALL(RegQueryValueEx)(key, MKSTR("count"), NULL, &t, (LPBYTE) &count, &l)) && t == REG_DWORD)
365 {
366 DWORD j;
367 T valname[128], mode[3];
368
369 for (j = 0; ++j <= count;)
370 {
371 CALL_wsprintf(valname, MKSTR("%u.mode"), j);
372 l = sizeof(mode);
373 if (FAILED(CALL(RegQueryValueEx)(key, valname, NULL, &t, (LPBYTE) mode, &l)) || t != REG_SZ)
374 continue;
375
376 CALL_wsprintf(valname, MKSTR("%u.file"), j);
377 l = STR_SIZE*sizeof(T);
378 if (FAILED(CALL(RegQueryValueEx)(key, valname, NULL, &t, (LPBYTE) file, &l)) || t != REG_SZ)
379 continue;
380
381 // JP: Note, if this mode[1] is used as anything but a boolean later on,
382 // we'll need to consider the next line carefully.
383 RegFile(mode[0], file, mode[1] == 'X');
384 }
385 }
386
387 RegCloseKey(key);
388
389 CALL(RegDeleteKey)(rootkey, keyname);
390 }
391 keyidx++; // Must increment this so we don't loop forever if a non-admin accidentally executes RegTool /S
392 }
393
394 RegCloseKey(rootkey);
395 RegDeleteKeyA(HKEY_LOCAL_MACHINE, "Software\\NSIS.Library.RegTool.v3");
396 }
397
398 {
399 if (CALL(GetModuleFileName)(HINST_APPLICATION, file, STR_SIZE))
400 {
401 DeleteFileOnReboot(file);
402 }
403 }
404 GlobalFree(keyname);
405 GlobalFree(file);
406 }
407 else
408 {
409 DWORD orgerrmode = SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
410 OleInitialize(NULL);
411
412 if (*cmdline == ('D'))
413 {
414 RegDll(cmdline + 1);
415 }
416 else if (*cmdline == ('T'))
417 {
418 RegTypeLib(cmdline + 1);
419 }
420
421 OleUninitialize();
422 SetErrorMode(orgerrmode);
423 }
424
425 return 0;
426 }
427
428 NSIS_ENTRYPOINT_GUINOCRT
429 EXTERN_C void NSISWinMainNOCRT()
430 {
431 int ec = IsWinNT() ? RegTool<WCHAR>() : RegTool<CHAR>();
432 ExitProcess(ec);
433 }