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.

285 lines
7.3 KiB

  1. #include "precomp.h"
  2. #include "global.h"
  3. #include "PolData.h"
  4. /* static */ void CPolicyData::FlushCachedInfData( HANDLE hFile ) {
  5. std::list< std::string >::iterator I = ms_CachedInfData . begin();
  6. DWORD cbWritten;
  7. while( I != ms_CachedInfData . end() ) {
  8. if( !WriteFile( hFile, ( *I ) . c_str(), lstrlen( ( *I ) . c_str() ), &cbWritten, NULL ) ) {
  9. assert( 0 );
  10. ErrorMessage();
  11. return;
  12. }
  13. I++;
  14. }
  15. ms_CachedInfData . erase( ms_CachedInfData . begin(), ms_CachedInfData . end() );
  16. }
  17. /* static */ std::list< std::string > CPolicyData::ms_CachedInfData;
  18. CPolicyData::CPolicyData( eKeyType KeyType, const char* szKeyName, const char* szValueName, DWORD dwVal ) {
  19. m_KeyType = KeyType;
  20. m_ValueType = ValueType_DWORD;
  21. m_dwVal = dwVal;
  22. m_szKeyName = MakeCopyOfString( szKeyName );
  23. m_szValueName = MakeCopyOfString( szValueName );
  24. }
  25. CPolicyData::CPolicyData( eKeyType KeyType, const char* szKeyName, const char* szValueName, const char* szVal ) {
  26. m_KeyType = KeyType;
  27. m_ValueType = ValueType_String;
  28. m_szVal = MakeCopyOfString( szVal );
  29. m_szKeyName = MakeCopyOfString( szKeyName );
  30. m_szValueName = MakeCopyOfString( szValueName );
  31. }
  32. CPolicyData::CPolicyData( eValueType ValType, eKeyType KeyType, const char* szKeyName, const char* szValueName, const char* szVal ) {
  33. m_KeyType = KeyType;
  34. m_ValueType = ValType;
  35. m_szVal = MakeCopyOfString( szVal );
  36. m_szKeyName = MakeCopyOfString( szKeyName );
  37. m_szValueName = MakeCopyOfString( szValueName );
  38. }
  39. CPolicyData::CPolicyData( eKeyType KeyType, const char* szKeyName, const char* szValueName, const OpDelete& r ) {
  40. m_KeyType = KeyType;
  41. m_ValueType = ValueType_Delete;
  42. m_szKeyName = MakeCopyOfString( szKeyName );
  43. m_szValueName = MakeCopyOfString( szValueName );
  44. }
  45. CPolicyData::CPolicyData( const CPolicyData& r )
  46. : m_ValueType ( ValueType_NotInitialized )
  47. { *this = r; }
  48. CPolicyData::~CPolicyData( void ) {
  49. _KillHeapData();
  50. }
  51. CPolicyData& CPolicyData::operator=( const CPolicyData& r )
  52. {
  53. _KillHeapData();
  54. m_KeyType = r . m_KeyType;
  55. switch( m_ValueType = r . m_ValueType )
  56. {
  57. case ValueType_BinaryAsString:
  58. case ValueType_String:
  59. m_szVal = MakeCopyOfString( r . m_szVal );
  60. break;
  61. case ValueType_DWORD:
  62. m_dwVal = r.m_dwVal;
  63. break;
  64. }
  65. m_szKeyName = MakeCopyOfString( r . m_szKeyName );
  66. m_szValueName = MakeCopyOfString( r . m_szValueName );
  67. return *this;
  68. }
  69. #define KEYSIZE 1000
  70. BOOL CPolicyData::SaveToINFFile( HANDLE hFile ) {
  71. DWORD cbWritten;
  72. static char szLastKey[ KEYSIZE ] = "";
  73. // We have to save the key name first
  74. char szKey[ KEYSIZE ];
  75. switch( m_KeyType ) {
  76. case eKeyType_HKEY_CURRENT_USER:
  77. strcpy( szKey, "HKCU," );
  78. break;
  79. case eKeyType_HKEY_LOCAL_MACHINE:
  80. strcpy( szKey, "HKLM," );
  81. break;
  82. default:
  83. assert( 0 );
  84. break;
  85. }
  86. strcat( szKey, "\"" );
  87. strcat( szKey, m_szKeyName );
  88. strcat( szKey, "\",\"" );
  89. strcat( szKey, m_szValueName );
  90. strcat( szKey, "\"" );
  91. switch( m_ValueType ) {
  92. case ValueType_String:
  93. // 0 is FLG_ADDREG_TYPE_SZ
  94. strcat( szKey, ",0,\"" );
  95. if( NULL == m_szVal ) {
  96. strcat( szKey, "" );
  97. }
  98. else {
  99. strcat( szKey, m_szVal );
  100. }
  101. strcat( szKey, "\"" );
  102. break;
  103. case ValueType_DWORD:
  104. // 1 is FLG_ADDREG_TYPE_BINARY
  105. // NOTE that DWORDs are 4 byte BINARY values. This works on
  106. // all platforms. The later FLG_ADDREG_TYPE_DWORD isn't
  107. // supported on Win95.
  108. strcat( szKey, ",1," );
  109. sprintf( szKey + lstrlen( szKey ), "%x, %x, %x, %x",
  110. LOBYTE( LOWORD( m_dwVal ) ),
  111. HIBYTE( LOWORD( m_dwVal ) ),
  112. LOBYTE( HIWORD( m_dwVal ) ),
  113. HIBYTE( HIWORD( m_dwVal ) ));
  114. break;
  115. case ValueType_BinaryAsString:
  116. // 1 is FLG_ADDREG_TYPE_BINARY
  117. strcat( szKey, ",1," );
  118. strcat( szKey, m_szVal );
  119. break;
  120. case ValueType_Delete:
  121. strcat( szKey, "\r\n" );
  122. ms_CachedInfData . push_back( szKey );
  123. return TRUE;
  124. default:
  125. assert( 0 );
  126. break;
  127. }
  128. strcat( szKey, "\r\n" );
  129. RETFAIL( WriteFile( hFile, szKey, lstrlen( szKey ), &cbWritten, NULL ) );
  130. return TRUE;
  131. }
  132. // This will break if the following occurs:
  133. // Same key name as the last call in this process
  134. // but a new file....
  135. BOOL CPolicyData::SaveToREGFile( HANDLE hFile ) {
  136. DWORD cbWritten;
  137. static char szLastKey[ KEYSIZE ] = "";
  138. // We have to save the key name first
  139. char szKey[ KEYSIZE ];
  140. strcpy( szKey, "[" );
  141. switch( m_KeyType ) {
  142. case eKeyType_HKEY_CURRENT_USER:
  143. strcat( szKey, "HKEY_CURRENT_USER\\" );
  144. break;
  145. case eKeyType_HKEY_LOCAL_MACHINE:
  146. strcat( szKey, "HKEY_LOCAL_MACHINE\\" );
  147. break;
  148. default:
  149. assert( 0 );
  150. break;
  151. }
  152. strcat( szKey, m_szKeyName );
  153. strcat( szKey, "]\n" );
  154. if( strcmp( szLastKey, szKey ) ) {
  155. // We hawe to save the key first if it is different
  156. // From the last one
  157. RETFAIL( WriteFile( hFile, szKey, lstrlen( szKey ), &cbWritten, NULL ) );
  158. strcpy( szLastKey, szKey );
  159. }
  160. char szVal[ KEYSIZE ];
  161. strcpy( szVal, "\"" );
  162. strcat( szVal, m_szValueName );
  163. strcat( szVal, "\"=" );
  164. switch( m_ValueType ) {
  165. case ValueType_String:
  166. if( NULL == m_szVal ) {
  167. strcat( szVal, "\"\"\n" );
  168. }
  169. else {
  170. strcat( szVal, "\"" );
  171. strcat( szVal, m_szVal );
  172. strcat( szVal, "\"\n" );
  173. }
  174. break;
  175. case ValueType_DWORD:
  176. strcat( szVal, "dword:" );
  177. sprintf( szVal + lstrlen( szVal ), "%x", m_dwVal );
  178. strcat( szVal, "\n" );
  179. break;
  180. case ValueType_BinaryAsString:
  181. strcat( szVal, "hex:" );
  182. strcat( szVal, m_szVal );
  183. strcat( szVal, "\n" );
  184. break;
  185. case ValueType_Delete:
  186. strcat( szVal, "-\n" );
  187. break;
  188. default:
  189. assert( 0 );
  190. break;
  191. }
  192. RETFAIL( WriteFile( hFile, szVal, lstrlen( szVal ), &cbWritten, NULL ) );
  193. return TRUE;
  194. }
  195. void CPolicyData::_KillHeapData( void ) {
  196. if( ( ValueType_String == m_ValueType ) || ( ValueType_BinaryAsString == m_ValueType ) ) {
  197. delete [] m_szVal;
  198. m_szVal = NULL;
  199. m_ValueType = ValueType_NotInitialized;
  200. }
  201. delete [] m_szKeyName;
  202. m_szKeyName = NULL;
  203. delete m_szValueName;
  204. m_szValueName = NULL;
  205. }