📄 nt.c
字号:
/* Copyright (c) 1990-2005 Info-ZIP. All rights reserved. See the accompanying file LICENSE, version 2000-Apr-09 or later (the contents of which are also included in unzip.h) for terms of use. If, for some reason, all these files are missing, the Info-ZIP license also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html*//* Copyright (c) 1996 Scott Field (dedicated to Info-Zip group) Module Name: nt.c Abstract: This module implements WinNT security descriptor operations for the Win32 Info-ZIP project. Operation such as setting file security, using/querying local and remote privileges, and queuing of operations is performed here. The contents of this module are only relevant when the code is running on Windows NT, and the target volume supports persistent Acl storage. User privileges that allow accessing certain privileged aspects of the security descriptor (such as the Sacl) are only used if the user specified to do so. Author: Scott Field (sfield@microsoft.com) Last revised: 18 Jan 97 */#define WIN32_LEAN_AND_MEAN#define UNZIP_INTERNAL#include "../unzip.h"#include <windows.h>#ifdef __RSXNT__# include "../win32/rsxntwin.h"#endif#include "../win32/nt.h"#ifdef NTSD_EAS /* This file is only needed for NTSD handling *//* Borland C++ does not define FILE_SHARE_DELETE. Others also? */#ifndef FILE_SHARE_DELETE# define FILE_SHARE_DELETE 0x00000004#endif/* private prototypes */static BOOL Initialize(VOID);static VOID GetRemotePrivilegesSet(CHAR *FileName, PDWORD dwRemotePrivileges);static VOID InitLocalPrivileges(VOID);BOOL bInitialized = FALSE; /* module level stuff initialized? */HANDLE hInitMutex = NULL; /* prevent multiple initialization */BOOL g_bRestorePrivilege = FALSE; /* for local set file security override */BOOL g_bSaclPrivilege = FALSE; /* for local set sacl operations, only when restore privilege not present *//* our single cached volume capabilities structure that describes the last volume root we encountered. A single entry like this works well in the zip/unzip scenario for a number of reasons: 1. typically one extraction path during unzip. 2. typically process one volume at a time during zip, and then move on to the next. 3. no cleanup code required and no memory leaks. 4. simple code. This approach should be reworked to a linked list approach if we expect to be called by many threads which are processing a variety of input/output volumes, since lock contention and stale data may become a bottleneck. */VOLUMECAPS g_VolumeCaps;CRITICAL_SECTION VolumeCapsLock;static BOOL Initialize(VOID){ HANDLE hMutex; HANDLE hOldMutex; if(bInitialized) return TRUE; hMutex = CreateMutex(NULL, TRUE, NULL); if(hMutex == NULL) return FALSE; hOldMutex = (HANDLE)InterlockedExchange((LPLONG)&hInitMutex, (LONG)hMutex); if(hOldMutex != NULL) { /* somebody setup the mutex already */ InterlockedExchange((LPLONG)&hInitMutex, (LONG)hOldMutex); CloseHandle(hMutex); /* close new, un-needed mutex */ /* wait for initialization to complete and return status */ WaitForSingleObject(hOldMutex, INFINITE); ReleaseMutex(hOldMutex); return bInitialized; } /* initialize module level resources */ InitializeCriticalSection( &VolumeCapsLock ); memset(&g_VolumeCaps, 0, sizeof(VOLUMECAPS)); InitLocalPrivileges(); bInitialized = TRUE; ReleaseMutex(hMutex); /* release correct mutex */ return TRUE;}BOOL ValidateSecurity(uch *securitydata){ PSECURITY_DESCRIPTOR sd = (PSECURITY_DESCRIPTOR)securitydata; PACL pAcl; PSID pSid; BOOL bAclPresent; BOOL bDefaulted; if(!IsWinNT()) return TRUE; /* don't do anything if not on WinNT */ if(!IsValidSecurityDescriptor(sd)) return FALSE; /* verify Dacl integrity */ if(!GetSecurityDescriptorDacl(sd, &bAclPresent, &pAcl, &bDefaulted)) return FALSE; if(bAclPresent) { if(!IsValidAcl(pAcl)) return FALSE; } /* verify Sacl integrity */ if(!GetSecurityDescriptorSacl(sd, &bAclPresent, &pAcl, &bDefaulted)) return FALSE; if(bAclPresent) { if(!IsValidAcl(pAcl)) return FALSE; } /* verify owner integrity */ if(!GetSecurityDescriptorOwner(sd, &pSid, &bDefaulted)) return FALSE; if(pSid != NULL) { if(!IsValidSid(pSid)) return FALSE; } /* verify group integrity */ if(!GetSecurityDescriptorGroup(sd, &pSid, &bDefaulted)) return FALSE; if(pSid != NULL) { if(!IsValidSid(pSid)) return FALSE; } return TRUE;}static VOID GetRemotePrivilegesSet(char *FileName, PDWORD dwRemotePrivileges){ HANDLE hFile; *dwRemotePrivileges = 0; /* see if we have the SeRestorePrivilege */ hFile = CreateFileA( FileName, ACCESS_SYSTEM_SECURITY | WRITE_DAC | WRITE_OWNER | READ_CONTROL, FILE_SHARE_READ | FILE_SHARE_DELETE, /* no sd updating allowed here */ NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL ); if(hFile != INVALID_HANDLE_VALUE) { /* no remote way to determine SeRestorePrivilege -- just try a read/write to simulate it */ SECURITY_INFORMATION si = DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION; PSECURITY_DESCRIPTOR sd; DWORD cbBuf = 0; GetKernelObjectSecurity(hFile, si, NULL, cbBuf, &cbBuf); if(ERROR_INSUFFICIENT_BUFFER == GetLastError()) { if((sd = HeapAlloc(GetProcessHeap(), 0, cbBuf)) != NULL) { if(GetKernelObjectSecurity(hFile, si, sd, cbBuf, &cbBuf)) { if(SetKernelObjectSecurity(hFile, si, sd)) *dwRemotePrivileges |= OVERRIDE_RESTORE; } HeapFree(GetProcessHeap(), 0, sd); } } CloseHandle(hFile); } else { /* see if we have the SeSecurityPrivilege */ /* note we don't need this if we have SeRestorePrivilege */ hFile = CreateFileA( FileName, ACCESS_SYSTEM_SECURITY, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, /* max */ NULL, OPEN_EXISTING, 0, NULL ); if(hFile != INVALID_HANDLE_VALUE) { CloseHandle(hFile); *dwRemotePrivileges |= OVERRIDE_SACL; } }}BOOL GetVolumeCaps( char *rootpath, /* filepath, or NULL */ char *name, /* filename associated with rootpath */ PVOLUMECAPS VolumeCaps /* result structure describing capabilities */ ){ char TempRootPath[MAX_PATH + 1]; DWORD cchTempRootPath = 0; BOOL bSuccess = TRUE; /* assume success until told otherwise */ if(!bInitialized) if(!Initialize()) return FALSE; /* process the input path to produce a consistent path suitable for compare operations and also suitable for certain picky Win32 API that don't like forward slashes */ if(rootpath != NULL && rootpath[0] != '\0') { DWORD i; cchTempRootPath = lstrlen(rootpath); if(cchTempRootPath > MAX_PATH) return FALSE; /* copy input, converting forward slashes to back slashes as we go */ for(i = 0 ; i <= cchTempRootPath ; i++) { if(rootpath[i] == '/') TempRootPath[i] = '\\'; else TempRootPath[i] = rootpath[i]; } /* check for UNC and Null terminate or append trailing \ as
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -