/*---------------------------------------------------------------------------*\
 * NAME: GetNamedValueSD 
 * --------------------------------------------------------------------------*
 * DESCRIPTION: Retrieves a designated security descriptor from the 
 * registry.
\*---------------------------------------------------------------------------*/
DWORD GetNamedValueSD (
    HKEY hkeyRoot,
    LPTSTR tszKeyName,
    LPTSTR tszValueName,
    SECURITY_DESCRIPTOR **ppSecurityDesc,
    BOOL *psdNew
    )
{
    DWORD  dwReturnValue = ERROR_SUCCESS;
    HKEY   hkeyReg       = NULL;
    DWORD  dwType        = 0;
    DWORD  cbSize        = 0;

    if(!ppSecurityDesc) return ERROR_BAD_ARGUMENTS;

    if(psdNew) *psdNew = FALSE;
    *ppSecurityDesc = NULL;

    // Get the security descriptor from the named value. If it doesn't
    // exist, create a fresh one.
    dwReturnValue = RegOpenKeyEx (hkeyRoot, tszKeyName, 0, KEY_ALL_ACCESS, &hkeyReg);

    if (dwReturnValue != ERROR_SUCCESS)
    {
        if(dwReturnValue == ERROR_FILE_NOT_FOUND)
        {
            goto CLEANUP;
        }
        
        return dwReturnValue;
    }
    
    dwReturnValue = RegQueryValueEx (hkeyReg, tszValueName, NULL, &dwType, NULL, &cbSize);

    if (dwReturnValue != ERROR_SUCCESS && dwReturnValue != ERROR_INSUFFICIENT_BUFFER)
    {
        goto CLEANUP;
    } 

    *ppSecurityDesc = (SECURITY_DESCRIPTOR *) malloc (cbSize);

    dwReturnValue = RegQueryValueEx (hkeyReg, tszValueName, NULL, &dwType, (LPBYTE) *ppSecurityDesc, &cbSize);


CLEANUP:

    if(dwReturnValue != ERROR_SUCCESS)
    {
        if(*ppSecurityDesc) free(*ppSecurityDesc);
    
        *ppSecurityDesc = NULL;

        if(psdNew)
        {
            dwReturnValue = CreateNewSD (ppSecurityDesc);
            if (dwReturnValue == ERROR_SUCCESS)
            {
                *psdNew = TRUE;
            }
        }
    }

    if(hkeyReg) RegCloseKey (hkeyReg);

    return dwReturnValue;
}
Beispiel #2
0
DWORD
GetNamedValueSD (
    HKEY RootKey,
    LPTSTR KeyName,
    LPTSTR ValueName,
    SECURITY_DESCRIPTOR **SD,
    BOOL *NewSD
    )
{
    DWORD               returnValue;
    HKEY                registryKey;
    DWORD               valueType;
    DWORD               valueSize;

    *NewSD = FALSE;

    //
    // Get the security descriptor from the named value. If it doesn't
    // exist, create a fresh one.
    //

    returnValue = RegOpenKeyEx (RootKey, KeyName, 0, KEY_ALL_ACCESS, &registryKey);

    if (returnValue != ERROR_SUCCESS)
    {
        if (returnValue == ERROR_FILE_NOT_FOUND)
        {
            *SD = NULL;
            returnValue = CreateNewSD (SD);
            if (returnValue != ERROR_SUCCESS)
                return returnValue;

            *NewSD = TRUE;
            return ERROR_SUCCESS;
        } else
            return returnValue;
    }

    returnValue = RegQueryValueEx (registryKey, ValueName, NULL, &valueType, NULL, &valueSize);

    if (returnValue && returnValue != ERROR_INSUFFICIENT_BUFFER)
    {
        *SD = NULL;
        returnValue = CreateNewSD (SD);
        if (returnValue != ERROR_SUCCESS)
            return returnValue;

        *NewSD = TRUE;
    } else
    {
        *SD = (SECURITY_DESCRIPTOR *) malloc (valueSize);

        returnValue = RegQueryValueEx (registryKey, ValueName, NULL, &valueType, (LPBYTE) *SD, &valueSize);
        if (returnValue)
        {
            free (*SD);

            *SD = NULL;
            returnValue = CreateNewSD (SD);
            if (returnValue != ERROR_SUCCESS)
                return returnValue;

            *NewSD = TRUE;
        }
    }

    RegCloseKey (registryKey);

    return ERROR_SUCCESS;
}