Ejemplo n.º 1
0
DWORD
VmDirRESTAuthTokenParse(
    PVDIR_REST_AUTH_TOKEN   pAuthToken,
    PCSTR                   pszAuthData
    )
{
    DWORD   dwError = 0;
    PSTR    pszAuthDataCp = NULL;
    PSTR    pszTokenType = NULL;
    PSTR    pszTokenData = NULL;
    PSTR    pszSignatureHex = NULL;

    if (!pAuthToken || IsNullOrEmptyString(pszAuthData))
    {
        BAIL_WITH_VMDIR_ERROR(dwError, VMDIR_ERROR_INVALID_PARAMETER);
    }

    dwError = VmDirAllocateStringA(pszAuthData, &pszAuthDataCp);
    BAIL_ON_VMDIR_ERROR(dwError);

    pszTokenType = VmDirStringTokA(pszAuthDataCp, " ", &pszTokenData);
    if (IsNullOrEmptyString(pszTokenType) ||
        IsNullOrEmptyString(pszTokenData))
    {
        BAIL_WITH_VMDIR_ERROR(dwError, VMDIR_ERROR_AUTH_BAD_DATA);
    }

    if (VmDirStringCompareA(pszTokenType, "Bearer", FALSE) == 0)
    {
        pAuthToken->tokenType = VDIR_REST_AUTH_TOKEN_BEARER;

        dwError = VmDirAllocateStringA(pszTokenData, &pAuthToken->pszAccessToken);
        BAIL_ON_VMDIR_ERROR(dwError);
    }
    else if (VmDirStringCompareA(pszTokenType, "hotk-pk", FALSE) == 0)
    {
        pAuthToken->tokenType = VDIR_REST_AUTH_TOKEN_HOTK;

        pszTokenData = VmDirStringTokA(pszTokenData, ":", &pszSignatureHex);
        if (IsNullOrEmptyString(pszTokenData))
        {
            BAIL_WITH_VMDIR_ERROR(dwError, VMDIR_ERROR_AUTH_BAD_DATA);
        }

        dwError = VmDirAllocateStringA(pszTokenData, &pAuthToken->pszAccessToken);
        BAIL_ON_VMDIR_ERROR(dwError);

        if (!IsNullOrEmptyString(pszSignatureHex))
        {
            dwError = VmDirAllocateStringA(pszSignatureHex, &pAuthToken->pszSignatureHex);
            BAIL_ON_VMDIR_ERROR(dwError);
        }
    }
    else
    {
        BAIL_WITH_VMDIR_ERROR(dwError, VMDIR_ERROR_AUTH_METHOD_NOT_SUPPORTED);
    }

cleanup:
    VMDIR_SAFE_FREE_MEMORY(pszAuthDataCp);
    return dwError;

error:
    // don't log error if VMDIR_ERROR_AUTH_METHOD_NOT_SUPPORTED,
    // because it will try other available auth methods
    if (dwError != VMDIR_ERROR_AUTH_METHOD_NOT_SUPPORTED)
    {
        VMDIR_LOG_ERROR(
                VMDIR_LOG_MASK_ALL,
                "%s failed, error (%d)",
                __FUNCTION__,
                dwError);
    }
    goto cleanup;
}
Ejemplo n.º 2
0
/*
 * Convert AttrIndexDesc content into VDIR_CFG_ATTR_INDEX_DECS
 *
 * NOTE: pszStr content will be change
 */
DWORD
VdirstrToAttrIndexDesc(
    PSTR    pszStr,
    PVDIR_CFG_ATTR_INDEX_DESC   pDesc)
{
    DWORD   dwError = 0;
    PSTR    pToken = NULL;
    PSTR    pRest = NULL;
    PSTR    pSep = " ";
    PSTR    pszLocal = NULL;

    assert (pszStr && pDesc);

    // make a local copy
    dwError = VmDirAllocateStringA(
            pszStr,
            &pszLocal);
    BAIL_ON_VMDIR_ERROR(dwError);

    pDesc->status = VDIR_CFG_ATTR_INDEX_ENABLED;

    pToken = VmDirStringTokA(pszLocal, pSep, &pRest);
    if (!pToken)
    {
        dwError = ERROR_INVALID_CONFIGURATION;
        BAIL_ON_VMDIR_ERROR(dwError);
    }

    dwError = VmDirAllocateStringA(
            pToken,
            &(pDesc->pszAttrName));
    BAIL_ON_VMDIR_ERROR(dwError);

    for (pToken=VmDirStringTokA(NULL, pSep, &pRest);
        pToken;
        pToken=VmDirStringTokA(NULL, pSep, &pRest))
    {
        if (VmDirStringCompareA(pToken, "eq", FALSE) == 0)
        {
            pDesc->iTypes |= INDEX_TYPE_EQUALITY;
        }
        else if (VmDirStringCompareA(pToken, "sub", FALSE) == 0)
        {
            pDesc->iTypes |= INDEX_TYPE_SUBSTR;
        }
        else if (VmDirStringCompareA(pToken, "pres", FALSE) == 0)
        {
            pDesc->iTypes |= INDEX_TYPE_PRESENCE;
        }
        else if (VmDirStringCompareA(pToken, "approx", FALSE) == 0)
        {
            pDesc->iTypes |= INDEX_TYPE_APPROX;
        }
        else if (VmDirStringCompareA(pToken, "unique", FALSE) == 0)
        {
            pDesc->bIsUnique = TRUE;
        }
        else if (VmDirStringCompareA(pToken, ATTR_INDEX_BUILDING_FLAG, FALSE) == 0)
        {
            //TODO, need to remove building flag off indices entry
            //if crash or power off during indexing, we will get here
            pDesc->status = VDIR_CFG_ATTR_INDEX_ABORTED;
        }
        else
        {
            dwError = ERROR_INVALID_CONFIGURATION;
            BAIL_ON_VMDIR_ERROR(dwError);
        }
    }



cleanup:

    VMDIR_SAFE_FREE_MEMORY(pszLocal);

    return dwError;

error:

    VMDIR_SAFE_FREE_MEMORY(pDesc->pszAttrName);

    goto cleanup;
}
Ejemplo n.º 3
0
DWORD
VmDirIndexCfgRestoreProgress(
    PVDIR_BACKEND_CTX   pBECtx,
    PVDIR_INDEX_CFG     pIndexCfg,
    PBOOLEAN            pbRestore
    )
{
    DWORD   dwError = 0;
    PSTR    pszStatusKey = NULL;
    PSTR    pszStatusVal = NULL;
    PSTR    pszInitOffsetKey = NULL;
    PSTR    pszInitOffsetVal = NULL;
    PSTR    pszScopesKey = NULL;
    PSTR    pszScopesVal = NULL;
    PSTR    pszNewScopesKey = NULL;
    PSTR    pszNewScopesVal = NULL;
    PSTR    pszDelScopesKey = NULL;
    PSTR    pszDelScopesVal = NULL;
    PSTR    pszScope = NULL;

    if (!pBECtx || !pIndexCfg || !pbRestore)
    {
        dwError = ERROR_INVALID_PARAMETER;
        BAIL_ON_VMDIR_ERROR(dwError);
    }

    dwError = _BuildAllKeyStrs(
            pIndexCfg,
            &pszStatusKey,
            &pszInitOffsetKey,
            &pszScopesKey,
            &pszNewScopesKey,
            &pszDelScopesKey);
    BAIL_ON_VMDIR_ERROR(dwError);

    if (gVdirIndexGlobals.bFirstboot)
    {
        pIndexCfg->status = VDIR_INDEXING_COMPLETE;
    }
    else
    {
        PSTR pszToken = NULL;
        char* save = NULL;

        dwError = pBECtx->pBE->pfnBEUniqKeyGetValue(
                pBECtx, pszStatusKey, &pszStatusVal);
        BAIL_ON_VMDIR_ERROR(dwError);

        pIndexCfg->status = VmDirStringToIA(pszStatusVal);

        dwError = pBECtx->pBE->pfnBEUniqKeyGetValue(
                pBECtx, pszInitOffsetKey, &pszInitOffsetVal);
        BAIL_ON_VMDIR_ERROR(dwError);

        pIndexCfg->initOffset = VmDirStringToIA(pszInitOffsetVal);

        dwError = pBECtx->pBE->pfnBEUniqKeyGetValue(
                pBECtx, pszScopesKey, &pszScopesVal);
        BAIL_ON_VMDIR_ERROR(dwError);

        if (!IsNullOrEmptyString(pszScopesVal))
        {
            pszToken = VmDirStringTokA(pszScopesVal, INDEX_SEP, &save);
            while (pszToken)
            {
                dwError = VmDirAllocateStringA(pszToken, &pszScope);
                BAIL_ON_VMDIR_ERROR(dwError);

                dwError = LwRtlHashMapInsert(
                        pIndexCfg->pUniqScopes, pszScope, NULL, NULL);
                BAIL_ON_VMDIR_ERROR(dwError);
                pszScope = NULL;

                pszToken = VmDirStringTokA(NULL, INDEX_SEP, &save);
            }
        }

        dwError = pBECtx->pBE->pfnBEUniqKeyGetValue(
                pBECtx, pszNewScopesKey, &pszNewScopesVal);
        BAIL_ON_VMDIR_ERROR(dwError);

        if (!IsNullOrEmptyString(pszNewScopesVal))
        {
            pszToken = VmDirStringTokA(pszNewScopesVal, INDEX_SEP, &save);
            while (pszToken)
            {
                dwError = VmDirAllocateStringA(pszToken, &pszScope);
                BAIL_ON_VMDIR_ERROR(dwError);

                dwError = VmDirLinkedListInsertHead(
                        pIndexCfg->pNewUniqScopes, pszScope, NULL);
                BAIL_ON_VMDIR_ERROR(dwError);
                pszScope = NULL;

                pszToken = VmDirStringTokA(NULL, INDEX_SEP, &save);
            }
        }

        dwError = pBECtx->pBE->pfnBEUniqKeyGetValue(
                pBECtx, pszDelScopesKey, &pszDelScopesVal);
        BAIL_ON_VMDIR_ERROR(dwError);

        if (!IsNullOrEmptyString(pszDelScopesVal))
        {
            pszToken = VmDirStringTokA(pszDelScopesVal, INDEX_SEP, &save);
            while (pszToken)
            {
                dwError = VmDirAllocateStringA(pszToken, &pszScope);
                BAIL_ON_VMDIR_ERROR(dwError);

                dwError = VmDirLinkedListInsertHead(
                        pIndexCfg->pDelUniqScopes, pszScope, NULL);
                BAIL_ON_VMDIR_ERROR(dwError);
                pszScope = NULL;

                pszToken = VmDirStringTokA(NULL, INDEX_SEP, &save);
            }
        }

        *pbRestore = TRUE;
    }

cleanup:
    VMDIR_SAFE_FREE_MEMORY(pszStatusKey);
    VMDIR_SAFE_FREE_MEMORY(pszStatusVal);
    VMDIR_SAFE_FREE_MEMORY(pszInitOffsetKey);
    VMDIR_SAFE_FREE_MEMORY(pszInitOffsetVal);
    VMDIR_SAFE_FREE_MEMORY(pszScopesKey);
    VMDIR_SAFE_FREE_MEMORY(pszScopesVal);
    VMDIR_SAFE_FREE_MEMORY(pszNewScopesKey);
    VMDIR_SAFE_FREE_MEMORY(pszNewScopesVal);
    VMDIR_SAFE_FREE_MEMORY(pszDelScopesKey);
    VMDIR_SAFE_FREE_MEMORY(pszDelScopesVal);
    return dwError;

error:
    VMDIR_LOG_ERROR( VMDIR_LOG_MASK_ALL,
            "%s failed, error (%d)", __FUNCTION__, dwError );

    VMDIR_SAFE_FREE_MEMORY(pszScope);
    goto cleanup;
}
Ejemplo n.º 4
0
static
DWORD
_PopulateOperationModAttributes(
        LDAP *pLd,
        LDAPMessage *pEntry,
        PVDIR_OPERATION pLdapOp
        )
{
    DWORD dwError = 0;
    struct berval** ppValues = NULL;
    PLW_HASHMAP pHashMap = NULL;

    PSTR pszBuf = NULL;
    PSTR pszAttrName = NULL;
    PSTR pszAttrMetaData = NULL;
    PSTR pszAttrUsnlessMetaData = NULL;
    PSTR pszAttrNewMetaData = NULL;
    PSTR pszKey = NULL;
    PSTR pszValue = NULL;
    USN localUsn = 0;
    PVDIR_BERVALUE pBerValue = NULL;
    size_t iBerValueSize = 0;
    DWORD i = 0, j = 0;

    dwError = LwRtlCreateHashMap(
            &pHashMap,
            LwRtlHashDigestPstrCaseless,
            LwRtlHashEqualPstrCaseless,
            NULL
            );
    BAIL_ON_VMDIR_ERROR(dwError);

    dwError = pLdapOp->pBEIF->pfnBEGetNextUSN(pLdapOp->pBECtx, &localUsn);
    BAIL_ON_VMDIR_ERROR(dwError);

    if (!(ppValues = ldap_get_values_len(pLd, pEntry, ATTR_ATTR_META_DATA)))
    {
        dwError = VMDIR_ERROR_SCHEMA_BAD_METADATA;
        BAIL_ON_VMDIR_ERROR(dwError);
    }

    for (i = 0; ppValues[i] != NULL; i++)
    {
        dwError = VmDirAllocateStringA(ppValues[i]->bv_val, &pszBuf);
        BAIL_ON_VMDIR_ERROR(dwError);

        pszAttrName = VmDirStringTokA(pszBuf, ":", &pszAttrMetaData);
        VmDirStringTokA(pszAttrMetaData, ":", &pszAttrUsnlessMetaData);

        dwError = VmDirAllocateStringPrintf(
                &pszAttrNewMetaData,
                "%ld:%s",
                localUsn,
                pszAttrUsnlessMetaData);
        BAIL_ON_VMDIR_ERROR(dwError);

        dwError = VmDirAllocateStringA(pszAttrName, &pszKey);
        BAIL_ON_VMDIR_ERROR(dwError);

        dwError = LwRtlHashMapInsert(pHashMap, pszKey, pszAttrNewMetaData, NULL);
        BAIL_ON_VMDIR_ERROR(dwError);

        VMDIR_SAFE_FREE_MEMORY(pszBuf);
    }
    ldap_value_free_len(ppValues);
    ppValues = NULL;

    for (i = 0; ppszSchemaEntryAttrs[i] != NULL; i++)
    {
        if (VmDirStringCompareA(ppszSchemaEntryAttrs[i], ATTR_ATTR_META_DATA, FALSE) != 0)
        {
            ppValues = ldap_get_values_len(pLd, pEntry, ppszSchemaEntryAttrs[i]);
            if (ppValues)
            {
                iBerValueSize = ldap_count_values_len(ppValues);
                dwError = VmDirAllocateMemory(
                        sizeof(VDIR_BERVALUE) * iBerValueSize,
                        (PVOID*)&pBerValue);
                BAIL_ON_VMDIR_ERROR(dwError);

                if (VmDirStringCompareA(ppszSchemaEntryAttrs[i], ATTR_USN_CHANGED, FALSE) == 0)
                {
                    assert(iBerValueSize == 1);

                    dwError = VmDirAllocateStringPrintf(&pBerValue[0].lberbv_val, "%ld", localUsn);
                    BAIL_ON_VMDIR_ERROR(dwError);
                    pBerValue[0].lberbv_len = VmDirStringLenA(pBerValue[0].lberbv_val);
                    pBerValue[0].bOwnBvVal = TRUE;
                }
                else
                {
                    for (j = 0; j < iBerValueSize; j++)
                    {
                        dwError = VmDirAllocateStringA(ppValues[j]->bv_val, &pBerValue[j].lberbv_val);
                        BAIL_ON_VMDIR_ERROR(dwError);
                        pBerValue[j].lberbv_len = ppValues[j]->bv_len;
                        pBerValue[j].bOwnBvVal = TRUE;
                    }
                }

                dwError = VmDirOperationAddModReq(
                        pLdapOp,
                        MOD_OP_REPLACE,
                        ppszSchemaEntryAttrs[i],
                        pBerValue,
                        iBerValueSize);
                BAIL_ON_VMDIR_ERROR(dwError);

                dwError = LwRtlHashMapFindKey(
                        pHashMap,
                        (PVOID*)&pszValue,
                        ppszSchemaEntryAttrs[i]);
                BAIL_ON_VMDIR_ERROR(dwError);

                dwError = VmDirStringCpyA(
                        pLdapOp->request.modifyReq.mods->attr.metaData,
                        VMDIR_MAX_ATTR_META_DATA_LEN,
                        pszValue);
                BAIL_ON_VMDIR_ERROR(dwError);

                ldap_value_free_len(ppValues);
                ppValues = NULL;
                for (j = 0; j < iBerValueSize; j++)
                {
                    VmDirFreeBervalContent(&pBerValue[j]);
                }
                VMDIR_SAFE_FREE_MEMORY(pBerValue);
            }
        }
    }

cleanup:
    LwRtlHashMapClear(pHashMap, _FreeStringPair, NULL);
    LwRtlFreeHashMap(&pHashMap);
    return dwError;

error:
    VMDIR_LOG_ERROR( VMDIR_LOG_MASK_ALL,
            "%s,%d failed, error(%d)", __FUNCTION__, __LINE__, dwError );

    VMDIR_SAFE_FREE_MEMORY(pszBuf);
    VMDIR_SAFE_FREE_MEMORY(pszAttrNewMetaData);
    if (ppValues)
    {
        ldap_value_free_len(ppValues);
    }
    for (j = 0; j < iBerValueSize; j++)
    {
        VmDirFreeBervalContent(&pBerValue[j]);
    }
    VMDIR_SAFE_FREE_MEMORY(pBerValue);
    goto cleanup;
}
Ejemplo n.º 5
0
DWORD
VmDirSchemaAttrIdMapReadDB(
    PVDIR_SCHEMA_ATTR_ID_MAP    pAttrIdMap
    )
{
    DWORD   dwError = 0;
    DWORD   i = 0;
    VDIR_BACKEND_CTX    beCtx = {0};
    BOOLEAN             bHasTxn = FALSE;
    PVMDIR_STRING_LIST  pStringList = NULL;
    PSTR                pszBuf = NULL;
    PSTR                pszName = NULL;
    PSTR                pszId = NULL;
    USHORT              usId = 0;

    if (!pAttrIdMap)
    {
        dwError = ERROR_INVALID_PARAMETER;
        BAIL_ON_VMDIR_ERROR(dwError);
    }

    beCtx.pBE = VmDirBackendSelect(PERSISTED_DSE_ROOT_DN);

    dwError = beCtx.pBE->pfnBETxnBegin(&beCtx, VDIR_BACKEND_TXN_READ, &bHasTxn);
    BAIL_ON_VMDIR_ERROR(dwError);

    dwError = beCtx.pBE->pfnBEDupKeyGetValues(
            &beCtx, ATTR_ID_MAP_KEY, &pStringList);
    BAIL_ON_VMDIR_ERROR(dwError);

    if (bHasTxn)
    {
        dwError = beCtx.pBE->pfnBETxnCommit(&beCtx);
        bHasTxn = FALSE;
        BAIL_ON_VMDIR_ERROR(dwError);
    }

    for (i = 0; i < pStringList->dwCount; i++)
    {
        dwError = VmDirAllocateStringA(pStringList->pStringList[i], &pszBuf);
        BAIL_ON_VMDIR_ERROR(dwError);

        pszId = VmDirStringTokA(pszBuf, SCHEMA_ATTR_ID_MAP_SEP, &pszName);
        usId = (USHORT)VmDirStringToIA(pszId);

        if (VmDirSchemaAttrIdMapGetAttrId(pAttrIdMap, pszName, NULL) != 0)
        {
            dwError = VmDirAllocateStringA(pszName, &pszName);
            BAIL_ON_VMDIR_ERROR(dwError);

            dwError = LwRtlHashMapInsert(pAttrIdMap->pStoredIds,
                    pszName, (PVOID)(uintptr_t)usId, NULL);
            BAIL_ON_VMDIR_ERROR(dwError);

            if (usId >= pAttrIdMap->usNextId)
            {
                pAttrIdMap->usNextId = usId + 1;
            }
        }

        VMDIR_SAFE_FREE_MEMORY(pszBuf);
    }

cleanup:
    VmDirBackendCtxContentFree(&beCtx);
    VmDirStringListFree(pStringList);
    return dwError;

error:
    if (bHasTxn)
    {
        beCtx.pBE->pfnBETxnAbort(&beCtx);
    }
    VMDIR_LOG_ERROR( VMDIR_LOG_MASK_ALL,
            "%s failed, error (%d)", __FUNCTION__, dwError );

    VMDIR_SAFE_FREE_MEMORY(pszBuf);
    goto cleanup;
}