Ejemplo n.º 1
0
/* *************************************************************
 * if success, pAttr takes ownership of pBervs and its contents
 * *************************************************************
 * Assume pBervs itself is from ONE memory allocate.
 */
static
DWORD
AttributeAppendBervArray(
    PVDIR_ATTRIBUTE  pAttr,
    PVDIR_BERVALUE     pBervs,
    USHORT      usBervSize
    )
{
    DWORD   dwError = 0;
    PVDIR_BERVALUE pNewBerv = NULL;

    if (!pAttr || !pBervs)
    {
        dwError = ERROR_INVALID_PARAMETER;
        BAIL_ON_VMDIR_ERROR(dwError);
    }

    // add one more BerValue in size as bdb-store/entry.c needs that.
    dwError = VmDirReallocateMemoryWithInit(
            pAttr->vals,
            (PVOID*) &pNewBerv,
            sizeof(VDIR_BERVALUE) * (pAttr->numVals + usBervSize + 1),
            sizeof(VDIR_BERVALUE) * (pAttr->numVals));
    BAIL_ON_VMDIR_ERROR(dwError);

    // takes over BerValue contents (i.e. lberbv.bv_val and lberbv.bv_len)
    dwError = VmDirCopyMemory(
        pNewBerv + pAttr->numVals,
        sizeof(VDIR_BERVALUE) * (pAttr->numVals + usBervSize + 1),
        pBervs,
        sizeof(VDIR_BERVALUE) * usBervSize
    );
    BAIL_ON_VMDIR_ERROR(dwError);

    // free array of BerValue itself
    VMDIR_SAFE_FREE_MEMORY(pBervs);

    pAttr->numVals += usBervSize;
    pAttr->vals = pNewBerv;

cleanup:

    return dwError;

error:

    VMDIR_SAFE_FREE_MEMORY(pNewBerv);

    goto cleanup;
}
Ejemplo n.º 2
0
static
DWORD
AddAttrValsToEntryStruct(
   VDIR_ENTRY *     e,
   VDIR_ATTRIBUTE * eAttr,    // Entry attribute to be updated with new attribute values
   VDIR_ATTRIBUTE * modAttr,  // Modify attribute containing new attribute values
   PSTR*            ppszErrMsg
   )
{
    unsigned int     i = 0;
    unsigned int     j = 0;
    DWORD   dwError = ERROR_SUCCESS;
    PSTR    pszErrMsg = NULL;

    assert(e->allocType == ENTRY_STORAGE_FORMAT_NORMAL);

    if ((size_t)eAttr->numVals + (size_t)modAttr->numVals > UINT16_MAX)
    {
        dwError = VMDIR_ERROR_DATA_CONSTRAINT_VIOLATION;
        BAIL_ON_VMDIR_ERROR_WITH_MSG(dwError, (pszErrMsg),
                                      "Too many %s attribute values, max %u allowed.",
                                      VDIR_SAFE_STRING(eAttr->type.lberbv_val), UINT16_MAX);
    }

    dwError = VmDirReallocateMemoryWithInit(eAttr->vals, (PVOID*)(&(eAttr->vals)),
                                             (eAttr->numVals + modAttr->numVals + 1) * sizeof(VDIR_BERVALUE),
                                             (eAttr->numVals + 1) * sizeof(VDIR_BERVALUE));
    BAIL_ON_VMDIR_ERROR(dwError);

    for (i = 0, j = eAttr->numVals; i < modAttr->numVals; i++, j++)
    {
        dwError = VmDirBervalContentDup(&modAttr->vals[i], &eAttr->vals[j]);
        BAIL_ON_VMDIR_ERROR(dwError);
    }
    memset(&(eAttr->vals[j]), 0, sizeof(VDIR_BERVALUE)); // set last BerValue.lberbv.bv_val to NULL;
    eAttr->numVals += modAttr->numVals;

cleanup:
    VMDIR_SAFE_FREE_MEMORY(pszErrMsg);
    return dwError;

error:
    if (ppszErrMsg)
    {
        *ppszErrMsg = pszErrMsg;
        pszErrMsg = NULL;
    }

    goto cleanup;
}
Ejemplo n.º 3
0
/*
 * double the size of candidate eid buffer
 */
static
DWORD
VmDirReallocCandidates(
    PVDIR_CANDIDATES    pCands
    )
{
    DWORD   dwError = 0;
    int     iOldSize = pCands->max * sizeof(ENTRYID);
    int     iNewSize = iOldSize * 2;

    dwError = VmDirReallocateMemoryWithInit(pCands->eIds, (PVOID*)&pCands->eIds, iNewSize, iOldSize);
    BAIL_ON_VMDIR_ERROR(dwError);

    pCands->max *= 2;

error:

    return dwError;
}
Ejemplo n.º 4
0
DWORD
VmDirStringListAdd(
    PVMDIR_STRING_LIST pStringList,
    PCSTR pszString
    )
{
    DWORD dwError = 0;

    // keep string list null-terminated
    if (pStringList->dwCount + 1 == pStringList->dwSize)
    {
        DWORD dwOldSize = pStringList->dwSize;
        DWORD dwNewSize = pStringList->dwSize * 2;

        //
        // Check for overflow.
        //
        if (dwNewSize < pStringList->dwSize)
        {
            BAIL_WITH_VMDIR_ERROR(dwError, VMDIR_ERROR_SIZELIMIT_EXCEEDED);
        }

        dwError = VmDirReallocateMemoryWithInit(
                    pStringList->pStringList,
                    (PVOID*)&pStringList->pStringList,
                    dwNewSize * sizeof(PSTR),
                    dwOldSize * sizeof(PSTR));
        BAIL_ON_VMDIR_ERROR(dwError);

        pStringList->dwSize = dwNewSize;
    }

    pStringList->pStringList[pStringList->dwCount++] = pszString;

cleanup:
    return dwError;

error:
    goto cleanup;
}
Ejemplo n.º 5
0
/*
 * Read schema definition from file
 * We ignore -
 * 1. DN/EntryDN
 * 2. ldapSyntax
 * 3. matchingRules
 */
static
DWORD
schemaReadFile(
    PCSTR   pszSchemaFilePath,
    PSTR**  pppszDescs,
    USHORT*  pdwSize
    )
{
    static PSTR pszDNTag = "DN:";
    static PSTR pszENTRYDNTag = "ENTRYDN:";
    static PSTR pszSyntaxTag = "ldapSyntaxes:";
    static PSTR pszMatchingRuleTag = "matchingRules:";
    static PSTR pszName = "NAME ";

    DWORD dwError = 0;
    USHORT dwSize = 0;
    DWORD dwCnt = 100;
    size_t iOldLen = 0 ;
    size_t iNewLen = 0 ;

    PSTR*    ppszDescs = NULL;
    PSTR    pszTmp = NULL;

    char  pbuf[2048] = {0};
    FILE* fp = NULL;


    dwError = VmDirAllocateMemory(
            sizeof(*ppszDescs) * dwCnt,
            (PVOID*)&ppszDescs);
    BAIL_ON_VMDIR_ERROR(dwError);

#ifndef _WIN32
    fp=fopen(pszSchemaFilePath, "r");
#else
    if( fopen_s(&fp, pszSchemaFilePath, "r") != 0 )
    {
        fp = NULL;
    }
#endif
    if(NULL == fp)
    {
        dwError = errno;
        VMDIR_LOG_ERROR(    VMDIR_LOG_MASK_ALL,
                            "Open schema file (%s) failed. Error (%d)",
                            pszSchemaFilePath,
                            dwError);

        BAIL_ON_VMDIR_ERROR(dwError);
    }

    while (fgets(pbuf, sizeof(pbuf), fp) != NULL)
    {
        PSTR pszTag = NULL;
        size_t len = VmDirStringLenA(pbuf)-1;

        if (pbuf[len] == '\n')
        {
            pbuf[len] = '\0';
        }

        if ((pbuf[0] == '\0')    ||
            (pbuf[0] == '#')    ||
            (pbuf[0] != ' ' && (pszTag = VmDirStringChrA(pbuf, ':')) == NULL))
        {
            continue;
        }

        if (VmDirStringNCompareA(pbuf, pszDNTag, VmDirStringLenA(pszDNTag), FALSE) == 0              ||
            VmDirStringNCompareA(pbuf, pszENTRYDNTag, VmDirStringLenA(pszENTRYDNTag), FALSE) == 0    ||
            VmDirStringNCompareA(pbuf, pszSyntaxTag, VmDirStringLenA(pszSyntaxTag), FALSE) == 0        ||
            VmDirStringNCompareA(pbuf, pszMatchingRuleTag, VmDirStringLenA(pszMatchingRuleTag), FALSE) == 0)
        {
            continue;
        }

        if (dwCnt == dwSize + 1)
        {
            DWORD    dwOldSize = dwCnt;
            dwCnt = dwCnt * 2;

            dwError = VmDirReallocateMemoryWithInit(
                    ppszDescs,
                    (PVOID*)&ppszDescs,
                    dwCnt * sizeof(*ppszDescs),
                    dwOldSize * sizeof(*ppszDescs));
            BAIL_ON_VMDIR_ERROR(dwError);
        }

        if (pbuf[0] != ' ')
        {
            dwError = VmDirAllocateStringA(pbuf,
                    &(ppszDescs[dwSize++]));
            BAIL_ON_VMDIR_ERROR(dwError);
        }
        else
        {
            // consolidate leading spaces into one
            char* pszNonSpace = pbuf+1;
            while (*pszNonSpace == ' ') { pszNonSpace++; }
            {

                // Normalising NAME field for everything attributeTypes,objectClasses,ditContentRules , i.e Removing multiple spaces
                if (VmDirStringNCompareA(pszNonSpace, pszName, VmDirStringLenA(pszName), TRUE) == 0)
                 {
                     _VmDirNormaliseNameField (pszNonSpace, VmDirStringLenA(pszNonSpace));
                 }

                iOldLen = VmDirStringLenA(ppszDescs[dwSize-1]);
                iNewLen = VmDirStringLenA(pszNonSpace-1);

                dwError = VmDirReallocateMemoryWithInit(
                        ppszDescs[dwSize-1],
                        (PVOID*)&pszTmp,
                        sizeof(char) * (iOldLen + iNewLen + 1),
                        sizeof(char) * iOldLen);
                BAIL_ON_VMDIR_ERROR(dwError);

                dwError = VmDirCopyMemory(
                    &pszTmp[iOldLen],
                    sizeof(char) * (iOldLen + iNewLen + 1),
                    &(pszNonSpace-1)[0],
                    iNewLen
                );
                BAIL_ON_VMDIR_ERROR(dwError);

                ppszDescs[dwSize-1] = pszTmp;
                pszTmp = NULL;
            }

        }
    }

    *pppszDescs = ppszDescs;
    *pdwSize = dwSize;

cleanup:

    if (fp)
    {
        fclose(fp);
    }

    return dwError;

error:

    *pppszDescs = NULL;
    *pdwSize = 0;

    if (ppszDescs)
    {
        VmDirFreeStringArrayA(ppszDescs);
        VMDIR_SAFE_FREE_MEMORY(ppszDescs);
    }

    goto cleanup;
}