advapi32: add support for computer SID

Robert Reif reif at earthlink.net
Sat Jul 22 12:26:38 CDT 2006


-------------- next part --------------
diff -puN wine.cvs/dlls/advapi32/lsa.c wine/dlls/advapi32/lsa.c
--- wine.cvs/dlls/advapi32/lsa.c	2006-07-15 07:06:19.000000000 -0400
+++ wine/dlls/advapi32/lsa.c	2006-07-22 12:06:19.000000000 -0400
@@ -33,6 +33,8 @@
 
 #include "wine/debug.h"
 
+#include "security.h"
+
 WINE_DEFAULT_DEBUG_CHANNEL(advapi);
 
 #define ADVAPI_ForceLocalComputer(ServerName, FailureCode) \
@@ -54,30 +56,6 @@ static void dumpLsaAttributes(PLSA_OBJEC
     }
 }
 
-/************************************************************
- * ADVAPI_IsLocalComputer
- *
- * Checks whether the server name indicates local machine.
- */
-static BOOL ADVAPI_IsLocalComputer(LPCWSTR ServerName)
-{
-    DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
-    BOOL Result;
-    LPWSTR buf;
-
-    if (!ServerName || !ServerName[0])
-        return TRUE;
-
-    buf = HeapAlloc(GetProcessHeap(), 0, dwSize * sizeof(WCHAR));
-    Result = GetComputerNameW(buf,  &dwSize);
-    if (Result && (ServerName[0] == '\\') && (ServerName[1] == '\\'))
-        ServerName += 2;
-    Result = Result && !lstrcmpW(ServerName, buf);
-    HeapFree(GetProcessHeap(), 0, buf);
-
-    return Result;
-}
-
 /******************************************************************************
  * LsaAddAccountRights [ADVAPI32.@]
  *
@@ -417,10 +395,9 @@ NTSTATUS WINAPI LsaQueryInformationPolic
             {
                 POLICY_PRIMARY_DOMAIN_INFO ppdi;
                 SID sid;
+                DWORD padding[3];
             };
 
-            SID_IDENTIFIER_AUTHORITY localSidAuthority = {SECURITY_NT_AUTHORITY};
-
             struct di * xdi = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*xdi));
             HKEY key;
             BOOL useDefault = TRUE;
@@ -454,15 +431,36 @@ NTSTATUS WINAPI LsaQueryInformationPolic
                 RegCloseKey(key);
             }
             if (useDefault)
-                RtlCreateUnicodeStringFromAsciiz(&(xdi->ppdi.Name), "DOMAIN");
+            {
+                DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
+                LPWSTR buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize * sizeof(WCHAR));
+                if (GetComputerNameW(buf, &dwSize))
+                {
+                    xdi->ppdi.Name.Buffer = buf;
+                    xdi->ppdi.Name.Length = dwSize * sizeof(WCHAR);
+                }
+            }
 
             TRACE("setting domain to %s\n", debugstr_w(xdi->ppdi.Name.Buffer));
 
             xdi->ppdi.Sid = &(xdi->sid);
-            xdi->sid.Revision = SID_REVISION;
-            xdi->sid.SubAuthorityCount = 1;
-            xdi->sid.IdentifierAuthority = localSidAuthority;
-            xdi->sid.SubAuthority[0] = SECURITY_LOCAL_SYSTEM_RID;
+
+            /* read the computer SID from the registry */
+            if (!ADVAPI_GetComputerSid(&(xdi->sid)))
+            {
+                SID_IDENTIFIER_AUTHORITY localSidAuthority = {SECURITY_NT_AUTHORITY};
+
+                xdi->sid.Revision = SID_REVISION;
+                xdi->sid.SubAuthorityCount = 4;
+                xdi->sid.IdentifierAuthority = localSidAuthority;
+                xdi->sid.SubAuthority[0] = SECURITY_NT_NON_UNIQUE;
+                xdi->sid.SubAuthority[1] = 0;
+                xdi->sid.SubAuthority[2] = 0;
+                xdi->sid.SubAuthority[3] = 0;
+            }
+
+            TRACE("setting SID to %s\n", debugstr_sid(&xdi->sid));
+
             *Buffer = xdi;
         }
         break;
diff -puN wine.cvs/dlls/advapi32/security.c wine/dlls/advapi32/security.c
--- wine.cvs/dlls/advapi32/security.c	2006-07-22 13:15:26.000000000 -0400
+++ wine/dlls/advapi32/security.c	2006-07-22 13:10:57.000000000 -0400
@@ -39,6 +39,8 @@
 #include "wine/debug.h"
 #include "wine/unicode.h"
 
+#include "security.h"
+
 WINE_DEFAULT_DEBUG_CHANNEL(advapi);
 
 static BOOL ParseStringSidToSid(LPCWSTR StringSid, PSID pSid, LPDWORD cBytes);
@@ -59,57 +61,60 @@ typedef struct _ACEFLAG
    DWORD value;
 } ACEFLAG, *LPACEFLAG;
 
-typedef struct WELLKNOWNSID
+typedef struct _MAX_SID
 {
-    WCHAR wstr[2];
-    WELL_KNOWN_SID_TYPE Type;
-
-    /* same fields as struct _SID */
     BYTE Revision;
     BYTE SubAuthorityCount;
     SID_IDENTIFIER_AUTHORITY IdentifierAuthority;
     DWORD SubAuthority[SID_MAX_SUB_AUTHORITIES];
+} MAX_SID;
+
+typedef struct WELLKNOWNSID
+{
+    WCHAR wstr[2];
+    WELL_KNOWN_SID_TYPE Type;
+    MAX_SID Sid;
 } WELLKNOWNSID;
 
 static const WELLKNOWNSID WellKnownSids[] =
 {
-    { {0,0}, WinNullSid, SID_REVISION, 1, { SECURITY_NULL_SID_AUTHORITY }, { SECURITY_NULL_RID } },
-    { {'W','D'}, WinWorldSid, SID_REVISION, 1, { SECURITY_WORLD_SID_AUTHORITY }, { SECURITY_WORLD_RID } },
-    { {0,0}, WinLocalSid, SID_REVISION, 1, { SECURITY_LOCAL_SID_AUTHORITY }, { SECURITY_LOCAL_RID } },
-    { {'C','O'}, WinCreatorOwnerSid, SID_REVISION, 1, { SECURITY_CREATOR_SID_AUTHORITY }, { SECURITY_CREATOR_OWNER_RID } },
-    { {'C','G'}, WinCreatorGroupSid, SID_REVISION, 1, { SECURITY_CREATOR_SID_AUTHORITY }, { SECURITY_CREATOR_GROUP_RID } },
-    { {0,0}, WinCreatorOwnerServerSid, SID_REVISION, 1, { SECURITY_CREATOR_SID_AUTHORITY }, { SECURITY_CREATOR_OWNER_SERVER_RID } },
-    { {0,0}, WinCreatorGroupServerSid, SID_REVISION, 1, { SECURITY_CREATOR_SID_AUTHORITY }, { SECURITY_CREATOR_GROUP_SERVER_RID } },
-    { {0,0}, WinNtAuthoritySid, SID_REVISION, 0, { SECURITY_NT_AUTHORITY }, { } },
-    { {0,0}, WinDialupSid, SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_DIALUP_RID } },
-    { {'N','U'}, WinNetworkSid, SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_NETWORK_RID } },
-    { {0,0}, WinBatchSid, SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_BATCH_RID } },
-    { {'I','U'}, WinInteractiveSid, SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_INTERACTIVE_RID } },
-    { {'S','U'}, WinServiceSid, SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_SERVICE_RID } },
-    { {'A','N'}, WinAnonymousSid, SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_ANONYMOUS_LOGON_RID } },
-    { {0,0}, WinProxySid, SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_PROXY_RID } },
-    { {'E','D'}, WinEnterpriseControllersSid, SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_ENTERPRISE_CONTROLLERS_RID } },
-    { {'P','S'}, WinSelfSid, SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_PRINCIPAL_SELF_RID } },
-    { {'A','U'}, WinAuthenticatedUserSid, SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_AUTHENTICATED_USER_RID } },
-    { {'R','C'}, WinRestrictedCodeSid, SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_RESTRICTED_CODE_RID } },
-    { {0,0}, WinTerminalServerSid, SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_TERMINAL_SERVER_RID } },
-    { {0,0}, WinRemoteLogonIdSid, SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_REMOTE_LOGON_RID } },
-    { {'S','Y'}, WinLocalSystemSid, SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_LOCAL_SYSTEM_RID } },
-    { {'L','S'}, WinLocalServiceSid, SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_LOCAL_SERVICE_RID } },
-    { {'N','S'}, WinNetworkServiceSid, SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_NETWORK_SERVICE_RID } },
-    { {0,0}, WinBuiltinDomainSid, SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID } },
-    { {'B','A'}, WinBuiltinAdministratorsSid, SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS } },
-    { {'B','U'}, WinBuiltinUsersSid, SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_USERS } },
-    { {'B','G'}, WinBuiltinGuestsSid, SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_GUESTS } },
-    { {'P','U'}, WinBuiltinPowerUsersSid, SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_POWER_USERS } },
-    { {'A','O'}, WinBuiltinAccountOperatorsSid, SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ACCOUNT_OPS } },
-    { {'S','O'}, WinBuiltinSystemOperatorsSid, SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_SYSTEM_OPS } },
-    { {'P','O'}, WinBuiltinPrintOperatorsSid, SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_PRINT_OPS } },
-    { {'B','O'}, WinBuiltinBackupOperatorsSid, SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_BACKUP_OPS } },
-    { {'R','E'}, WinBuiltinReplicatorSid, SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_REPLICATOR } },
-    { {'R','U'}, WinBuiltinPreWindows2000CompatibleAccessSid, SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_PREW2KCOMPACCESS } },
-    { {'R','D'}, WinBuiltinRemoteDesktopUsersSid, SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_REMOTE_DESKTOP_USERS } },
-    { {'N','O'}, WinBuiltinNetworkConfigurationOperatorsSid, SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_NETWORK_CONFIGURATION_OPS } },
+    { {0,0}, WinNullSid, { SID_REVISION, 1, { SECURITY_NULL_SID_AUTHORITY }, { SECURITY_NULL_RID } } },
+    { {'W','D'}, WinWorldSid, { SID_REVISION, 1, { SECURITY_WORLD_SID_AUTHORITY }, { SECURITY_WORLD_RID } } },
+    { {0,0}, WinLocalSid, { SID_REVISION, 1, { SECURITY_LOCAL_SID_AUTHORITY }, { SECURITY_LOCAL_RID } } },
+    { {'C','O'}, WinCreatorOwnerSid, { SID_REVISION, 1, { SECURITY_CREATOR_SID_AUTHORITY }, { SECURITY_CREATOR_OWNER_RID } } },
+    { {'C','G'}, WinCreatorGroupSid, { SID_REVISION, 1, { SECURITY_CREATOR_SID_AUTHORITY }, { SECURITY_CREATOR_GROUP_RID } } },
+    { {0,0}, WinCreatorOwnerServerSid, { SID_REVISION, 1, { SECURITY_CREATOR_SID_AUTHORITY }, { SECURITY_CREATOR_OWNER_SERVER_RID } } },
+    { {0,0}, WinCreatorGroupServerSid, { SID_REVISION, 1, { SECURITY_CREATOR_SID_AUTHORITY }, { SECURITY_CREATOR_GROUP_SERVER_RID } } },
+    { {0,0}, WinNtAuthoritySid, { SID_REVISION, 0, { SECURITY_NT_AUTHORITY }, { } } },
+    { {0,0}, WinDialupSid, { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_DIALUP_RID } } },
+    { {'N','U'}, WinNetworkSid, { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_NETWORK_RID } } },
+    { {0,0}, WinBatchSid, { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_BATCH_RID } } },
+    { {'I','U'}, WinInteractiveSid, { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_INTERACTIVE_RID } } },
+    { {'S','U'}, WinServiceSid, { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_SERVICE_RID } } },
+    { {'A','N'}, WinAnonymousSid, { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_ANONYMOUS_LOGON_RID } } },
+    { {0,0}, WinProxySid, { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_PROXY_RID } } },
+    { {'E','D'}, WinEnterpriseControllersSid, { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_ENTERPRISE_CONTROLLERS_RID } } },
+    { {'P','S'}, WinSelfSid, { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_PRINCIPAL_SELF_RID } } },
+    { {'A','U'}, WinAuthenticatedUserSid, { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_AUTHENTICATED_USER_RID } } },
+    { {'R','C'}, WinRestrictedCodeSid, { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_RESTRICTED_CODE_RID } } },
+    { {0,0}, WinTerminalServerSid, { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_TERMINAL_SERVER_RID } } },
+    { {0,0}, WinRemoteLogonIdSid, { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_REMOTE_LOGON_RID } } },
+    { {'S','Y'}, WinLocalSystemSid, { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_LOCAL_SYSTEM_RID } } },
+    { {'L','S'}, WinLocalServiceSid, { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_LOCAL_SERVICE_RID } } },
+    { {'N','S'}, WinNetworkServiceSid, { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_NETWORK_SERVICE_RID } } },
+    { {0,0}, WinBuiltinDomainSid, { SID_REVISION, 1, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID } } },
+    { {'B','A'}, WinBuiltinAdministratorsSid, { SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS } } },
+    { {'B','U'}, WinBuiltinUsersSid, { SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_USERS } } },
+    { {'B','G'}, WinBuiltinGuestsSid, { SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_GUESTS } } },
+    { {'P','U'}, WinBuiltinPowerUsersSid, { SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_POWER_USERS } } },
+    { {'A','O'}, WinBuiltinAccountOperatorsSid, { SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ACCOUNT_OPS } } },
+    { {'S','O'}, WinBuiltinSystemOperatorsSid, { SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_SYSTEM_OPS } } },
+    { {'P','O'}, WinBuiltinPrintOperatorsSid, { SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_PRINT_OPS } } },
+    { {'B','O'}, WinBuiltinBackupOperatorsSid, { SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_BACKUP_OPS } } },
+    { {'R','E'}, WinBuiltinReplicatorSid, { SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_REPLICATOR } } },
+    { {'R','U'}, WinBuiltinPreWindows2000CompatibleAccessSid, { SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_PREW2KCOMPACCESS } } },
+    { {'R','D'}, WinBuiltinRemoteDesktopUsersSid, { SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_REMOTE_DESKTOP_USERS } } },
+    { {'N','O'}, WinBuiltinNetworkConfigurationOperatorsSid, { SID_REVISION, 2, { SECURITY_NT_AUTHORITY }, { SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_NETWORK_CONFIGURATION_OPS } } },
 };
 
 static SID const sidWorld = { SID_REVISION, 1, { SECURITY_WORLD_SID_AUTHORITY} , { SECURITY_WORLD_RID } };
@@ -138,6 +143,7 @@ static const WCHAR DIALUP[] = { 'D','I',
 static const WCHAR DOMAIN[] = {'D','O','M','A','I','N',0};
 static const WCHAR ENTERPRISE_DOMAIN_CONTROLLERS[] = { 'E','N','T','E','R','P','R','I','S','E',' ','D','O','M','A','I','N',' ','C','O','N','T','R','O','L','L','E','R','S',0 };
 static const WCHAR Everyone[] = { 'E','v','e','r','y','o','n','e',0 };
+static const WCHAR Guest[] = { 'G','u','e','s','t',0 };
 static const WCHAR Guests[] = { 'G','u','e','s','t','s',0 };
 static const WCHAR INTERACTIVE[] = { 'I','N','T','E','R','A','C','T','I','V','E',0 };
 static const WCHAR LOCAL[] = { 'L','O','C','A','L',0 };
@@ -235,7 +241,7 @@ static const WCHAR SDDL_INHERITED[]     
 static const WCHAR SDDL_AUDIT_SUCCESS[]      = {'S','A',0};
 static const WCHAR SDDL_AUDIT_FAILURE[]      = {'F','A',0};
 
-static const char * debugstr_sid(PSID sid)
+const char * debugstr_sid(PSID sid)
 {
     int auth = 0;
     SID * psid = (SID *)sid;
@@ -314,29 +320,77 @@ static void GetWorldAccessACL(PACL pACL)
 }
 
 /************************************************************
+ *                ADVAPI_GetComputerNameW
+ *
+ * Gets the local computer name.
+ */
+LPCWSTR ADVAPI_GetComputerNameW()
+{
+    static WCHAR buf[MAX_COMPUTERNAME_LENGTH + 1];
+    DWORD dwSize = sizeof(buf);
+    if (!GetComputerNameW(buf, &dwSize))
+        buf[0] = 0;
+    return buf;
+}
+
+/************************************************************
  *                ADVAPI_IsLocalComputer
  *
  * Checks whether the server name indicates local machine.
  */
-static BOOL ADVAPI_IsLocalComputer(LPCWSTR ServerName)
+BOOL ADVAPI_IsLocalComputer(LPCWSTR ServerName)
 {
-    DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1;
     BOOL Result;
-    LPWSTR buf;
+    LPCWSTR buf;
 
     if (!ServerName || !ServerName[0])
         return TRUE;
-    
-    buf = HeapAlloc(GetProcessHeap(), 0, dwSize * sizeof(WCHAR));
-    Result = GetComputerNameW(buf,  &dwSize);
-    if (Result && (ServerName[0] == '\\') && (ServerName[1] == '\\'))
+
+    buf = ADVAPI_GetComputerNameW();
+    if ((ServerName[0] == '\\') && (ServerName[1] == '\\'))
         ServerName += 2;
-    Result = Result && !lstrcmpW(ServerName, buf);
-    HeapFree(GetProcessHeap(), 0, buf);
+    Result = !lstrcmpW(ServerName, buf);
 
     return Result;
 }
 
+/************************************************************
+ *                ADVAPI_GetComputerSid
+ *
+ * Reads the computer SID from the registry.
+ */
+BOOL ADVAPI_GetComputerSid(PSID sid)
+{
+    HKEY key;
+    LONG ret;
+
+   if ((ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
+       "SECURITY\\SAM\\Domains\\Account", 0,
+       KEY_READ, &key)) == ERROR_SUCCESS)
+   {
+        static const WCHAR V[] = { 'V',0 };
+        DWORD size = 0;
+        ret = RegQueryValueExW(key, V, NULL, NULL, NULL, &size);
+        if (ret == ERROR_MORE_DATA || ret == ERROR_SUCCESS)
+        {
+            BYTE * data = HeapAlloc(GetProcessHeap(), 0, size);
+            if (data)
+            {
+                if ((ret = RegQueryValueExW(key, V, NULL, NULL,
+                     data, &size)) == ERROR_SUCCESS)
+                {
+                    /* the SID is in the last 24 bytes of the binary data */
+                    CopyMemory(sid, &data[size-24], 24);
+                    return TRUE;
+                }
+            }
+        }
+        RegCloseKey(key);
+    }
+
+    return FALSE;
+}
+
 /*	##############################
 	######	TOKEN FUNCTIONS ######
 	##############################
@@ -664,14 +718,14 @@ CreateWellKnownSid( WELL_KNOWN_SID_TYPE 
 
     for (i = 0; i < sizeof(WellKnownSids)/sizeof(WellKnownSids[0]); i++) {
         if (WellKnownSids[i].Type == WellKnownSidType) {
-            DWORD length = GetSidLengthRequired(WellKnownSids[i].SubAuthorityCount);
+            DWORD length = GetSidLengthRequired(WellKnownSids[i].Sid.SubAuthorityCount);
 
             if (*cbSid < length) {
                 SetLastError(ERROR_INSUFFICIENT_BUFFER);
                 return FALSE;
             }
 
-            CopyMemory(pSid, &WellKnownSids[i].Revision, length);
+            CopyMemory(pSid, &WellKnownSids[i].Sid.Revision, length);
             *cbSid = length;
             return TRUE;
         }
@@ -692,7 +746,7 @@ IsWellKnownSid( PSID pSid, WELL_KNOWN_SI
 
     for (i = 0; i < sizeof(WellKnownSids)/sizeof(WellKnownSids[0]); i++)
         if (WellKnownSids[i].Type == WellKnownSidType)
-            if (EqualSid(pSid, (PSID)&(WellKnownSids[i].Revision)))
+            if (EqualSid(pSid, (PSID)&(WellKnownSids[i].Sid.Revision)))
                 return TRUE;
 
     return FALSE;
@@ -1771,7 +1825,6 @@ LookupAccountSidA(
  *   domainSize  []
  *   name_use    []
  */
-
 BOOL WINAPI
 LookupAccountSidW(
 	IN LPCWSTR system,
@@ -1783,9 +1836,9 @@ LookupAccountSidW(
 	OUT PSID_NAME_USE name_use )
 {
     int i, j;
-    const WCHAR * ac = Administrator;	/* FIXME */
-    const WCHAR * dm = DOMAIN;		/* FIXME */
-    SID_NAME_USE use = SidTypeUser;	/* FIXME */
+    const WCHAR * ac = NULL;
+    const WCHAR * dm = NULL;
+    SID_NAME_USE use = 0;
 
     TRACE("(%s,sid=%s,%p,%p(%lu),%p,%p(%lu),%p)\n",
 	  debugstr_w(system),debugstr_sid(sid),
@@ -1799,6 +1852,7 @@ LookupAccountSidW(
         return FALSE;
     }
     
+    /* check the well known SIDs first */
     for (i = 0; i <= 60; i++) {
         if (IsWellKnownSid(sid, i)) {
             for (j = 0; j < (sizeof(ACCOUNT_SIDS) / sizeof(ACCOUNT_SIDS[0])); j++) {
@@ -1812,16 +1866,51 @@ LookupAccountSidW(
         }
     }
 
-    *accountSize = strlenW(ac)+1;
-    if (account && (*accountSize > strlenW(ac)))
-        strcpyW(account, ac);
-
-    *domainSize = strlenW(dm)+1;
-    if (domain && (*domainSize > strlenW(dm)))
-        strcpyW(domain,dm);
+    if (dm == NULL) {
+        MAX_SID local;
+        MAX_SID admin;
+        MAX_SID guest;
+
+        /* check for the local computer next */
+        if (ADVAPI_GetComputerSid(&local)) {
+            CopySid(GetSidLengthRequired(local.SubAuthorityCount), &admin, &local);
+            admin.SubAuthorityCount++;
+            admin.SubAuthority[4] = DOMAIN_USER_RID_ADMIN;
+            CopySid(GetSidLengthRequired(local.SubAuthorityCount), &guest, &local);
+            guest.SubAuthorityCount++;
+            guest.SubAuthority[4] = DOMAIN_USER_RID_GUEST;
+
+            if (EqualSid(sid, &local)) {
+                dm = ADVAPI_GetComputerNameW();
+                ac = Blank;
+                use = 3;
+            } else if (EqualSid(sid, &admin)) {
+                dm = ADVAPI_GetComputerNameW();
+                ac = Administrator;
+                use = 1;
+            } else if (EqualSid(sid, &guest)) {
+                dm = ADVAPI_GetComputerNameW();
+                ac = Guest;
+                use = 1;
+            }
+        }
+    }
 
-    *name_use = use;
-    return TRUE;
+    if (dm) {
+        *accountSize = strlenW(ac)+1;
+        if (account && (*accountSize > strlenW(ac)))
+            strcpyW(account, ac);
+
+        *domainSize = strlenW(dm)+1;
+        if (domain && (*domainSize > strlenW(dm)))
+            strcpyW(domain,dm);
+
+        *name_use = use;
+        return TRUE;
+    }
+
+    SetLastError(ERROR_NONE_MAPPED);
+    return FALSE;
 }
 
 /******************************************************************************
@@ -3601,10 +3690,17 @@ static DWORD ComputeStringSidSize(LPCWST
     else /* String constant format  - Only available in winxp and above */
     {
         int i;
+        const WCHAR LA[] = { 'L','A',0 };
+        const WCHAR LG[] = { 'L','G',0 };
 
-        for (i = 0; i < sizeof(WellKnownSids)/sizeof(WellKnownSids[0]); i++)
-            if (!strncmpW(WellKnownSids[i].wstr, StringSid, 2))
-                size += (WellKnownSids[i].SubAuthorityCount - 1) * sizeof(DWORD);
+        if (!strncmpW(LA, StringSid, 2))
+             size = GetSidLengthRequired(5);
+        else if (!strncmpW(LG, StringSid, 2))
+             size = GetSidLengthRequired(5);
+        else
+            for (i = 0; i < sizeof(WellKnownSids)/sizeof(WellKnownSids[0]); i++)
+                if (!strncmpW(WellKnownSids[i].wstr, StringSid, 2))
+                    size = GetSidLengthRequired(WellKnownSids[i].Sid.SubAuthorityCount);
     }
 
     return size;
@@ -3695,21 +3791,37 @@ static BOOL ParseStringSidToSid(LPCWSTR 
     else /* String constant format  - Only available in winxp and above */
     {
         int i;
-        pisid->Revision = SDDL_REVISION;
 
+        /* check the well known SIDs first */
         for (i = 0; i < sizeof(WellKnownSids)/sizeof(WellKnownSids[0]); i++)
+        {
             if (!strncmpW(WellKnownSids[i].wstr, StringSid, 2))
             {
-                DWORD j;
-                pisid->SubAuthorityCount = WellKnownSids[i].SubAuthorityCount;
-                pisid->IdentifierAuthority = WellKnownSids[i].IdentifierAuthority;
-                for (j = 0; j < WellKnownSids[i].SubAuthorityCount; j++)
-                    pisid->SubAuthority[j] = WellKnownSids[i].SubAuthority[j];
+                CopySid(*cBytes, pisid, (PSID)&WellKnownSids[i].Sid);
                 bret = TRUE;
+                break;
             }
+        }
 
         if (!bret)
-            FIXME("String constant not supported: %s\n", debugstr_wn(StringSid, 2));
+        {
+            const WCHAR LA[] = { 'L','A',0 };
+            const WCHAR LG[] = { 'L','G',0 };
+            if (!strncmpW(LA, StringSid, 2))
+            {
+                ADVAPI_GetComputerSid(pisid);
+                pisid->SubAuthority[pisid->SubAuthorityCount++] = DOMAIN_USER_RID_ADMIN;
+                bret = TRUE;
+            }
+            else if (!strncmpW(LG, StringSid, 2))
+            {
+                ADVAPI_GetComputerSid(pisid);
+                pisid->SubAuthority[pisid->SubAuthorityCount++] = DOMAIN_USER_RID_GUEST;
+                bret = TRUE;
+            }
+            else
+                FIXME("String constant not supported: %s\n", debugstr_wn(StringSid, 2));
+        }
     }
 
 lend:
diff -puN wine.cvs/dlls/advapi32/security.h wine/dlls/advapi32/security.h
--- wine.cvs/dlls/advapi32/security.h	1969-12-31 19:00:00.000000000 -0500
+++ wine/dlls/advapi32/security.h	2006-07-22 11:12:10.000000000 -0400
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2006 Robert Reif
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ *
+ */
+
+#ifndef __WINE_SECURITY_H
+#define __WINE_SECURITY_H
+
+const char * debugstr_sid(PSID sid);
+BOOL ADVAPI_IsLocalComputer(LPCWSTR ServerName);
+BOOL ADVAPI_GetComputerSid(PSID sid);
+
+#endif /* __WINE_SECURITY_H */


More information about the wine-patches mailing list