void SetJobDeviceName()
{
    if (GetComputerHostName(g_JobInformation.deviceName, (int)DEVICE_NAME_SIZE) != 0)
    {
        Stprintf(g_JobInformation.deviceName, DEVICE_NAME_SIZE, MI_T("%T"), EMPTY_STRING);
        return;
    }

}
MI_Result GetThumbprintForRegisteredServerURL(
    _In_ RegistrationManager* self,
    _In_ MI_Instance* registrationData,
    _Outptr_result_maybenull_z_ MI_Char** thumbprint,
    _Outptr_result_maybenull_ MI_Instance **cimErrorDetails)
{
    size_t dwSize;
    MI_Value value;
    int retValue;
    MI_Uint32 i = 0;
    MI_Result result = MI_RESULT_OK;

    if (cimErrorDetails)
    {
        *cimErrorDetails = NULL;
    }

    *thumbprint = 0;

    if (self->serverURLs == NULL)
    {
        InitializeServerURLs(self, cimErrorDetails);
    }

    if (self->serverURLs != NULL)
    {
        result = MI_Instance_GetElement(registrationData, MSFT_ServerURL_Name, &value, NULL, NULL, NULL);
        EH_CheckResult(result);

        for (i = 0; i < self->numberOfServerURLs; i++)
        {
            if (self->serverURLs[i] != NULL && Tcscasecmp(self->serverURLs[i], value.string) == 0)
            {
                dwSize = Tcslen(self->serverCertificateThumbprints[i]) + 1;
                *thumbprint = (MI_Char*)DSC_malloc(dwSize*sizeof(MI_Char), NitsHere());
                if (*thumbprint == NULL)
                {
                    EH_Fail_(GetCimMIError(MI_RESULT_SERVER_LIMITS_EXCEEDED, cimErrorDetails, ID_LCMHELPER_MEMORY_ERROR));
                }

                retValue = Stprintf(*thumbprint, dwSize, MI_T("%T"), self->serverCertificateThumbprints[i]);
                if (retValue == -1 || NitsShouldFault(NitsHere(), NitsAutomatic))
                {
                    DSC_free(*thumbprint);
                    EH_Fail_(GetCimMIError(MI_RESULT_FAILED, cimErrorDetails, ID_LCMHELPER_PRINTF_ERROR));
                }

                result = MI_RESULT_OK;
                break;
            }
        }
    }

EH_UNWIND:

    return result;
}
Exemplo n.º 3
0
void CALogSetJobDeviceName()
{
   
    MI_Uint32 jobInfoCompNameLen=CA_DEVICE_NAME_SIZE;
    int result = 0;
#if defined(_MSC_VER)
    result = !GetComputerName(g_CAJobInformation.deviceName, (LPDWORD)&jobInfoCompNameLen);
#else
    result = gethostname(g_CAJobInformation.deviceName, jobInfoCompNameLen); 
#endif

    if (result != MI_FALSE) 
    {
            Stprintf(g_CAJobInformation.deviceName, CA_DEVICE_NAME_SIZE, EMPTY_STRING);
            return;
    }


}
MI_Result GetRegisteredServerURLsFromCache(
    _Outptr_result_maybenull_z_ MI_Char** registeredServerURLs,
    _Outptr_result_maybenull_ MI_Instance **cimErrorDetails)
{
    MI_Value value;
    MI_Type  type;
    MI_Uint32 flags;
    size_t dwSize;
    int retValue;

    if (cimErrorDetails)
    {
        *cimErrorDetails = NULL;
    }

    memset(registeredServerURLs, 0, sizeof(MI_Char));

    if (g_DSCInternalCache)
    {
        if (DSC_MI_Instance_GetElement(g_DSCInternalCache, DSC_InternalStateCache_RegisteredServerURLs, &value, &type, &flags, NULL) == MI_RESULT_OK)
        {
            dwSize = Tcslen(value.string) + 1;
            *registeredServerURLs = (MI_Char*)DSC_malloc(dwSize*sizeof(MI_Char), NitsHere());
            if (*registeredServerURLs == NULL)
            {
                EH_Fail_(GetCimMIError(MI_RESULT_SERVER_LIMITS_EXCEEDED, cimErrorDetails, ID_LCMHELPER_MEMORY_ERROR));
            }
            memset(*registeredServerURLs, 0, dwSize);

            retValue = Stprintf(*registeredServerURLs, dwSize, MI_T("%T"), value.string);
            if (retValue == -1 || NitsShouldFault(NitsHere(), NitsAutomatic))
            {
                DSC_free(*registeredServerURLs);
                EH_Fail_(GetCimMIError(MI_RESULT_FAILED, cimErrorDetails, ID_LCMHELPER_PRINTF_ERROR));
            }
        }
    }

    EH_UNWIND:
        return MI_RESULT_OK;
}
MI_Result UpdateTask(
    _In_z_ MI_Char* taskName,
    _In_z_ MI_Char* taskTime,
    _In_ MI_Uint32 refreshFrequencyInSeconds,
    _Outptr_result_maybenull_ MI_Instance **extendedError)
{
    int retValue = -1;
    ssize_t read = 0;
    size_t readLength = 0, writeLength = 0;
	MI_Char *line = NULL, *lineToWrite = NULL, *taskInCrontab = NULL;
	FILE *cronFile = NULL, *cronFileTmp = NULL;
	MI_Uint32 errorCode = 0;

	if (extendedError == NULL)
	{
		return MI_RESULT_INVALID_PARAMETER;
	}
	*extendedError = NULL;


    // Preparation
    if (refreshFrequencyInSeconds > THIRTY_ONE_DAYS_IN_SECONDS)
    {
		refreshFrequencyInSeconds = THIRTY_ONE_DAYS_IN_SECONDS;
    }
    if (refreshFrequencyInSeconds < FIFTEEN_MINUTES_IN_SECONDS)
    {
		refreshFrequencyInSeconds = FIFTEEN_MINUTES_IN_SECONDS;
    }

    // PLAN 1 -- process each line of crontab file to a tmp file and then copy tmp file back
    // taskTime string is not used in Linux since we can simply register a task with frequency

	// Allocate memory
	line = (MI_Char *)DSC_malloc(UNIT_LINE_SIZE * sizeof(MI_Char), NitsHere());
	if (line == NULL)
	{
		errorCode = ID_LCMHELPER_MEMORY_ERROR;
		goto ExitWithError;
	}
    lineToWrite = (MI_Char *)DSC_malloc(UNIT_LINE_SIZE * sizeof(MI_Char), NitsHere());
	if (lineToWrite == NULL)
	{
		errorCode = ID_LCMHELPER_MEMORY_ERROR;
		goto ExitWithError;
	}
	taskInCrontab = (MI_Char *)DSC_malloc(UNIT_LINE_SIZE * sizeof(MI_Char), NitsHere());
	if (taskInCrontab == NULL)
	{
		errorCode = ID_LCMHELPER_MEMORY_ERROR;
		goto ExitWithError;
	}

	// Open files
	if (File_ExistT(CRON_FILE) != -1)
	{
		cronFile = File_OpenT(CRON_FILE, MI_T("r"));
		if (cronFile == NULL)
		{
			// Fail to open existing cron file should lead to error exit
			errorCode = ID_ENGINEHELPER_OPENFILE_ERROR;
			goto ExitWithError;
		}
	}
    cronFileTmp = File_OpenT(CRON_FILE_TMP, MI_T("w"));
    if (cronFileTmp == NULL)
	{
		// Fail to create tmp file should lead to error exit
		errorCode = ID_LCMHELPER_CREATEFILE_ERROR;
		goto ExitWithError;
    }

	// Read and process crontab file if it exists and opens appropriately
    while (cronFile != NULL && (read = readline(&line, &readLength, cronFile)) != -1)
    {
        retValue = TcsStrlcpy(lineToWrite, line, Tcslen(line)+1);
		retValue = sscanf(line, MI_T("%*s %*s %*s %*s %*s %*s %s"), taskInCrontab);
		if (retValue == 0)
		{
			// Ignore the bad line that does not comply with crontab file format
			continue;
		}
		else
		{
			if (Tcsncasecmp(taskName, taskInCrontab, Tcslen(taskName)) == 0)
			{
				// Ignore entry that duplicates registration of task
				continue;
			}
			else
			{
				// Write the entry to tmp file
				writeLength = fwrite(lineToWrite, 1, Tcslen(lineToWrite), cronFileTmp);
				if (writeLength != read)
				{
					errorCode = ID_LCMHELPER_WRITEFILE_ERROR;
					goto ExitWithError;
				}
			}
		}
    }
    if (readLength == -1)
	{
		// Deal memory failure in readline function
		errorCode = ID_ENGINEHELPER_READFILE_ERROR;
		goto ExitWithError;
	}

    // Add the task entry
    retValue = Stprintf(lineToWrite, UNIT_LINE_SIZE, MI_T("*/%d * * * * root %T\n"), refreshFrequencyInSeconds / 60, taskName);
	if (retValue == -1)
	{
		errorCode = ID_LCMHELPER_PRINTF_ERROR;
		goto ExitWithError;
	}
    writeLength = fwrite(lineToWrite, 1, Tcslen(lineToWrite), cronFileTmp);
	if (writeLength != Tcslen(lineToWrite))
	{
		errorCode = ID_LCMHELPER_WRITEFILE_ERROR;
		goto ExitWithError;
	}

	if (cronFile != NULL)
		File_Close(cronFile);
    File_Close(cronFileTmp);

    File_CopyT(CRON_FILE_TMP, CRON_FILE);
    File_RemoveT(CRON_FILE_TMP);    
    
    DSC_free(line);
    DSC_free(lineToWrite);
	DSC_free(taskInCrontab);

    return MI_RESULT_OK;

ExitWithError:
	// Close files and remove temp file if it exists
	if (cronFile)
		File_Close(cronFile);
	if (cronFileTmp)
		File_Close(cronFileTmp);
    if (File_ExistT(CRON_FILE_TMP) != -1)
        File_RemoveT(CRON_FILE_TMP);
	
	// Free memory allocations
	if (line != NULL)
		DSC_free(line);
	if (lineToWrite != NULL)
		DSC_free(lineToWrite);
	if (taskInCrontab != NULL)
		DSC_free(taskInCrontab);

	// Form rich CIM error
	if (errorCode != 0)
		return GetCimMIError(MI_RESULT_SERVER_LIMITS_EXCEEDED, extendedError, errorCode);
	else
		return MI_RESULT_FAILED;
}
MI_Result ResolvePath(_Outptr_opt_result_maybenull_z_ MI_Char **envResolvedPath,
                      _Outptr_opt_result_maybenull_z_ MI_Char **searchPath,
                      _In_z_ const MI_Char *envPath,
                      _In_z_ const MI_Char *searchPattern,
                      _Outptr_result_maybenull_ MI_Instance **extendedError)
{
    MI_Uint32 dwReturnSizeInitial = 0, dwReturnSize = (MI_Uint32) (Tcslen(envPath)+1);
    int result = 0;
    const MI_Char *pathToUse = envPath;

    if (extendedError == NULL)
    {
        return MI_RESULT_INVALID_PARAMETER;
    }
    *extendedError = NULL;  // Explicitly set *extendedError to NULL as _Outptr_ requires setting this at least once.

    if( searchPath)
    {
        *searchPath = NULL;
    }

    if( envResolvedPath != NULL )
    {
#if defined(_MSC_VER)
        dwReturnSizeInitial = ExpandEnvironmentStrings(envPath, NULL, 0);
#else
        dwReturnSizeInitial = Tcslen(envPath) + 1;
#endif

        *envResolvedPath = (MI_Char*)DSC_malloc(dwReturnSizeInitial* sizeof(MI_Char), NitsHere());
        if( *envResolvedPath == NULL)
        {
            return GetCimMIError(MI_RESULT_SERVER_LIMITS_EXCEEDED, extendedError, ID_LCMHELPER_MEMORY_ERROR);
        }

#if defined(_MSC_VER)
        dwReturnSize = ExpandEnvironmentStrings(envPath, *envResolvedPath, dwReturnSizeInitial);
        if( dwReturnSize == 0 || (dwReturnSize >  dwReturnSizeInitial ) || NitsShouldFault(NitsHere(), NitsAutomatic))
        {
            //memory error
            DSC_free(*envResolvedPath);
            *envResolvedPath = NULL;
            return GetCimMIError(MI_RESULT_SERVER_LIMITS_EXCEEDED, extendedError, ID_LCMHELPER_EXPANDENV_FAILED);
        }
#else
        memcpy(*envResolvedPath, envPath, dwReturnSizeInitial * sizeof(MI_Char));
#endif
        pathToUse = *envResolvedPath;
    }

    if( searchPath != NULL)
    {

        dwReturnSize += (MI_Uint32) (Tcslen(searchPattern) + 1);// %s\\%s

        /* Create Search Path*/
        *searchPath = (MI_Char*)DSC_malloc(dwReturnSize* sizeof(MI_Char), NitsHere()); // %s\\%s
        if( *searchPath == NULL)
        {
            if( envResolvedPath != NULL )
            {
                DSC_free(*envResolvedPath);
                *envResolvedPath = NULL;
            }
            return GetCimMIError(MI_RESULT_SERVER_LIMITS_EXCEEDED, extendedError, ID_LCMHELPER_MEMORY_ERROR);
        }
#if defined(_MSC_VER)
        result = Stprintf(*searchPath, dwReturnSize, MI_T("%T\\%T"), pathToUse, searchPattern);
#else
        result = Stprintf(*searchPath, dwReturnSize, MI_T("%T/%T"), pathToUse, searchPattern);
#endif

        if( result <= 0 || NitsShouldFault(NitsHere(), NitsAutomatic))
        {
            if( envResolvedPath != NULL )
            {
                DSC_free(*envResolvedPath);
                *envResolvedPath = NULL;
            }

            DSC_free(*searchPath);
            return GetCimMIError(MI_RESULT_FAILED, extendedError, ID_LCMHELPER_PRINTF_ERROR);
        }
    }
    return MI_RESULT_OK;
}
Exemplo n.º 7
0
MI_Boolean IsMatchedKeyProperties(
    _In_ MI_Instance* instance0,
    _In_ MI_Instance* instance1,
    _Outptr_result_maybenull_z_ MI_Char** keywords,
    _Out_ MI_Result* miResult,
    _Outptr_result_maybenull_ MI_Instance **extendedError)
{
    MI_Uint32 i, j;
    MI_Result result0, result1;
    MI_Value value0, value1;
    MI_Type type0, type1;
    Intlstr intlstr = Intlstr_Null;
    MI_Char* tempKeywords = NULL;
    MI_Char* tempKeywordsBackup = NULL;
    size_t length;
    MI_PropertyDecl** properties;

    if (extendedError == NULL)
    {        
        return MI_RESULT_INVALID_PARAMETER; 
    }
    *extendedError = NULL;	// Explicitly set *extendedError to NULL as _Outptr_ requires setting this at least once.	

    *miResult = MI_RESULT_OK;
    *keywords = NULL;
    properties = (MI_PropertyDecl**)instance0->classDecl->properties;
    for (i = 0; i < instance0->classDecl->numProperties; i++)
    {
        for (j = 0; j < properties[i]->numQualifiers; j++)
        {
            if (Tcscasecmp(properties[i]->qualifiers[j]->name, MI_T("Key")) == 0)
            {
                result0 = DSC_MI_Instance_GetElement(instance0, properties[i]->name, &value0, &type0, NULL, NULL);
                result1 = DSC_MI_Instance_GetElement(instance1, properties[i]->name, &value1, &type1, NULL, NULL);
                if (result0 == MI_RESULT_OK
                    && result1 == MI_RESULT_OK
                    && type0 == type1
                    && IsSameMiValue(&value0, &value1, type0))
                {
                    // key is the same, building keywords list.
                    length = Tcslen(properties[i]->name) + 1;
                    if (tempKeywords == NULL)
                    {
                        // the first keyword.
                        tempKeywords = (MI_Char*)DSC_malloc(length * sizeof (MI_Char), NitsHere());
                        if (tempKeywords == NULL)
                        {
                            *miResult = CreateMemoryError(extendedError);
                            return MI_TRUE;
                        }

                        memcpy(tempKeywords, properties[i]->name, length * sizeof(MI_Char) );
                    }
                    else
                    {
                        // the second or more keywords.
                        if (intlstr.str == NULL)
                        {
                            // create separator string once.
                            GetResourceString(ID_CA_COMMA_SEPARATOR, &intlstr);
                            if (intlstr.str == NULL)
                            {
                                *miResult = CreateMemoryError(extendedError);
                                DSC_free(tempKeywords);
                                return MI_TRUE;
                            }
                        }

                        length += Tcslen(tempKeywords) + Tcslen(intlstr.str);
                        tempKeywordsBackup = tempKeywords;
                        tempKeywords = (MI_Char*)DSC_realloc(tempKeywordsBackup, length * sizeof (MI_Char), NitsHere());
                        if (tempKeywords == NULL)
                        {
                            *miResult = CreateMemoryError(extendedError);
                            DSC_free(tempKeywordsBackup);
                            Intlstr_Free(intlstr);
                            return MI_TRUE;
                        }
                        Stprintf(tempKeywords, length, MI_T("%T%T"), intlstr.str, properties[i]->name);
                    }
                }
                else
                {
                    if (tempKeywords)
                    {
                        DSC_free(tempKeywords);
                    }
                    
                    if (intlstr.str)
                    {
                        Intlstr_Free(intlstr);
                    }
                    
                    if(result0 != MI_RESULT_OK)
                    {
                        *miResult = result0;
                    }
                    else if(result1 != MI_RESULT_OK)
                    {
                        *miResult = result1;
                    }

                    return MI_FALSE;
                }

                break;
            }
        }
    }

    if (intlstr.str)
    {
        Intlstr_Free(intlstr);
    }

    // at least one key was found, and all matched.
    if (tempKeywords)
    {
        *keywords = tempKeywords;
        return MI_TRUE;
    }

    return MI_FALSE;
}