[PATCH 11/12] LookupAccountNameW() - refactor to eliminate code duplication
Paul Bryan Roberts
pbronline-wine at yahoo.co.uk
Sat Oct 18 18:42:33 CDT 2008
---
dlls/advapi32/security.c | 131 +++++++++++++++++++---------------------------
1 files changed, 54 insertions(+), 77 deletions(-)
diff --git a/dlls/advapi32/security.c b/dlls/advapi32/security.c
index 74918fa..86b0adf 100644
--- a/dlls/advapi32/security.c
+++ b/dlls/advapi32/security.c
@@ -2525,24 +2525,26 @@ BOOL WINAPI LookupAccountNameW( LPCWSTR lpSystemName, LPCWSTR lpAccountName, PSI
LPDWORD cchReferencedDomainName, PSID_NAME_USE peUse )
{
BOOL ret;
- PSID pSid;
+ PSID pSid = NULL;
unsigned int i;
DWORD sidLen;
DWORD nameLen;
LPWSTR userName = NULL;
LPWSTR computerName = NULL;
- LPCWSTR domainName;
+ LPCWSTR domainName = NULL;
+ SID_NAME_USE sidUse = 0;
- FIXME("%s %s %p %p %p %p %p - stub\n", debugstr_w(lpSystemName), debugstr_w(lpAccountName),
+ TRACE("%s %s %p %p %p %p %p - stub\n", debugstr_w(lpSystemName), debugstr_w(lpAccountName),
Sid, cbSid, ReferencedDomainName, cchReferencedDomainName, peUse);
if (!ADVAPI_IsLocalComputer(lpSystemName))
{
+ FIXME("Only local computer supported!\n");
SetLastError(RPC_S_SERVER_UNAVAILABLE);
return FALSE;
}
- if (!lpAccountName || !strcmpW(lpAccountName, Blank))
+ if (!lpAccountName || !lstrcmpW(lpAccountName, Blank))
{
lpAccountName = BUILTIN;
}
@@ -2551,7 +2553,7 @@ BOOL WINAPI LookupAccountNameW( LPCWSTR lpSystemName, LPCWSTR lpAccountName, PSI
for (i = 0; i < (sizeof(ACCOUNT_SIDS) / sizeof(ACCOUNT_SIDS[0])); i++)
{
- if (!strcmpW(lpAccountName, ACCOUNT_SIDS[i].account))
+ if (!lstrcmpW(lpAccountName, ACCOUNT_SIDS[i].account))
{
sidLen = SECURITY_MAX_SID_SIZE;
@@ -2559,88 +2561,71 @@ BOOL WINAPI LookupAccountNameW( LPCWSTR lpSystemName, LPCWSTR lpAccountName, PSI
ret = CreateWellKnownSid(ACCOUNT_SIDS[i].type, NULL, pSid, &sidLen);
- if (ret)
- {
- if (*cbSid < sidLen)
- {
- SetLastError(ERROR_INSUFFICIENT_BUFFER);
- ret = FALSE;
- }
- else if (Sid)
- {
- CopySid(*cbSid, Sid, pSid);
- }
+ domainName = ACCOUNT_SIDS[i].domain;
- *cbSid = sidLen;
- }
+ sidUse = ACCOUNT_SIDS[i].name_use;
- domainName = ACCOUNT_SIDS[i].domain;
- nameLen = strlenW(domainName);
+ break;
+ }
+ }
- if (*cchReferencedDomainName <= nameLen || !ret)
- {
- SetLastError(ERROR_INSUFFICIENT_BUFFER);
- nameLen += 1;
- ret = FALSE;
- }
- else if (ReferencedDomainName && domainName)
- {
- strcpyW(ReferencedDomainName, domainName);
- }
+ if (pSid == NULL)
+ {
+ /* Let the current Unix user id masquerade as first Windows user account */
- *cchReferencedDomainName = nameLen;
+ nameLen = UNLEN + 1;
- if (ret)
- {
- *peUse = ACCOUNT_SIDS[i].name_use;
- }
+ userName = HeapAlloc(GetProcessHeap(), 0, nameLen);
- HeapFree(GetProcessHeap(), 0, pSid);
+ ret = GetUserNameW(userName, &nameLen);
- return ret;
- }
- }
+ if (ret && !lstrcmpW(lpAccountName, userName))
+ {
+ /* Find SID for the first user account */
- /* Let the current Unix user id masquerade as first Windows user account */
+ sidLen = SECURITY_MAX_SID_SIZE;
- nameLen = UNLEN + 1;
+ pSid = HeapAlloc(GetProcessHeap(), 0, sidLen);
- userName = HeapAlloc(GetProcessHeap(), 0, nameLen);
+ ret = ADVAPI_GetComputerSid(pSid);
- ret = GetUserNameW(userName, &nameLen);
+ if (ret)
+ {
+ MAX_SID *max_sid = pSid;
- if (ret && strcmpW(lpAccountName, userName) != 0)
- {
- SetLastError(ERROR_NONE_MAPPED);
- ret = FALSE;
- }
+ int count = *GetSidSubAuthorityCount(pSid);
- HeapFree(GetProcessHeap(), 0, userName);
+ max_sid->SubAuthority[count] = 1000; /* first user account */
+ max_sid->SubAuthorityCount = count + 1;
- if (!ret)
- {
- return ret;
- }
+ sidLen = GetSidLengthRequired(max_sid->SubAuthorityCount);
+ }
- /* Find SID for the first user account */
+ /* Let the computer name be the domain name */
- sidLen = SECURITY_MAX_SID_SIZE;
+ nameLen = MAX_COMPUTERNAME_LENGTH + 1;
- pSid = HeapAlloc(GetProcessHeap(), 0, sidLen);
+ computerName = HeapAlloc(GetProcessHeap(), 0, nameLen * sizeof(WCHAR));
- ret = ADVAPI_GetComputerSid(pSid);
+ ret &= GetComputerNameW(computerName, &nameLen);
- if (ret)
- {
- MAX_SID *max_sid = pSid;
+ domainName = computerName;
- int count = *GetSidSubAuthorityCount(pSid);
+ sidUse = SidTypeUser;
+ }
+ }
- max_sid->SubAuthority[count] = 1000; /* first user account */
- max_sid->SubAuthorityCount = count + 1;
+ if (pSid == NULL)
+ {
+ FIXME("Only first user account supported!\n");
+ SetLastError(ERROR_NONE_MAPPED);
+ return FALSE;
+ }
- sidLen = GetSidLengthRequired(max_sid->SubAuthorityCount);
+ /* Return the information looked up */
+ if (ret)
+ {
if (*cbSid < sidLen)
{
SetLastError(ERROR_INSUFFICIENT_BUFFER);
@@ -2654,17 +2639,7 @@ BOOL WINAPI LookupAccountNameW( LPCWSTR lpSystemName, LPCWSTR lpAccountName, PSI
*cbSid = sidLen;
}
- HeapFree(GetProcessHeap(), 0, pSid);
-
- /* Let the computer name be the domain name */
-
- nameLen = MAX_COMPUTERNAME_LENGTH + 1;
-
- computerName = HeapAlloc(GetProcessHeap(), 0, nameLen * sizeof(WCHAR));
-
- ret &= GetComputerNameW(computerName, &nameLen);
-
- domainName = computerName;
+ nameLen = lstrlenW(domainName);
if (*cchReferencedDomainName <= nameLen || !ret)
{
@@ -2674,17 +2649,19 @@ BOOL WINAPI LookupAccountNameW( LPCWSTR lpSystemName, LPCWSTR lpAccountName, PSI
}
else if (ReferencedDomainName && domainName)
{
- strcpyW(ReferencedDomainName, domainName);
+ lstrcpyW(ReferencedDomainName, domainName);
}
*cchReferencedDomainName = nameLen;
if (ret)
{
- *peUse = SidTypeUser;
+ *peUse = sidUse;
}
+ HeapFree(GetProcessHeap(), 0, userName);
HeapFree(GetProcessHeap(), 0, computerName);
+ HeapFree(GetProcessHeap(), 0, pSid);
return ret;
}
--
1.5.4.3
--------------070900030602000802020706--
More information about the wine-patches
mailing list