LookupAccountName всегда завершается ошибкой 122 (ERROR_INSUFFICIENT_BUFFER) - PullRequest
0 голосов
/ 09 апреля 2020

Возможно, кто-то может просветить меня здесь.

Я пытаюсь автоматизировать процесс подключения WiFi, где SSID определяется серийным номером. Поскольку это всегда отличается, я решил, что мне нужно сохранять временный профиль каждый раз, когда я подключаюсь sh.

WlanSaveTemporaryProfile() хочет LPCWSTR strAllUserProfileSecurity для определения разрешений для этого профиля. Пока что кроличья нора заставила меня попробовать LookupAccountNameW(). Я пытался AllocateAndInitializeSid() безрезультатно. Я попытался подключить пустой буфер с тем же результатом. В обоих случаях я получаю ошибку 122, в которой говорится, что буфер слишком мал.

Любая помощь здесь искренне приветствуется.


Вот соответствующий код. В основном построен на примерах из документации Microsoft.

DWORD GetStringSecurityDescriptor(
    PWCHAR ps_securityDescriptor, /* This needs to be populated when this function completes. */
    PULONG pul_securityDescriptorLen,
    LPWSTR ps_accountName
    )
{
    DWORD dw_result = NULL; 
    DWORD dw_lastError = NULL;
    DWORD dw_bufferSizeOfUserAccount = NULL;

    /* Create a security descriptor for the profile. */
    SECURITY_DESCRIPTOR secDesc;
    bool success = InitializeSecurityDescriptor(&secDesc, SECURITY_DESCRIPTOR_REVISION);
    if (!success)
    {
        wprintf(L"Security Descriptor Initialization Failed.\n");
    }

    PSID p_userSid = NULL;
    /* Attempt 2: Straight up malloc the memory. Doesn't work any better.*/
    //p_userSid = malloc(100);

    /* Attempt 1: Allocate and Initialize an SID for LookupAccountNameW(). */
    SID_IDENTIFIER_AUTHORITY auth = SECURITY_WORLD_SID_AUTHORITY;
    BOOL b_sidReady = AllocateAndInitializeSid(
        &auth,
        6,
        SECURITY_NULL_RID,
        SECURITY_WORLD_RID,
        SECURITY_LOCAL_RID,
        SECURITY_LOCAL_LOGON_RID,
        SECURITY_CREATOR_OWNER_RID,
        SECURITY_CREATOR_GROUP_RID,
        0, 0,
        &p_userSid
        );

    LPDWORD buf = &dw_bufferSizeOfUserAccount;
    WCHAR domainName[1000] = { 0 }; // Perhaps DNLEN + 1 was too small?
    DWORD domainNameLen = 1000;
    SID_NAME_USE use = SidTypeUser;

    // Currently failing. dw_bufferSizeOfUserAccount still recieves a 28, so that wasn't it.
    success = LookupAccountNameW(
        NULL, 
        ps_accountName, 
        p_userSid, 
        buf, 
        domainName, 
        &domainNameLen, 
        &use);
    if (!success) 
    {
        dw_lastError = GetLastError();
        switch (dw_lastError)
        {
        case ERROR_INSUFFICIENT_BUFFER: // LookupAccountNameW() always ends up here.
            wprintf(L"The data area passed to a system call is too small.\n");
            FreeSid(p_userSid);
            return dw_lastError;
        default:
            wprintf(L"Looking up Account Name failed. See Error 0x%x.\n", dw_lastError);
            FreeSid(p_userSid);
            return dw_lastError;
        }
    }

// ... more code irrelevant to this problem...

}

1 Ответ

0 голосов
/ 09 апреля 2020

Огромное спасибо Георгию Фирсову !

Я пропустил утверждение в документации.

Подсчитав размер SID и сохранив его в dw_bufferSizeOfUserAccount, функция успешно запустилась.

dw_bufferSizeOfUserAccount = GetLengthSid(p_userSid);
...