Source code of Windows XP (NT5)
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.

996 lines
28 KiB

  1. //
  2. //
  3. //
  4. #include "olecnfg.h"
  5. BOOL SetGlobalKey( int Key, int Value )
  6. {
  7. DWORD RegStatus;
  8. HKEY hReg;
  9. DWORD Disposition;
  10. char * ValueName;
  11. if ( hRegOle == 0 )
  12. {
  13. RegStatus = RegCreateKeyEx(
  14. HKEY_LOCAL_MACHINE,
  15. "SOFTWARE\\Microsoft\\OLE",
  16. 0,
  17. "REG_SZ",
  18. REG_OPTION_NON_VOLATILE,
  19. KEY_READ | KEY_WRITE,
  20. NULL,
  21. &hRegOle,
  22. &Disposition );
  23. if ( RegStatus != ERROR_SUCCESS )
  24. {
  25. printf( "Could not open HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\OLE for writing\n" );
  26. return FALSE;
  27. }
  28. }
  29. // TODO : Extra stuff to do for PersonalClasses and InstallCommon.
  30. if ( (Key == DEFAULT_LAUNCH_PERMISSION) ||
  31. (Key == DEFAULT_ACCESS_PERMISSION) )
  32. {
  33. RegStatus = RegCreateKeyEx(
  34. hRegOle,
  35. GlobalKeyNames[Key],
  36. 0,
  37. "REG_SZ",
  38. REG_OPTION_NON_VOLATILE,
  39. KEY_READ | KEY_WRITE,
  40. NULL,
  41. &hReg,
  42. &Disposition );
  43. if ( RegStatus != ERROR_SUCCESS )
  44. {
  45. printf( "Unable to open or add global key %s (status %d)\n",
  46. GlobalKeyNames[Key],
  47. RegStatus );
  48. return FALSE;
  49. }
  50. ValueName = NULL;
  51. }
  52. else
  53. {
  54. hReg = hRegOle;
  55. ValueName = (char *)GlobalKeyNames[Key];
  56. }
  57. if ( Key == LEGACY_AUTHENTICATION_LEVEL )
  58. {
  59. RegStatus = RegSetValueEx(
  60. hReg,
  61. ValueName,
  62. 0,
  63. REG_DWORD,
  64. (LPBYTE)&Value,
  65. sizeof(DWORD) );
  66. }
  67. else if ( Key != DEFAULT_ACCESS_PERMISSION )
  68. {
  69. RegStatus = RegSetValueEx(
  70. hReg,
  71. ValueName,
  72. 0,
  73. REG_SZ,
  74. (LPBYTE)(Value == YES ? "Y" : "N"),
  75. 2 * sizeof(char) );
  76. }
  77. else
  78. RegStatus = ERROR_SUCCESS;
  79. if ( RegStatus != ERROR_SUCCESS )
  80. {
  81. printf( "Unable to set value for %s (status %d)\n",
  82. GlobalKeyNames[Key],
  83. RegStatus );
  84. return FALSE;
  85. }
  86. if ( Key == LEGACY_AUTHENTICATION_LEVEL )
  87. {
  88. printf( "Global setting %s set to %d.\n",
  89. GlobalKeyNames[Key],
  90. Value );
  91. }
  92. else if ( Key == DEFAULT_ACCESS_PERMISSION )
  93. {
  94. printf( "Global setting %s set to on.\n",
  95. GlobalKeyNames[Key] );
  96. }
  97. else
  98. {
  99. printf( "Global setting %s set to %c.\n",
  100. GlobalKeyNames[Key],
  101. Value == YES ? 'Y' : 'N' );
  102. }
  103. return TRUE;
  104. }
  105. void DisplayGlobalSettings()
  106. {
  107. HKEY hReg;
  108. DWORD RegStatus;
  109. int Key;
  110. DWORD Type;
  111. DWORD Value;
  112. DWORD BufSize;
  113. char * ValueName;
  114. RegStatus = RegOpenKeyEx(
  115. HKEY_LOCAL_MACHINE,
  116. "SOFTWARE\\Microsoft\\OLE",
  117. 0,
  118. KEY_READ,
  119. &hRegOle );
  120. if ( RegStatus != ERROR_SUCCESS )
  121. {
  122. printf( "Could not open HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\OLE\n" );
  123. return;
  124. }
  125. printf( "\nGlobal OLE registry settings :\n" );
  126. for ( Key = 1; Key <= GLOBAL_KEYS; Key++ )
  127. {
  128. if ( (Key == DEFAULT_LAUNCH_PERMISSION) ||
  129. (Key == DEFAULT_ACCESS_PERMISSION) )
  130. {
  131. RegStatus = RegOpenKeyEx(
  132. hRegOle,
  133. GlobalKeyNames[Key],
  134. 0,
  135. KEY_READ,
  136. &hReg );
  137. if ( RegStatus != ERROR_SUCCESS )
  138. {
  139. printf( " %-28sN (key does not exist or could not be opened)\n",
  140. GlobalKeyNames[Key] );
  141. continue;
  142. }
  143. ValueName = NULL;
  144. }
  145. else
  146. {
  147. hReg = hRegOle;
  148. ValueName = (char *)GlobalKeyNames[Key];
  149. }
  150. if ( Key != DEFAULT_ACCESS_PERMISSION )
  151. {
  152. BufSize = sizeof(DWORD);
  153. RegStatus = RegQueryValueEx(
  154. hReg,
  155. ValueName,
  156. 0,
  157. &Type,
  158. (LPBYTE) &Value,
  159. &BufSize );
  160. }
  161. else
  162. RegStatus = ERROR_SUCCESS;
  163. if ( RegStatus != ERROR_SUCCESS )
  164. {
  165. if ( Key == DEFAULT_LAUNCH_PERMISSION )
  166. printf( " %-28sN (key value could not be read)\n",
  167. GlobalKeyNames[Key] );
  168. else
  169. printf( " %-28s%c (value not present)\n",
  170. GlobalKeyNames[Key],
  171. (Key == LEGACY_AUTHENTICATION_LEVEL) ? '2' : 'N' );
  172. continue;
  173. }
  174. if ( Key == LEGACY_AUTHENTICATION_LEVEL )
  175. {
  176. printf( " %-28s%d\n",
  177. GlobalKeyNames[Key],
  178. Value );
  179. }
  180. else if ( Key == DEFAULT_ACCESS_PERMISSION )
  181. {
  182. printf( " %-28son\n",
  183. GlobalKeyNames[Key] );
  184. }
  185. else
  186. {
  187. printf( " %-28s%c\n",
  188. GlobalKeyNames[Key],
  189. (char)CharUpper((LPSTR)((char *)&Value)[0]) );
  190. }
  191. }
  192. }
  193. void DisplayClsidKeys(
  194. CLSID_INFO * ClsidInfo )
  195. {
  196. HKEY hProgId;
  197. HKEY hClsid;
  198. HKEY hProgIdClsid;
  199. HKEY hKey;
  200. DWORD RegStatus;
  201. DWORD RegType;
  202. DWORD BufSize;
  203. char ProgIdClsid[64];
  204. char Value[128];
  205. int Key;
  206. BOOL HasRunAs;
  207. char Password[64];
  208. LSA_HANDLE hPolicy;
  209. LSA_OBJECT_ATTRIBUTES ObjAttributes;
  210. LSA_UNICODE_STRING LsaKey;
  211. LSA_UNICODE_STRING * LsaData;
  212. WCHAR wszKey[64];
  213. WCHAR wszPassword[64];
  214. NTSTATUS NtStatus;
  215. RegStatus = RegOpenKeyEx(
  216. HKEY_CLASSES_ROOT,
  217. "CLSID",
  218. 0,
  219. KEY_READ,
  220. &hRegClsid );
  221. if ( RegStatus != ERROR_SUCCESS )
  222. {
  223. printf( "Could not open HKEY_CLASSES_ROOT\\CLSID for reading.\n" );
  224. return;
  225. }
  226. if ( ClsidInfo->ProgId )
  227. {
  228. RegStatus = RegOpenKeyEx(
  229. HKEY_CLASSES_ROOT,
  230. ClsidInfo->ProgId,
  231. 0,
  232. KEY_READ,
  233. &hProgId );
  234. if ( RegStatus != ERROR_SUCCESS )
  235. {
  236. printf( "Couldn't open ProgID %s\n", ClsidInfo->ProgId );
  237. return;
  238. }
  239. RegStatus = RegOpenKeyEx(
  240. hProgId,
  241. "CLSID",
  242. 0,
  243. KEY_READ,
  244. &hProgIdClsid );
  245. if ( RegStatus != ERROR_SUCCESS )
  246. {
  247. printf( "Couldn't open CLSID key for ProgID %s\n", ClsidInfo->ProgId );
  248. return;
  249. }
  250. BufSize = sizeof(ProgIdClsid);
  251. RegStatus = RegQueryValueEx(
  252. hProgIdClsid,
  253. NULL,
  254. 0,
  255. &RegType,
  256. (LPBYTE) ProgIdClsid,
  257. &BufSize );
  258. if ( RegStatus != ERROR_SUCCESS )
  259. {
  260. printf( "Couldn't open CLSID value for ProgID %s\n", ClsidInfo->ProgId );
  261. return;
  262. }
  263. if ( ClsidInfo->Clsid &&
  264. (_stricmp( ClsidInfo->Clsid, ProgIdClsid ) != 0) )
  265. {
  266. printf( "ProgID %s CLSID key value %s differs from given CLSID %s.\n",
  267. ClsidInfo->ProgId,
  268. ProgIdClsid,
  269. ClsidInfo->Clsid );
  270. return;
  271. }
  272. else
  273. ClsidInfo->Clsid = ProgIdClsid;
  274. }
  275. if ( ! ClsidInfo->Clsid )
  276. {
  277. printf( "Could not determine CLSID.\n" );
  278. return;
  279. }
  280. RegStatus = RegOpenKeyEx(
  281. hRegClsid,
  282. ClsidInfo->Clsid,
  283. 0,
  284. KEY_READ,
  285. &hClsid );
  286. if ( RegStatus != ERROR_SUCCESS )
  287. {
  288. printf( "Could not open CLSID %s\n", ClsidInfo->Clsid );
  289. return;
  290. }
  291. putchar( '\n' );
  292. if ( ClsidInfo->ProgId )
  293. printf( "Server settings for ProgID %s, ", ClsidInfo->ProgId );
  294. else
  295. printf( "Server settings for " );
  296. printf( "CLSID %s\n", ClsidInfo->Clsid );
  297. HasRunAs = FALSE;
  298. for ( Key = 1; Key <= CLSID_KEYS; Key++ )
  299. {
  300. RegStatus = RegOpenKeyEx(
  301. hClsid,
  302. ClsidKeyNames[Key],
  303. 0,
  304. KEY_READ,
  305. &hKey );
  306. if ( RegStatus != ERROR_SUCCESS )
  307. continue;
  308. BufSize = sizeof(Value);
  309. if ( Key != ACCESS_PERMISSION )
  310. {
  311. RegStatus = RegQueryValueEx(
  312. hKey,
  313. NULL,
  314. 0,
  315. &RegType,
  316. (LPBYTE) Value,
  317. &BufSize );
  318. }
  319. else
  320. RegStatus = ERROR_SUCCESS;
  321. if ( RegStatus != ERROR_SUCCESS )
  322. {
  323. printf( " %-28s(key exists, but value could not be read)\n",
  324. ClsidKeyNames[Key] );
  325. continue;
  326. }
  327. printf( " %-28s%s\n",
  328. ClsidKeyNames[Key],
  329. (Key == ACCESS_PERMISSION) ? "on" : Value );
  330. if ( (Key == RUN_AS) && (_stricmp(Value,"Interactive User") != 0) )
  331. HasRunAs = TRUE;
  332. }
  333. if ( ! HasRunAs )
  334. return;
  335. //
  336. // Give the option of verifying the RunAs password.
  337. //
  338. printf( "\nCLSID configured with RunAs. Would you like to verify the password? " );
  339. if ( (char)CharUpper((LPSTR)UIntToPtr(getchar())) != 'Y' )
  340. return;
  341. while ( getchar() != '\n' )
  342. ;
  343. putchar( '\n' );
  344. lstrcpyW( wszKey, L"SCM:" );
  345. MultiByteToWideChar( CP_ACP,
  346. MB_PRECOMPOSED,
  347. ClsidInfo->Clsid,
  348. -1,
  349. &wszKey[lstrlenW(wszKey)],
  350. sizeof(wszKey)/2 - lstrlenW(wszKey) );
  351. LsaKey.Length = (USHORT)((lstrlenW(wszKey) + 1) * sizeof(WCHAR));
  352. LsaKey.MaximumLength = sizeof(wszKey);
  353. LsaKey.Buffer = wszKey;
  354. InitializeObjectAttributes( &ObjAttributes, NULL, 0L, NULL, NULL );
  355. // Open the local security policy
  356. NtStatus = LsaOpenPolicy( NULL,
  357. &ObjAttributes,
  358. POLICY_CREATE_SECRET,
  359. &hPolicy );
  360. if ( ! NT_SUCCESS( NtStatus ) )
  361. {
  362. printf( "Could not open RunAs password (0x%x)\n", NtStatus );
  363. return;
  364. }
  365. // Retrive private data
  366. NtStatus = LsaRetrievePrivateData( hPolicy, &LsaKey, &LsaData );
  367. if ( ! NT_SUCCESS(NtStatus) )
  368. {
  369. printf( "Could not open RunAs password (0x%x)\n", NtStatus );
  370. return;
  371. }
  372. LsaClose(hPolicy);
  373. for (;;)
  374. {
  375. printf( "Password : " );
  376. ReadPassword( Password );
  377. if ( strcmp( Password, "dcom4ever" ) == 0 )
  378. {
  379. printf( "\nThe RunAs password is %ws\n", LsaData->Buffer );
  380. return;
  381. }
  382. MultiByteToWideChar( CP_ACP,
  383. MB_PRECOMPOSED,
  384. Password,
  385. -1,
  386. wszPassword,
  387. sizeof(wszPassword) / sizeof(WCHAR) );
  388. if ( lstrcmpW( wszPassword, LsaData->Buffer ) != 0 )
  389. {
  390. printf( "\nPassword does not match RunAs password.\n" );
  391. printf( "Enter another password or hit Control-C to exit.\n\n" );
  392. }
  393. else
  394. {
  395. printf( "\nPasswords match.\n" );
  396. return;
  397. }
  398. }
  399. }
  400. void UpdateClsidKeys( CLSID_INFO * ClsidInfo )
  401. {
  402. HKEY hProgId;
  403. HKEY hClsid;
  404. HKEY hProgIdClsid;
  405. HKEY hKey;
  406. DWORD RegStatus;
  407. DWORD Disposition;
  408. DWORD RegType;
  409. char ProgIdClsid[64];
  410. char Response[64];
  411. DWORD BufSize;
  412. int n;
  413. RegStatus = RegOpenKeyEx(
  414. HKEY_CLASSES_ROOT,
  415. "CLSID",
  416. 0,
  417. KEY_READ | KEY_WRITE,
  418. &hRegClsid );
  419. if ( RegStatus != ERROR_SUCCESS )
  420. {
  421. printf( "Could not open HKEY_CLASSES_ROOT\\CLSID for writing\n" );
  422. return;
  423. }
  424. hProgId = 0;
  425. hClsid = 0;
  426. if ( ClsidInfo->ProgId )
  427. {
  428. RegStatus = RegCreateKeyEx(
  429. HKEY_CLASSES_ROOT,
  430. ClsidInfo->ProgId,
  431. 0,
  432. "REG_SZ",
  433. REG_OPTION_NON_VOLATILE,
  434. KEY_READ | KEY_WRITE,
  435. NULL,
  436. &hProgId,
  437. &Disposition );
  438. if ( RegStatus != ERROR_SUCCESS )
  439. {
  440. printf( "Could not open or create ProgID key %s.\n",
  441. ClsidInfo->ProgId);
  442. return;
  443. }
  444. if ( Disposition == REG_CREATED_NEW_KEY )
  445. printf( "ProgId key %s created.\n", ClsidInfo->ProgId );
  446. if ( ClsidInfo->ProgIdDescription )
  447. {
  448. RegStatus = RegSetValueEx(
  449. hProgId,
  450. NULL,
  451. 0,
  452. REG_SZ,
  453. (LPBYTE) ClsidInfo->ProgIdDescription,
  454. strlen(ClsidInfo->ProgIdDescription) + sizeof(char) );
  455. if ( RegStatus != ERROR_SUCCESS )
  456. {
  457. printf( "Could not set description value for ProgID %s.\n", ClsidInfo->ProgId );
  458. return;
  459. }
  460. printf( "Setting description value %s for ProgID %s.\n",
  461. ClsidInfo->ProgIdDescription,
  462. ClsidInfo->ProgId );
  463. }
  464. RegStatus = RegCreateKeyEx(
  465. hProgId,
  466. "CLSID",
  467. 0,
  468. "REG_SZ",
  469. REG_OPTION_NON_VOLATILE,
  470. KEY_READ | KEY_WRITE,
  471. NULL,
  472. &hProgIdClsid,
  473. &Disposition );
  474. if ( RegStatus != ERROR_SUCCESS )
  475. {
  476. printf( "Could not open or create CLSID key for ProgID %s.\n",
  477. ClsidInfo->ProgId );
  478. return;
  479. }
  480. //
  481. // Check if a CLSID key value already exists for this ProgID. If so,
  482. // and a CLSID was specified to us then check if they differ.
  483. //
  484. BufSize = sizeof(ProgIdClsid);
  485. RegStatus = RegQueryValueEx(
  486. hProgIdClsid,
  487. NULL,
  488. 0,
  489. &RegType,
  490. (LPBYTE) ProgIdClsid,
  491. &BufSize );
  492. if ( RegStatus == ERROR_SUCCESS )
  493. {
  494. if ( ClsidInfo->Clsid &&
  495. (_stricmp(ClsidInfo->Clsid, ProgIdClsid) != 0) )
  496. {
  497. printf( "ProgID %s has existing CLSID key value %s\n",
  498. ClsidInfo->ProgId,
  499. ProgIdClsid );
  500. printf( "which differs from given CLSID %s.\n",
  501. ClsidInfo->Clsid );
  502. printf( "Would you like to replace the existing CLSID value with the new CLSID value? " );
  503. gets( Response );
  504. if ( (char)CharUpper((LPSTR)Response[0]) != 'Y' )
  505. ClsidInfo->Clsid = ProgIdClsid;
  506. }
  507. else
  508. ClsidInfo->Clsid = ProgIdClsid;
  509. }
  510. if ( ! ClsidInfo->Clsid )
  511. {
  512. printf( "CLSID for ProgID %s not specified.\n",
  513. ClsidInfo->ProgId );
  514. return;
  515. }
  516. if ( ClsidInfo->Clsid != ProgIdClsid )
  517. {
  518. RegStatus = RegSetValueEx(
  519. hProgIdClsid,
  520. NULL,
  521. 0,
  522. REG_SZ,
  523. (LPBYTE) ClsidInfo->Clsid,
  524. strlen(ClsidInfo->Clsid) + sizeof(char) );
  525. if ( RegStatus != ERROR_SUCCESS )
  526. {
  527. printf( "Could not set CLSID value for ProgID %s.\n", ClsidInfo->ProgId );
  528. return;
  529. }
  530. printf( "Setting CLSID value %s for ProgID %s.\n",
  531. ClsidInfo->Clsid,
  532. ClsidInfo->ProgId );
  533. }
  534. }
  535. RegStatus = RegCreateKeyEx(
  536. hRegClsid,
  537. ClsidInfo->Clsid,
  538. 0,
  539. "REG_SZ",
  540. REG_OPTION_NON_VOLATILE,
  541. KEY_READ | KEY_WRITE,
  542. NULL,
  543. &hClsid,
  544. &Disposition );
  545. if ( RegStatus != ERROR_SUCCESS )
  546. {
  547. printf( "Could not open or create CLSID key %s.\n", ClsidInfo->Clsid );
  548. return;
  549. }
  550. if ( Disposition == REG_CREATED_NEW_KEY )
  551. printf( "CLSID key %s created.\n", ClsidInfo->Clsid );
  552. if ( ClsidInfo->ClsidDescription )
  553. {
  554. RegStatus = RegSetValueEx(
  555. hClsid,
  556. NULL,
  557. 0,
  558. REG_SZ,
  559. (LPBYTE) ClsidInfo->ClsidDescription,
  560. strlen(ClsidInfo->ClsidDescription) + sizeof(char) );
  561. if ( RegStatus != ERROR_SUCCESS )
  562. {
  563. printf( "Could not set description value for CLSID %s.\n", ClsidInfo->Clsid );
  564. return;
  565. }
  566. printf( "Setting description value %s for CLSID %s.\n",
  567. ClsidInfo->ClsidDescription,
  568. ClsidInfo->Clsid );
  569. }
  570. //
  571. // Now add and delete individual keys on this CLSID.
  572. //
  573. if ( (ClsidInfo->LaunchPermission == YES) ||
  574. (ClsidInfo->LaunchPermission == NO) )
  575. {
  576. SetClsidKey( hClsid,
  577. ClsidInfo->Clsid,
  578. ClsidKeyNames[LAUNCH_PERMISSION],
  579. (ClsidInfo->LaunchPermission == YES) ? "Y" : "N" );
  580. }
  581. if ( ClsidInfo->AccessPermission == YES )
  582. {
  583. SetClsidKey( hClsid,
  584. ClsidInfo->Clsid,
  585. ClsidKeyNames[ACCESS_PERMISSION],
  586. NULL );
  587. }
  588. if ( (ClsidInfo->ActivateAtStorage == YES) ||
  589. (ClsidInfo->ActivateAtStorage == NO) )
  590. {
  591. SetClsidKey( hClsid,
  592. ClsidInfo->Clsid,
  593. ClsidKeyNames[ACTIVATE_AT_STORAGE],
  594. (ClsidInfo->ActivateAtStorage == YES) ? "Y" : "N" );
  595. }
  596. for ( n = 1; n <= CLSID_PATH_KEYS; n++ )
  597. {
  598. if ( ! ClsidInfo->ServerPaths[n] )
  599. continue;
  600. if ( ClsidInfo->ServerPaths[n][0] == '\0' )
  601. DeleteClsidKey( hClsid,
  602. ClsidInfo->Clsid,
  603. ClsidKeyNames[n] );
  604. else
  605. SetClsidKey( hClsid,
  606. ClsidInfo->Clsid,
  607. ClsidKeyNames[n],
  608. ClsidInfo->ServerPaths[n] );
  609. }
  610. if ( ClsidInfo->RemoteServerName )
  611. {
  612. if ( ClsidInfo->RemoteServerName[0] == '\0' )
  613. DeleteClsidKey( hClsid,
  614. ClsidInfo->Clsid,
  615. ClsidKeyNames[REMOTE_SERVER_NAME] );
  616. else
  617. SetClsidKey( hClsid,
  618. ClsidInfo->Clsid,
  619. ClsidKeyNames[REMOTE_SERVER_NAME],
  620. ClsidInfo->RemoteServerName );
  621. }
  622. if ( ClsidInfo->RunAsUserName )
  623. {
  624. DWORD CharRead;
  625. char Password1[64];
  626. char Password2[64];
  627. LSA_HANDLE hPolicy;
  628. LSA_OBJECT_ATTRIBUTES ObjAttributes;
  629. LSA_UNICODE_STRING LsaKey;
  630. LSA_UNICODE_STRING LsaData;
  631. WCHAR wszKey[64];
  632. WCHAR wszPassword[64];
  633. NTSTATUS NtStatus;
  634. BOOL Status;
  635. BOOL RunAsInteractiveUser;
  636. RunAsInteractiveUser = (_stricmp(ClsidInfo->RunAsUserName,"Interactive User") == 0);
  637. if ( ! RunAsInteractiveUser )
  638. {
  639. InitializeObjectAttributes( &ObjAttributes, NULL, 0L, NULL, NULL );
  640. // Open the local security policy
  641. NtStatus = LsaOpenPolicy( NULL,
  642. &ObjAttributes,
  643. POLICY_CREATE_SECRET,
  644. &hPolicy );
  645. if ( ! NT_SUCCESS( NtStatus ) )
  646. {
  647. printf( "Could not setup RunAs (0x%x)\n", NtStatus );
  648. return;
  649. }
  650. lstrcpyW( wszKey, L"SCM:" );
  651. MultiByteToWideChar( CP_ACP,
  652. MB_PRECOMPOSED,
  653. ClsidInfo->Clsid,
  654. -1,
  655. &wszKey[lstrlenW(wszKey)],
  656. sizeof(wszKey)/2 - lstrlenW(wszKey) );
  657. LsaKey.Length = (USHORT)((lstrlenW(wszKey) + 1) * sizeof(WCHAR));
  658. LsaKey.MaximumLength = sizeof(wszKey);
  659. LsaKey.Buffer = wszKey;
  660. }
  661. if ( ClsidInfo->RunAsUserName[0] == '\0' )
  662. {
  663. DeleteClsidKey( hClsid,
  664. ClsidInfo->Clsid,
  665. ClsidKeyNames[RUN_AS] );
  666. LsaStorePrivateData( hPolicy, &LsaKey, NULL );
  667. }
  668. else
  669. {
  670. Status = SetClsidKey( hClsid,
  671. ClsidInfo->Clsid,
  672. ClsidKeyNames[RUN_AS],
  673. ClsidInfo->RunAsUserName );
  674. if ( ! Status )
  675. return;
  676. if ( ! RunAsInteractiveUser && (ClsidInfo->RunAsPassword[0] == '*') )
  677. {
  678. for (;;)
  679. {
  680. printf( "Enter RunAs password for %s : ", ClsidInfo->RunAsUserName );
  681. ReadPassword( Password1 );
  682. printf( "Confirm password : " );
  683. ReadPassword( Password2 );
  684. if ( strcmp( Password1, Password2 ) != 0 )
  685. {
  686. printf( "Passwords differ, try again or hit Control-C to exit.\n" );
  687. continue;
  688. }
  689. if ( Password1[0] == '\0' )
  690. {
  691. printf( "Do you really want a blank password? " );
  692. gets( Response );
  693. if ( (char)CharUpper((LPSTR)Response[0]) != 'Y' )
  694. continue;
  695. }
  696. break;
  697. }
  698. ClsidInfo->RunAsPassword = Password1;
  699. } // if password == "*"
  700. // Got a good one!
  701. if ( ! RunAsInteractiveUser )
  702. {
  703. MultiByteToWideChar( CP_ACP,
  704. MB_PRECOMPOSED,
  705. ClsidInfo->RunAsPassword,
  706. -1,
  707. wszPassword,
  708. sizeof(wszPassword)/2 );
  709. LsaData.Length = (USHORT)((lstrlenW(wszPassword) + 1) * sizeof(WCHAR));
  710. LsaData.MaximumLength = sizeof(wszPassword);
  711. LsaData.Buffer = wszPassword;
  712. // Store private data
  713. NtStatus = LsaStorePrivateData( hPolicy, &LsaKey, &LsaData );
  714. if ( ! NT_SUCCESS(NtStatus) )
  715. {
  716. printf( "Could not store password securely (0x%x)\n", NtStatus );
  717. return;
  718. }
  719. LsaClose(hPolicy);
  720. }
  721. }
  722. }
  723. printf( "CLSID keys updated successfully.\n" );
  724. }
  725. BOOL SetClsidKey(
  726. HKEY hClsid,
  727. char * Clsid,
  728. const char * Key,
  729. char * Value )
  730. {
  731. HKEY hKey;
  732. DWORD RegStatus;
  733. DWORD Disposition;
  734. DWORD ValueType;
  735. DWORD ValueSize;
  736. char OldValue[256];
  737. BOOL HasOldValue;
  738. HasOldValue = FALSE;
  739. RegStatus = RegCreateKeyEx(
  740. hClsid,
  741. Key,
  742. 0,
  743. "REG_SZ",
  744. REG_OPTION_NON_VOLATILE,
  745. KEY_READ | KEY_WRITE,
  746. NULL,
  747. &hKey,
  748. &Disposition );
  749. if ( RegStatus != ERROR_SUCCESS )
  750. {
  751. printf( "Could not create key %s for CLSID %s\n", Key, Clsid );
  752. return FALSE;
  753. }
  754. if ( Disposition == REG_CREATED_NEW_KEY )
  755. {
  756. printf( "Added key %s for CLSID %s\n", Key, Clsid );
  757. }
  758. else
  759. {
  760. ValueSize = sizeof(OldValue);
  761. RegStatus = RegQueryValueEx(
  762. hKey,
  763. NULL,
  764. 0,
  765. &ValueType,
  766. OldValue,
  767. &ValueSize );
  768. HasOldValue = (RegStatus == ERROR_SUCCESS);
  769. }
  770. if ( ! Value )
  771. return TRUE;
  772. RegStatus = RegSetValueEx(
  773. hKey,
  774. NULL,
  775. 0,
  776. REG_SZ,
  777. (LPBYTE) Value,
  778. strlen(Value) + sizeof(char) );
  779. if ( RegStatus != ERROR_SUCCESS )
  780. {
  781. printf( "Could not set value %s for key %s\n", Value, Key );
  782. return FALSE;
  783. }
  784. if ( HasOldValue )
  785. printf( "Changed value from %s to %s for key %s\n", OldValue, Value, Key );
  786. else
  787. printf( "Added value %s for key %s\n", Value, Key );
  788. return TRUE;
  789. }
  790. BOOL DeleteClsidKey(
  791. HKEY hClsid,
  792. char * Clsid,
  793. const char * Key )
  794. {
  795. DWORD RegStatus;
  796. RegStatus = RegDeleteKey( hClsid, Key );
  797. if ( RegStatus != ERROR_SUCCESS )
  798. {
  799. printf( "Could not delete key %s for CLSID %s\n", Key, Clsid );
  800. return FALSE;
  801. }
  802. printf( "Deleted key %s for CLSID %s\n", Key, Clsid );
  803. return TRUE;
  804. }
  805. void ReadPassword( char * Password )
  806. {
  807. int c, n;
  808. n = 0;
  809. for (;;)
  810. {
  811. c = _getch();
  812. // ^C
  813. if ( c == 0x3 )
  814. {
  815. putchar( '\n' );
  816. ExitProcess( 0 );
  817. }
  818. // Backspace
  819. if ( c == 0x8 )
  820. {
  821. if ( n )
  822. {
  823. n--;
  824. _putch( 0x8 );
  825. _putch( ' ' );
  826. _putch( 0x8 );
  827. }
  828. continue;
  829. }
  830. // Return
  831. if ( c == '\r' )
  832. break;
  833. Password[n++] = (char) c;
  834. _putch( '*' );
  835. }
  836. Password[n] = 0;
  837. putchar( '\n' );
  838. }
  839. BOOL ControlCConsoleHandler( DWORD ControlType )
  840. {
  841. if ( (ControlType == CTRL_C_EVENT) || (ControlType == CTRL_BREAK_EVENT) )
  842. {
  843. printf( "RunAs password unchanged\n" );
  844. ExitProcess( 0 );
  845. }
  846. return FALSE;
  847. }