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.

159 lines
4.0 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996
  5. //
  6. // Purpose: Hack app for setting/viewing password entries used by
  7. // CI to index remote scopes. Someday, this will be
  8. // handled by a real GUI admin tool.
  9. //
  10. // History: 28-Oct-96 dlee Created
  11. //
  12. //--------------------------------------------------------------------------
  13. #include <pch.cxx>
  14. #pragma hdrstop
  15. extern "C"
  16. {
  17. #include <ntsam.h>
  18. #include <ntlsa.h>
  19. }
  20. #include <cisecret.hxx>
  21. void DumpSecrets( WCHAR const * pwcMachine )
  22. {
  23. // list all of the ci secret items
  24. CCiSecretRead secret( pwcMachine );
  25. CCiSecretItem * pItem = secret.NextItem();
  26. while ( 0 != pItem )
  27. {
  28. printf( " catalog, user, password: '%ws' '%ws' '%ws'\n",
  29. pItem->getCatalog(),
  30. pItem->getUser(),
  31. pItem->getPassword() );
  32. pItem = secret.NextItem();
  33. }
  34. } //DumpSecrets
  35. void AddOrReplaceSecret(
  36. WCHAR const * pwcCat,
  37. WCHAR const * pwcUser,
  38. WCHAR const * pwcPW )
  39. {
  40. // write objects start blank -- the old entries must be copied
  41. // into the write object, along with the new entry.
  42. CCiSecretWrite secretWrite;
  43. CCiSecretRead secret;
  44. CCiSecretItem * pItem = secret.NextItem();
  45. while ( 0 != pItem )
  46. {
  47. if ( ( !_wcsicmp( pwcCat, pItem->getCatalog() ) ) &&
  48. ( !_wcsicmp( pwcUser, pItem->getUser() ) ) )
  49. {
  50. // don't add this -- replace it below
  51. }
  52. else
  53. {
  54. // just copy the item
  55. secretWrite.Add( pItem->getCatalog(),
  56. pItem->getUser(),
  57. pItem->getPassword() );
  58. }
  59. pItem = secret.NextItem();
  60. }
  61. // add the new item
  62. secretWrite.Add( pwcCat, pwcUser, pwcPW );
  63. // write it to the sam database
  64. secretWrite.Flush();
  65. } //AddOrReplaceSecret
  66. void EmptySecrets()
  67. {
  68. CCiSecretWrite secretWrite;
  69. secretWrite.Flush();
  70. } //EmptySecrets
  71. void Usage()
  72. {
  73. printf( "usage: cipwd catalogname domain\\user pwd\n"
  74. " or: cipwd -d[ump] [machine] (dump the entry list) \n"
  75. " or: cipwd -e[mpty] (empty the entry list)\n" );
  76. exit( 1 );
  77. } //Usage
  78. int __cdecl main( int argc, char *argv[] )
  79. {
  80. TRANSLATE_EXCEPTIONS;
  81. TRY
  82. {
  83. if ( argc < 2 || argc > 4 )
  84. Usage();
  85. if ( 2 == argc || 3 == argc )
  86. {
  87. if ( argv[1][0] == '-' && argv[1][1] == 'd' )
  88. {
  89. if ( 2 == argc )
  90. DumpSecrets( 0 );
  91. else
  92. {
  93. WCHAR awcMachine[ MAX_PATH ];
  94. mbstowcs( awcMachine, argv[2], sizeof awcMachine );
  95. DumpSecrets( awcMachine );
  96. }
  97. }
  98. else if ( argv[1][0] == '-' && argv[1][1] == 'e' )
  99. EmptySecrets();
  100. else
  101. Usage();
  102. }
  103. else
  104. {
  105. WCHAR awcCat[ MAX_PATH + 1 ];
  106. size_t c = mbstowcs( awcCat, argv[1], MAX_PATH );
  107. if ( ( c == -1 ) || ( c == MAX_PATH ) )
  108. THROW( CException( E_INVALIDARG ) );
  109. WCHAR awcUser[ MAX_PATH + 1 ];
  110. c = mbstowcs( awcUser, argv[2], MAX_PATH );
  111. if ( ( c == -1 ) || ( c == MAX_PATH ) )
  112. THROW( CException( E_INVALIDARG ) );
  113. WCHAR awcPwd[ MAX_PATH + 1 ];
  114. c = mbstowcs( awcPwd, argv[3], MAX_PATH );
  115. if ( ( c == -1 ) || ( c == MAX_PATH ) )
  116. THROW( CException( E_INVALIDARG ) );
  117. AddOrReplaceSecret( awcCat, awcUser, awcPwd );
  118. }
  119. }
  120. CATCH ( CException, e )
  121. {
  122. printf( "caught exception 0x%x\n", e.GetErrorCode() );
  123. }
  124. END_CATCH
  125. UNTRANSLATE_EXCEPTIONS;
  126. return 0;
  127. } //main