Exemple #1
0
DWORD
LwCAGetRegKeyValue(
    PCSTR   pszConfigParamKeyPath,
    PCSTR   pszKey,
    PSTR    pszValue,
    size_t  valueLen
    )
{
    DWORD   dwError = 0;
    PSTR    pszLocalValue = NULL;
    DWORD   dwLocalValueLen = 0;

    if (pszValue == NULL)
    {
        dwError = LWCA_ERROR_INVALID_PARAMETER;
        BAIL_ON_LWCA_ERROR(dwError);
    }

    dwError = RegUtilGetValue(
        NULL,
        HKEY_THIS_MACHINE,
        NULL,
        pszConfigParamKeyPath,
        pszKey,
        NULL,
        (PVOID*)&pszLocalValue,
        &dwLocalValueLen);
    BAIL_ON_LWCA_ERROR(dwError);

    if (dwLocalValueLen > valueLen) // in case of string values, dwLocalValueLen includes '\0' and therefore valueLen
        // should also include space for '\0'
    {
        dwError = LWCA_ERROR_INVALID_PARAMETER; // TBD: Better error code??
        BAIL_ON_LWCA_ERROR(dwError);
    }


    memcpy( pszValue, pszLocalValue, dwLocalValueLen );

cleanup:
    if (pszLocalValue)
    {
        RegFreeMemory(pszLocalValue);
    }
    return dwError;

error:

    goto cleanup;
}
Exemple #2
0
NTSTATUS
SrvShareRegAdd(
    IN HANDLE hRepository,
    IN PWSTR  pwszShareName,
    IN PWSTR  pwszPath,
    IN PWSTR  pwszComment,
    IN PBYTE  pSecDesc,
    IN ULONG  ulSecDescLen,
    IN PWSTR  pwszService,
    IN ULONG  ulFlags
    )
{
    NTSTATUS ntStatus    = 0;
    HKEY     hRootKey    = NULL;
    HKEY     hKey        = NULL;
    HKEY     hSecKey     = NULL;
    PWSTR*   ppwszValues = NULL;
    PBYTE    pOutData    = NULL;
    SSIZE_T  cOutDataLen = 0;
    PWSTR    pwszFlags   = NULL;
    ULONG    ulCount     = 0;
    wchar16_t wszHKTM[]          = HKEY_THIS_MACHINE_W;
    wchar16_t wszSharesKey[]     = REG_KEY_PATH_SRV_SHARES_W;
    wchar16_t wszShareSecKey[]   = REG_KEY_PATH_SRV_SHARES_SECURITY_W;
    wchar16_t wszPathPrefix[]    = REG_KEY_PATH_PREFIX_W;
    wchar16_t wszFlagsPrefix[]   = REG_KEY_FLAGS_PREFIX_W;
    ULONG     ulPathPrefixLen =
                        (sizeof(wszPathPrefix)/sizeof(wchar16_t)) - 1;
    ULONG     ulFlagsPrefixLen =
                        (sizeof(wszFlagsPrefix)/sizeof(wchar16_t)) - 1;

    if (IsNullOrEmptyString(pwszShareName))
    {
        ntStatus = STATUS_INVALID_PARAMETER_2;
        BAIL_ON_NT_STATUS(ntStatus);
    }
    if (IsNullOrEmptyString(pwszPath))
    {
        ntStatus = STATUS_INVALID_PARAMETER_3;
        BAIL_ON_NT_STATUS(ntStatus);
    }

    ntStatus = NtRegOpenKeyExW(
                    hRepository,
                    NULL,
                    &wszHKTM[0],
                    0,
                    KEY_ALL_ACCESS,
                    &hRootKey);
    BAIL_ON_NT_STATUS(ntStatus);

    ntStatus = NtRegOpenKeyExW(
                    hRepository,
                    hRootKey,
                    &wszSharesKey[0],
                    0,
                    KEY_ALL_ACCESS,
                    &hKey);
    BAIL_ON_NT_STATUS(ntStatus);

    ntStatus = SrvAllocateMemory(sizeof(PWSTR) * 5, (PVOID*)&ppwszValues);
    BAIL_ON_NT_STATUS(ntStatus);

    // Path
    ntStatus = SrvAllocateMemory(
                    sizeof(wszPathPrefix) + wc16slen(pwszPath) * sizeof(wchar16_t),
                    (PVOID*)&ppwszValues[ulCount]);
    BAIL_ON_NT_STATUS(ntStatus);

    memcpy( (PBYTE)&ppwszValues[ulCount][0],
            (PBYTE)&wszPathPrefix[0],
            sizeof(wszPathPrefix) - sizeof(wchar16_t));
    memcpy( (PBYTE)&ppwszValues[ulCount][ulPathPrefixLen],
            (PBYTE)pwszPath,
            wc16slen(pwszPath) * sizeof(wchar16_t));

    if (!IsNullOrEmptyString(pwszComment))
    {
        wchar16_t wszCommentPrefix[] = REG_KEY_COMMENT_PREFIX_W;
        ULONG     ulCommentPrefixLen =
                            (sizeof(wszCommentPrefix)/sizeof(wchar16_t)) - 1;

        ntStatus = SrvAllocateMemory(
                            sizeof(wszCommentPrefix) + wc16slen(pwszComment) * sizeof(wchar16_t),
                            (PVOID*)&ppwszValues[++ulCount]);
        BAIL_ON_NT_STATUS(ntStatus);

        memcpy( (PBYTE)&ppwszValues[ulCount][0],
                (PBYTE)&wszCommentPrefix[0],
                sizeof(wszCommentPrefix) - sizeof(wchar16_t));
        memcpy( (PBYTE)&ppwszValues[ulCount][ulCommentPrefixLen],
                (PBYTE)pwszComment,
                wc16slen(pwszComment) * sizeof(wchar16_t));
    }

    if (!IsNullOrEmptyString(pwszService))
    {
        wchar16_t wszServicePrefix[] = REG_KEY_SERVICE_PREFIX_W;
        ULONG     ulServicePrefixLen =
                            (sizeof(wszServicePrefix)/sizeof(wchar16_t)) - 1;

        ntStatus = SrvAllocateMemory(
                            sizeof(wszServicePrefix) + wc16slen(pwszService) * sizeof(wchar16_t),
                            (PVOID*)&ppwszValues[++ulCount]);
        BAIL_ON_NT_STATUS(ntStatus);

        memcpy( (PBYTE)&ppwszValues[ulCount][0],
                (PBYTE)&wszServicePrefix[0],
                sizeof(wszServicePrefix) - sizeof(wchar16_t));
        memcpy( (PBYTE)&ppwszValues[ulCount][ulServicePrefixLen],
                (PBYTE)pwszService,
                wc16slen(pwszService) * sizeof(wchar16_t));
    }

    // Flags
    ntStatus = LwRtlWC16StringAllocatePrintfW(&pwszFlags, L"0x%08x", ulFlags);
    BAIL_ON_NT_STATUS(ntStatus);

    ntStatus = SrvAllocateMemory(
                            sizeof(wszFlagsPrefix) + wc16slen(pwszFlags) * sizeof(wchar16_t),
                            OUT_PPVOID(&ppwszValues[++ulCount]));
    BAIL_ON_NT_STATUS(ntStatus);

    memcpy((PBYTE)&ppwszValues[ulCount][0],
           (PBYTE)&wszFlagsPrefix[0],
           sizeof(wszFlagsPrefix) - sizeof(wchar16_t));
    memcpy((PBYTE)&ppwszValues[ulCount][ulFlagsPrefixLen],
           (PBYTE)pwszFlags,
           wc16slen(pwszFlags) * sizeof(wchar16_t));

    ntStatus = NtRegMultiStrsToByteArrayW(ppwszValues, &pOutData, &cOutDataLen);
    BAIL_ON_NT_STATUS(ntStatus);

    ntStatus = NtRegSetValueExW(
                    hRepository,
                    hKey,
                    pwszShareName,
                    0,
                    REG_MULTI_SZ,
                    pOutData,
                    cOutDataLen);
    BAIL_ON_NT_STATUS(ntStatus);

    if ((pSecDesc && !ulSecDescLen) || (!pSecDesc && ulSecDescLen))
    {
        ntStatus = STATUS_INVALID_PARAMETER_5;
        BAIL_ON_NT_STATUS(ntStatus);
    }

    ntStatus = NtRegOpenKeyExW(
                    hRepository,
                    hRootKey,
                    &wszShareSecKey[0],
                    0,
                    KEY_ALL_ACCESS,
                    &hSecKey);
    BAIL_ON_NT_STATUS(ntStatus);

    ntStatus = NtRegSetValueExW(
                    hRepository,
                    hSecKey,
                    pwszShareName,
                    0,
                    REG_BINARY,
                    pSecDesc,
                    ulSecDescLen);
    BAIL_ON_NT_STATUS(ntStatus);

cleanup:

    if (hRootKey)
    {
	NtRegCloseKey(hRepository, hRootKey);
    }
    if (hKey)
    {
	NtRegCloseKey(hRepository, hKey);
    }
    if (hSecKey)
    {
	NtRegCloseKey(hRepository, hSecKey);
    }
    if (ppwszValues)
    {
        SrvShareFreeStringArray(ppwszValues, 4);
    }
    if (pOutData)
    {
        RegFreeMemory(pOutData);
    }

    RTL_FREE(&pwszFlags);

    return ntStatus;

error:

    goto cleanup;
}