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.

216 lines
5.5 KiB

  1. ///+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1999.
  5. //
  6. // File: CiSecret.hxx
  7. //
  8. // Contents: secret-related classes and functions
  9. //
  10. // Classes: CCiSecretItem - a sub-secret ( cat-username/ password pair)
  11. // CCiSecretRead - used to read ci secrets
  12. // CCiSecretWrite - used to write ci secrets
  13. //
  14. // History: 29-Oct-96 dlee Created.
  15. //
  16. //----------------------------------------------------------------------------
  17. #pragma once
  18. #define CI_USER_PW_SECRET_NAME L"ci_secret_key_name"
  19. BOOL CiGetPassword(
  20. WCHAR const * pwcCatalog,
  21. WCHAR const * pwcUsername,
  22. WCHAR * pwcPassword );
  23. void SetSecret(
  24. WCHAR const * Server,
  25. WCHAR const * SecretName,
  26. WCHAR const * pSecret,
  27. DWORD cbSecret );
  28. BOOL GetSecret(
  29. WCHAR const * Server,
  30. WCHAR const * SecretName,
  31. WCHAR ** ppSecret );
  32. //+---------------------------------------------------------------------------
  33. //
  34. // Class: CCiSecretItem
  35. //
  36. // Purpose: Encapsulats a single "catname domain\user + password"
  37. // subsecret.
  38. //
  39. // Notes: Data is in the form catalog,domain\user,password\0
  40. //
  41. // History: 29-Oct-96 dlee Created.
  42. //
  43. //----------------------------------------------------------------------------
  44. class CCiSecretItem
  45. {
  46. public:
  47. CCiSecretItem() : _pwcCatalog( 0 ) {}
  48. void Init( WCHAR *pwc )
  49. {
  50. if ( 0 == pwc || 0 == *pwc )
  51. {
  52. _pwcCatalog = 0;
  53. return;
  54. }
  55. // The catalog, comma, domain\user, and comma must be present.
  56. // The password may be an empty string.
  57. _pwcCatalog = pwc;
  58. _pwcUser = wcschr( pwc, L',' );
  59. if ( 0 == _pwcUser )
  60. THROW( CException( STATUS_INTERNAL_ERROR ) );
  61. *_pwcUser++ = 0;
  62. _pwcPassword = wcschr( _pwcUser, L',' );
  63. if ( 0 == _pwcPassword )
  64. THROW( CException( STATUS_INTERNAL_ERROR ) );
  65. *_pwcPassword++ = 0;
  66. // verify the data looks good
  67. if ( ( 0 == *_pwcCatalog ) ||
  68. ( 0 == *_pwcUser ) ||
  69. ( wcslen( _pwcCatalog ) >= MAX_PATH ) ||
  70. ( wcslen( _pwcUser ) >= UNLEN ) ||
  71. ( wcslen( _pwcPassword ) >= PWLEN ) )
  72. THROW( CException( STATUS_INTERNAL_ERROR ) );
  73. }
  74. WCHAR * getCatalog() { return _pwcCatalog; }
  75. WCHAR * getUser() { return _pwcUser; }
  76. WCHAR * getPassword() { return _pwcPassword; }
  77. WCHAR * getNext() { return _pwcPassword + wcslen( _pwcPassword ) + 1; }
  78. private:
  79. WCHAR * _pwcCatalog;
  80. WCHAR * _pwcUser;
  81. WCHAR * _pwcPassword;
  82. };
  83. //+---------------------------------------------------------------------------
  84. //
  85. // Class: CCiSecretRead
  86. //
  87. // Purpose: Reads CCiSecretItems from the ci secret
  88. //
  89. // History: 29-Oct-96 dlee Created.
  90. //
  91. //----------------------------------------------------------------------------
  92. class CCiSecretRead
  93. {
  94. public:
  95. CCiSecretRead( WCHAR const * pwcMachine = 0 ) : _fInit( FALSE )
  96. {
  97. WCHAR *pwc = 0;
  98. if ( GetSecret( pwcMachine, CI_USER_PW_SECRET_NAME, &pwc ) )
  99. _xBuf.Set( pwc );
  100. }
  101. CCiSecretItem * NextItem()
  102. {
  103. _Advance();
  104. return ( 0 == _item.getCatalog() ) ? 0 : & _item;
  105. }
  106. private:
  107. void _Advance()
  108. {
  109. if ( !_fInit )
  110. {
  111. _item.Init( (WCHAR *) _xBuf.Get() );
  112. _fInit = TRUE;
  113. }
  114. else
  115. {
  116. if ( 0 != _item.getCatalog() )
  117. _item.Init( _item.getNext() );
  118. }
  119. }
  120. BOOL _fInit;
  121. XLocalAllocMem _xBuf;
  122. CCiSecretItem _item;
  123. };
  124. //+---------------------------------------------------------------------------
  125. //
  126. // Class: CCiSecretRead
  127. //
  128. // Purpose: Writes entries to the ci secret
  129. //
  130. // Notes: Secrets are of the form:
  131. // catalognameA,usernameA,passwordA\0
  132. // catalognameB,usernameA,passwordB\0
  133. // \0
  134. //
  135. // History: 29-Oct-96 dlee Created.
  136. //
  137. //----------------------------------------------------------------------------
  138. class CCiSecretWrite
  139. {
  140. public:
  141. CCiSecretWrite( WCHAR const * pwcMachine = 0 )
  142. : _xData( 1024 )
  143. {
  144. if ( 0 != pwcMachine )
  145. {
  146. unsigned cc = wcslen( pwcMachine ) + 1;
  147. _xwcsMachine.SetSize( cc );
  148. RtlCopyMemory( (void *)_xwcsMachine.GetPointer(),
  149. pwcMachine,
  150. cc * sizeof(WCHAR) );
  151. }
  152. }
  153. void Add( WCHAR const * pwcCatalogName,
  154. WCHAR const * pwcUsername,
  155. WCHAR const * pwcPassword )
  156. {
  157. _Append( pwcCatalogName );
  158. _xData[ _xData.Count() ] = L',';
  159. _Append( pwcUsername );
  160. _xData[ _xData.Count() ] = L',';
  161. _Append( pwcPassword );
  162. // add a null to signify end-of-record
  163. _xData[ _xData.Count() ] = 0;
  164. }
  165. void Flush()
  166. {
  167. // add another null to signify end-of-data
  168. _xData[ _xData.Count() ] = 0;
  169. SetSecret( (0 == _xwcsMachine.Size()) ? 0 : _xwcsMachine.GetPointer(),
  170. CI_USER_PW_SECRET_NAME,
  171. _xData.GetPointer(),
  172. _xData.SizeOfInUse() );
  173. }
  174. private:
  175. void _Append( WCHAR const * pwc )
  176. {
  177. while ( 0 != *pwc )
  178. _xData[ _xData.Count() ] = *pwc++;
  179. }
  180. CDynArrayInPlace<WCHAR> _xData;
  181. CDynArrayInPlace<WCHAR> _xwcsMachine;
  182. };