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.

416 lines
9.1 KiB

  1. //+-------------------------------------------------------------------------
  2. // Microsoft Windows
  3. //
  4. // Copyright (C) Microsoft Corporation, 1995 - 1999
  5. //
  6. // File: iesetreg.cpp
  7. //
  8. // Contents: Set Registry Key Values
  9. //
  10. // See Usage() for syntax and list of options.
  11. //
  12. // Functions: wmain
  13. //
  14. // History: 28-Jul-96 philh created
  15. // 02-May-97 xiaohs updated for Localiztion and Consistency
  16. // 28-July-97 xiaohs reduce size for ie
  17. // 31-Oct-97 pberkman changed to be a Windows App instead of Console.
  18. //--------------------------------------------------------------------------
  19. #include <windows.h>
  20. #include <assert.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <memory.h>
  25. #include <time.h>
  26. #include <wchar.h>
  27. #include "malloc.h"
  28. #include "wintrust.h"
  29. #include "cryptreg.h"
  30. #include "unicode.h"
  31. #include "resource.h"
  32. typedef struct _FlagNames
  33. {
  34. int idsName;
  35. DWORD dwMask;
  36. } FlagNames;
  37. static FlagNames SoftPubFlags[] =
  38. {
  39. IDS_NAME_TEST_ROOT, WTPF_TRUSTTEST | WTPF_TESTCANBEVALID,
  40. IDS_NAME_EXPIRATION, WTPF_IGNOREEXPIRATION,
  41. IDS_NAME_REVOCATION, WTPF_IGNOREREVOKATION,
  42. IDS_NAME_OFFLINE_INDIVIDUAL, WTPF_OFFLINEOK_IND,
  43. IDS_NAME_OFFLINE_COMMERCIAL, WTPF_OFFLINEOK_COM,
  44. IDS_NAME_JAVA_INDIVIDUAL, WTPF_OFFLINEOKNBU_IND,
  45. IDS_NAME_JAVA_COMMERCIAL, WTPF_OFFLINEOKNBU_COM,
  46. IDS_NAME_VERSION_ONE, WTPF_VERIFY_V1_OFF,
  47. IDS_NAME_REVOCATIONONTS, WTPF_IGNOREREVOCATIONONTS,
  48. IDS_NAME_ALLOWONLYPERTRUST, WTPF_ALLOWONLYPERTRUST
  49. };
  50. #define NSOFTPUBFLAGS (sizeof(SoftPubFlags)/sizeof(SoftPubFlags[0]))
  51. HMODULE hModule=NULL;
  52. static BOOL IsWinNt(void) {
  53. static BOOL fIKnow = FALSE;
  54. static BOOL fIsWinNT = FALSE;
  55. OSVERSIONINFO osVer;
  56. if(fIKnow)
  57. return(fIsWinNT);
  58. memset(&osVer, 0, sizeof(OSVERSIONINFO));
  59. osVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  60. if( GetVersionEx(&osVer) )
  61. fIsWinNT = (osVer.dwPlatformId == VER_PLATFORM_WIN32_NT);
  62. // even on an error, this is as good as it gets
  63. fIKnow = TRUE;
  64. return(fIsWinNT);
  65. }
  66. int __cdecl _mywcsicmp(const wchar_t * wsz1, const wchar_t * wsz2)
  67. //
  68. // REVIEW: Who calls this function, and should they be doing so?
  69. //
  70. // Return:
  71. // <0 if wsz1 < wsz2
  72. // 0 if wsz1 = wsz2
  73. // >0 if wsz1 > wsz2
  74. {
  75. if(IsWinNt())
  76. {
  77. //
  78. // Just do the Unicode compare
  79. //
  80. return lstrcmpiW(wsz1, wsz2);
  81. }
  82. else
  83. {
  84. //
  85. // Convert to multibyte and let the system do it
  86. //
  87. int cch1 = lstrlenW(wsz1);
  88. int cch2 = lstrlenW(wsz2);
  89. int cb1 = (cch1+1) * sizeof(WCHAR);
  90. int cb2 = (cch2+1) * sizeof(WCHAR);
  91. char* sz1= (char*) _alloca(cb1);
  92. char* sz2= (char*) _alloca(cb2);
  93. WideCharToMultiByte(CP_ACP, 0, wsz1, -1, sz1, cb1, NULL, NULL);
  94. WideCharToMultiByte(CP_ACP, 0, wsz2, -1, sz2, cb2, NULL, NULL);
  95. return lstrcmpiA(sz1, sz2);
  96. }
  97. }
  98. //---------------------------------------------------------------------------
  99. // Set Software Publisher State Key Value
  100. //
  101. //---------------------------------------------------------------------------
  102. static void SetSoftPubKey(DWORD dwMask, BOOL fOn)
  103. {
  104. DWORD dwState;
  105. LONG lErr;
  106. HKEY hKey;
  107. DWORD dwDisposition;
  108. DWORD dwType;
  109. DWORD cbData;
  110. //WCHAR wszState[10];
  111. LPWSTR wszState=REGNAME_WINTRUST_POLICY_FLAGS;
  112. //If load string failed, no need to flag the failure since
  113. //no output is possible
  114. // if(!LoadStringU(hModule, IDS_KEY_STATE,wszState, 10))
  115. // return;
  116. // Set the State in the registry
  117. if (ERROR_SUCCESS != (lErr = RegCreateKeyExU(
  118. HKEY_CURRENT_USER,
  119. REGPATH_WINTRUST_POLICY_FLAGS,
  120. 0, // dwReserved
  121. NULL, // lpszClass
  122. REG_OPTION_NON_VOLATILE,
  123. KEY_ALL_ACCESS,
  124. NULL, // lpSecurityAttributes
  125. &hKey,
  126. &dwDisposition)))
  127. {
  128. return;
  129. }
  130. dwState = 0;
  131. cbData = sizeof(dwState);
  132. lErr = RegQueryValueExU
  133. (
  134. hKey,
  135. wszState,
  136. NULL, // lpReserved
  137. &dwType,
  138. (BYTE *) &dwState,
  139. &cbData
  140. );
  141. if (ERROR_SUCCESS != lErr)
  142. {
  143. if (lErr == ERROR_FILE_NOT_FOUND)
  144. {
  145. dwState = 0;
  146. }
  147. else
  148. {
  149. goto CLEANUP;
  150. }
  151. }
  152. else if ((dwType != REG_DWORD) && (dwType != REG_BINARY))
  153. {
  154. goto CLEANUP;
  155. }
  156. switch(dwMask)
  157. {
  158. case WTPF_IGNOREREVOCATIONONTS:
  159. case WTPF_IGNOREREVOKATION:
  160. case WTPF_IGNOREEXPIRATION:
  161. // Revocation and expiration are a double negative so the bit set
  162. // means revocation and expriation checking is off.
  163. fOn = !fOn;
  164. break;
  165. default:
  166. break;
  167. };
  168. if (fOn)
  169. dwState |= dwMask;
  170. else
  171. dwState &= ~dwMask;
  172. lErr = RegSetValueExU(
  173. hKey,
  174. wszState,
  175. 0, // dwReserved
  176. REG_DWORD,
  177. (BYTE *) &dwState,
  178. sizeof(dwState)
  179. );
  180. CLEANUP:
  181. if(hKey)
  182. RegCloseKey(hKey);
  183. }
  184. //---------------------------------------------------------------------------
  185. // wmain
  186. //
  187. //---------------------------------------------------------------------------
  188. #define MAX_ARGV_PARAMS 32
  189. extern "C" int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd)
  190. {
  191. WCHAR *wargv1[MAX_ARGV_PARAMS];
  192. WCHAR **wargv;
  193. WCHAR *pwsz;
  194. int argc;
  195. WCHAR wszExeName[MAX_PATH];
  196. memset(wargv1, 0x00, sizeof(WCHAR *) * MAX_ARGV_PARAMS);
  197. wargv = &wargv1[0];
  198. wszExeName[0] = NULL;
  199. GetModuleFileNameU(GetModuleHandle(NULL), &wszExeName[0], MAX_PATH);
  200. argc = 1;
  201. wargv[0] = &wszExeName[0];
  202. wargv[1] = NULL;
  203. if (lpCmdLine)
  204. {
  205. while (*lpCmdLine == L' ')
  206. {
  207. lpCmdLine++;
  208. }
  209. if (*lpCmdLine)
  210. {
  211. wargv[argc] = lpCmdLine;
  212. argc++;
  213. wargv[argc] = NULL;
  214. }
  215. }
  216. pwsz = lpCmdLine;
  217. while ((pwsz) && (*pwsz) && (argc < MAX_ARGV_PARAMS))
  218. {
  219. if (*pwsz == L' ')
  220. {
  221. *pwsz = NULL;
  222. pwsz++;
  223. while (*pwsz == L' ')
  224. {
  225. pwsz++;
  226. }
  227. wargv[argc] = pwsz;
  228. argc++;
  229. }
  230. pwsz++;
  231. }
  232. //
  233. // now that we have argv/argc style params, go into existing code ...
  234. //
  235. int ReturnStatus = 0;
  236. LPWSTR *prgwszKeyName=NULL;
  237. LPWSTR *prgwszValue=NULL;
  238. DWORD dwIndex=0;
  239. DWORD dwCountKey=0;
  240. DWORD dwCountValue=0;
  241. DWORD dwMask = 0;
  242. BOOL fOn=TRUE;
  243. BOOL fQuiet = FALSE;
  244. DWORD dwEntry=0;
  245. WCHAR *pArg=NULL;
  246. WCHAR wszTRUE[10];
  247. WCHAR wszFALSE[10];
  248. if(!(hModule=GetModuleHandle(NULL)))
  249. {
  250. ReturnStatus=-1;
  251. goto CommonReturn;
  252. }
  253. //load the string
  254. if(!LoadStringU(hModule, IDS_TRUE, wszTRUE, 10) ||
  255. !LoadStringU(hModule, IDS_FALSE, wszFALSE, 10))
  256. {
  257. ReturnStatus=-1;
  258. goto CommonReturn;
  259. }
  260. //convert the multitype registry path to the wchar version
  261. prgwszKeyName=(LPWSTR *)malloc(sizeof(LPWSTR)*argc);
  262. prgwszValue=(LPWSTR *)malloc(sizeof(LPWSTR)*argc);
  263. if(!prgwszKeyName || !prgwszValue)
  264. {
  265. ReturnStatus = -1;
  266. goto CommonReturn;
  267. }
  268. //memset
  269. memset(prgwszKeyName, 0, sizeof(LPWSTR)*argc);
  270. memset(prgwszValue, 0, sizeof(LPWSTR)*argc);
  271. while (--argc>0)
  272. {
  273. pArg=*++wargv;
  274. if(dwCountKey==dwCountValue)
  275. {
  276. prgwszKeyName[dwCountKey]=pArg;
  277. dwCountKey++;
  278. }
  279. else
  280. {
  281. if(dwCountKey==(dwCountValue+1))
  282. {
  283. prgwszValue[dwCountValue]=pArg;
  284. dwCountValue++;
  285. }
  286. else
  287. {
  288. goto BadUsage;
  289. }
  290. }
  291. }
  292. if(dwCountKey!=dwCountValue)
  293. {
  294. goto BadUsage;
  295. }
  296. if(dwCountKey==0)
  297. {
  298. //Display the Software Publisher State Key Values
  299. //DisplaySoftPubKeys();
  300. goto CommonReturn;
  301. }
  302. for(dwIndex=0; dwIndex<dwCountKey; dwIndex++)
  303. {
  304. //the choice has to be one character long
  305. if((prgwszKeyName[dwIndex][0]==L'1') && (prgwszKeyName[dwIndex][1]==L'0') &&
  306. (prgwszKeyName[dwIndex][2]==L'\0'))
  307. dwEntry=10;
  308. else
  309. {
  310. if(prgwszKeyName[dwIndex][1]!=L'\0')
  311. goto BadUsage;
  312. //get the character
  313. dwEntry=(ULONG)(prgwszKeyName[dwIndex][0])-(ULONG)(L'0');
  314. }
  315. if((dwEntry < 1) || (dwEntry > NSOFTPUBFLAGS+1))
  316. goto BadUsage;
  317. //get the Key mask
  318. dwMask = SoftPubFlags[dwEntry-1].dwMask;
  319. if (0 == _mywcsicmp(prgwszValue[dwIndex], wszTRUE))
  320. fOn = TRUE;
  321. else if (0 == _mywcsicmp(prgwszValue[dwIndex], wszFALSE))
  322. fOn = FALSE;
  323. else
  324. {
  325. goto BadUsage;
  326. }
  327. SetSoftPubKey(dwMask, fOn);
  328. }
  329. goto CommonReturn;
  330. BadUsage:
  331. ReturnStatus = -1;
  332. CommonReturn:
  333. //free the memory
  334. if(prgwszKeyName)
  335. free(prgwszKeyName);
  336. if(prgwszValue)
  337. free(prgwszValue);
  338. return ReturnStatus;
  339. }