int RemoveDotsFromPath()

in src/common/commonutils/OtherUtils.c [486:585]


int RemoveDotsFromPath(OsConfigLogHandle log)
{
    const char* path = "PATH";
    const char* dot = ".";
    const char* printenv = "printenv PATH";
    const char* setenvTemplate = "setenv PATH '%s'";

    PathLocations pathLocations[] = {
        { "/etc/sudoers", "secure_path" },
        { "/etc/environment", "PATH" },
        { "/etc/profile", "PATH" },
        { "/root/.profile", "PATH" }
    };
    unsigned int numPathLocations = ARRAY_SIZE(pathLocations), i = 0;
    char* setenv = NULL;
    char* currentPath = NULL;
    char* newPath = NULL;
    int status = 0, _status = 0;

    if (0 != CheckTextNotFoundInEnvironmentVariable(path, dot, false, NULL, log))
    {
        if (0 == (status == ExecuteCommand(NULL, printenv, false, false, 0, 0, &currentPath, NULL, log)))
        {
            if (NULL != (newPath = RemoveCharacterFromString(currentPath, dot[0], log)))
            {
                if (NULL != (setenv = FormatAllocateString(setenvTemplate, newPath)))
                {
                    if (0 == (status == ExecuteCommand(NULL, setenv, false, false, 0, 0, NULL, NULL, log)))
                    {
                        OsConfigLogInfo(log, "RemoveDotsFromPath: successfully set 'PATH' to '%s'", newPath);
                    }
                    else
                    {
                        OsConfigLogInfo(log, "RemoveDotsFromPath: '%s failed with %d", setenv, status);
                    }

                    FREE_MEMORY(setenv);
                }
                else
                {
                    OsConfigLogError(log, "RemoveDotsFromPath: out of memory");
                    status = ENOMEM;
                }

                FREE_MEMORY(newPath);
            }
            else
            {
                OsConfigLogInfo(log, "RemoveDotsFromPath: cannot remove '%c' from '%s'", dot[0], currentPath);
                status = EINVAL;
            }

            FREE_MEMORY(currentPath);
        }
        else
        {
            OsConfigLogInfo(log, "RemoveDotsFromPath: '%s' failed with %d", printenv, status);
        }
    }

    if (0 == status)
    {
        for (i = 0; i < numPathLocations; i++)
        {
            if (0 == CheckMarkedTextNotFoundInFile(pathLocations[i].location, pathLocations[i].path, dot, '#', NULL, log))
            {
                continue;
            }

            if (NULL != (currentPath = GetStringOptionFromFile(pathLocations[i].location, pathLocations[i].path, ' ', log)))
            {
                if (NULL != (newPath = RemoveCharacterFromString(currentPath, dot[0], log)))
                {
                    if (0 == (_status = SetEtcConfValue(pathLocations[i].location, pathLocations[i].path, newPath, log)))
                    {
                        OsConfigLogInfo(log, "RemoveDotsFromPath: successfully set '%s' to '%s' in '%s'",
                            pathLocations[i].path, pathLocations[i].location, newPath);
                    }

                    FREE_MEMORY(newPath);
                }
                else
                {
                    OsConfigLogInfo(log, "RemoveDotsFromPath: cannot remove '%c' from '%s' for '%s'",
                        dot[0], currentPath, pathLocations[i].location);
                    _status = EINVAL;
                }

                FREE_MEMORY(currentPath);
            }

            if (_status && (0 == status))
            {
                status = _status;
            }
        }
    }

    return status;
}