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.

224 lines
5.9 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. ULONG * pcbSecret );
  33. //+---------------------------------------------------------------------------
  34. //
  35. // Class: CCiSecretItem
  36. //
  37. // Purpose: Encapsulats a single "catname domain\user + password"
  38. // subsecret.
  39. //
  40. // Notes: Data is in the form catalog,domain\user,password\0
  41. //
  42. // History: 29-Oct-96 dlee Created.
  43. //
  44. //----------------------------------------------------------------------------
  45. class CCiSecretItem
  46. {
  47. public:
  48. CCiSecretItem() : _pwcCatalog( 0 ), _pwcUser( 0 ), _pwcPassword( 0 ) {}
  49. void Init( WCHAR *pwc )
  50. {
  51. if ( 0 == pwc || 0 == *pwc )
  52. {
  53. _pwcCatalog = 0;
  54. return;
  55. }
  56. // The catalog, comma, domain\user, and comma must be present.
  57. // The password may be an empty string.
  58. _pwcCatalog = pwc;
  59. _pwcUser = wcschr( pwc, L',' );
  60. if ( 0 == _pwcUser )
  61. THROW( CException( STATUS_INTERNAL_ERROR ) );
  62. *_pwcUser++ = 0;
  63. _pwcPassword = wcschr( _pwcUser, L',' );
  64. if ( 0 == _pwcPassword )
  65. THROW( CException( STATUS_INTERNAL_ERROR ) );
  66. *_pwcPassword++ = 0;
  67. // verify the data looks good
  68. if ( ( 0 == *_pwcCatalog ) ||
  69. ( 0 == *_pwcUser ) ||
  70. ( wcslen( _pwcCatalog ) >= MAX_PATH ) ||
  71. ( wcslen( _pwcUser ) >= UNLEN ) ||
  72. ( wcslen( _pwcPassword ) >= PWLEN ) )
  73. THROW( CException( STATUS_INTERNAL_ERROR ) );
  74. }
  75. WCHAR * getCatalog() { return _pwcCatalog; }
  76. WCHAR * getUser() { return _pwcUser; }
  77. WCHAR * getPassword() { return _pwcPassword; }
  78. WCHAR * getNext() { return _pwcPassword + wcslen( _pwcPassword ) + 1; }
  79. private:
  80. WCHAR * _pwcCatalog;
  81. WCHAR * _pwcUser;
  82. WCHAR * _pwcPassword;
  83. };
  84. //+---------------------------------------------------------------------------
  85. //
  86. // Class: CCiSecretRead
  87. //
  88. // Purpose: Reads CCiSecretItems from the ci secret
  89. //
  90. // History: 29-Oct-96 dlee Created.
  91. //
  92. //----------------------------------------------------------------------------
  93. class CCiSecretRead
  94. {
  95. public:
  96. CCiSecretRead( WCHAR const * pwcMachine = 0 ) : _fInit( FALSE ), _cbBuf( 0 )
  97. {
  98. WCHAR *pwc = 0;
  99. if ( GetSecret( pwcMachine, CI_USER_PW_SECRET_NAME, &pwc, &_cbBuf ) )
  100. _xBuf.Set( pwc );
  101. }
  102. ~CCiSecretRead()
  103. {
  104. if ( 0 != _xBuf.Get() )
  105. SecureZeroMemory( _xBuf.Get(), _cbBuf );
  106. }
  107. CCiSecretItem * NextItem()
  108. {
  109. _Advance();
  110. return ( 0 == _item.getCatalog() ) ? 0 : & _item;
  111. }
  112. private:
  113. void _Advance()
  114. {
  115. if ( !_fInit )
  116. {
  117. _item.Init( (WCHAR *) _xBuf.Get() );
  118. _fInit = TRUE;
  119. }
  120. else
  121. {
  122. if ( 0 != _item.getCatalog() )
  123. _item.Init( _item.getNext() );
  124. }
  125. }
  126. BOOL _fInit;
  127. XLocalAllocMem _xBuf;
  128. ULONG _cbBuf;
  129. CCiSecretItem _item;
  130. };
  131. //+---------------------------------------------------------------------------
  132. //
  133. // Class: CCiSecretRead
  134. //
  135. // Purpose: Writes entries to the ci secret
  136. //
  137. // Notes: Secrets are of the form:
  138. // catalognameA,usernameA,passwordA\0
  139. // catalognameB,usernameA,passwordB\0
  140. // \0
  141. //
  142. // History: 29-Oct-96 dlee Created.
  143. //
  144. //----------------------------------------------------------------------------
  145. class CCiSecretWrite
  146. {
  147. public:
  148. CCiSecretWrite( WCHAR const * pwcMachine = 0 )
  149. : _xData( 1024 )
  150. {
  151. if ( 0 != pwcMachine )
  152. {
  153. unsigned cc = wcslen( pwcMachine ) + 1;
  154. _xwcsMachine.SetSize( cc );
  155. RtlCopyMemory( (void *)_xwcsMachine.GetPointer(),
  156. pwcMachine,
  157. cc * sizeof(WCHAR) );
  158. }
  159. }
  160. void Add( WCHAR const * pwcCatalogName,
  161. WCHAR const * pwcUsername,
  162. WCHAR const * pwcPassword )
  163. {
  164. _Append( pwcCatalogName );
  165. _xData[ _xData.Count() ] = L',';
  166. _Append( pwcUsername );
  167. _xData[ _xData.Count() ] = L',';
  168. _Append( pwcPassword );
  169. // add a null to signify end-of-record
  170. _xData[ _xData.Count() ] = 0;
  171. }
  172. void Flush()
  173. {
  174. // add another null to signify end-of-data
  175. _xData[ _xData.Count() ] = 0;
  176. SetSecret( (0 == _xwcsMachine.Size()) ? 0 : _xwcsMachine.GetPointer(),
  177. CI_USER_PW_SECRET_NAME,
  178. _xData.GetPointer(),
  179. _xData.SizeOfInUse() );
  180. }
  181. private:
  182. void _Append( WCHAR const * pwc )
  183. {
  184. while ( 0 != *pwc )
  185. _xData[ _xData.Count() ] = *pwc++;
  186. }
  187. CDynArrayInPlace<WCHAR> _xData;
  188. CDynArrayInPlace<WCHAR> _xwcsMachine;
  189. };