bool CheckOsAndKernelMatchDistro()

in src/common/commonutils/DeviceInfoUtils.c [500:593]


bool CheckOsAndKernelMatchDistro(char** reason, OsConfigLogHandle log)
{
    const char* linuxName = "Linux";
    const char* ubuntuName = "Ubuntu";
    const char* debianName = "Debian";
    const char* none = "<null>";

    OsDistroInfo distro = {0}, os = {0};
    char* kernelName = GetOsKernelName(log);
    char* kernelVersion = GetOsKernelVersion(log);
    bool match = false;

    // Distro

    distro.id = GetEtcReleaseEntry("DISTRIB_ID", log);
    if (0 == strcmp(distro.id, none))
    {
        FREE_MEMORY(distro.id);
        distro.id = GetLsbReleaseEntry("Distributor ID", log);
    }

    distro.release = GetEtcReleaseEntry("DISTRIB_RELEASE", log);
    if (0 == strcmp(distro.release, none))
    {
        FREE_MEMORY(distro.release);
        distro.release = GetLsbReleaseEntry("Release", log);
    }

    distro.codename = GetEtcReleaseEntry("DISTRIB_CODENAME", log);
    if (0 == strcmp(distro.codename, none))
    {
        FREE_MEMORY(distro.codename);
        distro.codename = GetLsbReleaseEntry("Codename", log);
    }

    distro.description = GetEtcReleaseEntry("DISTRIB_DESCRIPTION", log);
    if (0 == strcmp(distro.description, none))
    {
        FREE_MEMORY(distro.description);
        distro.description = GetLsbReleaseEntry("Description", log);
    }

    // installed OS

    os.id = GetEtcReleaseEntry("-w NAME", log);
    os.release = GetEtcReleaseEntry("VERSION_ID", log);
    os.codename = GetEtcReleaseEntry("VERSION_CODENAME", log);
    os.description = GetEtcReleaseEntry("PRETTY_NAME", log);

    if (IsCurrentOs(ubuntuName, log) || IsCurrentOs(debianName, log))
    {
        if ((0 == strncmp(distro.id, os.id, strlen(distro.id))) &&
            (0 == strcmp(distro.release, os.release)) &&
            (0 == strcmp(distro.codename, os.codename)) &&
            (0 == strcmp(distro.description, os.description)) &&
            (0 == strcmp(kernelName, linuxName)))
        {
            OsConfigLogInfo(log, "CheckOsAndKernelMatchDistro: distro and installed image match ('%s', '%s', '%s', '%s', '%s')",
                distro.id, distro.release, distro.codename, distro.description, kernelName);
            OsConfigCaptureSuccessReason(reason, "Distro and installed image match ('%s', '%s', '%s', '%s', '%s')",
                distro.id, distro.release, distro.codename, distro.description, kernelName);
            match = true;
        }
        else
        {
            OsConfigLogInfo(log, "CheckOsAndKernelMatchDistro: distro ('%s', '%s', '%s', '%s', '%s') and installed image ('%s', '%s', '%s', '%s', '%s') do not match",
                distro.id, distro.release, distro.codename, distro.description, linuxName, os.id, os.release, os.codename, os.description, kernelName);
            OsConfigCaptureReason(reason, "Distro ('%s', '%s', '%s', '%s', '%s') and installed image ('%s', '%s', '%s', '%s', '%s') do not match, automatic remediation is not possible",
                distro.id, distro.release, distro.codename, distro.description, linuxName, os.id, os.release, os.codename, os.description, kernelName);
        }
    }
    else
    {
        if (0 == strcmp(kernelName, linuxName))
        {
            OsConfigLogInfo(log, "CheckOsAndKernelMatchDistro: distro and installed image match ('%s', '%s')", kernelName, kernelVersion);
            OsConfigCaptureSuccessReason(reason, "Distro and installed image match ('%s', '%s')", kernelName, kernelVersion);
            match = true;
        }
        else
        {
            OsConfigLogInfo(log, "CheckOsAndKernelMatchDistro: distro ('%s') and installed image ('%s', '%s') do not match", linuxName, kernelName, kernelVersion);
            OsConfigCaptureReason(reason, "Distro ('%s') and installed image ('%s', '%s') do not match, automatic remediation is not possible", linuxName, kernelName, kernelVersion);
        }
    }

    FREE_MEMORY(kernelName);
    FREE_MEMORY(kernelVersion);

    ClearOsDistroInfo(&distro);
    ClearOsDistroInfo(&os);

    return match;
}