in src/adapters/mc/OsConfigResource.c [499:1006]
void MI_CALL OsConfigResource_Invoke_GetTargetResource(
OsConfigResource_Self* self,
MI_Context* context,
const MI_Char* nameSpace,
const MI_Char* className,
const MI_Char* methodName,
const OsConfigResource* instanceName,
const OsConfigResource_GetTargetResource* in)
{
MI_UNREFERENCED_PARAMETER(self);
MI_UNREFERENCED_PARAMETER(nameSpace);
MI_UNREFERENCED_PARAMETER(className);
MI_UNREFERENCED_PARAMETER(methodName);
MI_UNREFERENCED_PARAMETER(instanceName);
const char* auditPassed = "Audit passed";
const char* auditFailed = "Audit failed. See /var/log/osconfig_nrp.*";
const char* auditPassedInvalidResourceOrRuleId = "Audit passed for an invalid resource and/or rule id. See /var/log/osconfig_nrp.*";
const char* auditFailedInvalidResourceOrRuleId = "Audit failed for an invalid resource and/or rule id. See /var/log/osconfig_nrp.*";
const char* auditPassedCodeTemplate = "BaselineSettingCompliant:{%s}";
const char* auditFailedCodeTemplate = "BaselineSettingNotCompliant:{%s}";
char* reasonCode = NULL;
char* reasonPhrase = NULL;
MI_Result miResult = MI_RESULT_OK;
MI_Result miCleanup = MI_RESULT_OK;
MI_Instance* resultResourceObject = NULL;
MI_Instance* reasonObject = NULL;
MI_Value miValue = {0};
MI_Value miValueResult = {0};
MI_Value miValueResource = {0};
MI_Value miValueReasonResult = {0};
MI_Boolean isCompliant = MI_FALSE;
OsConfigResource_GetTargetResource get_result_object = {{0},{0},{0},{0},{0}};
if ((NULL == in) || (MI_FALSE == in->InputResource.exists) || (NULL == in->InputResource.value))
{
miResult = MI_RESULT_FAILED;
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] Invalid Get argument");
goto Exit;
}
// Try to read the resource id from the input resource values, do not fail here if we cannot
FREE_MEMORY(g_resourceId);
if ((MI_TRUE == in->InputResource.value->ResourceId.exists) && (NULL != in->InputResource.value->ResourceId.value))
{
g_resourceId = DuplicateString(in->InputResource.value->ResourceId.value);
}
else
{
g_resourceId = NULL;
}
// Read the rule id from the input resource values
if ((MI_TRUE == in->InputResource.value->RuleId.exists) && (NULL != in->InputResource.value->RuleId.value))
{
FREE_MEMORY(g_ruleId);
if (NULL == (g_ruleId = DuplicateString(in->InputResource.value->RuleId.value)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] DuplicateString(%s) failed", in->InputResource.value->RuleId.value);
g_ruleId = DuplicateString(g_defaultValue);
miResult = MI_RESULT_FAILED;
goto Exit;
}
}
else
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] No RuleId");
miResult = MI_RESULT_FAILED;
goto Exit;
}
// Read the payload key from the input resource values
if ((MI_TRUE == in->InputResource.value->PayloadKey.exists) && (NULL != in->InputResource.value->PayloadKey.value))
{
FREE_MEMORY(g_payloadKey);
if (NULL == (g_payloadKey = DuplicateString(in->InputResource.value->PayloadKey.value)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] DuplicateString(%s) failed", in->InputResource.value->PayloadKey.value);
g_payloadKey = DuplicateString(g_defaultValue);
miResult = MI_RESULT_FAILED;
goto Exit;
}
}
else
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] No PayloadKey");
miResult = MI_RESULT_FAILED;
goto Exit;
}
// Read the MIM component name from the input resource values
if ((MI_TRUE == in->InputResource.value->ComponentName.exists) && (NULL != in->InputResource.value->ComponentName.value))
{
FREE_MEMORY(g_componentName);
if (NULL == (g_componentName = DuplicateString(in->InputResource.value->ComponentName.value)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] DuplicateString(%s) failed", in->InputResource.value->ComponentName.value);
g_componentName = DuplicateString(g_defaultValue);
miResult = MI_RESULT_FAILED;
goto Exit;
}
}
else
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] No ComponentName");
miResult = MI_RESULT_FAILED;
goto Exit;
}
// Read the MIM initialization object name from the input resource values
if ((MI_TRUE == in->InputResource.value->InitObjectName.exists) && (NULL != in->InputResource.value->InitObjectName.value))
{
FREE_MEMORY(g_initObjectName);
if (NULL == (g_initObjectName = DuplicateString(in->InputResource.value->InitObjectName.value)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] DuplicateString(%s) failed", in->InputResource.value->InitObjectName.value);
g_initObjectName = DuplicateString(g_defaultValue);
miResult = MI_RESULT_FAILED;
goto Exit;
}
}
else
{
// Not an error
LogInfo(context, GetLog(), "[OsConfigResource.Get] No InitObjectName");
FREE_MEMORY(g_initObjectName);
}
// Check if we have the optional MIM procedure object name in the input resource value
if ((MI_TRUE == in->InputResource.value->ProcedureObjectName.exists) && (NULL != in->InputResource.value->ProcedureObjectName.value))
{
FREE_MEMORY(g_procedureObjectName);
if (NULL == (g_procedureObjectName = DuplicateString(in->InputResource.value->ProcedureObjectName.value)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] DuplicateString(%s) failed", in->InputResource.value->ProcedureObjectName.value);
g_procedureObjectName = DuplicateString(g_defaultValue);
miResult = MI_RESULT_FAILED;
goto Exit;
}
else
{
// We have a procedure object name, next see if we also have a procedure object value (also optional)
if ((MI_TRUE == in->InputResource.value->ProcedureObjectValue.exists) && (NULL != in->InputResource.value->ProcedureObjectValue.value))
{
FREE_MEMORY(g_procedureObjectValue);
if (NULL == (g_procedureObjectValue = DuplicateString(in->InputResource.value->ProcedureObjectValue.value)))
{
miResult = MI_RESULT_FAILED;
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] DuplicateString(%s) failed", in->InputResource.value->ProcedureObjectValue.value);
g_procedureObjectValue = DuplicateString(g_defaultValue);
goto Exit;
}
else
{
// We have both a procedure object name and value, we need to set then now to apply context for compliance
SetDesiredObjectValueToDevice("OsConfigResource.Get", g_componentName, g_procedureObjectName, g_procedureObjectValue, context);
}
}
else
{
// Cannot have a procedure object name without a procedure object value
miResult = MI_RESULT_FAILED;
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] No ProcedureObjectValue");
FREE_MEMORY(g_procedureObjectValue);
goto Exit;
}
}
}
else
{
// Not an error
LogInfo(context, GetLog(), "[OsConfigResource.Get] No ProcedureObjectName");
FREE_MEMORY(g_procedureObjectName);
}
// Read the MIM reported object name from the input resource values
if ((MI_TRUE == in->InputResource.value->ReportedObjectName.exists) && (NULL != in->InputResource.value->ReportedObjectName.value))
{
FREE_MEMORY(g_reportedObjectName);
if (NULL == (g_reportedObjectName = DuplicateString(in->InputResource.value->ReportedObjectName.value)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] DuplicateString(%s) failed", in->InputResource.value->ReportedObjectName.value);
g_reportedObjectName = DuplicateString(g_defaultValue);
miResult = MI_RESULT_FAILED;
goto Exit;
}
}
else
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] No ReportedObjectName");
miResult = MI_RESULT_FAILED;
goto Exit;
}
// Read the MIM desired object name from the input resource values
if ((MI_TRUE == in->InputResource.value->DesiredObjectName.exists) && (NULL != in->InputResource.value->DesiredObjectName.value))
{
FREE_MEMORY(g_desiredObjectName);
if (NULL == (g_desiredObjectName = DuplicateString(in->InputResource.value->DesiredObjectName.value)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] DuplicateString(%s) failed", in->InputResource.value->DesiredObjectName.value);
g_desiredObjectName = DuplicateString(g_defaultValue);
}
}
else
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] No DesiredObjectName");
}
// Read the desired MIM object value from the input resource values
if ((in->InputResource.value->DesiredObjectValue.exists == MI_TRUE) && (in->InputResource.value->DesiredObjectValue.value != NULL))
{
FREE_MEMORY(g_desiredObjectValue);
if (NULL == (g_desiredObjectValue = DuplicateString(in->InputResource.value->DesiredObjectValue.value)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] DuplicateString(%s) failed", in->InputResource.value->DesiredObjectValue.value);
g_desiredObjectValue = DuplicateString(g_failValue);
}
}
// Read the reported MIM object value from the local device
if (MI_RESULT_OK != (miResult = GetReportedObjectValueFromDevice("OsConfigResource.Get", g_componentName, context)))
{
goto Exit;
}
// Read the expected MIM object value from the input resource values, we'll use this to determine compliance
if ((in->InputResource.value->ExpectedObjectValue.exists == MI_TRUE) && (in->InputResource.value->ExpectedObjectValue.value != NULL))
{
FREE_MEMORY(g_expectedObjectValue);
if (NULL == (g_expectedObjectValue = DuplicateString(in->InputResource.value->ExpectedObjectValue.value)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] DuplicateString(%s) failed", in->InputResource.value->ExpectedObjectValue.value);
g_expectedObjectValue = DuplicateString(g_passValue);
}
}
else
{
LogInfo(context, GetLog(), "[OsConfigResource.Get] %s: no ExpectedObjectValue, assuming '%s' is expected", g_payloadKey, g_passValue);
}
isCompliant = (g_expectedObjectValue && (0 == strncmp(g_expectedObjectValue, g_reportedObjectValue, strlen(g_expectedObjectValue)))) ? MI_TRUE : MI_FALSE;
// Create the output resource
if (MI_RESULT_OK != (miResult = OsConfigResource_GetTargetResource_Construct(&get_result_object, context)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] GetTargetResource_Construct failed with %d", miResult);
goto Exit;
}
if (MI_RESULT_OK != (miResult = OsConfigResource_GetTargetResource_Set_MIReturn(&get_result_object, 0)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] GetTargetResource_Set_MIReturn failed with %d", miResult);
goto Exit;
}
if (MI_RESULT_OK != (miResult = MI_Context_NewInstance(context, &OsConfigResource_rtti, &resultResourceObject)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] MI_Context_NewInstance failed with %d", miResult);
goto Exit;
}
miValueResource.instance = resultResourceObject;
// Write the rule id to the output resource values
miValue.string = (MI_Char*)(g_ruleId);
if (MI_RESULT_OK != (miResult = MI_Instance_SetElement(resultResourceObject, MI_T("RuleId"), &miValue, MI_STRING, 0)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] MI_Instance_SetElement(RuleId) to string value '%s' failed with miResult %d", miValue.string, miResult);
goto Exit;
}
// Write the payload key to the output resource values
miValue.string = (MI_Char*)(g_payloadKey);
if (MI_RESULT_OK != (miResult = MI_Instance_SetElement(resultResourceObject, MI_T("PayloadKey"), &miValue, MI_STRING, 0)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] MI_Instance_SetElement(PayloadKey) to string value '%s' failed with miResult %d", miValue.string, miResult);
goto Exit;
}
// Write the MIM component name to the output resource values
memset(&miValue, 0, sizeof(miValue));
miValue.string = (MI_Char*)(g_componentName);
if (MI_RESULT_OK != (miResult = MI_Instance_SetElement(resultResourceObject, MI_T("ComponentName"), &miValue, MI_STRING, 0)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] MI_Instance_SetElement(ComponentName) to string value '%s' failed with miResult %d", miValue.string, miResult);
goto Exit;
}
// Write the reported MIM object name to the output resource values
memset(&miValue, 0, sizeof(miValue));
miValue.string = (MI_Char*)(g_reportedObjectName);
if (MI_RESULT_OK != (miResult = MI_Instance_SetElement(resultResourceObject, MI_T("ReportedObjectName"), &miValue, MI_STRING, 0)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] MI_Instance_SetElement(ReportedObjectName) to string value '%s' failed with miResult %d", miValue.string, miResult);
goto Exit;
}
// Write the reported MIM object value read from local device to the output resource values
memset(&miValue, 0, sizeof(miValue));
miValue.string = (MI_Char*)(g_reportedObjectValue);
if (MI_RESULT_OK != (miResult = MI_Instance_SetElement(resultResourceObject, MI_T("ReportedObjectValue"), &miValue, MI_STRING, 0)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] MI_Instance_SetElement(ReportedObjectValue) to string value '%s' failed with miResult %d", miValue.string, miResult);
goto Exit;
}
// Write the expected MIM object value to the output resource values if present in input resource values
if ((in->InputResource.value->ExpectedObjectValue.exists == MI_TRUE) && (NULL != in->InputResource.value->ExpectedObjectValue.value))
{
memset(&miValue, 0, sizeof(miValue));
miValue.string = (MI_Char*)(g_expectedObjectValue);
if (MI_RESULT_OK != (miResult = MI_Instance_SetElement(resultResourceObject, MI_T("ExpectedObjectValue"), &miValue, MI_STRING, 0)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] MI_Instance_SetElement(ExpectedObjectValue) to string value '%s' failed with miResult %d", miValue.string, miResult);
goto Exit;
}
}
// Write the desired MIM object name to the output resource values if present in input resource values
if ((MI_TRUE == in->InputResource.value->DesiredObjectName.exists) && (NULL != in->InputResource.value->DesiredObjectName.value))
{
memset(&miValue, 0, sizeof(miValue));
miValue.string = (MI_Char*)(g_desiredObjectName);
if (MI_RESULT_OK != (miResult = MI_Instance_SetElement(resultResourceObject, MI_T("DesiredObjectName"), &miValue, MI_STRING, 0)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] MI_Instance_SetElement(DesiredObjectName) to string value '%s' failed with miResult %d", miValue.string, miResult);
goto Exit;
}
}
// Write the desired MIM object value to the output resource values if present in input resource values
if ((in->InputResource.value->DesiredObjectValue.exists == MI_TRUE) && (NULL != in->InputResource.value->DesiredObjectValue.value))
{
memset(&miValue, 0, sizeof(miValue));
miValue.string = (MI_Char*)(g_desiredObjectValue);
if (MI_RESULT_OK != (miResult = MI_Instance_SetElement(resultResourceObject, MI_T("DesiredObjectValue"), &miValue, MI_STRING, 0)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] MI_Instance_SetElement(DesiredObjectValue) to string value '%s' failed with miResult %d", miValue.string, miResult);
goto Exit;
}
}
// Write the desired procedure MIM object name to the output resource values if present in input resource values
if ((in->InputResource.value->ProcedureObjectName.exists == MI_TRUE) && (NULL != in->InputResource.value->ProcedureObjectName.value))
{
memset(&miValue, 0, sizeof(miValue));
miValue.string = (MI_Char*)(g_procedureObjectName);
if (MI_RESULT_OK != (miResult = MI_Instance_SetElement(resultResourceObject, MI_T("ProcedureObjectName"), &miValue, MI_STRING, 0)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] MI_Instance_SetElement(ProcedureObjectName) to string value '%s' failed with miResult %d", miValue.string, miResult);
goto Exit;
}
}
// Write the desired procedure MIM object value to the output resource values if present in input resource values
if ((in->InputResource.value->ProcedureObjectValue.exists == MI_TRUE) && (NULL != in->InputResource.value->ProcedureObjectValue.value))
{
memset(&miValue, 0, sizeof(miValue));
miValue.string = (MI_Char*)(g_procedureObjectValue);
if (MI_RESULT_OK != (miResult = MI_Instance_SetElement(resultResourceObject, MI_T("ProcedureObjectValue"), &miValue, MI_STRING, 0)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] MI_Instance_SetElement(ProcedureObjectValue) to string value '%s' failed with miResult %d", miValue.string, miResult);
goto Exit;
}
}
// Write the MPI result for the MpiGet that returned the reported MIM object value to the output resource values
memset(&miValue, 0, sizeof(miValue));
miValue.uint32 = (MI_Uint32)(g_reportedMpiResult);
if (MI_RESULT_OK != (miResult = MI_Instance_SetElement(resultResourceObject, MI_T("ReportedMpiResult"), &miValue, MI_UINT32, 0)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] MI_Instance_SetElement(ReportedMpiResult) to integer value '%d' failed with miResult %d", miValue.uint32, miResult);
goto Exit;
}
// Generate and report the reason for the result of this audit to the output resource values
if (MI_TRUE == isCompliant)
{
if (0 == BaselineIsValidResourceIdRuleId(g_resourceId, g_ruleId, g_payloadKey, GetLog()))
{
reasonCode = FormatAllocateString(auditPassedCodeTemplate, g_ruleId);
}
else
{
reasonCode = DuplicateString(auditPassedInvalidResourceOrRuleId);
}
if ((0 == strcmp(g_reportedObjectValue, g_expectedObjectValue)) ||
((strlen(g_reportedObjectValue) > strlen(g_expectedObjectValue)) && (NULL == (reasonPhrase = DuplicateString(g_reportedObjectValue + strlen(g_expectedObjectValue))))))
{
reasonPhrase = DuplicateString(auditPassed);
}
}
else
{
if (0 == BaselineIsValidResourceIdRuleId(g_resourceId, g_ruleId, g_payloadKey, GetLog()))
{
reasonCode = FormatAllocateString(auditFailedCodeTemplate, g_ruleId);
}
else
{
reasonCode = DuplicateString(auditFailedInvalidResourceOrRuleId);
}
if ((0 == strcmp(g_reportedObjectValue, g_failValue)) || (NULL == (reasonPhrase = DuplicateString(g_reportedObjectValue))))
{
reasonPhrase = DuplicateString(auditFailed);
}
}
LogInfo(context, GetLog(), "[OsConfigResource.Get] %s: '%s', '%s'", g_reportedObjectName, reasonCode, reasonPhrase);
if (reasonCode && reasonPhrase)
{
if (MI_RESULT_OK != (miResult = MI_Context_NewInstance(context, &ReasonClass_rtti, &reasonObject)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] MI_Context_NewInstance for a reasons class instance failed with %d", miResult);
goto Exit;
}
miValue.string = (MI_Char*)reasonCode;
if (MI_RESULT_OK != (miResult = MI_Instance_SetElement(reasonObject, MI_T("Code"), &miValue, MI_STRING, 0)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] MI_Instance_SetElement(ReasonClass.Code) failed with %d", miResult);
goto Exit;
}
miValue.string = (MI_Char*)reasonPhrase;
if (MI_RESULT_OK != (miResult = MI_Instance_SetElement(reasonObject, MI_T("Phrase"), &miValue, MI_STRING, 0)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] MI_Instance_SetElement(ReasonClass.Phrase) failed with %d", miResult);
goto Exit;
}
miValueReasonResult.instancea.size = 1;
miValueReasonResult.instancea.data = &reasonObject;
if (MI_RESULT_OK != (miResult = MI_Instance_SetElement(resultResourceObject, MI_T("Reasons"), &miValueReasonResult, MI_INSTANCEA, 0)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] MI_Instance_SetElement(reason code '%s', phrase '%s') failed with %d", reasonCode, reasonPhrase, miResult);
goto Exit;
}
}
else
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] Failed reporting a reason code and phrase due to low memory");
miResult = MI_RESULT_FAILED;
goto Exit;
}
// Set the created output resource instance as the output resource in the GetTargetResource instance
if (MI_RESULT_OK != (miResult = MI_Instance_SetElement(&get_result_object.__instance, MI_T("OutputResource"), &miValueResource, MI_INSTANCE, 0)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] MI_Instance_SetElement(OutputResource) failed with %d", miResult);
goto Exit;
}
// Post the GetTargetResource instance
if (MI_RESULT_OK != (miResult = OsConfigResource_GetTargetResource_Post(&get_result_object, context)))
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] OsConfigResource_GetTargetResource_Post failed with %d", miResult);
goto Exit;
}
Exit:
FREE_MEMORY(reasonCode);
FREE_MEMORY(reasonPhrase);
// Clean up the reasons class instance
if ((NULL != reasonObject) && (MI_RESULT_OK != (miCleanup = MI_Instance_Delete(reasonObject))))
{
LogInfo(context, GetLog(), "[OsConfigResource.Get] MI_Instance_Delete(reasonObject) failed with %d", miCleanup);
}
// Clean up the Result MI value instance if needed
if ((NULL != miValueResult.instance) && (MI_RESULT_OK != (miCleanup = MI_Instance_Delete(miValueResult.instance))))
{
LogInfo(context, GetLog(), "[OsConfigResource.Get] MI_Instance_Delete(miValueResult) failed with %d", miCleanup);
}
// Clean up the output resource instance
if ((NULL != resultResourceObject) && (MI_RESULT_OK != (miCleanup = MI_Instance_Delete(resultResourceObject))))
{
LogInfo(context, GetLog(), "[OsConfigResource.Get] MI_Instance_Delete(resultResourceObject) failed with %d", miCleanup);
}
// Clean up the GetTargetResource instance
if (MI_RESULT_OK != (miCleanup = OsConfigResource_GetTargetResource_Destruct(&get_result_object)))
{
LogInfo(context, GetLog(), "[OsConfigResource.Get] GetTargetResource_Destruct failed with %d", miCleanup);
}
// Post MI result back to MI to finish
if (MI_RESULT_OK != miResult)
{
LogError(context, miResult, GetLog(), "[OsConfigResource.Get] Get complete with miResult %d", miResult);
}
MI_Context_PostResult(context, miResult);
}