Ignore:
Timestamp:
Jul 3, 2009, 11:59:02 PM (16 years ago)
Author:
ydario
Message:

advapi32 and crypt32 updates.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/advapi32/security.c

    r10272 r21325  
    99#include "heap.h"
    1010#include "ntddk.h"
     11#include "ntstatus.h"
    1112#include "ntsecapi.h"
     13#include "sddl.h"
     14//#include "wine/debug.h"
    1215#include "debugtools.h"
     16#include "wine/unicode.h"
    1317
    1418#ifdef __WIN32OS2__
    15 #include <heapstring.h>
     19//#include <heapstring.h>
     20LPWSTR WIN32API HEAP_strdupAtoW( HANDLE heap, DWORD flags, LPCSTR str );
    1621#endif
    1722
    18 DEFAULT_DEBUG_CHANNEL(advapi);
    19 
    20 #define CallWin32ToNt(func) \
    21         { NTSTATUS ret; \
    22           ret = (func); \
    23           if (ret !=STATUS_SUCCESS) \
    24           { SetLastError (RtlNtStatusToDosError(ret)); return FALSE; } \
    25           return TRUE; \
     23WINE_DEFAULT_DEBUG_CHANNEL(advapi);
     24
     25
     26/* set last error code from NT status and get the proper boolean return value */
     27/* used for functions that are a simple wrapper around the corresponding ntdll API */
     28static inline BOOL set_ntstatus( NTSTATUS status )
     29{
     30    if (status) SetLastError( RtlNtStatusToDosError( status ));
     31    return !status;
    2632        }
    2733
     
    3036        if (oa)
    3137        {
    32           TRACE("\n\tlength=%lu, rootdir=0x%08x, objectname=%s\n\tattr=0x%08lx, sid=%p qos=%p\n",
     38          TRACE("\n\tlength=%lu, rootdir=%p, objectname=%s\n\tattr=0x%08lx, sid=%p qos=%p\n",
    3339                oa->Length, oa->RootDirectory,
    3440                oa->ObjectName?debugstr_w(oa->ObjectName->Buffer):"null",
     
    4450/******************************************************************************
    4551 * OpenProcessToken                     [ADVAPI32.@]
    46  * Opens the access token associated with a process
     52 * Opens the access token associated with a process handle.
    4753 *
    4854 * PARAMS
     
    5157 *   TokenHandle   [O] Pointer to handle of open access token
    5258 *
    53  * RETURNS STD
     59 * RETURNS
     60 *  Success: TRUE. TokenHandle contains the access token.
     61 *  Failure: FALSE.
     62 *
     63 * NOTES
     64 *  See NtOpenProcessToken.
    5465 */
    5566BOOL WINAPI
     
    5768                  HANDLE *TokenHandle )
    5869{
    59         CallWin32ToNt(NtOpenProcessToken( ProcessHandle, DesiredAccess, TokenHandle ));
     70        return set_ntstatus(NtOpenProcessToken( ProcessHandle, DesiredAccess, TokenHandle ));
    6071}
    6172
     
    6374 * OpenThreadToken [ADVAPI32.@]
    6475 *
    65  * PARAMS
    66  *   thread        []
    67  *   desiredaccess []
    68  *   openasself    []
    69  *   thandle       []
     76 * Opens the access token associated with a thread handle.
     77 *
     78 * PARAMS
     79 *   ThreadHandle  [I] Handle to process
     80 *   DesiredAccess [I] Desired access to the thread
     81 *   OpenAsSelf    [I] ???
     82 *   TokenHandle   [O] Destination for the token handle
     83 *
     84 * RETURNS
     85 *  Success: TRUE. TokenHandle contains the access token.
     86 *  Failure: FALSE.
     87 *
     88 * NOTES
     89 *  See NtOpenThreadToken.
    7090 */
    7191BOOL WINAPI
     
    7393                 BOOL OpenAsSelf, HANDLE *TokenHandle)
    7494{
    75         CallWin32ToNt (NtOpenThreadToken(ThreadHandle, DesiredAccess, OpenAsSelf, TokenHandle));
     95        return set_ntstatus( NtOpenThreadToken(ThreadHandle, DesiredAccess, OpenAsSelf, TokenHandle));
    7696}
    7797
     
    7999 * AdjustTokenPrivileges [ADVAPI32.@]
    80100 *
    81  * PARAMS
    82  *   TokenHandle          []
    83  *   DisableAllPrivileges []
    84  *   NewState             []
    85  *   BufferLength         []
    86  *   PreviousState        []
    87  *   ReturnLength         []
     101 * Adjust the privileges of an open token handle.
     102 *
     103 * PARAMS
     104 *  TokenHandle          [I]   Handle from OpenProcessToken() or OpenThreadToken()
     105 *  DisableAllPrivileges [I]   TRUE=Remove all privileges, FALSE=Use NewState
     106 *  NewState             [I]   Desired new privileges of the token
     107 *  BufferLength         [I]   Length of NewState
     108 *  PreviousState        [O]   Destination for the previous state
     109 *  ReturnLength         [I/O] Size of PreviousState
     110 *
     111 *
     112 * RETURNS
     113 *  Success: TRUE. Privileges are set to NewState and PreviousState is updated.
     114 *  Failure: FALSE.
     115 *
     116 * NOTES
     117 *  See NtAdjustPrivilegesToken.
    88118 */
    89119BOOL WINAPI
     
    92122                       LPVOID PreviousState, LPDWORD ReturnLength )
    93123{
    94         CallWin32ToNt(NtAdjustPrivilegesToken(TokenHandle, DisableAllPrivileges, NewState, BufferLength, PreviousState, ReturnLength));
     124        return set_ntstatus( NtAdjustPrivilegesToken(TokenHandle, DisableAllPrivileges,
     125                                                     NewState, BufferLength, PreviousState,
     126                                                     ReturnLength));
    95127}
    96128
     
    98130 * CheckTokenMembership [ADVAPI32.@]
    99131 *
    100  * PARAMS
    101  *   TokenHandle []
    102  *   SidToCheck  []
    103  *   IsMember    []
     132 * Determine if an access token is a member of a SID.
     133 *
     134 * PARAMS
     135 *   TokenHandle [I] Handle from OpenProcessToken() or OpenThreadToken()
     136 *   SidToCheck  [I] SID that possibly contains the token
     137 *   IsMember    [O] Destination for result.
     138 *
     139 * RETURNS
     140 *  Success: TRUE. IsMember is TRUE if TokenHandle is a member, FALSE otherwise.
     141 *  Failure: FALSE.
    104142 */
    105143BOOL WINAPI
     
    107145                      PBOOL IsMember )
    108146{
    109   FIXME("(0x%08x %p %p) stub!\n", TokenHandle, SidToCheck, IsMember);
     147  FIXME("(%p %p %p) stub!\n", TokenHandle, SidToCheck, IsMember);
    110148
    111149  *IsMember = TRUE;
     
    117155 *
    118156 * PARAMS
    119  *   token           []
    120  *   tokeninfoclass  []
    121  *   tokeninfo       []
    122  *   tokeninfolength []
    123  *   retlen          []
    124  *
     157 *   token           [I] Handle from OpenProcessToken() or OpenThreadToken()
     158 *   tokeninfoclass  [I] A TOKEN_INFORMATION_CLASS from "winnt.h"
     159 *   tokeninfo       [O] Destination for token information
     160 *   tokeninfolength [I] Length of tokeninfo
     161 *   retlen          [O] Destination for returned token information length
     162 *
     163 * RETURNS
     164 *  Success: TRUE. tokeninfo contains retlen bytes of token information
     165 *  Failure: FALSE.
     166 *
     167 * NOTES
     168 *  See NtQueryInformationToken.
    125169 */
    126170BOOL WINAPI
     
    128172                     LPVOID tokeninfo, DWORD tokeninfolength, LPDWORD retlen )
    129173{
    130         CallWin32ToNt (NtQueryInformationToken( token, tokeninfoclass, tokeninfo, tokeninfolength, retlen));
     174    return set_ntstatus (NtQueryInformationToken( token, tokeninfoclass, tokeninfo, tokeninfolength, retlen));
    131175}
    132176
     
    134178 * SetThreadToken [ADVAPI32.@]
    135179 *
    136  * Assigns an "impersonation token" to a thread so it can assume the
    137  * security privledges of another thread or process.  Can also remove
    138  * a previously assigned token.  Only supported on NT - it's a stub
    139  * exactly like this one on Win9X.
    140  *
    141  */
    142 
     180 * Assigns an 'impersonation token' to a thread so it can assume the
     181 * security privileges of another thread or process.  Can also remove
     182 * a previously assigned token.
     183 *
     184 * PARAMS
     185 *   thread          [O] Handle to thread to set the token for
     186 *   token           [I] Token to set
     187 *
     188 * RETURNS
     189 *  Success: TRUE. The threads access token is set to token
     190 *  Failure: FALSE.
     191 *
     192 * NOTES
     193 *  Only supported on NT or higher. On Win9X this function does nothing.
     194 *  See SetTokenInformation.
     195 */
    143196BOOL WINAPI SetThreadToken(PHANDLE thread, HANDLE token)
    144197{
     
    344397 */
    345398BOOL WINAPI
    346 InitializeSecurityDescriptor( SECURITY_DESCRIPTOR *pDescr, DWORD revision )
    347 {
    348         CallWin32ToNt (RtlCreateSecurityDescriptor(pDescr, revision ));
    349 }
     399InitializeSecurityDescriptor( PSECURITY_DESCRIPTOR pDescr, DWORD revision )
     400{
     401        return set_ntstatus( RtlCreateSecurityDescriptor(pDescr, revision ));
     402}
     403
    350404
    351405/******************************************************************************
    352406 * GetSecurityDescriptorLength [ADVAPI32.@]
    353407 */
    354 DWORD WINAPI GetSecurityDescriptorLength( SECURITY_DESCRIPTOR *pDescr)
    355 {
    356         return (RtlLengthSecurityDescriptor(pDescr));
     408DWORD WINAPI GetSecurityDescriptorLength( PSECURITY_DESCRIPTOR pDescr)
     409{
     410        return RtlLengthSecurityDescriptor(pDescr);
    357411}
    358412
     
    365419 */
    366420BOOL WINAPI
    367 GetSecurityDescriptorOwner( SECURITY_DESCRIPTOR *pDescr, PSID *pOwner,
     421GetSecurityDescriptorOwner( PSECURITY_DESCRIPTOR pDescr, PSID *pOwner,
    368422                            LPBOOL lpbOwnerDefaulted )
    369423{
    370         CallWin32ToNt (RtlGetOwnerSecurityDescriptor( pDescr, pOwner, (PBOOLEAN)lpbOwnerDefaulted ));
     424    BOOLEAN defaulted;
     425    BOOL ret = set_ntstatus( RtlGetOwnerSecurityDescriptor( pDescr, pOwner, &defaulted ));
     426    *lpbOwnerDefaulted = defaulted;
     427    return ret;
    371428}
    372429
     
    379436                                   PSID pOwner, BOOL bOwnerDefaulted)
    380437{
    381         CallWin32ToNt (RtlSetOwnerSecurityDescriptor(pSecurityDescriptor, pOwner, bOwnerDefaulted));
     438    return set_ntstatus( RtlSetOwnerSecurityDescriptor(pSecurityDescriptor, pOwner, bOwnerDefaulted));
    382439}
    383440/******************************************************************************
     
    389446        LPBOOL GroupDefaulted)
    390447{
    391         CallWin32ToNt (RtlGetGroupSecurityDescriptor(SecurityDescriptor, Group, (PBOOLEAN)GroupDefaulted));
     448    BOOLEAN defaulted;
     449    BOOL ret = set_ntstatus( RtlGetGroupSecurityDescriptor(SecurityDescriptor, Group, &defaulted ));
     450    *GroupDefaulted = defaulted;
     451    return ret;
    392452}       
    393453/******************************************************************************
     
    397457                                           PSID Group, BOOL GroupDefaulted)
    398458{
    399         CallWin32ToNt (RtlSetGroupSecurityDescriptor( SecurityDescriptor, Group, GroupDefaulted));
     459    return set_ntstatus( RtlSetGroupSecurityDescriptor( SecurityDescriptor, Group, GroupDefaulted));
    400460}
    401461
     
    409469IsValidSecurityDescriptor( PSECURITY_DESCRIPTOR SecurityDescriptor )
    410470{
    411         CallWin32ToNt (RtlValidSecurityDescriptor(SecurityDescriptor));
     471    return set_ntstatus( RtlValidSecurityDescriptor(SecurityDescriptor));
    412472}
    413473
     
    421481        OUT LPBOOL lpbDaclDefaulted)
    422482{
    423         CallWin32ToNt (RtlGetDaclSecurityDescriptor(pSecurityDescriptor, (PBOOLEAN)lpbDaclPresent,
    424                                                pDacl, (PBOOLEAN)lpbDaclDefaulted));
     483    BOOLEAN present, defaulted;
     484    BOOL ret = set_ntstatus( RtlGetDaclSecurityDescriptor(pSecurityDescriptor, &present, pDacl, &defaulted));
     485    *lpbDaclPresent = present;
     486    *lpbDaclDefaulted = defaulted;
     487    return ret;
    425488}       
    426489
     
    435498        BOOL dacldefaulted )
    436499{
    437         CallWin32ToNt (RtlSetDaclSecurityDescriptor (lpsd, daclpresent, dacl, dacldefaulted ));
     500    return set_ntstatus( RtlSetDaclSecurityDescriptor (lpsd, daclpresent, dacl, dacldefaulted ) );
    438501}
    439502/******************************************************************************
     
    446509        OUT LPBOOL lpbSaclDefaulted)
    447510{
    448         CallWin32ToNt (RtlGetSaclSecurityDescriptor(lpsd,
    449            (PBOOLEAN)lpbSaclPresent, pSacl, (PBOOLEAN)lpbSaclDefaulted));
     511    BOOLEAN present, defaulted;
     512    BOOL ret = set_ntstatus( RtlGetSaclSecurityDescriptor(lpsd, &present, pSacl, &defaulted) );
     513    *lpbSaclPresent = present;
     514    *lpbSaclDefaulted = defaulted;
     515    return ret;
    450516}       
    451517
     
    459525        BOOL sacldefaulted)
    460526{
    461         CallWin32ToNt (RtlSetSaclSecurityDescriptor(lpsd, saclpresent, lpsacl, sacldefaulted));
     527    return set_ntstatus (RtlSetSaclSecurityDescriptor(lpsd, saclpresent, lpsacl, sacldefaulted));
    462528}
    463529/******************************************************************************
     
    475541        IN OUT LPDWORD lpdwBufferLength)
    476542{
    477         CallWin32ToNt (RtlMakeSelfRelativeSD(pAbsoluteSecurityDescriptor,pSelfRelativeSecurityDescriptor, lpdwBufferLength));
     543    return set_ntstatus( RtlMakeSelfRelativeSD( pAbsoluteSecurityDescriptor,
     544                                                pSelfRelativeSecurityDescriptor, lpdwBufferLength));
    478545}
    479546
     
    485552                 PSECURITY_DESCRIPTOR_CONTROL pControl, LPDWORD lpdwRevision)
    486553{
    487         CallWin32ToNt (RtlGetControlSecurityDescriptor(pSecurityDescriptor,pControl,lpdwRevision));
     554        return set_ntstatus (RtlGetControlSecurityDescriptor(pSecurityDescriptor,pControl,lpdwRevision));
    488555}               
    489556
     
    496563 * InitializeAcl [ADVAPI32.@]
    497564 */
    498 DWORD WINAPI InitializeAcl(PACL acl, DWORD size, DWORD rev)
    499 {
    500         CallWin32ToNt (RtlCreateAcl(acl, size, rev));
    501 }
    502 
     565BOOL WINAPI InitializeAcl(PACL acl, DWORD size, DWORD rev)
     566{
     567    return set_ntstatus( RtlCreateAcl(acl, size, rev));
     568}
     569
     570/******************************************************************************
     571 *  AddAccessAllowedAceEx [ADVAPI32.@]
     572 */
     573BOOL WINAPI AddAccessAllowedAceEx(
     574        IN OUT PACL pAcl,
     575        IN DWORD dwAceRevision,
     576        IN DWORD AceFlags,
     577        IN DWORD AccessMask,
     578        IN PSID pSid)
     579{
     580  FIXME("AddAccessAllowedAceEx (%x, %x, %x, %x, %x): stub\n", pAcl, dwAceRevision, AceFlags, AccessMask, pSid);
     581      return FALSE;
     582//    return set_ntstatus(RtlAddAccessAllowedAceEx(pAcl, dwAceRevision, AceFlags, AccessMask, pSid));
     583}
     584
     585/******************************************************************************
     586 * GetAce [ADVAPI32.@]
     587 */
     588BOOL WINAPI GetAce(PACL pAcl,DWORD dwAceIndex,LPVOID *pAce )
     589{
     590    return set_ntstatus(RtlGetAce(pAcl, dwAceIndex, pAce));
     591}
    503592/*      ##############################
    504593        ######  MISC FUNCTIONS  ######
     
    547636 * GetFileSecurityA [ADVAPI32.@]
    548637 *
    549  * Obtains Specified information about the security of a file or directory
    550  * The information obtained is constrained by the callers access rights and
    551  * privileges
     638 * Obtains Specified information about the security of a file or directory.
     639 *
     640 * PARAMS
     641 *  lpFileName           [I] Name of the file to get info for
     642 *  RequestedInformation [I] SE_ flags from "winnt.h"
     643 *  pSecurityDescriptor  [O] Destination for security information
     644 *  nLength              [I] Length of pSecurityDescriptor
     645 *  lpnLengthNeeded      [O] Destination for length of returned security information
     646 *
     647 * RETURNS
     648 *  Success: TRUE. pSecurityDescriptor contains the requested information.
     649 *  Failure: FALSE. lpnLengthNeeded contains the required space to return the info.
     650 *
     651 * NOTES
     652 *  The information returned is constrained by the callers access rights and
     653 *  privileges.
    552654 */
    553655BOOL WINAPI
     
    557659                    DWORD nLength, LPDWORD lpnLengthNeeded )
    558660{
    559   FIXME("(%s) : stub\n", debugstr_a(lpFileName));
    560   return TRUE;
     661    DWORD len;
     662    BOOL r;
     663    LPWSTR name = NULL;
     664
     665    if( lpFileName )
     666    {
     667        len = MultiByteToWideChar( CP_ACP, 0, lpFileName, -1, NULL, 0 );
     668        name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
     669        MultiByteToWideChar( CP_ACP, 0, lpFileName, -1, name, len );
     670    }
     671
     672    r = GetFileSecurityW( name, RequestedInformation, pSecurityDescriptor,
     673                          nLength, lpnLengthNeeded );
     674    HeapFree( GetProcessHeap(), 0, name );
     675
     676    return r;
    561677}
    562678
     
    564680 * GetFileSecurityW [ADVAPI32.@]
    565681 *
    566  * Obtains Specified information about the security of a file or directory
    567  * The information obtained is constrained by the callers access rights and
    568  * privileges
    569  *
    570  * PARAMS
    571  *   lpFileName           []
    572  *   RequestedInformation []
    573  *   pSecurityDescriptor  []
    574  *   nLength              []
    575  *   lpnLengthNeeded      []
     682 * See GetFileSecurityA.
    576683 */
    577684BOOL WINAPI
     
    725832 *
    726833 * PARAMS
    727  *   x1 []
    728  *   x2 []
    729  *   x3 []
    730  *   x4 []
     834 *   SystemName       [I]
     835 *   ObjectAttributes [I]
     836 *   DesiredAccess    [I]
     837 *   PolicyHandle     [I/O]
    731838 */
    732839NTSTATUS WINAPI
     
    742849        dumpLsaAttributes(ObjectAttributes);
    743850        if(PolicyHandle) *PolicyHandle = (LSA_HANDLE)0xcafe;
    744         return TRUE;
     851        return STATUS_SUCCESS;
    745852}
    746853
     
    9071014        LPBOOL AccessStatus)
    9081015{
    909         CallWin32ToNt (NtAccessCheck(SecurityDescriptor, ClientToken, DesiredAccess,
     1016        return set_ntstatus (NtAccessCheck(SecurityDescriptor, ClientToken, DesiredAccess,
    9101017          GenericMapping, PrivilegeSet, PrivilegeSetLength, GrantedAccess, (PBOOLEAN)AccessStatus));
    9111018}
     
    9191026        IN PSECURITY_DESCRIPTOR SecurityDescriptor )
    9201027{
    921         CallWin32ToNt (NtSetSecurityObject (Handle, SecurityInformation, SecurityDescriptor));
    922 }
     1028        return set_ntstatus (NtSetSecurityObject (Handle, SecurityInformation, SecurityDescriptor));
     1029}
     1030
    9231031
    9241032/******************************************************************************
     
    9521060
    9531061/******************************************************************************
    954  * GetAce [ADVAPI32.@]
    955  */
    956 BOOL WINAPI GetAce(PACL pAcl,DWORD dwAceIndex,LPVOID *pAce )
    957 {
    958     CallWin32ToNt(RtlGetAce(pAcl, dwAceIndex, pAce));
    959 }
     1062 * ConvertSidToStringSidW [ADVAPI32.@]
     1063 *
     1064 *  format of SID string is:
     1065 *    S-<count>-<auth>-<subauth1>-<subauth2>-<subauth3>...
     1066 *  where
     1067 *    <rev> is the revision of the SID encoded as decimal
     1068 *    <auth> is the identifier authority encoded as hex
     1069 *    <subauthN> is the subauthority id encoded as decimal
     1070 */
     1071BOOL WINAPI ConvertSidToStringSidW( PSID pSid, LPWSTR *pstr )
     1072{
     1073    DWORD sz, i;
     1074    LPWSTR str;
     1075    WCHAR fmt[] = { 'S','-','%','u','-','%','d',0 };
     1076    WCHAR subauthfmt[] = { '-','%','u',0 };
     1077    SID* pisid=pSid;
     1078
     1079    TRACE("%p %p\n", pSid, pstr );
     1080
     1081    if( !IsValidSid( pSid ) )
     1082        return FALSE;
     1083
     1084    if (pisid->Revision != SDDL_REVISION)
     1085        return FALSE;
     1086    if (pisid->IdentifierAuthority.Value[0] ||
     1087     pisid->IdentifierAuthority.Value[1])
     1088    {
     1089        FIXME("not matching MS' bugs\n");
     1090        return FALSE;
     1091    }
     1092
     1093    sz = 14 + pisid->SubAuthorityCount * 11;
     1094    str = LocalAlloc( 0, sz*sizeof(WCHAR) );
     1095    sprintfW( str, fmt, pisid->Revision, MAKELONG(
     1096     MAKEWORD( pisid->IdentifierAuthority.Value[5],
     1097     pisid->IdentifierAuthority.Value[4] ),
     1098     MAKEWORD( pisid->IdentifierAuthority.Value[3],
     1099     pisid->IdentifierAuthority.Value[2] ) ) );
     1100    for( i=0; i<pisid->SubAuthorityCount; i++ )
     1101        sprintfW( str + strlenW(str), subauthfmt, pisid->SubAuthority[i] );
     1102    *pstr = str;
     1103
     1104    return TRUE;
     1105}
     1106
     1107/******************************************************************************
     1108 * ConvertSidToStringSidA [ADVAPI32.@]
     1109 */
     1110BOOL WINAPI ConvertSidToStringSidA(PSID pSid, LPSTR *pstr)
     1111{
     1112    LPWSTR wstr = NULL;
     1113    LPSTR str;
     1114    UINT len;
     1115
     1116    TRACE("%p %p\n", pSid, pstr );
     1117
     1118    if( !ConvertSidToStringSidW( pSid, &wstr ) )
     1119        return FALSE;
     1120
     1121    len = WideCharToMultiByte( CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL );
     1122    str = LocalAlloc( 0, len );
     1123    WideCharToMultiByte( CP_ACP, 0, wstr, -1, str, len, NULL, NULL );
     1124    LocalFree( wstr );
     1125
     1126    *pstr = str;
     1127
     1128    return TRUE;
     1129}
Note: See TracChangeset for help on using the changeset viewer.