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.

145 lines
3.7 KiB

  1. #include "stdafx.h"
  2. #include "common.h"
  3. #include "windns.h"
  4. #ifdef _DEBUG
  5. #undef THIS_FILE
  6. static char BASED_CODE THIS_FILE[] = __FILE__;
  7. #endif
  8. #define new DEBUG_NEW
  9. extern HINSTANCE hDLLInstance;
  10. // NOTE: this function only handles limited cases, e.g., no ip address
  11. BOOL IsLocalComputer(IN LPCTSTR lpszComputer)
  12. {
  13. if (!lpszComputer || !*lpszComputer)
  14. {
  15. return TRUE;
  16. }
  17. if ( _tcslen(lpszComputer) > 2 && *lpszComputer == _T('\\') && *(lpszComputer + 1) == _T('\\') )
  18. {
  19. lpszComputer += 2;
  20. }
  21. BOOL bReturn = FALSE;
  22. DWORD dwErr = 0;
  23. TCHAR szBuffer[DNS_MAX_NAME_BUFFER_LENGTH];
  24. DWORD dwSize = DNS_MAX_NAME_BUFFER_LENGTH;
  25. // 1st: compare against local Netbios computer name
  26. if ( !GetComputerNameEx(ComputerNameNetBIOS, szBuffer, &dwSize) )
  27. {
  28. dwErr = GetLastError();
  29. }
  30. else
  31. {
  32. bReturn = (0 == lstrcmpi(szBuffer, lpszComputer));
  33. if (!bReturn)
  34. {
  35. // 2nd: compare against local Dns computer name
  36. dwSize = DNS_MAX_NAME_BUFFER_LENGTH;
  37. if (GetComputerNameEx(ComputerNameDnsFullyQualified, szBuffer, &dwSize))
  38. {
  39. bReturn = (0 == lstrcmpi(szBuffer, lpszComputer));
  40. }
  41. else
  42. {
  43. dwErr = GetLastError();
  44. }
  45. }
  46. }
  47. if (dwErr)
  48. {
  49. TRACE(_T("IsLocalComputer dwErr = %x\n"), dwErr);
  50. }
  51. return bReturn;
  52. }
  53. void GetFullPathLocalOrRemote(
  54. IN LPCTSTR lpszServer,
  55. IN LPCTSTR lpszDir,
  56. OUT CString& cstrPath
  57. )
  58. {
  59. ASSERT(lpszDir && *lpszDir);
  60. if (IsLocalComputer(lpszServer))
  61. {
  62. cstrPath = lpszDir;
  63. }
  64. else
  65. {
  66. // Check if it's already pointing to a share...
  67. if (*lpszDir == _T('\\') || *(lpszDir + 1) == _T('\\'))
  68. {
  69. cstrPath = lpszDir;
  70. }
  71. else
  72. {
  73. if (*lpszServer != _T('\\') || *(lpszServer + 1) != _T('\\'))
  74. {
  75. cstrPath = _T("\\\\");
  76. cstrPath += lpszServer;
  77. }
  78. else
  79. {
  80. cstrPath = lpszServer;
  81. }
  82. cstrPath += _T("\\");
  83. cstrPath += lpszDir;
  84. int i = cstrPath.Find(_T(':'));
  85. ASSERT(-1 != i);
  86. cstrPath.SetAt(i, _T('$'));
  87. }
  88. }
  89. }
  90. BOOL SupportsSecurityACLs(LPCTSTR path)
  91. {
  92. const UINT BUFF_LEN = 32; // Should be large enough to hold the volume and the file system type
  93. // set to true by default, since most likely it will be
  94. // and if this function fails, then no big deal...
  95. BOOL bReturn = TRUE;
  96. TCHAR root[MAX_PATH];
  97. DWORD len = 0;
  98. DWORD flg = 0;
  99. TCHAR fs[BUFF_LEN];
  100. StrCpyN(root, path, MAX_PATH);
  101. if (PathIsUNC(root))
  102. {
  103. LPTSTR p = NULL;
  104. while (!PathIsUNCServerShare(root))
  105. {
  106. p = StrRChr(root, p, _T('\\'));
  107. if (p != NULL)
  108. *p = 0;
  109. }
  110. StrCat(root, _T("\\"));
  111. // NET_API_STATUS rc = NetShareGetInfo(server, share,
  112. if (GetVolumeInformation(root, NULL, 0, NULL, &len, &flg, fs, BUFF_LEN))
  113. {
  114. bReturn = 0 != (flg & FS_PERSISTENT_ACLS);
  115. }
  116. else
  117. {
  118. DWORD err = GetLastError();
  119. }
  120. }
  121. else
  122. {
  123. if (PathStripToRoot(root))
  124. {
  125. if (GetVolumeInformation(root, NULL, 0, NULL, &len, &flg, fs, BUFF_LEN))
  126. {
  127. bReturn = 0 != (flg & FS_PERSISTENT_ACLS);
  128. }
  129. }
  130. }
  131. return bReturn;
  132. }