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.

599 lines
14 KiB

  1. //+-------------------------------------------------------------------------
  2. // Microsoft Windows
  3. //
  4. // Copyright (C) Microsoft Corporation, 1995 - 1999
  5. //
  6. // File: setreg.cpp
  7. //
  8. // Contents: Set Registry Key Values
  9. //
  10. // See Usage() for syntax and list of options.
  11. //
  12. // Functions: main
  13. //
  14. // History: 28-Jul-96 philh created
  15. // 02-May-97 xiaohs updated for Localiztion and Consistency
  16. //--------------------------------------------------------------------------
  17. #include <windows.h>
  18. #include <assert.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <memory.h>
  23. #include <time.h>
  24. #include <wchar.h>
  25. #include <stdarg.h>
  26. #include "wintrust.h"
  27. #include "cryptreg.h"
  28. #include "resource.h"
  29. #include "unicode.h"
  30. typedef struct _FlagNames
  31. {
  32. int idsName;
  33. DWORD dwMask;
  34. } FlagNames;
  35. static FlagNames SoftPubFlags[] =
  36. {
  37. IDS_NAME_TEST_ROOT, WTPF_TRUSTTEST | WTPF_TESTCANBEVALID,
  38. IDS_NAME_EXPIRATION, WTPF_IGNOREEXPIRATION,
  39. IDS_NAME_REVOCATION, WTPF_IGNOREREVOKATION,
  40. IDS_NAME_OFFLINE_INDIVIDUAL, WTPF_OFFLINEOK_IND,
  41. IDS_NAME_OFFLINE_COMMERCIAL, WTPF_OFFLINEOK_COM,
  42. IDS_NAME_JAVA_INDIVIDUAL, WTPF_OFFLINEOKNBU_IND,
  43. IDS_NAME_JAVA_COMMERCIAL, WTPF_OFFLINEOKNBU_COM,
  44. IDS_NAME_VERSION_ONE, WTPF_VERIFY_V1_OFF,
  45. IDS_NAME_REVOCATIONONTS, WTPF_IGNOREREVOCATIONONTS,
  46. IDS_NAME_ALLOWONLYPERTRUST, WTPF_ALLOWONLYPERTRUST
  47. };
  48. #define NSOFTPUBFLAGS (sizeof(SoftPubFlags)/sizeof(SoftPubFlags[0]))
  49. //Global Data for loading the string
  50. #define MAX_STRING_RSC_SIZE 512
  51. #define OPTION_SWITCH_SIZE 5
  52. HMODULE hModule=NULL;
  53. WCHAR wszBuffer[MAX_STRING_RSC_SIZE];
  54. DWORD dwBufferSize=sizeof(wszBuffer)/sizeof(wszBuffer[0]);
  55. WCHAR wszBuffer2[MAX_STRING_RSC_SIZE];
  56. WCHAR wszBuffer3[MAX_STRING_RSC_SIZE];
  57. //Global Data for wchar version of the registry path.
  58. //---------------------------------------------------------------------------
  59. // The private version of _wcsicmp
  60. //----------------------------------------------------------------------------
  61. int IDSwcsicmp(WCHAR *pwsz, int idsString)
  62. {
  63. assert(pwsz);
  64. //load the string
  65. if(!LoadStringU(hModule, idsString, wszBuffer, dwBufferSize))
  66. return -1;
  67. return _wcsicmp(pwsz, wszBuffer);
  68. }
  69. //-------------------------------------------------------------------------
  70. //
  71. // The private version of wprintf. Input is an ID for a stirng resource
  72. // and the output is the standard output of wprintf.
  73. //
  74. //-------------------------------------------------------------------------
  75. void IDSwprintf(int idsString, ...)
  76. {
  77. va_list vaPointer;
  78. va_start(vaPointer, idsString);
  79. //load the string
  80. LoadStringU(hModule, idsString, wszBuffer, dwBufferSize);
  81. vwprintf(wszBuffer,vaPointer);
  82. return;
  83. }
  84. void IDS_IDS_DWwprintf(int idString, int idStringTwo, DWORD dw)
  85. {
  86. //load the string
  87. LoadStringU(hModule, idString, wszBuffer, dwBufferSize);
  88. //load the string two
  89. LoadStringU(hModule, idStringTwo, wszBuffer2, dwBufferSize);
  90. wprintf(wszBuffer,wszBuffer2,dw);
  91. return;
  92. }
  93. void IDS_IDSwprintf(int idString, int idStringTwo)
  94. {
  95. //load the string
  96. LoadStringU(hModule, idString, wszBuffer, dwBufferSize);
  97. //load the string two
  98. LoadStringU(hModule, idStringTwo, wszBuffer2, dwBufferSize);
  99. wprintf(wszBuffer,wszBuffer2);
  100. return;
  101. }
  102. void IDS_DW_IDS_IDSwprintf(int ids1,DWORD dw,int ids2,int ids3)
  103. {
  104. //load the string
  105. LoadStringU(hModule, ids1, wszBuffer, dwBufferSize);
  106. //load the string two
  107. LoadStringU(hModule, ids2, wszBuffer2, dwBufferSize);
  108. //load the string three
  109. LoadStringU(hModule, ids3, wszBuffer3, dwBufferSize);
  110. wprintf(wszBuffer,dw,wszBuffer2,wszBuffer3,dw);
  111. return;
  112. }
  113. //---------------------------------------------------------------------------
  114. //
  115. // Convert STR to WSTR
  116. //---------------------------------------------------------------------------
  117. BOOL SZtoWSZ(LPSTR szStr,LPWSTR *pwsz)
  118. {
  119. DWORD dwSize=0;
  120. assert(pwsz);
  121. *pwsz=NULL;
  122. //return NULL
  123. if(!szStr)
  124. return TRUE;
  125. dwSize=MultiByteToWideChar(0, 0,szStr, -1,NULL,0);
  126. if(dwSize==0)
  127. return FALSE;
  128. //allocate memory
  129. *pwsz=(LPWSTR)malloc(dwSize * sizeof(WCHAR));
  130. if(*pwsz==NULL)
  131. return FALSE;
  132. if(MultiByteToWideChar(0, 0,szStr, -1,
  133. *pwsz,dwSize))
  134. {
  135. return TRUE;
  136. }
  137. free(*pwsz);
  138. return FALSE;
  139. }
  140. //---------------------------------------------------------------------------
  141. // Get the hModule hanlder and init two DLLMain.
  142. //
  143. //---------------------------------------------------------------------------
  144. BOOL InitModule()
  145. {
  146. if(!(hModule=GetModuleHandle(NULL)))
  147. return FALSE;
  148. return TRUE;
  149. }
  150. //---------------------------------------------------------------------------
  151. // Dispaly the usage
  152. //
  153. //---------------------------------------------------------------------------
  154. static void Usage(void)
  155. {
  156. IDSwprintf(IDS_SYNTAX);
  157. IDSwprintf(IDS_OPTIONS);
  158. IDS_IDSwprintf(IDS_OPTION_Q_DESC, IDS_OPTION_Q);
  159. IDSwprintf(IDS_ENDLN);
  160. IDSwprintf(IDS_CHOICES);
  161. for (int i = 0; i < NSOFTPUBFLAGS; i++)
  162. {
  163. IDS_IDS_DWwprintf(IDS_DESC,SoftPubFlags[i].idsName,(i+1));
  164. }
  165. IDSwprintf(IDS_VALUE);
  166. IDSwprintf(IDS_ENDLN);
  167. }
  168. //---------------------------------------------------------------------------
  169. // Display Software Publisher State Key Value
  170. //
  171. //---------------------------------------------------------------------------
  172. static void DisplaySoftPubKeys()
  173. {
  174. DWORD dwState = 0;
  175. LONG lErr;
  176. HKEY hKey = NULL;
  177. DWORD dwType;
  178. DWORD cbData = sizeof(dwState);
  179. // WCHAR wszState[10];
  180. int i=0;
  181. LPWSTR wszState=REGNAME_WINTRUST_POLICY_FLAGS;
  182. //If load string failed, no need to flag the failure since
  183. //no output is possible
  184. // if(!LoadStringU(hModule, IDS_KEY_STATE,wszState, 10))
  185. // return;
  186. lErr = RegOpenHKCUKeyExU(
  187. HKEY_CURRENT_USER,
  188. REGPATH_WINTRUST_POLICY_FLAGS,
  189. 0, // dwReserved
  190. KEY_READ,
  191. &hKey);
  192. if (ERROR_SUCCESS != lErr)
  193. {
  194. if (lErr == ERROR_FILE_NOT_FOUND)
  195. IDSwprintf(IDS_NO_VALUE,REGPATH_WINTRUST_POLICY_FLAGS,NULL);
  196. else
  197. IDSwprintf(IDS_REG_OPEN_FAILED,
  198. REGPATH_WINTRUST_POLICY_FLAGS, L" ", lErr);
  199. return;
  200. }
  201. lErr = RegQueryValueExU(
  202. hKey,
  203. wszState,
  204. NULL, // lpReserved
  205. &dwType,
  206. (BYTE *) &dwState,
  207. &cbData
  208. );
  209. if (ERROR_SUCCESS != lErr)
  210. {
  211. if (lErr == ERROR_FILE_NOT_FOUND)
  212. IDSwprintf(IDS_NO_VALUE, REGPATH_WINTRUST_POLICY_FLAGS,NULL);
  213. else
  214. IDSwprintf(IDS_REG_QUERY_FAILED, REGPATH_WINTRUST_POLICY_FLAGS,NULL, lErr);
  215. goto CLEANUP;
  216. }
  217. //
  218. // 04-Aug-1997 pberkman:
  219. // added check for reg_binary because on WIN95 OSR2 when the machine is changed
  220. // from mutli-user profiles to single user profile, the registry DWORD values
  221. // change to BINARY
  222. //
  223. if ((dwType != REG_DWORD) &&
  224. (dwType != REG_BINARY))
  225. {
  226. IDSwprintf(IDS_WRONG_TYPE, REGPATH_WINTRUST_POLICY_FLAGS,NULL, dwType);
  227. goto CLEANUP;
  228. }
  229. IDSwprintf(IDS_STATE, dwState);
  230. for (i=0; i < NSOFTPUBFLAGS; i++)
  231. {
  232. BOOL fOn = (dwState & SoftPubFlags[i].dwMask);
  233. int idsValue;
  234. switch(SoftPubFlags[i].dwMask)
  235. {
  236. case WTPF_IGNOREREVOCATIONONTS:
  237. case WTPF_IGNOREREVOKATION:
  238. case WTPF_IGNOREEXPIRATION:
  239. // Revocation is a double negative so the bit set
  240. // means revocation is off.
  241. idsValue= fOn ? IDS_FALSE : IDS_TRUE;
  242. break;
  243. default:
  244. idsValue = fOn ? IDS_TRUE : IDS_FALSE;
  245. };
  246. if (i < 9)
  247. {
  248. IDS_DW_IDS_IDSwprintf(IDS_DISPLAY_LT_10, (i + 1), SoftPubFlags[i].idsName, idsValue);
  249. }
  250. else
  251. {
  252. IDS_DW_IDS_IDSwprintf(IDS_DISPLAY, (i + 1), SoftPubFlags[i].idsName, idsValue);
  253. }
  254. }
  255. CLEANUP:
  256. if (hKey != NULL)
  257. RegCloseKey(hKey);
  258. }
  259. //---------------------------------------------------------------------------
  260. // Set Software Publisher State Key Value
  261. //
  262. //---------------------------------------------------------------------------
  263. static void SetSoftPubKey(DWORD dwMask, BOOL fOn)
  264. {
  265. DWORD dwState;
  266. LONG lErr;
  267. HKEY hKey;
  268. DWORD dwDisposition;
  269. DWORD dwType;
  270. DWORD cbData;
  271. // WCHAR wszState[10];
  272. LPWSTR wszState=L"State";
  273. //If load string failed, no need to flag the failure since
  274. //no output is possible
  275. // if(!LoadStringU(hModule, IDS_KEY_STATE,wszState, 10))
  276. // return;
  277. // Set the State in the registry
  278. if (ERROR_SUCCESS != (lErr = RegCreateKeyExU(
  279. HKEY_CURRENT_USER,
  280. REGPATH_WINTRUST_POLICY_FLAGS,
  281. 0, // dwReserved
  282. NULL, // lpszClass
  283. REG_OPTION_NON_VOLATILE,
  284. KEY_ALL_ACCESS,
  285. NULL, // lpSecurityAttributes
  286. &hKey,
  287. &dwDisposition)))
  288. {
  289. IDSwprintf(IDS_REG_CREATE_FAILED, REGPATH_WINTRUST_POLICY_FLAGS, L" ", lErr);
  290. return;
  291. }
  292. dwState = 0;
  293. cbData = sizeof(dwState);
  294. lErr = RegQueryValueExU
  295. (
  296. hKey,
  297. wszState,
  298. NULL, // lpReserved
  299. &dwType,
  300. (BYTE *) &dwState,
  301. &cbData
  302. );
  303. if (ERROR_SUCCESS != lErr)
  304. {
  305. if (lErr == ERROR_FILE_NOT_FOUND)
  306. {
  307. dwState = 0;
  308. IDSwprintf(IDS_NO_VALUE,REGPATH_WINTRUST_POLICY_FLAGS,NULL);
  309. }
  310. else
  311. {
  312. IDSwprintf(IDS_REG_QUERY_FAILED,REGPATH_WINTRUST_POLICY_FLAGS,NULL, lErr);
  313. goto CLEANUP;
  314. }
  315. }
  316. else if ((dwType != REG_DWORD) && (dwType != REG_BINARY))
  317. {
  318. IDSwprintf(IDS_WRONG_TYPE,REGPATH_WINTRUST_POLICY_FLAGS,NULL, dwType);
  319. goto CLEANUP;
  320. }
  321. switch(dwMask) {
  322. case WTPF_IGNOREREVOCATIONONTS:
  323. case WTPF_IGNOREREVOKATION:
  324. case WTPF_IGNOREEXPIRATION:
  325. // Revocation and expiration are a double negative so the bit set
  326. // means revocation and expriation checking is off.
  327. fOn = !fOn;
  328. break;
  329. default:
  330. break;
  331. };
  332. if (fOn)
  333. dwState |= dwMask;
  334. else
  335. dwState &= ~dwMask;
  336. lErr = RegSetValueExU(
  337. hKey,
  338. wszState,
  339. 0, // dwReserved
  340. REG_DWORD,
  341. (BYTE *) &dwState,
  342. sizeof(dwState)
  343. );
  344. if (ERROR_SUCCESS != lErr)
  345. IDSwprintf(IDS_REG_SET_FAILED, lErr);
  346. CLEANUP:
  347. if(hKey)
  348. RegCloseKey(hKey);
  349. }
  350. //---------------------------------------------------------------------------
  351. // wmain
  352. //
  353. //---------------------------------------------------------------------------
  354. extern "C" int __cdecl wmain(int argc, WCHAR ** wargv)
  355. {
  356. int ReturnStatus = 0;
  357. LPWSTR *prgwszKeyName = NULL;
  358. LPWSTR *prgwszValue = NULL;
  359. DWORD dwIndex=0;
  360. DWORD dwCountKey=0;
  361. DWORD dwCountValue=0;
  362. DWORD dwMask = 0;
  363. BOOL fOn=TRUE;
  364. BOOL fQuiet = FALSE;
  365. DWORD dwEntry=0;
  366. WCHAR *pArg=NULL;
  367. WCHAR wszSwitch1[OPTION_SWITCH_SIZE];
  368. WCHAR wszSwitch2[OPTION_SWITCH_SIZE];
  369. //get the module handle
  370. if(!InitModule())
  371. return -1;
  372. //load the strings necessary for parsing the parameters
  373. if( !LoadStringU(hModule, IDS_SWITCH1, wszSwitch1, OPTION_SWITCH_SIZE)
  374. ||!LoadStringU(hModule, IDS_SWITCH2, wszSwitch2, OPTION_SWITCH_SIZE)
  375. )
  376. return -1;
  377. //convert the multitype registry path to the wchar version
  378. prgwszKeyName=(LPWSTR *)malloc(sizeof(LPWSTR)*argc);
  379. prgwszValue=(LPWSTR *)malloc(sizeof(LPWSTR)*argc);
  380. if(!prgwszKeyName || !prgwszValue)
  381. {
  382. IDSwprintf(IDS_FAILED);
  383. ReturnStatus = -1;
  384. goto CommonReturn;
  385. }
  386. //memset
  387. memset(prgwszKeyName, 0, sizeof(LPWSTR)*argc);
  388. memset(prgwszValue, 0, sizeof(LPWSTR)*argc);
  389. while (--argc>0)
  390. {
  391. pArg=*++wargv;
  392. if (*pArg == *wszSwitch1 || *pArg == *wszSwitch2)
  393. {
  394. if(IDSwcsicmp(&(pArg[1]),IDS_OPTION_Q)==0)
  395. fQuiet = TRUE;
  396. else
  397. goto BadUsage;
  398. }
  399. else
  400. {
  401. if(dwCountKey==dwCountValue)
  402. {
  403. prgwszKeyName[dwCountKey]=pArg;
  404. dwCountKey++;
  405. }
  406. else
  407. {
  408. if(dwCountKey==(dwCountValue+1))
  409. {
  410. prgwszValue[dwCountValue]=pArg;
  411. dwCountValue++;
  412. }
  413. else
  414. {
  415. goto BadUsage;
  416. }
  417. }
  418. }
  419. }
  420. if(dwCountKey!=dwCountValue)
  421. {
  422. if(dwCountValue==0)
  423. {
  424. IDSwprintf(IDS_MISS_ARG);
  425. }
  426. else
  427. {
  428. IDSwprintf(IDS_MANY_ARG);
  429. }
  430. goto BadUsage;
  431. }
  432. if(dwCountKey==0)
  433. {
  434. //Display the Software Publisher State Key Values
  435. DisplaySoftPubKeys();
  436. goto CommonReturn;
  437. }
  438. for(dwIndex=0; dwIndex<dwCountKey; dwIndex++)
  439. {
  440. dwEntry = _wtoi(prgwszKeyName[dwIndex]);
  441. if(dwEntry < 1 || dwEntry > NSOFTPUBFLAGS+1)
  442. {
  443. IDSwprintf(IDS_INVALID_CHOICE);
  444. goto BadUsage;
  445. }
  446. //get the Key mask
  447. dwMask = SoftPubFlags[dwEntry-1].dwMask;
  448. if (0 == IDSwcsicmp(prgwszValue[dwIndex], IDS_TRUE))
  449. fOn = TRUE;
  450. else if (0 == IDSwcsicmp(prgwszValue[dwIndex], IDS_FALSE))
  451. fOn = FALSE;
  452. else
  453. {
  454. IDSwprintf(IDS_BAD_VALUE);
  455. goto BadUsage;
  456. }
  457. SetSoftPubKey(dwMask, fOn);
  458. }
  459. if (!fQuiet)
  460. {
  461. IDSwprintf(IDS_UPDATED);
  462. DisplaySoftPubKeys();
  463. }
  464. goto CommonReturn;
  465. BadUsage:
  466. Usage();
  467. ReturnStatus = -1;
  468. CommonReturn:
  469. //free the memory
  470. if(prgwszKeyName)
  471. free(prgwszKeyName);
  472. if(prgwszValue)
  473. free(prgwszValue);
  474. return ReturnStatus;
  475. }