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.

171 lines
5.1 KiB

  1. #include <TChar.h>
  2. #include <StdLib.h>
  3. #include <Windows.h>
  4. #include <Folders.h>
  5. namespace nsFolders
  6. {
  7. const _TCHAR REGKEY_MCSHKCU[] = _T("Software\\Mission Critical Software\\EnterpriseAdmin");
  8. const _TCHAR REGKEY_MSHKCU[] = _T("Software\\ADMT\\EnterpriseAdmin");
  9. const _TCHAR REGKEY_MSADMT[] = _T("Software\\Microsoft\\ADMT");
  10. const _TCHAR REGKEY_MCSADMT[] = _T("Software\\Mission Critical Software\\DomainAdmin");
  11. const _TCHAR REGKEY_ADMT[] = _T("Software\\Microsoft\\ADMT");
  12. const _TCHAR REGKEY_REPORTING[] = _T("Software\\Microsoft\\ADMT\\Reporting");
  13. const _TCHAR REGKEY_EXTENSIONS[] = _T("Software\\Microsoft\\ADMT\\Extensions");
  14. const _TCHAR REGVAL_DIRECTORY[] = _T("Directory");
  15. const _TCHAR REGVAL_DIRECTORY_MIGRATIONLOG[] = _T("DirectoryMigrationLog");
  16. const _TCHAR REGVAL_REGISTRYUPDATED[] = _T("RegistryUpdated");
  17. const _TCHAR REGKEY_APPLICATION_LOG[] = _T("System\\CurrentControlSet\\Services\\EventLog\\Application");
  18. const _TCHAR REGKEY_ADMTAGENT_EVENT_SOURCE[] = _T("ADMTAgent");
  19. const _TCHAR REGVAL_EVENT_CATEGORYCOUNT[] = _T("CategoryCount");
  20. const _TCHAR REGVAL_EVENT_CATEGORYMESSAGEFILE[] = _T("CategoryMessageFile");
  21. const _TCHAR REGVAL_EVENT_EVENTMESSAGEFILE[] = _T("EventMessageFile");
  22. const _TCHAR REGVAL_EVENT_TYPESSUPPORTED[] = _T("TypesSupported");
  23. const _TCHAR REGKEY_CURRENT_VERSION[] = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion");
  24. const _TCHAR REGVAL_PROGRAM_FILES_DIRECTORY[] = _T("ProgramFilesDir");
  25. const _TCHAR REGVAL_EXCHANGE_LDAP_PORT[] = _T("ExchangeLDAPPort");
  26. const _TCHAR REGVAL_EXCHANGE_SSL_PORT[] = _T("ExchangeSSLPort");
  27. const _TCHAR REGVAL_ALLOW_NON_CLOSEDSET_MOVE[] = _T("AllowNonClosedSetMove");
  28. const _TCHAR DIR_LOGS[] = _T("Logs");
  29. const _TCHAR DIR_REPORTS[] = _T("Reports");
  30. const _TCHAR FNAME_MIGRATION[] = _T("Migration");
  31. const _TCHAR FNAME_DISPATCH[] = _T("Dispatch");
  32. const _TCHAR EXT_LOG[] = _T(".log");
  33. _bstr_t __stdcall GetPath(LPCTSTR pszRegKey, LPCTSTR pszRegVal, LPCTSTR pszDir, LPCTSTR pszFName = NULL, LPCTSTR pszExt = NULL);
  34. }
  35. using namespace nsFolders;
  36. // GetLogsFolder Method
  37. //
  38. // Retrieves default Logs folder.
  39. _bstr_t __stdcall GetLogsFolder()
  40. {
  41. return GetPath(REGKEY_ADMT, REGVAL_DIRECTORY, DIR_LOGS);
  42. }
  43. // GetReportsFolder Method
  44. //
  45. // Retrieves default Reports folder and also creates folder if it doesn't exist.
  46. _bstr_t __stdcall GetReportsFolder()
  47. {
  48. _bstr_t strFolder = GetPath(REGKEY_REPORTING, REGVAL_DIRECTORY, DIR_REPORTS);
  49. if (strFolder.length())
  50. {
  51. if (!CreateDirectory(strFolder, NULL))
  52. {
  53. DWORD dwError = GetLastError();
  54. if (dwError != ERROR_ALREADY_EXISTS)
  55. {
  56. _com_issue_error(HRESULT_FROM_WIN32(dwError));
  57. }
  58. }
  59. }
  60. return strFolder;
  61. }
  62. // GetMigrationLogPath Method
  63. //
  64. // Retrieves path to migration log. First tries user specified path and then default path.
  65. _bstr_t __stdcall GetMigrationLogPath()
  66. {
  67. // retrieve user specified path
  68. _bstr_t strPath = GetPath(REGKEY_ADMT, REGVAL_DIRECTORY_MIGRATIONLOG, NULL, FNAME_MIGRATION, EXT_LOG);
  69. // if user path not specified...
  70. if (strPath.length() == 0)
  71. {
  72. // then retrieve default path
  73. strPath = GetPath(REGKEY_ADMT, REGVAL_DIRECTORY, DIR_LOGS, FNAME_MIGRATION, EXT_LOG);
  74. }
  75. return strPath;
  76. }
  77. // GetDispatchLogPath Method
  78. //
  79. // Retrieves default path to dispatch log.
  80. _bstr_t __stdcall GetDispatchLogPath()
  81. {
  82. return GetPath(REGKEY_ADMT, REGVAL_DIRECTORY, DIR_LOGS, FNAME_DISPATCH, EXT_LOG);
  83. }
  84. namespace nsFolders
  85. {
  86. // GetPath Function
  87. //
  88. // This function attempts to generate a complete path to a folder or file. The function first retrieves
  89. // a folder path from the specified registry value. If a sub-folder is specified then the sub-folder is
  90. // concatenated onto the path. If file name and/or file name extension is specified then they are also
  91. // concatenated onto the path. The function returns an empty string if unable to query the specified
  92. // registry value.
  93. _bstr_t __stdcall GetPath(LPCTSTR pszRegKey, LPCTSTR pszRegVal, LPCTSTR pszDir, LPCTSTR pszFName, LPCTSTR pszExt)
  94. {
  95. _TCHAR szPath[_MAX_PATH];
  96. _TCHAR szDrive[_MAX_DRIVE];
  97. _TCHAR szDir[_MAX_DIR];
  98. memset(szPath, 0, sizeof(szPath));
  99. HKEY hKey;
  100. DWORD dwError = RegOpenKey(HKEY_LOCAL_MACHINE, pszRegKey, &hKey);
  101. if (dwError == ERROR_SUCCESS)
  102. {
  103. DWORD cbPath = sizeof(szPath);
  104. dwError = RegQueryValueEx(hKey, pszRegVal, NULL, NULL, (LPBYTE)szPath, &cbPath);
  105. if (dwError == ERROR_SUCCESS)
  106. {
  107. // if the path does not contain a trailing backslash character the
  108. // splitpath function assumes the last component of the path is a file name
  109. // this function assumes that only folder paths are specified in the registry
  110. if (szPath[_tcslen(szPath) - 1] != _T('\\'))
  111. {
  112. _tcscat(szPath, _T("\\"));
  113. }
  114. _tsplitpath(szPath, szDrive, szDir, NULL, NULL);
  115. // if sub-folder specified then add to path
  116. if (pszDir)
  117. {
  118. _tcscat(szDir, pszDir);
  119. }
  120. _tmakepath(szPath, szDrive, szDir, pszFName, pszExt);
  121. }
  122. RegCloseKey(hKey);
  123. }
  124. return szPath;
  125. }
  126. }