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.

233 lines
8.3 KiB

  1. // ImportExportConfig.cpp : Implementation of CImportExportConfig
  2. #include "stdafx.h"
  3. #include "IISUIObj.h"
  4. #include "ImportExportConfig.h"
  5. #include "ExportUI.h"
  6. #include "ImportUI.h"
  7. #include "util.h"
  8. #include <strsafe.h>
  9. #include "cryptpass.h"
  10. /////////////////////////////////////////////////////////////////////////////
  11. // CImportExportConfig
  12. // Checks a pointer which should be non NULL - can be used as follows.
  13. #define CheckPointer(p,ret) {if((p)==NULL) return (ret);}
  14. // HRESULT Foo(VOID *pBar)
  15. // {
  16. // CheckPointer(pBar,E_INVALIDARG)
  17. // }
  18. //
  19. // Or if the function returns a boolean
  20. //
  21. // BOOL Foo(VOID *pBar)
  22. // {
  23. // CheckPointer(pBar,FALSE)
  24. // }
  25. HRESULT ValidateBSTRIsntNULL(BSTR pbstrString)
  26. {
  27. if( !pbstrString ) return E_INVALIDARG;
  28. if( pbstrString[0] == 0 ) return E_INVALIDARG;
  29. return NOERROR;
  30. }
  31. STDMETHODIMP CImportExportConfig::get_MachineName(BSTR *pVal)
  32. {
  33. CheckPointer(pVal, E_POINTER);
  34. _bstr_t bstrTempName = (LPCTSTR) m_strMachineName;
  35. *pVal = bstrTempName.copy();
  36. return S_OK;
  37. }
  38. STDMETHODIMP CImportExportConfig::put_MachineName(BSTR newVal)
  39. {
  40. HRESULT hr = S_OK;
  41. if(FAILED(hr = ValidateBSTRIsntNULL(newVal))){return hr;}
  42. // buffer overflow paranoia, make sure it's less than 255 characters long
  43. if (wcslen(newVal) > _MAX_PATH){return RPC_S_STRING_TOO_LONG;}
  44. m_strMachineName = newVal;
  45. return S_OK;
  46. }
  47. STDMETHODIMP CImportExportConfig::get_UserName(BSTR *pVal)
  48. {
  49. CheckPointer(pVal, E_POINTER);
  50. _bstr_t bstrTempName = (LPCTSTR) m_strUserName;
  51. *pVal = bstrTempName.copy();
  52. return S_OK;
  53. }
  54. STDMETHODIMP CImportExportConfig::put_UserName(BSTR newVal)
  55. {
  56. HRESULT hr = S_OK;
  57. if(FAILED(hr = ValidateBSTRIsntNULL(newVal))){return hr;}
  58. // buffer overflow paranoia, make sure it's less than 255 characters long
  59. if (wcslen(newVal) > _MAX_PATH){return RPC_S_STRING_TOO_LONG;}
  60. m_strUserName = newVal;
  61. return S_OK;
  62. }
  63. STDMETHODIMP CImportExportConfig::put_UserPassword(BSTR newVal)
  64. {
  65. HRESULT hr = S_OK;
  66. if(FAILED(hr = ValidateBSTRIsntNULL(newVal))){return hr;}
  67. // buffer overflow paranoia, make sure it's less than 255 characters long
  68. if (wcslen(newVal) > _MAX_PATH){return RPC_S_STRING_TOO_LONG;}
  69. m_strUserPasswordEncrypted = NULL;
  70. m_cbUserPasswordEncrypted = 0;
  71. // encrypt the password in memory (CryptProtectMemory)
  72. // this way if the process get's paged out to the swapfile,
  73. // the password won't be in clear text.
  74. if (FAILED(EncryptMemoryPassword(newVal,&m_strUserPasswordEncrypted,&m_cbUserPasswordEncrypted)))
  75. {
  76. return E_FAIL;
  77. }
  78. return S_OK;
  79. }
  80. STDMETHODIMP CImportExportConfig::ExportConfigToFile(BSTR bstrFileNameAndPath,BSTR bstrMetabasePath,BSTR bstrPassword)
  81. {
  82. HRESULT hr = S_OK;
  83. if(FAILED(hr = ValidateBSTRIsntNULL(bstrFileNameAndPath))){return hr;}
  84. if(FAILED(hr = ValidateBSTRIsntNULL(bstrMetabasePath))){return hr;}
  85. if(FAILED(hr = ValidateBSTRIsntNULL(bstrPassword))){return hr;}
  86. CONNECTION_INFO ConnectionInfo;
  87. memset((void *)&ConnectionInfo,0,sizeof(CONNECTION_INFO));
  88. ConnectionInfo.IsLocal = IsLocalComputer(m_strMachineName);
  89. ConnectionInfo.pszMachineName = m_strMachineName;
  90. ConnectionInfo.pszUserName = m_strUserName;
  91. ConnectionInfo.pszUserPasswordEncrypted = m_strUserPasswordEncrypted;
  92. ConnectionInfo.cbUserPasswordEncrypted = m_cbUserPasswordEncrypted;
  93. return DoExportConfigToFile(&ConnectionInfo,bstrFileNameAndPath,bstrMetabasePath,bstrPassword,m_dwExportFlags);;
  94. }
  95. STDMETHODIMP CImportExportConfig::ExportConfigToFileUI(BSTR bstrMetabasePath)
  96. {
  97. HRESULT hr = S_OK;
  98. if(FAILED(hr = ValidateBSTRIsntNULL(bstrMetabasePath))){return hr;}
  99. m_strMetabasePath = bstrMetabasePath;
  100. // verify Parameters
  101. if (_tcslen(m_strMachineName) > _MAX_PATH){return RPC_S_STRING_TOO_LONG;}
  102. if (_tcslen(m_strMetabasePath) > _MAX_PATH){return RPC_S_STRING_TOO_LONG;}
  103. COMMONDLGPARAM dlgParam;
  104. memset((void *)&dlgParam,0,sizeof(COMMONDLGPARAM));
  105. // Copy into a structure so that we can pass it to the dialog function.
  106. dlgParam.ConnectionInfo.IsLocal = IsLocalComputer(m_strMachineName);
  107. dlgParam.ConnectionInfo.pszMachineName = (LPCTSTR) m_strMachineName;
  108. dlgParam.ConnectionInfo.pszUserName = (LPCTSTR) m_strUserName;
  109. dlgParam.ConnectionInfo.pszUserPasswordEncrypted = (LPCTSTR) m_strUserPasswordEncrypted;
  110. dlgParam.ConnectionInfo.cbUserPasswordEncrypted = m_cbUserPasswordEncrypted;
  111. dlgParam.pszMetabasePath = (LPCTSTR) m_strMetabasePath;
  112. dlgParam.pszKeyType = NULL;
  113. dlgParam.dwImportFlags = m_dwImportFlags;
  114. dlgParam.dwExportFlags = m_dwExportFlags;
  115. if (FALSE == DialogBoxParam((HINSTANCE) _Module.m_hInst, MAKEINTRESOURCE(IDD_DIALOG_EXPORT), GetActiveWindow(), ShowExportDlgProc, (LPARAM) &dlgParam))
  116. {
  117. return HRESULT_FROM_WIN32(GetLastError());
  118. }
  119. else
  120. {
  121. return S_OK;
  122. }
  123. }
  124. STDMETHODIMP CImportExportConfig::ImportConfigFromFile(BSTR bstrFileNameAndPath, BSTR bstrSourcePath, BSTR bstrDestinationPath, BSTR bstrPassword)
  125. {
  126. HRESULT hr = S_OK;
  127. if(FAILED(hr = ValidateBSTRIsntNULL(bstrFileNameAndPath))){return hr;}
  128. if(FAILED(hr = ValidateBSTRIsntNULL(bstrSourcePath))){return hr;}
  129. if(FAILED(hr = ValidateBSTRIsntNULL(bstrDestinationPath))){return hr;}
  130. if(FAILED(hr = ValidateBSTRIsntNULL(bstrPassword))){return hr;}
  131. CONNECTION_INFO ConnectionInfo;
  132. memset((void *)&ConnectionInfo,0,sizeof(CONNECTION_INFO));
  133. ConnectionInfo.IsLocal = IsLocalComputer(m_strMachineName);
  134. ConnectionInfo.pszMachineName = m_strMachineName;
  135. ConnectionInfo.pszUserName = m_strUserName;
  136. ConnectionInfo.pszUserPasswordEncrypted = m_strUserPasswordEncrypted;
  137. ConnectionInfo.cbUserPasswordEncrypted = m_cbUserPasswordEncrypted;
  138. return DoImportConfigFromFile(&ConnectionInfo,bstrFileNameAndPath,bstrSourcePath,bstrDestinationPath,bstrPassword,m_dwImportFlags);
  139. }
  140. STDMETHODIMP CImportExportConfig::ImportConfigFromFileUI(BSTR bstrMetabasePath,BSTR bstrKeyType)
  141. {
  142. HRESULT hr = S_OK;
  143. if(FAILED(hr = ValidateBSTRIsntNULL(bstrMetabasePath))){return hr;}
  144. if(FAILED(hr = ValidateBSTRIsntNULL(bstrKeyType))){return hr;}
  145. m_strMetabasePath = bstrMetabasePath;
  146. m_strKeyType = bstrKeyType;
  147. if (_tcslen(m_strMachineName) > _MAX_PATH){return RPC_S_STRING_TOO_LONG;}
  148. if (_tcslen(m_strMetabasePath) > _MAX_PATH){return RPC_S_STRING_TOO_LONG;}
  149. if (_tcslen(m_strKeyType) > _MAX_PATH){return RPC_S_STRING_TOO_LONG;}
  150. COMMONDLGPARAM dlgParam;
  151. memset((void *)&dlgParam,0,sizeof(COMMONDLGPARAM));
  152. // Copy into a structure so that we can pass it to the dialog function.
  153. dlgParam.ConnectionInfo.IsLocal = IsLocalComputer(m_strMachineName);
  154. dlgParam.ConnectionInfo.pszMachineName = (LPCTSTR) m_strMachineName;
  155. dlgParam.ConnectionInfo.pszUserName = (LPCTSTR) m_strUserName;
  156. dlgParam.ConnectionInfo.pszUserPasswordEncrypted = (LPCTSTR) m_strUserPasswordEncrypted;
  157. dlgParam.ConnectionInfo.cbUserPasswordEncrypted = m_cbUserPasswordEncrypted;
  158. dlgParam.pszMetabasePath = (LPCTSTR) m_strMetabasePath;
  159. dlgParam.pszKeyType = (LPCTSTR) m_strKeyType;
  160. dlgParam.dwImportFlags = m_dwImportFlags;
  161. dlgParam.dwExportFlags = m_dwExportFlags;
  162. if (FALSE == DialogBoxParam((HINSTANCE) _Module.m_hInst, MAKEINTRESOURCE(IDD_DIALOG_IMPORT), GetActiveWindow(), ShowImportDlgProc, (LPARAM) &dlgParam))
  163. {
  164. return HRESULT_FROM_WIN32(GetLastError());
  165. }
  166. else
  167. {
  168. return S_OK;
  169. }
  170. }
  171. STDMETHODIMP CImportExportConfig::get_ImportFlags(DWORD *pVal)
  172. {
  173. CheckPointer(pVal, E_POINTER);
  174. *pVal = m_dwImportFlags;
  175. return S_OK;
  176. }
  177. STDMETHODIMP CImportExportConfig::put_ImportFlags(DWORD newVal)
  178. {
  179. // 0 = default
  180. //#define MD_IMPORT_INHERITED 0x00000001
  181. //#define MD_IMPORT_NODE_ONLY 0x00000002
  182. //#define MD_IMPORT_MERGE 0x00000004
  183. m_dwImportFlags = newVal;
  184. return S_OK;
  185. }
  186. STDMETHODIMP CImportExportConfig::get_ExportFlags(DWORD *pVal)
  187. {
  188. CheckPointer(pVal, E_POINTER);
  189. *pVal = m_dwExportFlags;
  190. return S_OK;
  191. }
  192. STDMETHODIMP CImportExportConfig::put_ExportFlags(DWORD newVal)
  193. {
  194. //#define MD_EXPORT_INHERITED 0x00000001 (default)
  195. //#define MD_EXPORT_NODE_ONLY 0x00000002
  196. m_dwExportFlags = newVal;
  197. return S_OK;
  198. }