Archive-Unzip-Burst
view release on metacpan or search on metacpan
unzip-6.0/win32/nt.c view on Meta::CPAN
/*
Copyright (c) 1990-2007 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
/* This macro definition is missing in old versions of MS' winbase.h. */
#ifndef InterlockedExchangePointer
# define InterlockedExchangePointer(Target, Value) \
(PVOID)InterlockedExchange((PLONG)(Target), (LONG)(Value))
#endif
/* private prototypes */
static BOOL Initialize(VOID);
static VOID GetRemotePrivilegesSet(CHAR *FileName, PDWORD dwRemotePrivileges);
static VOID InitLocalPrivileges(VOID);
volatile 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)InterlockedExchangePointer((void *)&hInitMutex,
hMutex);
if (hOldMutex != NULL) {
/* somebody setup the mutex already */
InterlockedExchangePointer((void *)&hInitMutex,
hOldMutex);
CloseHandle(hMutex); /* close new, un-needed mutex */
/* wait for initialization to complete and return status */
WaitForSingleObject(hOldMutex, INFINITE);
ReleaseMutex(hOldMutex);
return bInitialized;
}
if (!bInitialized) {
/* initialize module level resources */
InitializeCriticalSection( &VolumeCapsLock );
memset(&g_VolumeCaps, 0, sizeof(VOLUMECAPS));
InitLocalPrivileges();
bInitialized = TRUE;
}
InterlockedExchangePointer((void *)&hInitMutex,
NULL);
ReleaseMutex(hMutex); /* release correct mutex */
CloseHandle(hMutex); /* free the no longer needed handle resource */
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 && pAcl!=NULL) {
if(!IsValidAcl(pAcl)) return FALSE;
}
/* verify Sacl integrity */
if(!GetSecurityDescriptorSacl(sd, &bAclPresent, &pAcl, &bDefaulted))
return FALSE;
if(bAclPresent && pAcl!=NULL) {
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;
}
( run in 1.696 second using v1.01-cache-2.11-cpan-0b58ddf2af1 )