📄 nt.c
字号:
appropriate */ /* possible valid UNCs we are passed follow: \\machine\foo\bar (path is \\machine\foo\) \\machine\foo (path is \\machine\foo\) \\machine\foo\ \\.\c$\ (FIXFIX: Win32API doesn't like this - GetComputerName()) LATERLATER: handling mounted DFS drives in the future will require slightly different logic which isn't available today. This is required because directories can point at different servers which have differing capabilities. */ if(TempRootPath[0] == '\\' && TempRootPath[1] == '\\') { DWORD slash = 0; for(i = 2 ; i < cchTempRootPath ; i++) { if(TempRootPath[i] == '\\') { slash++; if(slash == 2) { i++; TempRootPath[i] = '\0'; cchTempRootPath = i; break; } } } /* if there was only one slash found, just tack another onto the end */ if(slash == 1 && TempRootPath[cchTempRootPath] != '\\') { TempRootPath[cchTempRootPath] = TempRootPath[0]; /* '\\' */ TempRootPath[cchTempRootPath+1] = '\0'; cchTempRootPath++; } } else { if(TempRootPath[1] == ':') { /* drive letter specified, truncate to root */ TempRootPath[2] = '\\'; TempRootPath[3] = '\0'; cchTempRootPath = 3; } else { /* must be file on current drive */ TempRootPath[0] = '\0'; cchTempRootPath = 0; } } } /* if path != NULL */ /* grab lock protecting cached entry */ EnterCriticalSection( &VolumeCapsLock ); if(!g_VolumeCaps.bValid || lstrcmpi(g_VolumeCaps.RootPath, TempRootPath) != 0) { /* no match found, build up new entry */ DWORD dwFileSystemFlags; DWORD dwRemotePrivileges = 0; BOOL bRemote = FALSE; /* release lock during expensive operations */ LeaveCriticalSection( &VolumeCapsLock ); bSuccess = GetVolumeInformation( (TempRootPath[0] == '\0') ? NULL : TempRootPath, NULL, 0, NULL, NULL, &dwFileSystemFlags, NULL, 0); /* only if target volume supports Acls, and we were told to use privileges do we need to go out and test for the remote case */ if(bSuccess && (dwFileSystemFlags & FS_PERSISTENT_ACLS) && VolumeCaps->bUsePrivileges) { if(GetDriveType( (TempRootPath[0] == '\0') ? NULL : TempRootPath ) == DRIVE_REMOTE) { bRemote = TRUE; /* make a determination about our remote capabilities */ GetRemotePrivilegesSet(name, &dwRemotePrivileges); } } /* always take the lock again, since we release it below */ EnterCriticalSection( &VolumeCapsLock ); /* replace the existing data if successful */ if(bSuccess) { lstrcpynA(g_VolumeCaps.RootPath, TempRootPath, cchTempRootPath+1); g_VolumeCaps.dwFileSystemFlags = dwFileSystemFlags; g_VolumeCaps.bRemote = bRemote; g_VolumeCaps.dwRemotePrivileges = dwRemotePrivileges; g_VolumeCaps.bValid = TRUE; } } if(bSuccess) { /* copy input elements */ g_VolumeCaps.bUsePrivileges = VolumeCaps->bUsePrivileges; g_VolumeCaps.dwFileAttributes = VolumeCaps->dwFileAttributes; /* give caller results */ memcpy(VolumeCaps, &g_VolumeCaps, sizeof(VOLUMECAPS)); } else { g_VolumeCaps.bValid = FALSE; } LeaveCriticalSection( &VolumeCapsLock ); /* release lock */ return bSuccess;}BOOL SecuritySet(char *resource, PVOLUMECAPS VolumeCaps, uch *securitydata){ HANDLE hFile; DWORD dwDesiredAccess = 0; DWORD dwFlags = 0; PSECURITY_DESCRIPTOR sd = (PSECURITY_DESCRIPTOR)securitydata; SECURITY_DESCRIPTOR_CONTROL sdc; SECURITY_INFORMATION RequestedInfo = 0; DWORD dwRev; BOOL bRestorePrivilege = FALSE; BOOL bSaclPrivilege = FALSE; BOOL bSuccess; if(!bInitialized) if(!Initialize()) return FALSE; /* defer directory processing */ if(VolumeCaps->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { /* opening a directory requires FILE_FLAG_BACKUP_SEMANTICS */ dwFlags |= FILE_FLAG_BACKUP_SEMANTICS; } /* evaluate the input security descriptor and act accordingly */ if(!IsValidSecurityDescriptor(sd)) return FALSE; if(!GetSecurityDescriptorControl(sd, &sdc, &dwRev)) return FALSE; /* setup privilege usage based on if told we can use privileges, and if so, what privileges we have */ if(VolumeCaps->bUsePrivileges) { if(VolumeCaps->bRemote) { /* use remotely determined privileges */ if(VolumeCaps->dwRemotePrivileges & OVERRIDE_RESTORE) bRestorePrivilege = TRUE; if(VolumeCaps->dwRemotePrivileges & OVERRIDE_SACL) bSaclPrivilege = TRUE; } else { /* use local privileges */ bRestorePrivilege = g_bRestorePrivilege; bSaclPrivilege = g_bSaclPrivilege; } } /* if a Dacl is present write Dacl out */ /* if we have SeRestorePrivilege, write owner and group info out */ if(sdc & SE_DACL_PRESENT) { dwDesiredAccess |= WRITE_DAC; RequestedInfo |= DACL_SECURITY_INFORMATION; if(bRestorePrivilege) { dwDesiredAccess |= WRITE_OWNER; RequestedInfo |= (OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION); } } /* if a Sacl is present and we have either SeRestorePrivilege or SeSystemSecurityPrivilege try to write Sacl out */ if((sdc & SE_SACL_PRESENT) && (bRestorePrivilege || bSaclPrivilege)) { dwDesiredAccess |= ACCESS_SYSTEM_SECURITY; RequestedInfo |= SACL_SECURITY_INFORMATION; } if(RequestedInfo == 0) /* nothing to do */ return FALSE; if(bRestorePrivilege) dwFlags |= FILE_FLAG_BACKUP_SEMANTICS; hFile = CreateFileA( resource, dwDesiredAccess, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,/* max sharing */ NULL, OPEN_EXISTING, dwFlags, NULL ); if(hFile == INVALID_HANDLE_VALUE) return FALSE; bSuccess = SetKernelObjectSecurity(hFile, RequestedInfo, sd); CloseHandle(hFile); return bSuccess;}static VOID InitLocalPrivileges(VOID){ HANDLE hToken; TOKEN_PRIVILEGES tp; /* try to enable some interesting privileges that give us the ability to get some security information that we normally cannot. note that enabling privileges is only relevant on the local machine; when accessing files that are on a remote machine, any privileges that are present on the remote machine get enabled by default. */ if(!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, &hToken)) return; tp.PrivilegeCount = 1; tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; if(LookupPrivilegeValue(NULL, SE_RESTORE_NAME, &tp.Privileges[0].Luid)) { /* try to enable SeRestorePrivilege; if this succeeds, we can write all aspects of the security descriptor */ if(AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL) && GetLastError() == ERROR_SUCCESS) g_bRestorePrivilege = TRUE; } /* try to enable SeSystemSecurityPrivilege, if SeRestorePrivilege not present; if this succeeds, we can write the Sacl */ if(!g_bRestorePrivilege && LookupPrivilegeValue(NULL, SE_SECURITY_NAME, &tp.Privileges[0].Luid)) { if(AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL) && GetLastError() == ERROR_SUCCESS) g_bSaclPrivilege = TRUE; } CloseHandle(hToken);}#endif /* NTSD_EAS */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -