MI_Result IssueGetActionRequest()

in LCM/dsc/engine/ca/CAInfrastructure/WebPullClient.c [1271:1453]


MI_Result  IssueGetActionRequest( _In_z_ const MI_Char *configurationID,
                                  _In_z_ const MI_Char *certificateID,
                                  _In_z_ const MI_Char *checkSum,
                                  _In_ MI_Boolean complianceStatus,
                                  _In_ MI_Uint32 lastGetActionStatusCode,
                                  _Outptr_result_maybenull_z_  MI_Char** result,
                                  _Out_ MI_Uint32* getActionStatusCode,
                                  _In_reads_z_(URL_SIZE) const MI_Char *url,
                                  _In_ MI_Uint32 port,
                                  _In_reads_z_(SUBURL_SIZE) const MI_Char *subUrl,
                                  MI_Boolean bIsHttps,
                                  _Outptr_result_maybenull_ OverAllGetActionResponse** serverAssignedConfigurations,
                                  _Outptr_result_maybenull_ MI_Instance **extendedError)
{
    MI_Result r = MI_RESULT_OK;
    const char *emptyString = "";
    char *bodyContent = NULL;
    const char *checkSumFinalValue = emptyString;
    RequestContainer requestParam = {0};
    MI_Char actionUrl[MAX_URL_LENGTH];
    char * getActionStatus = NULL;
    long responseCode = 0;

    CURL *curl = NULL;
    CURLcode res = CURLE_OK;
    struct Chunk headerChunk;
    struct Chunk dataChunk;
    struct curl_slist *list = NULL;

    requestParam.serverOperation = OPERATION_GETACTION;
    if( checkSum != NULL)
        checkSumFinalValue = checkSum;

    *result = NULL;

    r = GetSSLOptions(extendedError);
    if( r != MI_RESULT_OK)
    {
        return r;
    }

    DSC_EventWriteWebDownloadManagerDoActionServerUrl(configurationID, url);

    // Get body for GetAction request
    bodyContent = GetGetActionBodyContent(checkSumFinalValue, complianceStatus, AllowedChecksumAlgorithm, lastGetActionStatusCode);

    if( bodyContent == NULL )
    {
        *getActionStatusCode = DownloadManagerInitializationFailure;
        return GetCimMIError(r, extendedError, ID_ENGINEHELPER_MEMORY_ERROR);
    }

    curl = curl_easy_init();
    if (!curl)
    {
        return GetCimMIError(MI_RESULT_FAILED, extendedError, ID_PULL_CURLFAILEDTOINITIALIZE);
    }

    if (bIsHttps)
    {
        Snprintf(actionUrl, MAX_URL_LENGTH, "https://%s:%d/%s/Nodes(AgentId='%s')/GetDscAction", url, port, subUrl, configurationID);

    }
    else
    {
        Snprintf(actionUrl, MAX_URL_LENGTH, "http://%s:%d/%s/Nodes(AgentId='%s')/GetDscAction", url, port, subUrl, configurationID);
    }

    r = SetGeneralCurlOptions(curl, extendedError);
    if (r != MI_RESULT_OK)
    {
	DSC_free(bodyContent);
	curl_easy_cleanup(curl);
	return r;
    }

    curl_easy_setopt(curl, CURLOPT_URL, actionUrl);

    headerChunk.data = (char *)malloc(1);
    headerChunk.size = 0;
    dataChunk.data = (char *)malloc(1);
    dataChunk.size = 0;


    list = curl_slist_append(list, "Accept: application/json");
    list = curl_slist_append(list, "Content-Type: application/json; charset=utf-8");
    list = curl_slist_append(list, "ProtocolVersion: 2.0");

    res = curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
    res = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, bodyContent);
    res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
    res = curl_easy_setopt(curl, CURLOPT_HEADERDATA, &headerChunk);
    res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &dataChunk);

    res = curl_easy_setopt(curl, CURLOPT_SSLCERT, OAAS_CERTPATH);
    if (res != CURLE_OK)
    {
        curl_slist_free_all(list);
        curl_easy_cleanup(curl);
        return GetCimMIError(MI_RESULT_FAILED, extendedError, ID_PULL_CERTOPTS_NOT_SUPPORTED);
    }
    res = curl_easy_setopt(curl, CURLOPT_SSLKEY, OAAS_KEYPATH);

    if (g_sslOptions.cipherList[0] != '\0')
    {
        res = curl_easy_setopt(curl, CURLOPT_SSL_CIPHER_LIST, g_sslOptions.cipherList);
        if (res != CURLE_OK)
        {
            *getActionStatusCode = GetDscActionCommandFailure;
            DSC_free(bodyContent);
            curl_slist_free_all(list);
            curl_easy_cleanup(curl);
            return GetCimMIError(MI_RESULT_FAILED, extendedError, ID_PULL_CURLFAILEDTOSETCIPHERLIST);
        }
    }

    if (g_sslOptions.NoSSLv3 == MI_TRUE)
    {
        res = curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
        if (res != CURLE_OK)
        {
            *getActionStatusCode = GetDscActionCommandFailure;
            DSC_free(bodyContent);
            curl_slist_free_all(list);
            curl_easy_cleanup(curl);
            return GetCimMIError(MI_RESULT_FAILED, extendedError, ID_PULL_CURLFAILEDTOSETNOSSLV3);
        }
    }


    res = curl_easy_perform(curl);
    DSC_free(bodyContent);

    if (res != CURLE_OK)
    {
        *getActionStatusCode = GetDscActionCommandFailure;
        free(headerChunk.data);
        free(dataChunk.data);
        curl_slist_free_all(list);
        curl_easy_cleanup(curl);

        return GetCimMIError2Params(MI_RESULT_FAILED, extendedError, ID_PULL_CURLPERFORMFAILED, url, curl_easy_strerror(res));
    }

    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode);
    curl_slist_free_all(list);
    curl_easy_cleanup(curl);

    if (responseCode != HTTP_SUCCESS_CODE)
    {
        *getActionStatusCode = GetDscActionCommandFailure;
        free(headerChunk.data);
        free(dataChunk.data);
        return GetCimMIError1Param(r, extendedError, ID_PULL_GETACTIONFAILED, webPulginName);
    }

    GetGetActionData(dataChunk.data, dataChunk.size, &getActionStatus, serverAssignedConfigurations);

    free(headerChunk.data);
    free(dataChunk.data);

    if( getActionStatus == NULL)
    {
        *getActionStatusCode = GetDscActionCommandFailure;
        return GetCimMIError1Param(MI_RESULT_FAILED, extendedError, ID_PULL_GETACTIONFAILED, webPulginName);
    }
    if (! (Strcasecmp( getActionStatus, GetActionResultOk) == 0 ||
        Strcasecmp( getActionStatus, GetActionResultGetConfiguration) == 0 ) )
    {
        *getActionStatusCode = GetDscActionCommandFailure;

        r = GetCimMIError2Params(MI_RESULT_INVALID_PARAMETER, extendedError, ID_PULL_GETACTIONUNEXPECTEDRESULT, getActionStatus, url);
        DSC_free(getActionStatus);
        return r;
    }

    *result = getActionStatus;
    *getActionStatusCode = Success;
    DSC_EventWriteWebDownloadManagerDoActionGetCall(configurationID, requestParam.u.action.getActionStatus);

    return MI_RESULT_OK;
}