home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip531.zip / win32 / nt.c < prev    next >
C/C++ Source or Header  |  1997-03-22  |  19KB  |  660 lines

  1. /*
  2.  
  3.   Copyright (c) 1996  Scott Field
  4.  
  5.   Module Name:
  6.  
  7.     nt.c
  8.  
  9.   Abstract:
  10.  
  11.     This module implements WinNT security descriptor operations for the
  12.     Win32 Info-ZIP project.  Operation such as setting file security,
  13.     using/querying local and remote privileges, and queuing of operations
  14.     is performed here.  The contents of this module are only relevant
  15.     when the code is running on Windows NT, and the target volume supports
  16.     persistent Acl storage.
  17.  
  18.     User privileges that allow accessing certain privileged aspects of the
  19.     security descriptor (such as the Sacl) are only used if the user specified
  20.     to do so.
  21.  
  22.   Author:
  23.  
  24.     Scott Field (sfield@microsoft.com)
  25.  
  26.   Last revised:  18 Jan 97
  27.  
  28.  */
  29.  
  30. #define WIN32_LEAN_AND_MEAN
  31. #define UNZIP_INTERNAL
  32. #include "unzip.h"
  33. #include <windows.h>
  34. #ifdef __RSXNT__
  35. #  include "win32/rsxntwin.h"
  36. #endif
  37. #include "win32/nt.h"
  38.  
  39.  
  40. #ifdef NTSD_EAS         /* This file is only needed for NTSD handling */
  41.  
  42. /* Borland C++ does not define FILE_SHARE_DELETE. Others also? */
  43. #ifndef FILE_SHARE_DELETE
  44. #  define FILE_SHARE_DELETE 0x00000004
  45. #endif
  46.  
  47.  
  48. /* private prototypes */
  49.  
  50. #ifndef NO_NTSD_WITH_RSXNT  /* RSXNT windows.h does not yet support NT sec. */
  51. static BOOL Initialize(VOID);
  52. #if 0   /* currently unused */
  53. static BOOL Shutdown(VOID);
  54. #endif
  55. static BOOL DeferSet(char *resource, PVOLUMECAPS VolumeCaps, uch *buffer);
  56. static VOID GetRemotePrivilegesSet(CHAR *FileName, PDWORD dwRemotePrivileges);
  57. static VOID InitLocalPrivileges(VOID);
  58.  
  59.  
  60. BOOL bInitialized = FALSE;  /* module level stuff initialized? */
  61. HANDLE hInitMutex = NULL;   /* prevent multiple initialization */
  62.  
  63. BOOL g_bRestorePrivilege = FALSE;   /* for local set file security override */
  64. BOOL g_bSaclPrivilege = FALSE;      /* for local set sacl operations, only when
  65.                                        restore privilege not present */
  66.  
  67. /* our single cached volume capabilities structure that describes the last
  68.    volume root we encountered.  A single entry like this works well in the
  69.    zip/unzip scenario for a number of reasons:
  70.    1. typically one extraction path during unzip.
  71.    2. typically process one volume at a time during zip, and then move
  72.       on to the next.
  73.    3. no cleanup code required and no memory leaks.
  74.    4. simple code.
  75.  
  76.    This approach should be reworked to a linked list approach if we expect to
  77.    be called by many threads which are processing a variety of input/output
  78.    volumes, since lock contention and stale data may become a bottleneck. */
  79.  
  80. VOLUMECAPS g_VolumeCaps;
  81. CRITICAL_SECTION VolumeCapsLock;
  82.  
  83.  
  84. /* our diferred set structure linked list element, used for making a copy
  85.    of input data which is used at a later time to process the original input
  86.    at a time when it makes more sense. eg, applying security to newly created
  87.    directories, after all files have been placed in such directories. */
  88.  
  89. CRITICAL_SECTION SetDeferLock;
  90.  
  91. typedef struct _DEFERRED_SET {
  92.     struct _DEFERRED_SET *Next;
  93.     uch *buffer;                /* must point to DWORD aligned block */
  94.     PVOLUMECAPS VolumeCaps;
  95.     char *resource;
  96. } DEFERRED_SET, *PDEFERRED_SET, *LPDEFERRED_SET;
  97.  
  98. PDEFERRED_SET pSetHead = NULL;
  99. PDEFERRED_SET pSetTail;
  100.  
  101. static BOOL Initialize(VOID)
  102. {
  103.     HANDLE hMutex;
  104.     HANDLE hOldMutex;
  105.  
  106.     if(bInitialized) return TRUE;
  107.  
  108.     hMutex = CreateMutex(NULL, TRUE, NULL);
  109.     if(hMutex == NULL) return FALSE;
  110.  
  111.     hOldMutex = (HANDLE)InterlockedExchange((LPLONG)&hInitMutex, (LONG)hMutex);
  112.  
  113.     if(hOldMutex != NULL) {
  114.         /* somebody setup the mutex already */
  115.         InterlockedExchange((LPLONG)&hInitMutex, (LONG)hOldMutex);
  116.  
  117.         CloseHandle(hMutex); /* close new, un-needed mutex */
  118.  
  119.         /* wait for initialization to complete and return status */
  120.         WaitForSingleObject(hOldMutex, INFINITE);
  121.         ReleaseMutex(hOldMutex);
  122.  
  123.         return bInitialized;
  124.     }
  125.  
  126.     /* initialize module level resources */
  127.  
  128.     InitializeCriticalSection( &SetDeferLock );
  129.  
  130.     InitializeCriticalSection( &VolumeCapsLock );
  131.     memset(&g_VolumeCaps, 0, sizeof(VOLUMECAPS));
  132.  
  133.     InitLocalPrivileges();
  134.  
  135.     bInitialized = TRUE;
  136.  
  137.     ReleaseMutex(hMutex); /* release correct mutex */
  138.  
  139.     return TRUE;
  140. }
  141.  
  142. #if 0   /* currently not used ! */
  143. static BOOL Shutdown(VOID)
  144. {
  145.     /* really need to free critical sections, disable enabled privilges, etc,
  146.        but doing so brings up possibility of race conditions if those resources
  147.        are about to be used.  The easiest way to handle this is let these
  148.        resources be freed when the process terminates... */
  149.  
  150.     return TRUE;
  151. }
  152. #endif /* never */
  153.  
  154.  
  155. static BOOL DeferSet(char *resource, PVOLUMECAPS VolumeCaps, uch *buffer)
  156. {
  157.     PDEFERRED_SET psd;
  158.     DWORD cbDeferSet;
  159.     DWORD cbResource;
  160.     DWORD cbBuffer;
  161.  
  162.     if(!bInitialized) if(!Initialize()) return FALSE;
  163.  
  164.     cbResource = lstrlenA(resource) + 1;
  165.     cbBuffer = GetSecurityDescriptorLength(buffer);
  166.     cbDeferSet = sizeof(DEFERRED_SET) + cbBuffer + sizeof(VOLUMECAPS) +
  167.       cbResource;
  168.  
  169.     psd = (PDEFERRED_SET)HeapAlloc(GetProcessHeap(), 0, cbDeferSet);
  170.     if(psd == NULL) return FALSE;
  171.  
  172.     psd->Next = NULL;
  173.     psd->buffer = (uch *)(psd+1);
  174.     psd->VolumeCaps = (PVOLUMECAPS)((char *)psd->buffer + cbBuffer);
  175.     psd->resource = (char *)((char *)psd->VolumeCaps + sizeof(VOLUMECAPS));
  176.  
  177.     memcpy(psd->buffer, buffer, cbBuffer);
  178.     memcpy(psd->VolumeCaps, VolumeCaps, sizeof(VOLUMECAPS));
  179.     psd->VolumeCaps->bProcessDefer = TRUE;
  180.     memcpy(psd->resource, resource, cbResource);
  181.  
  182.     /* take defer lock */
  183.     EnterCriticalSection( &SetDeferLock );
  184.  
  185.     /* add element at tail of list */
  186.  
  187.     if(pSetHead == NULL) {
  188.         pSetHead = psd;
  189.     } else {
  190.         pSetTail->Next = psd;
  191.     }
  192.  
  193.     pSetTail = psd;
  194.  
  195.     /* release defer lock */
  196.     LeaveCriticalSection( &SetDeferLock );
  197.  
  198.     return TRUE;
  199. }
  200.  
  201. BOOL ProcessDefer(PDWORD dwDirectoryCount, PDWORD dwBytesProcessed,
  202.                   PDWORD dwDirectoryFail, PDWORD dwBytesFail)
  203. {
  204.     PDEFERRED_SET This;
  205.     PDEFERRED_SET Next;
  206.  
  207.     *dwDirectoryCount = 0;
  208.     *dwBytesProcessed = 0;
  209.  
  210.     *dwDirectoryFail = 0;
  211.     *dwBytesFail = 0;
  212.  
  213.     if(!bInitialized) return TRUE; /* nothing to do */
  214.  
  215.     EnterCriticalSection( &SetDeferLock );
  216.  
  217.     This = pSetHead;
  218.  
  219.     while(This) {
  220.  
  221.         if(SecuritySet(This->resource, This->VolumeCaps, This->buffer)) {
  222.             (*dwDirectoryCount)++;
  223.             *dwBytesProcessed += GetSecurityDescriptorLength(This->buffer);
  224.         } else {
  225.             (*dwDirectoryFail)++;
  226.             *dwBytesFail += GetSecurityDescriptorLength(This->buffer);
  227.         }
  228.  
  229.         Next = This->Next;
  230.         HeapFree(GetProcessHeap(), 0, This);
  231.         This = Next;
  232.     }
  233.  
  234.     pSetHead = NULL;
  235.  
  236.     LeaveCriticalSection( &SetDeferLock );
  237.  
  238.     return TRUE;
  239. }
  240.  
  241. BOOL ValidateSecurity(uch *securitydata)
  242. {
  243.     PSECURITY_DESCRIPTOR sd = (PSECURITY_DESCRIPTOR)securitydata;
  244.     PACL pAcl;
  245.     PSID pSid;
  246.     BOOL bAclPresent;
  247.     BOOL bDefaulted;
  248.  
  249.     if(!IsWinNT()) return TRUE; /* don't do anything if not on WinNT */
  250.  
  251.     if(!IsValidSecurityDescriptor(sd)) return FALSE;
  252.  
  253.     /* verify Dacl integrity */
  254.  
  255.     if(!GetSecurityDescriptorDacl(sd, &bAclPresent, &pAcl, &bDefaulted))
  256.         return FALSE;
  257.  
  258.     if(bAclPresent) {
  259.         if(!IsValidAcl(pAcl)) return FALSE;
  260.     }
  261.  
  262.     /* verify Sacl integrity */
  263.  
  264.     if(!GetSecurityDescriptorSacl(sd, &bAclPresent, &pAcl, &bDefaulted))
  265.         return FALSE;
  266.  
  267.     if(bAclPresent) {
  268.         if(!IsValidAcl(pAcl)) return FALSE;
  269.     }
  270.  
  271.     /* verify owner integrity */
  272.  
  273.     if(!GetSecurityDescriptorOwner(sd, &pSid, &bDefaulted))
  274.         return FALSE;
  275.  
  276.     if(pSid != NULL) {
  277.         if(!IsValidSid(pSid)) return FALSE;
  278.     }
  279.  
  280.     /* verify group integrity */
  281.  
  282.     if(!GetSecurityDescriptorGroup(sd, &pSid, &bDefaulted))
  283.         return FALSE;
  284.  
  285.     if(pSid != NULL) {
  286.         if(!IsValidSid(pSid)) return FALSE;
  287.     }
  288.  
  289.     return TRUE;
  290. }
  291.  
  292. static VOID GetRemotePrivilegesSet(char *FileName, PDWORD dwRemotePrivileges)
  293. {
  294.     HANDLE hFile;
  295.  
  296.     *dwRemotePrivileges = 0;
  297.  
  298.     /* see if we have the SeRestorePrivilege */
  299.  
  300.     hFile = CreateFileA(
  301.         FileName,
  302.         ACCESS_SYSTEM_SECURITY | WRITE_DAC | WRITE_OWNER | READ_CONTROL,
  303.         FILE_SHARE_READ | FILE_SHARE_DELETE, /* no sd updating allowed here */
  304.         NULL,
  305.         OPEN_EXISTING,
  306.         FILE_FLAG_BACKUP_SEMANTICS,
  307.         NULL
  308.         );
  309.  
  310.     if(hFile != INVALID_HANDLE_VALUE) {
  311.         /* no remote way to determine SeRestorePrivilege -- just try a
  312.            read/write to simulate it */
  313.         SECURITY_INFORMATION si = DACL_SECURITY_INFORMATION |
  314.           SACL_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION |
  315.           GROUP_SECURITY_INFORMATION;
  316.         PSECURITY_DESCRIPTOR sd;
  317.         DWORD cbBuf = 0;
  318.  
  319.         GetKernelObjectSecurity(hFile, si, NULL, cbBuf, &cbBuf);
  320.  
  321.         if(ERROR_INSUFFICIENT_BUFFER == GetLastError()) {
  322.             if((sd = HeapAlloc(GetProcessHeap(), 0, cbBuf)) != NULL) {
  323.                 if(GetKernelObjectSecurity(hFile, si, sd, cbBuf, &cbBuf)) {
  324.                     if(SetKernelObjectSecurity(hFile, si, sd))
  325.                         *dwRemotePrivileges |= OVERRIDE_RESTORE;
  326.                 }
  327.                 HeapFree(GetProcessHeap(), 0, sd);
  328.             }
  329.         }
  330.  
  331.         CloseHandle(hFile);
  332.     } else {
  333.  
  334.         /* see if we have the SeSecurityPrivilege */
  335.         /* note we don't need this if we have SeRestorePrivilege */
  336.  
  337.         hFile = CreateFileA(
  338.             FileName,
  339.             ACCESS_SYSTEM_SECURITY,
  340.             FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, /* max */
  341.             NULL,
  342.             OPEN_EXISTING,
  343.             0,
  344.             NULL
  345.             );
  346.  
  347.         if(hFile != INVALID_HANDLE_VALUE) {
  348.             CloseHandle(hFile);
  349.             *dwRemotePrivileges |= OVERRIDE_SACL;
  350.         }
  351.     }
  352. }
  353.  
  354.  
  355. BOOL GetVolumeCaps(
  356.     char *rootpath,         /* filepath, or NULL */
  357.     char *name,             /* filename associated with rootpath */
  358.     PVOLUMECAPS VolumeCaps  /* result structure describing capabilities */
  359.     )
  360. {
  361.     char TempRootPath[MAX_PATH + 1];
  362.     DWORD cchTempRootPath = 0;
  363.     BOOL bSuccess = TRUE;   /* assume success until told otherwise */
  364.  
  365.     if(!bInitialized) if(!Initialize()) return FALSE;
  366.  
  367.     /* process the input path to produce a consistent path suitable for
  368.        compare operations and also suitable for certain picky Win32 API
  369.        that don't like forward slashes */
  370.  
  371.     if(rootpath != NULL && rootpath[0] != '\0') {
  372.         DWORD i;
  373.  
  374.         cchTempRootPath = lstrlen(rootpath);
  375.         if(cchTempRootPath > MAX_PATH) return FALSE;
  376.  
  377.         /* copy input, converting forward slashes to back slashes as we go */
  378.  
  379.         for(i = 0 ; i <= cchTempRootPath ; i++) {
  380.             if(rootpath[i] == '/') TempRootPath[i] = '\\';
  381.             else TempRootPath[i] = rootpath[i];
  382.         }
  383.  
  384.         /* check for UNC and Null terminate or append trailing \ as
  385.            appropriate */
  386.  
  387.         /* possible valid UNCs we are passed follow:
  388.            \\machine\foo\bar (path is \\machine\foo\)
  389.            \\machine\foo     (path is \\machine\foo\)
  390.            \\machine\foo\
  391.            \\.\c$\     (FIXFIX: Win32API doesn't like this - GetComputerName())
  392.            LATERLATER: handling mounted DFS drives in the future will require
  393.                        slightly different logic which isn't available today.
  394.                        This is required because directories can point at
  395.                        different servers which have differing capabilities.
  396.          */
  397.  
  398.         if(TempRootPath[0] == '\\' && TempRootPath[1] == '\\') {
  399.             DWORD slash = 0;
  400.  
  401.             for(i = 2 ; i < cchTempRootPath ; i++) {
  402.                 if(TempRootPath[i] == '\\') {
  403.                     slash++;
  404.  
  405.                     if(slash == 2) {
  406.                         i++;
  407.                         TempRootPath[i] = '\0';
  408.                         cchTempRootPath = i;
  409.                         break;
  410.                     }
  411.                 }
  412.             }
  413.  
  414.             /* if there was only one slash found, just tack another onto the
  415.                end */
  416.  
  417.             if(slash == 1 && TempRootPath[cchTempRootPath] != '\\') {
  418.                 TempRootPath[cchTempRootPath] = TempRootPath[0]; /* '\' */
  419.                 TempRootPath[cchTempRootPath+1] = '\0';
  420.                 cchTempRootPath++;
  421.             }
  422.  
  423.         } else {
  424.  
  425.             if(TempRootPath[1] == ':') {
  426.  
  427.                 /* drive letter specified, truncate to root */
  428.                 TempRootPath[2] = '\\';
  429.                 TempRootPath[3] = '\0';
  430.                 cchTempRootPath = 3;
  431.             } else {
  432.  
  433.                 /* must be file on current drive */
  434.                 TempRootPath[0] = '\0';
  435.                 cchTempRootPath = 0;
  436.             }
  437.  
  438.         }
  439.  
  440.     } /* if path != NULL */
  441.  
  442.     /* grab lock protecting cached entry */
  443.     EnterCriticalSection( &VolumeCapsLock );
  444.  
  445.     if(!g_VolumeCaps.bValid ||
  446.        lstrcmpi(g_VolumeCaps.RootPath, TempRootPath) != 0)
  447.     {
  448.  
  449.         /* no match found, build up new entry */
  450.  
  451.         DWORD dwFileSystemFlags;
  452.         DWORD dwRemotePrivileges = 0;
  453.         BOOL bRemote = FALSE;
  454.  
  455.         /* release lock during expensive operations */
  456.         LeaveCriticalSection( &VolumeCapsLock );
  457.  
  458.         bSuccess = GetVolumeInformation(
  459.             (TempRootPath[0] == '\0') ? NULL : TempRootPath,
  460.             NULL, 0,
  461.             NULL, NULL,
  462.             &dwFileSystemFlags,
  463.             NULL, 0);
  464.  
  465.  
  466.         /* only if target volume supports Acls, and we were told to use
  467.            privileges do we need to go out and test for the remote case */
  468.  
  469.         if(bSuccess && (dwFileSystemFlags & FS_PERSISTENT_ACLS) &&
  470.            VolumeCaps->bUsePrivileges)
  471.         {
  472.             if(GetDriveType( (TempRootPath[0] == '\0') ? NULL : TempRootPath )
  473.                == DRIVE_REMOTE)
  474.             {
  475.                 bRemote = TRUE;
  476.  
  477.                 /* make a determination about our remote capabilities */
  478.  
  479.                 GetRemotePrivilegesSet(name, &dwRemotePrivileges);
  480.             }
  481.         }
  482.  
  483.         /* always take the lock again, since we release it below */
  484.         EnterCriticalSection( &VolumeCapsLock );
  485.  
  486.         /* replace the existing data if successful */
  487.         if(bSuccess) {
  488.  
  489.             lstrcpynA(g_VolumeCaps.RootPath, TempRootPath, cchTempRootPath+1);
  490.             g_VolumeCaps.bProcessDefer = FALSE;
  491.             g_VolumeCaps.dwFileSystemFlags = dwFileSystemFlags;
  492.             g_VolumeCaps.bRemote = bRemote;
  493.             g_VolumeCaps.dwRemotePrivileges = dwRemotePrivileges;
  494.             g_VolumeCaps.bValid = TRUE;
  495.         }
  496.     }
  497.  
  498.     if(bSuccess) {
  499.         /* copy input elements */
  500.         g_VolumeCaps.bUsePrivileges = VolumeCaps->bUsePrivileges;
  501.         g_VolumeCaps.dwFileAttributes = VolumeCaps->dwFileAttributes;
  502.  
  503.         /* give caller results */
  504.         memcpy(VolumeCaps, &g_VolumeCaps, sizeof(VOLUMECAPS));
  505.     } else {
  506.         g_VolumeCaps.bValid = FALSE;
  507.     }
  508.  
  509.     LeaveCriticalSection( &VolumeCapsLock ); /* release lock */
  510.  
  511.     return bSuccess;
  512. }
  513.  
  514.  
  515. BOOL SecuritySet(char *resource, PVOLUMECAPS VolumeCaps, uch *securitydata)
  516. {
  517.     HANDLE hFile;
  518.     DWORD dwDesiredAccess = 0;
  519.     DWORD dwFlags = 0;
  520.     PSECURITY_DESCRIPTOR sd = (PSECURITY_DESCRIPTOR)securitydata;
  521.     SECURITY_DESCRIPTOR_CONTROL sdc;
  522.     SECURITY_INFORMATION RequestedInfo = 0;
  523.     DWORD dwRev;
  524.     BOOL bRestorePrivilege = FALSE;
  525.     BOOL bSaclPrivilege = FALSE;
  526.     BOOL bSuccess;
  527.  
  528.     if(!bInitialized) if(!Initialize()) return FALSE;
  529.  
  530.     /* defer directory processing */
  531.  
  532.     if(VolumeCaps->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  533.         if(!VolumeCaps->bProcessDefer) {
  534.             return DeferSet(resource, VolumeCaps, securitydata);
  535.         } else {
  536.             /* opening a directory requires FILE_FLAG_BACKUP_SEMANTICS */
  537.             dwFlags |= FILE_FLAG_BACKUP_SEMANTICS;
  538.         }
  539.     }
  540.  
  541.     /* evaluate the input security desriptor and act accordingly */
  542.  
  543.     if(!IsValidSecurityDescriptor(sd))
  544.         return FALSE;
  545.  
  546.     if(!GetSecurityDescriptorControl(sd, &sdc, &dwRev))
  547.         return FALSE;
  548.  
  549.     /* setup privilege usage based on if told we can use privileges, and if so,
  550.        what privileges we have */
  551.  
  552.     if(VolumeCaps->bUsePrivileges) {
  553.         if(VolumeCaps->bRemote) {
  554.             /* use remotely determined privileges */
  555.             if(VolumeCaps->dwRemotePrivileges & OVERRIDE_RESTORE)
  556.                 bRestorePrivilege = TRUE;
  557.  
  558.             if(VolumeCaps->dwRemotePrivileges & OVERRIDE_SACL)
  559.                 bSaclPrivilege = TRUE;
  560.  
  561.         } else {
  562.             /* use local privileges */
  563.             bRestorePrivilege = g_bRestorePrivilege;
  564.             bSaclPrivilege = g_bSaclPrivilege;
  565.         }
  566.     }
  567.  
  568.  
  569.     /* if a Dacl is present write Dacl out */
  570.     /* if we have SeRestorePrivilege, write owner and group info out */
  571.  
  572.     if(sdc & SE_DACL_PRESENT) {
  573.         dwDesiredAccess |= WRITE_DAC;
  574.         RequestedInfo |= DACL_SECURITY_INFORMATION;
  575.  
  576.         if(bRestorePrivilege) {
  577.             dwDesiredAccess |= WRITE_OWNER;
  578.             RequestedInfo |= (OWNER_SECURITY_INFORMATION |
  579.               GROUP_SECURITY_INFORMATION);
  580.         }
  581.     }
  582.  
  583.     /* if a Sacl is present and we have either SeRestorePrivilege or
  584.        SeSystemSecurityPrivilege try to write Sacl out */
  585.  
  586.     if((sdc & SE_SACL_PRESENT) && (bRestorePrivilege || bSaclPrivilege)) {
  587.         dwDesiredAccess |= ACCESS_SYSTEM_SECURITY;
  588.         RequestedInfo |= SACL_SECURITY_INFORMATION;
  589.     }
  590.  
  591.     if(RequestedInfo == 0)  /* nothing to do */
  592.         return FALSE;
  593.  
  594.     if(bRestorePrivilege)
  595.         dwFlags |= FILE_FLAG_BACKUP_SEMANTICS;
  596.  
  597.     hFile = CreateFileA(
  598.         resource,
  599.         dwDesiredAccess,
  600.         FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,/* max sharing */
  601.         NULL,
  602.         OPEN_EXISTING,
  603.         dwFlags,
  604.         NULL
  605.         );
  606.  
  607.     if(hFile == INVALID_HANDLE_VALUE)
  608.         return FALSE;
  609.  
  610.     bSuccess = SetKernelObjectSecurity(hFile, RequestedInfo, sd);
  611.  
  612.     CloseHandle(hFile);
  613.  
  614.     return bSuccess;
  615. }
  616.  
  617. static VOID InitLocalPrivileges(VOID)
  618. {
  619.     HANDLE hToken;
  620.     TOKEN_PRIVILEGES tp;
  621.  
  622.     /* try to enable some interesting privileges that give us the ability
  623.        to get some security information that we normally cannot.
  624.  
  625.        note that enabling privileges is only relevant on the local machine;
  626.        when accessing files that are on a remote machine, any privileges
  627.        that are present on the remote machine get enabled by default. */
  628.  
  629.     if(!OpenProcessToken(GetCurrentProcess(),
  630.         TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, &hToken))
  631.         return;
  632.  
  633.     tp.PrivilegeCount = 1;
  634.     tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  635.  
  636.     if(LookupPrivilegeValue(NULL, SE_RESTORE_NAME, &tp.Privileges[0].Luid)) {
  637.  
  638.         /* try to enable SeRestorePrivilege; if this succeeds, we can write
  639.            all aspects of the security descriptor */
  640.  
  641.         if(AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL) &&
  642.            GetLastError() == ERROR_SUCCESS) g_bRestorePrivilege = TRUE;
  643.  
  644.     }
  645.  
  646.     /* try to enable SeSystemSecurityPrivilege, if SeRestorePrivilege not
  647.        present; if this succeeds, we can write the Sacl */
  648.  
  649.     if(!g_bRestorePrivilege &&
  650.         LookupPrivilegeValue(NULL, SE_SECURITY_NAME, &tp.Privileges[0].Luid)) {
  651.  
  652.         if(AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL) &&
  653.            GetLastError() == ERROR_SUCCESS) g_bSaclPrivilege = TRUE;
  654.     }
  655.  
  656.     CloseHandle(hToken);
  657. }
  658. #endif /* !NO_NTSD_WITH_RSXNT */
  659. #endif /* NTSD_EAS */
  660.