?? nt.c
字號(hào):
/* Copyright (c) 1990-2000 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);#if 0 /* currently unused */static BOOL Shutdown(VOID);#endifstatic BOOL DeferSet(char *resource, PVOLUMECAPS VolumeCaps, uch *buffer);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;/* our deferred set structure linked list element, used for making a copy of input data which is used at a later time to process the original input at a time when it makes more sense. eg, applying security to newly created directories, after all files have been placed in such directories. */CRITICAL_SECTION SetDeferLock;typedef struct _DEFERRED_SET { struct _DEFERRED_SET *Next; uch *buffer; /* must point to DWORD aligned block */ PVOLUMECAPS VolumeCaps; char *resource;} DEFERRED_SET, *PDEFERRED_SET, *LPDEFERRED_SET;PDEFERRED_SET pSetHead = NULL;PDEFERRED_SET pSetTail;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( &SetDeferLock ); InitializeCriticalSection( &VolumeCapsLock ); memset(&g_VolumeCaps, 0, sizeof(VOLUMECAPS)); InitLocalPrivileges(); bInitialized = TRUE; ReleaseMutex(hMutex); /* release correct mutex */ return TRUE;}#if 0 /* currently not used ! */static BOOL Shutdown(VOID){ /* really need to free critical sections, disable enabled privilges, etc, but doing so brings up possibility of race conditions if those resources are about to be used. The easiest way to handle this is let these resources be freed when the process terminates... */ return TRUE;}#endif /* never */static BOOL DeferSet(char *resource, PVOLUMECAPS VolumeCaps, uch *buffer){ PDEFERRED_SET psd; DWORD cbDeferSet; DWORD cbResource; DWORD cbBuffer; if(!bInitialized) if(!Initialize()) return FALSE; cbResource = lstrlenA(resource) + 1; cbBuffer = GetSecurityDescriptorLength((PSECURITY_DESCRIPTOR)buffer); cbDeferSet = sizeof(DEFERRED_SET) + cbBuffer + sizeof(VOLUMECAPS) + cbResource; psd = (PDEFERRED_SET)HeapAlloc(GetProcessHeap(), 0, cbDeferSet); if(psd == NULL) return FALSE; psd->Next = NULL; psd->buffer = (uch *)(psd+1); psd->VolumeCaps = (PVOLUMECAPS)((char *)psd->buffer + cbBuffer); psd->resource = (char *)((char *)psd->VolumeCaps + sizeof(VOLUMECAPS)); memcpy(psd->buffer, buffer, cbBuffer); memcpy(psd->VolumeCaps, VolumeCaps, sizeof(VOLUMECAPS)); psd->VolumeCaps->bProcessDefer = TRUE; memcpy(psd->resource, resource, cbResource); /* take defer lock */ EnterCriticalSection( &SetDeferLock ); /* add element at tail of list */ if(pSetHead == NULL) { pSetHead = psd; } else { pSetTail->Next = psd; } pSetTail = psd; /* release defer lock */ LeaveCriticalSection( &SetDeferLock ); return TRUE;}BOOL ProcessDefer(PDWORD dwDirectoryCount, PDWORD dwBytesProcessed, PDWORD dwDirectoryFail, PDWORD dwBytesFail){ PDEFERRED_SET This; PDEFERRED_SET Next; *dwDirectoryCount = 0; *dwBytesProcessed = 0; *dwDirectoryFail = 0; *dwBytesFail = 0; if(!bInitialized) return TRUE; /* nothing to do */ EnterCriticalSection( &SetDeferLock ); This = pSetHead; while(This) { if(SecuritySet(This->resource, This->VolumeCaps, This->buffer)) { (*dwDirectoryCount)++; *dwBytesProcessed += GetSecurityDescriptorLength((PSECURITY_DESCRIPTOR)This->buffer); } else { (*dwDirectoryFail)++; *dwBytesFail += GetSecurityDescriptorLength((PSECURITY_DESCRIPTOR)This->buffer); } Next = This->Next; HeapFree(GetProcessHeap(), 0, This); This = Next; } pSetHead = NULL; LeaveCriticalSection( &SetDeferLock ); 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;
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -